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.

No comments: