India-STD-Identifier and a Thrift Interface Between Ruby and PHP

So there was interesting and possibly useful problem that I wanted to tackle. Get a landline STD BSNL number and find out the location and state. As you can see, it’s a simple mapping problem with the map being available from here (just leave everything blank and hit search). The idea was to parse that html file, extract the good bits and return an array of hashes. Since the easiest language to do this is Ruby I chose to use that.

The implementation itself is just bits of activesupport and hpricot magic (nothing to write home about), but after I did the simple ruby client, I thought about writing up a webserver frontend for it. In the ruby world, deployments a bitch (maybe phusion passenger – now that it has support for rack – will change this). The ideal way for a simple script like this to be deployed is just to write a single page PHP file. How to call the ruby function though?

Enter thrift [2]. I’ve always wanted to play around with it and this seemed a perfect opportunity. The magic with thrift is that it works with a service definition that defines the functions to be used and auto-generates most of the interface code. You just have to write it’s implementation.


First you have to make a service definition like this:


service STDService {
   map<string, string> lookup(1:string lookup)
}

Then run the thrift generator:


thrift --gen rb -php std.thrift

This will create two folders in the source tree: gen-rb and gen-php which contains stubs for client and server implementations of the thrift interface. A lot of the boilerplate code is written for you so you don’t have to bother, but you do have to write the implementation at the client and server end.

This is how the client bit looks like (in PHP):


function lookup_number($number) {
  $transport = new TBufferedTransport(new TSocket('localhost', 9090));
  $protocol = new TBinaryProtocol($transport);
  $client = new STDServiceClient($protocol);

  $transport->open();
  $info = $client->lookup($number);
  $transport->close();

  return $info;
}

The relevant server bit is this:


class STDServiceHandler
  def initialize
    @std_parser = STDParser.instance
  end

  def lookup(number)
    @std_parser.query(number)
  end
end

handler = STDServiceHandler.new()
processor = STDService::Processor.new(handler)
transport = TServerSocket.new(9090)
transportFactory = TBufferedTransportFactory.new()
server = TSimpleServer.new(processor, transport, transportFactory)

puts "Starting the STDService server..."
server.serve()
puts "Done"

The original files are here and here.

It’s nice to see how it works: I fire up Apache, point to my PHP file and once I submit the form, all the interaction happens behind the scenes automatically. Thrift makes it easy to call one language from the other.

India STD identifier is up at googlecode.

A quick addendum on how you get started with Thrift:

  1. You need boost C++ to compile thrift so get that installed.
  2. Download the thrift source from Facebook and do a ./configure make and make install
  3. Be sure to install the necessary language bindings from lib/. For PHP these are simple PHP files (although there seems to be a native C extension to speed things up), for Ruby and Python they employ the native setuptools interface.

4 responses

  1. thank you very much.
    I got some ideas from this sample.
    thanks again.

  2. Hi Vishnu!

    Do you have an idea as to whether it is possible to have a thrift service installed on WIN platform which is running xampp / xampplite?

    – Hetal

  3. Venkat: Nope that’s way beyond anything I’ve done. I would imagine it’d be implementing the thrift rpc layer as well as the API. Isn’t actionscript client-side? Let me know how it goes!

    Vish

  4. venkat Avatar

    thanks for the nice little example vysnu. I am looking into thrift to see if I cam write up an actionscript API for thrift. So, checking out all the examples I can. Have you done any research into what it takes to write thrift for a new language? Any thoughts?

Leave a Reply

Create a website or blog at WordPress.com