All Things CC:

All things Commuication & Computing….

OSGi Bundle Management Code [Part 4, ROM]

leave a comment »

In the part 4 of my series I am going to describe the OSGi/Java code on the Plug Computer that implements the bundle manager in the ROM (Remote OSGi Manager) project…. and one of the best parts of doing this particular portion was that I started getting a much better understanding of the XML fundamentals. Notionally I always understood XML, but this was my first time programming with it.

At the simplest level – there is an OSGi bundle that receives network command over a Sockets Interface from the IOS app or a Java Test app, and then performs the appropriate action. These commands are all listed in the overview post. And I did get some good help from my Java programming friends to implement the entire interface along with the Java test application that I used to test the interface and responses. First step to start a TCP server, here is the code snippet to accept a incoming connection and spin it on a new thread:

try {
ServerSocket ss = new ServerSocket(port);
ss.setSoTimeout(soTimeout);
System.out.println("\nSocket Server listening on port "+port);
while (stopFlag == false) {
try {
Socket s = ss.accept();
clients.addElement(new Worker(s,bc));
System.out.println("Worker Thread Created");
} catch (Exception ignore) {
System.out.println(ignore.getMessage());
}
}

One challenge, that I faced later, was the decision to use UTF8 functions in Java to avoid network ordering issues. This did create problems during the IOS app but it was a good learning experience that was overcome using Google Search and Stack Overflow. The spawned worker thread handles the commands using Switch/Case flow:

public void run() {
String message = null;
String content = null;
int option = -1;
int BId = -1;
String file = "";
try {
message = dis.readUTF();
System.out.println(message);
if (message != null)
option = Integer.parseInt((message.split(","))[0]);
BId = Integer.parseInt((message.split(","))[1]);
file = message.split(",",3)[2];
System.out.println("Recd packet from client Option:"+option+" BID:"+BId + " file:"+file);
} catch (IOException e1) {
System.out.println(e1.getMessage());

}

//System.out.print("message: "+message+"\n");
DoOps getb = new DoOps();
switch (option){
case LIST_BUNDLES:
content = getb.getBundleList(bc);
break;
case GET_BUNDLE_INFO:
content = getb.getBundleInfo(BId, bc);
break;
case STOP_BUNDLE:
content = getb.stopBundle(BId, bc);
break;
case START_BUNDLE:
content = getb.startBundle(BId, bc);
break;
case UPDATE_BUNDLE:
content = getb.updateBundle(BId, bc,file);
break;
case SYS_INFO:
content = getb.getControllerInfo(bc);
break;
case INSTALL_BUNDLE:
content = getb.installBundle(bc, file);
break;
}
    try {
    dos.writeUTF(content);
    dos.flush();
    dos.writeBoolean(true);
    this.stopWorker();
    s.close();
    return;
} catch (IOException e) {
System.out.println(e.getMessage());
}
}

All the commands are implemented in the Class DoOps, which perform the command and return the result. The first two commands above in the Switch/Case statements are returning XML schemas. The List of All Bundles returns all the bundles as XML content and the Get Bundle Info returns details of the requested bundle using an XML schema as well. Here is the XML schema/format for the List of Bundles:

<?xml version="1.0" encoding="UTF-8"?>
<bundles>
<bundle id="0">
<name>name of bundle</name>
<status>ACTIVE</status>
</bundle>
</bundles>
Advertisements

Written by Ashu Joshi

May 7, 2012 at 10:58 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: