Wordle Delicious

Tuesday, June 24, 2008

I just visualized my delicious tags using wordle, and they look good! Thought I’ll share! Note: they use Java instead of Flash for these effects, interesting!

Comments (1) | Permalink

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

Friday, June 13, 2008

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.

Comments (8) | Permalink

Five Awesome and Small Unix Tools

Sunday, June 8, 2008

The UNIX philosophy has always been to tie in small tools and make a bigger whole. During the course of my programming and sysadmin carer, I’ve found these tools to be invaluable, and I’ve often noticed that many people don’t know about them:

Rlwrap

This binds functionality of the readline library to applications which doesn’t have native readline bindings. Ever wanted the bash-shell Ctrl+R reverse history search in another REPL? Just start the program with Rlwrap like so (this is for Jan’s really nice php-shell):

rlwrap php-shell

This sort of a thing makes working with the otherwise clumsy php-shell a breeze.

Daemonize

Often you want to run programs as a daemon but they themselves don’t have any sort of functionality like that. Daemonize comes to the rescue. (For ruby programs that I write I use the similarly named but much more capable daemons gem). Use this like so:

daemonize tail -f log/apache>log > /home/vishnu/.log

Redir

Redir is another gem. It redirects transparently TCP/IP traffic from one port to another IP and port. Use this for IP masking and redirect:

redir --lport=81 --cport=80

Dtach

Dtach is again great at what it does. It’s like daemonize, but it allows you to detach and attach from a running process. I’ve written about this before.

Comment! | Permalink

View Vishnu Gopal's profile on LinkedIn

Twitter

Friday, June 27, 2008

(1) #

Thursday, June 26, 2008

(2) #

Wednesday, June 25, 2008

(0) #

Thursday, June 19, 2008

(2) #

Wednesday, June 18, 2008

(1) #

Tuesday, June 17, 2008

  • C++ like those higher level languages via a library. Not sure how much I like this idea, but I really got to brush up (or rather learn again) C++ and this might provide an easier start.
    (tags: c++ python)

(0) #

Monday, June 16, 2008

(0) #

Thursday, June 12, 2008

(0) #

Tuesday, June 10, 2008

(0) #

Saturday, June 7, 2008

(0) #

Friday, June 6, 2008

(0) #

Tuesday, June 3, 2008

(0) #

Sunday, June 1, 2008

(0) #