Monday, June 21, 2010

ClipBoards as Inter Process Communication (IPC) Tool

When you are dealing with a software with many different components, then passing the messages from one to other becomes very important. If your components are written in different languages then IPCs play an important role in proper synchronization and data sharing.
An alternate solution for IPC is to store your shared data in a file and read it from the components in a busy-wait manner. Major disadvantages of such approach are the unnecessary time taken to write and read a file and no privacy. Privacy in such situation can be taken care of if you store an encrypted data, but still time for reading and writing can't be dealt with.
When we talk of IPC's most common and widely used method(unix users and win32 Apps) is the Named Pipes. While you can easily use it on unix systems, it is limited to win32 Apps on Windows.
Other method is the sockets. Here you create socket connection between your components and transfer your data on this socket link. It is platform independent and can also connect to a remote method.
Both of the above mentioned methods provide the developer with lot of functionalities for large systems.
If your software is a small one then using above methods may add additional complexity. For the software that is running on same machine, a much easier way of IPC can be used. It is ClipBoards.
ClipBoards were first started by Microsoft for sharing the copied,cut data across the applications.
Now it is available on Unix and Mac systems too.
ClipBoards works like this:
1) You copy/cut data(text or files)
2) If data is a text then it is stored in Clipboard buffer else the path to files are stored.
3) When you paste, the data from this buffer is pasted.

So ClipBoards provide us with a ready-made buffer to use for our data sharing. You can access ClipBoards from almost all High Level Languages. Here I will be describing how to access it from Java and Python.


In Java you have the Clipboard component in the java.awt library and you can get the system clipboard from the Toolkit
So first get the system clipboard :
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Now to set contents :
clipboard.setContents( new StringSelection("your message"), this ); 
Similarly to get the contents :
 Transferable contents = clipboard.getContents(null);
 result = (String)contents.getTransferData(DataFlavor.stringFlavor);
DataFlavor can be image, filelist or plain text. Here we have used stringFlavour
 for string messages.
Since Java runs on JVM this code will do the work for you on all systems. 
Python is not OS independent so we will have to check for OS before using clipboards.
For windows: 
1)Import win32 libraries: 
    import win32clipboard as wc
    import win32con
2)Open Clipboard :
3) Set Content :
    wc.SetClipboardData(win32con.CF_TEXT, msg)
4) Get Content :   
    result = wc.GetClipboardData()
5) Close ClipBoard:

For Linux :
The steps are similar. 
1)Import libraries: 
    import pygtk
    import gtk

2)Open Clipboard :
    clipboard = gtk.clipboard_get()

3) Set Content:

4) Get Content:   
To get the content of the clipboard from pygtk, you have to define a callback function which is called 
after text is retrieved. The signature of callback function is:
    callback(clipboard, text, data)
text has the clipboard message. 
With callback defined, you can get clipboard content by:

The method of using ClipBoards are simple and easy. But it loses its power when the applications are 
not data-hungry.
If there is sufficient lag between the time when data was set and time when data was received, 
then data can be modified by other processes.

Sunday, May 9, 2010

Ajax-curl wrapper for google suggestion

When you search for something in google then google provides with the suggestion for the items related to that. It is very helpful sometimes.

This implementation does similar to this. For a given string it gets the list from the google search complete so that it can be used while searching in ones own application.

The implementation uses mootols for the UI interface.

Two variants of the implementations are :

1) Without #queries per item
2) With #queries per item
File structure :
|-- index.html [demo file ]
|-- js
| |-- Autocompleter.js
| |-- Autocompleter.Request.js
| |-- Observer.js
| |-- mootools-1.2-core-nc.js
|-- css
| |--Autocompleter.css
|-- php
|-- script.php

|< type="text" name="q" id="suggestion">|

Without number of queries :

