Balduran chang
2008/9/30
Balduran.cs96g@g2.nctu.edu.tw
EC719
(03)5712121#54819
 Bundle management portion
 GUI
 Bundle list
 un/install bundle
 Start/stop bundle
 Update/configre bundle
 EV bundle portion
 one trigger function to broadcast “Emergency event!”
 once receive EV message, response it and show
incoming message.
 Port number and message should be able to modify
 PPT slice
 Architectures
 System design
 Used module
 Source code
 Java code
 Jar file
 Knopflerfish
 http://www.knopflerfish.org/download.html
 Apache Felix
 http://felix.apache.org/site/downloads.cgi
 Equinox
 http://download.eclipse.org/equinox/
 Prosyst mBedded Server Equinox Edition
 http://dz.prosyst.com/oss/
 Install
 Use startinstall.bat
 Start framework
 C:mbs_equinoxbinv
msjdkserver.bat
 install
 uninstall
 start
 stop
 refresh
 update
 status
 ss
 services
 bundles
 bundle
 headers
 log
 gc
 sl
 exit
 init
 shutdown
 install file:../../../demo/bundles/httpdemo.jar
 start <bundle id>
 Check the bundle status is active
 Visit
http://<mbs_host>/images/imagination.jpg
 Stop <bundle id>
 Check the bundle status is resolved
 Uninstall <bundle id>
 JDK 1.6.07
 mBS Equinox edition 3.4
 Eclipse
 Configure build path…
 Add external JARs…
 bundles/org.eclipse.osgi.jar
 bundles/org.eclipse.osgi.services.jar
 bundles/org.eclipse.osgi.util.jar
1. Bundle implementation
2. Bundle activator
3. Compile and generate class file
4. Manifest.mf file
5. Generate bundle JAR file
6. Install and start bundle
HelloWorldThread.java
public class HelloWorldThread extends Thread {
private boolean running = true;
public HelloWorldThread() {}
public void run() {
while (running) {
System.out.println("Hello World!");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("ERROR: " + e);
}
}
}
public void stopThread() {
this.running = false;
}
}
Activator.java
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
public static BundleContext bc = null;
private HelloWorldThread thread = null;
public void start(BundleContext bc) throws Exception {
System.out.println("Bundle starting...");
Activator.bc = bc;
this.thread = new HelloWorldThread();
this.thread.start();
}
public void stop(BundleContext bc) throws Exception {
System.out.println("Bundle stopping...");
this.thread.stopThread();
this.thread.join();
Activator.bc = null;
}
}
Manifest.mf
Manifest-Version: 1.0
Bundle-Name: first bundle
Bundle-SymbolicName: firstbundle
Bundle-Version: 1.0.0
Bundle-Description: first Demo Bundle
Bundle-Vendor: vendorABC
Bundle-Activator: demo.Activator
Bundle-Category: demo
Import-Package: org.osgi.framework
 Compile CMD
 javac -classpath .;../bundles/org.eclipse.osgi.jar; *.java
 Jar CMD
 jar cmf ./manifest firstbundle.jar *.class
