Tuesday 24 April 2012

Fun with Flotype

I came across this new start up Flotype through a blog at hacker news.
It provides cross-language services to expose data and realtime updates to client devices and browsers.
They have simple APIs which can be used right away.  So I created a new account and started playing with the beta version. My first idea is to setup the server on my laptop and client on my android phone. So whenever server receives the message, it makes a system call for the message. That way I could use my laptops terminal remotely. Since it's available only in Javascript,Java,Ruby and Python, I chose Java.
First of all I created a server on my laptop:
 import com.flotype.bridge.*;  
 class ChatObj implements BridgeObject {  
   public void message(String sender, String message) {  
     System.out.println(sender + ":" + message);  
     Process execAcrobatPrs=null;  
     try{  
       execAcrobatPrs = Runtime.getRuntime().exec(new String[]{"sh", "-c",message});  
    }  
    catch(Exception ex)  
    {  
              System.out.println(ex);  
       if(execAcrobatPrs!=null)  
       {  
          execAcrobatPrs.destroy();  
       }  
       ex.printStackTrace();  
       System.exit(-1);  
    }  
   }  
 }  
 public class AuthServer {  
   public static void main(String[] args) throws Exception {  
     Bridge bridge = new Bridge()  
     .setApiKey("myprivatekey");  
     bridge.joinChannel("channel-name", new ChatObj());  
     bridge.connect();  
   }  
 }  
and the client for android:
 package com.jiten;  
 import java.io.IOException;  
 import com.flotype.bridge.*;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.util.Log;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.Button;  
 import android.widget.EditText;  
 interface RemoteChat extends BridgeRemoteObject {  
   public void message(String sender, String message);  
 }  
 public class MessageSenderActivity extends Activity implements OnClickListener{  
   /** Called when the activity is first created. */  
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.main);  
     Button b = (Button) findViewById(R.id.button1);  
     b.setOnClickListener((android.view.View.OnClickListener) this);  
   }  
      @Override  
      public void onClick(View arg0) {  
           if(arg0.getId() == R.id.button1) {  
                EditText t = (EditText) findViewById(R.id.editText1);  
                Bridge bridge = new Bridge().setApiKey("myprivatekey");  
          RemoteChat channel = bridge.getChannel("channel-name", RemoteChat.class);  
          channel.message("Jiten", t.getText().toString());  
          try {  
                     bridge.connect();  
                } catch (IOException e) {  
                     // TODO Auto-generated catch block  
                     e.printStackTrace();  
                }  
           }  
      }  
 }  


That's it. Now I can run any terminal command from my laptop through my android phone.
This still is not interactive so it's of no use for commands like 'ls' but you can run applications in which you don't need interaction.
Well, I can't think of any important use for this but it was fun starting 'Catalyst' in my laptop through a command in my mobile phone. In future I might improve it for interactivity. 



No comments:

Post a Comment