document.addEvent('domready', function() {
var inputWord = $('suggestion');
new Autocompleter.Request.HTML(inputWord, 'php/script.php', {
'indicatorClass': 'autocompleter-loading'

With number of queries :

document.addEvent('domready', function() {
var inputWord2 = $('suggestion');
new Autocompleter.Request.HTML(inputWord2, 'php/script.php', {
'indicatorClass': 'autocompleter-loading',
// send additional POST data to get the #queries
'postData': {
'extended': '1'
// to remove span
'injectChoice': function(choice) {
var text = choice.getFirst();
var value = text.innerHTML;
choice.inputValue = value;
text.set('html', this.markQueryValue(value));

To set proxy :
change $useProxy = true
and enter the proxy details
$proxy = new Proxy("server", port, "username", "password" );

Link for the Demo

Download from here

Tuesday, April 20, 2010

PHP wrapper for API is one of the sites which allows you to shorten the long URLs. This method of sharing URLs is becoming very popular these days.
Currently, provides a full fledged API to access its features.

I have created a PHP wrapper for the api so that one can use it in his applications. The wrapper takes the url and gives the required data in an array format.

Structure of the wrapper :
|--- bit_ly class

Requirements :
  • curl library to be installed on the server. PHP4 and 5 has it by default. You will have to enable it. This blog explains how to enable cURL in XAMPP.
  • login and API key. If you dont have API key register here and get your own key

To use this in your php file :
  • Download wrapper from here. Extract it and put it on your server.
  • add include ('path/bit_ly_php_api.php') in your php file
  • create bit_ly class instance (say bit)
$bit = new bit_ly(' login', ' api key')
  • configure the proxy if needed

Now you can perform following operations :
  • Shorten a long URL:
$short = $bit->shortenURL('');
$short is of type array and contains following :
$short['status']['code'] :- status code for the operation
$short['status']['text'] :- status text for the operation
$short['short_url'] :- shortened URL for ''
$short['long_url'] :- ''
$short['global_hash'] :- global hash value for the URL. This is other shortened URL :- '' + global_hash

  • Expand a URL :
$expand = $bit->expandURL('');
$expand is same as $short.

  • Validate a user :
$valid = $bit->validate(login, apikey);
$valid is of type array and contains following :
It contains same status messages as $short
$valid['valid'] :- returns 1 if (login, apikey) is valid else 0

  • Set new user and api key
$bit->setLoginAndKey(login, key);
This changes the login name and the api key.

Thursday, February 25, 2010

"The 200"

It is to complement G2's poster "Sachin in 200".

If not the full movie here are the trailers.

In English :

In some other language with reviews :

The original source of the videos are 1 and 2.

Sunday, February 14, 2010

General Aptitude -- an area of controversy

As it appears, GA is very vast field, encompassing almost every thing one can think of. So are the questions of this category.
But the point of concern is what should be the solutions of these questions.While solving GA questions, often you find yourself in the position where more than one logic works. And to make the situation even worse, the options given for the question has all those.
One similar thing happened in 2010 GATE paper.

Question was :
If 137 + 276 = 435 how much is 731 + 672 ?
a) 534 b) 1403 c) 1623 d) 1513

Solution 1:
Just by looking at the numbers one notices that all of them are less than 8. So try the base 8 and it works. 137 + 276 = 435 in base 8. So answer will be 1623 for 731 + 672.

Isnt that super easy?. But wait, some people who didnt get this tried a different approach.
Solution 2 :
137 + 276 = 435
Now, reverse each number:- 731 + 672 = 534.

Well some people may come with all other logic for other options too.

But the problem is still there, what should be the correct answer? What should one answer when he is writing the exam?

Well, people say, answer should be the one which is most general of all. But this statement is itself ambiguous. How you define the general ?
Clearly, for a student of digital logic answer 1 appears more general but to others answer 2.
On various GATE forums people are having debates over this question. All types of arguments are put forward like GA is aptitude not logic, Numbers are inherently decimal not octal.
These arguments are not limited to this question only. These are questioning the scope of this limitless area.

Saturday, January 2, 2010

Send Unlimited size Free SMS via IndyaRocks

All the existing free SMS services comes with the constraint on the length of the SMS you can send. Like
  • 160by2 allows only 80 characters + 80 characters of Ads(This annoys me, so I donot use it)
  • Indyarocks allows 140 characters
So, here is a simple hack which will allow you to send unlimited size SMS via Indyarocks.
While going through the source page of the Indyarocks I discovered that it uses Client-side validation of the length of the SMS. So we will be exploiting this fact in this hack.

Step -1 -> Login to your Indyarocks account.
Step -2 -> Go to the Send SMS page.
Step -3 -> View the source code of the page (Ctrl + U or View->Page Source).
Step -4 -> Save it on your system with html extension(say sms.html).
Step -5 -> Open sms.html in text editor.
Step -6 -> Search for function chngcnt. It is a JS function. Look for the number in the if conditions of this function. It will be 130 or 140, change this to some high number say 9999.
Step -7 -> Search for function sendfreemsg(). Do same here too, change 140 to some high number.
Step -8 -> Copy the address from your address bar of the indyarocks page(leaving sms.php). In my case it is
Step -9 -> Search for send_msgs.php in the sms.html and append to it in the front, so that it becomes
Step-10-> Now open sms.html in the browser and send the SMSs

In this, the javascript utilities like address from phone book doesnot work. To enable them correct the link to .js files by adding to the address.
Also, this works only for the SMS to Non-members option of Indyarocks.
Now, you can use this page whenever you need to send the SMS. You will only be needed to login to your Indyarocks account in another tab. (Site doesnot uses any local session variables, another demerit of the website).
Happy SMSing in New Year

Saturday, September 26, 2009

My First Hack into Netbeans

This post describes how to develop a simple macro for Netbeans to comment and uncomment the selected text. By default the purpose is done by a inbuilt template in Netbeans. To comment the selected text using this template follow :
  • Select the text to be commented.
  • Press Alt+Enter. A drop-down menu will come. Select Surround with /*$selection*/
This method becomes cumbersome when you keep adding your own Templates.(How to develop your own Template in another post :P ) NetBeans allows you to develop your own macros to perform such tasks.
  • Go to Tools -> Options.
  • In Options select the Editor -> Macros tab.
  • Now Click New. A pop-up will come. Enter the macro name Comment .
  • A new entry in the macro table is entered. Select the entry and paste the following code in the Macro Code window.
select-identifier cut-to-clipboard caret-begin-line insert-break "/*" insert-break delete-previous delete-previous paste-from-clipboard insert-break remove-line "*/" insert-break
  • Set Shortcut Alt+Q (or anything you like)
Comment macro is created. Similarly create uncomment macro. Paste following code in the Macro Code window:
select-identifier last-non-white remove-line cut-to-clipboard caret-begin-line paste-from-clipboard first-non-white remove-line remove-line
  • Set shortcut Shift+Alt+Q (or anything you like)
  • Click Ok.
Now both the macros are created. To use them Select the text from the editor and press Alt+Q to comment and press Shift+Alt+Q to uncomment the commented code. Here is the list of elementary macro functions. You can develop your own using these link.

Enjoy NetBeans :)