Build.xml
<?xml version="1.0"?>
<project name="simplebundle" default="all">
<target name="all" depends="init,compile,jar" />
<target name="init">
<mkdir dir="./classes" />
<mkdir dir="./bin" />
</target>
<target name="compile">
<javac destdir="./classes" debug="on" srcdir="./src">
</javac>
</target>
<target name="jar">
<jar basedir="./classes" jarfile="./bin/simplebundle.jar" compress="true"
includes="**/*" manifest="./meta-inf/MANIFEST.MF" />
</target>
<target name="clean">
<delete dir="./classes" />
<delete dir="./bin" />
</target>
</project>
GetService.java
import org.osgi.framework.ServiceReference;
import org.osgi.framework.BundleException;
public class GetService implements BundleActivator {
ServiceReference timeRef = null;
TimeService timeService = null;
public void start(BundleContext bc) throws BundleException {
timeRef = bc.getServiceReference(TimeService.class.getName());
if (timeRef != null) {
timeService = (TimeService) bc.getService(timeRef);
}
if (timeService == null) {
return;
}
}
public void stop(BundleContext bc) throws BundleException {
if (timeRef != null) {
bc.ungetService(timeRef);
timeRef = null;
timeService = null;
}
}
}
public class PMPDemo implements BundleActivator {
static String SYSTEM = "com.prosyst.util.system.SystemService";
public void start(BundleContext bx) throws BundleException {
try {
ServiceReference sr =
bx.getServiceReference(PMPService.class.getName());
if(sr == null) throw new BundleException("Can't get
PMPService");
PMPService pmp = (PMPService)bx.getService(sr);
if(pmp == null) throw new BundleException("Can't get
PMPService");
Hashtable props = new Hashtable (20);
props.put("username", "admin");
props.put("password", "admin");
PMPConnection con = pmp.connect("socket://127.0.0.1:1449",
props);
RemoteObject rObject = con.getReference(SYSTEM, "");
String [] str = {Byte.TYPE.getName()};
RemoteMethod rMethod = rObject.getMethod("getProperties", str);
rMethod.changeReturnType(ExternalizableDictionary.class);
Dictionary obj = (ExternalizableDictionary)rMethod.invoke(new
Object[] {new Byte((byte) 3)}, true);
Enumeration enum = obj.keys();
while(enum.hasMoreElements()) {
Object tmp = enum.nextElement();
System.out.println(tmp + " " + obj.get(tmp));
}
}
catch (Exception e) {
System.out.println(e);
}
}
public void stop(BundleContext bx) {
}
}
 Bundle Management portion
 Use PMP (prosyst message protocol) to get a
connection to remote framework.
 1. request PMPService
 2. obtain PMPConnection
 3. call remote service method
 4.To make a service PMP-accessible, you should
additionally implement
the com.prosyst.util.io.Remoteinterface in the
service class.
public class example implements Remote {
public Class[] remoteInterfaces() {
}
}
Ref:
https://dz.prosyst.com/pdoc/mbs_equinox_3.4/um/framework/bundles/p
rosyst/rpc/pmp/pmpbundle.html
 See also:
 com.prosyst.util.parser
▪ Parser Service
▪ https://dz.prosyst.com/pdoc/mbs_prof_6.2/um/framew
ork/bundles/system/putil/putil_parser.html
 mConsole APIs
▪ https://dz.prosyst.com/pdoc/mbs_prof_6.2/um/framew
ork/admins/mConsole/mConsole_programmer.html
 EV bundle portion
 Java.net.DatagramSocket
▪ SO_BROADCAST
▪ setBroadcast(boolean on)
▪ getBroadcast()
 Java.net.DatagramPacket
▪ Use DatagramPacket(int port)
 https://dz.prosyst.com/pdoc/mbs_equinox_3.
4/um/index.html
 http://www.knopflerfish.org/tutorials.html

OSGi framework overview

  • 1.
  • 2.
     Bundle managementportion  GUI  Bundle list  un/install bundle  Start/stop bundle  Update/configre bundle  EV bundle portion  one trigger function to broadcast “Emergency event!”  once receive EV message, response it and show incoming message.  Port number and message should be able to modify
  • 3.
     PPT slice Architectures  System design  Used module  Source code  Java code  Jar file
  • 4.
     Knopflerfish  http://www.knopflerfish.org/download.html Apache Felix  http://felix.apache.org/site/downloads.cgi  Equinox  http://download.eclipse.org/equinox/  Prosyst mBedded Server Equinox Edition  http://dz.prosyst.com/oss/
  • 5.
     Install  Usestartinstall.bat  Start framework  C:mbs_equinoxbinv msjdkserver.bat
  • 6.
     install  uninstall start  stop  refresh  update  status  ss  services  bundles  bundle  headers  log  gc  sl  exit  init  shutdown
  • 7.
     install file:../../../demo/bundles/httpdemo.jar start <bundle id>  Check the bundle status is active  Visit http://<mbs_host>/images/imagination.jpg
  • 8.
     Stop <bundleid>  Check the bundle status is resolved  Uninstall <bundle id>
  • 9.
     JDK 1.6.07 mBS Equinox edition 3.4  Eclipse  Configure build path…  Add external JARs…  bundles/org.eclipse.osgi.jar  bundles/org.eclipse.osgi.services.jar  bundles/org.eclipse.osgi.util.jar
  • 10.
    1. Bundle implementation 2.Bundle activator 3. Compile and generate class file 4. Manifest.mf file 5. Generate bundle JAR file 6. Install and start bundle
  • 11.
    HelloWorldThread.java public class HelloWorldThreadextends Thread { private boolean running = true; public HelloWorldThread() {} public void run() { while (running) { System.out.println("Hello World!"); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println("ERROR: " + e); } } } public void stopThread() { this.running = false; } }
  • 12.
    Activator.java import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; publicclass Activator implements BundleActivator { public static BundleContext bc = null; private HelloWorldThread thread = null; public void start(BundleContext bc) throws Exception { System.out.println("Bundle starting..."); Activator.bc = bc; this.thread = new HelloWorldThread(); this.thread.start(); } public void stop(BundleContext bc) throws Exception { System.out.println("Bundle stopping..."); this.thread.stopThread(); this.thread.join(); Activator.bc = null; } }
  • 13.
    Manifest.mf Manifest-Version: 1.0 Bundle-Name: firstbundle Bundle-SymbolicName: firstbundle Bundle-Version: 1.0.0 Bundle-Description: first Demo Bundle Bundle-Vendor: vendorABC Bundle-Activator: demo.Activator Bundle-Category: demo Import-Package: org.osgi.framework  Compile CMD  javac -classpath .;../bundles/org.eclipse.osgi.jar; *.java  Jar CMD  jar cmf ./manifest firstbundle.jar *.class
  • 14.
    Build.xml <?xml version="1.0"?> <project name="simplebundle"default="all"> <target name="all" depends="init,compile,jar" /> <target name="init"> <mkdir dir="./classes" /> <mkdir dir="./bin" /> </target> <target name="compile"> <javac destdir="./classes" debug="on" srcdir="./src"> </javac> </target> <target name="jar"> <jar basedir="./classes" jarfile="./bin/simplebundle.jar" compress="true" includes="**/*" manifest="./meta-inf/MANIFEST.MF" /> </target> <target name="clean"> <delete dir="./classes" /> <delete dir="./bin" /> </target> </project>
  • 15.
    GetService.java import org.osgi.framework.ServiceReference; import org.osgi.framework.BundleException; publicclass GetService implements BundleActivator { ServiceReference timeRef = null; TimeService timeService = null; public void start(BundleContext bc) throws BundleException { timeRef = bc.getServiceReference(TimeService.class.getName()); if (timeRef != null) { timeService = (TimeService) bc.getService(timeRef); } if (timeService == null) { return; } } public void stop(BundleContext bc) throws BundleException { if (timeRef != null) { bc.ungetService(timeRef); timeRef = null; timeService = null; } } }
  • 16.
    public class PMPDemoimplements BundleActivator { static String SYSTEM = "com.prosyst.util.system.SystemService"; public void start(BundleContext bx) throws BundleException { try { ServiceReference sr = bx.getServiceReference(PMPService.class.getName()); if(sr == null) throw new BundleException("Can't get PMPService"); PMPService pmp = (PMPService)bx.getService(sr); if(pmp == null) throw new BundleException("Can't get PMPService"); Hashtable props = new Hashtable (20); props.put("username", "admin"); props.put("password", "admin"); PMPConnection con = pmp.connect("socket://127.0.0.1:1449", props); RemoteObject rObject = con.getReference(SYSTEM, "");
  • 17.
    String [] str= {Byte.TYPE.getName()}; RemoteMethod rMethod = rObject.getMethod("getProperties", str); rMethod.changeReturnType(ExternalizableDictionary.class); Dictionary obj = (ExternalizableDictionary)rMethod.invoke(new Object[] {new Byte((byte) 3)}, true); Enumeration enum = obj.keys(); while(enum.hasMoreElements()) { Object tmp = enum.nextElement(); System.out.println(tmp + " " + obj.get(tmp)); } } catch (Exception e) { System.out.println(e); } } public void stop(BundleContext bx) { } }
  • 18.
     Bundle Managementportion  Use PMP (prosyst message protocol) to get a connection to remote framework.  1. request PMPService  2. obtain PMPConnection  3. call remote service method
  • 19.
     4.To makea service PMP-accessible, you should additionally implement the com.prosyst.util.io.Remoteinterface in the service class. public class example implements Remote { public Class[] remoteInterfaces() { } } Ref: https://dz.prosyst.com/pdoc/mbs_equinox_3.4/um/framework/bundles/p rosyst/rpc/pmp/pmpbundle.html
  • 20.
     See also: com.prosyst.util.parser ▪ Parser Service ▪ https://dz.prosyst.com/pdoc/mbs_prof_6.2/um/framew ork/bundles/system/putil/putil_parser.html  mConsole APIs ▪ https://dz.prosyst.com/pdoc/mbs_prof_6.2/um/framew ork/admins/mConsole/mConsole_programmer.html
  • 21.
     EV bundleportion  Java.net.DatagramSocket ▪ SO_BROADCAST ▪ setBroadcast(boolean on) ▪ getBroadcast()  Java.net.DatagramPacket ▪ Use DatagramPacket(int port)
  • 22.