Upgrade to Pro — share decks privately, control downloads, hide ads and more …

OSGi framework overview

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →
Avatar for balduran balduran
September 30, 2008

OSGi framework overview

When I'm a TA in NCTU, this deck is for telematics class.

Avatar for balduran

balduran

September 30, 2008

More Decks by balduran

Other Decks in Programming

Transcript

  1.  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
  2.  PPT slice  Architectures  System design  Used

    module  Source code  Java code  Jar file
  3.  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/
  4.  install  uninstall  start  stop  refresh

     update  status  ss  services  bundles  bundle  headers  log  gc  sl  exit  init  shutdown
  5.  install file:../../../demo/bundles/httpdemo.jar  start <bundle id>  Check the

    bundle status is active  Visit http://<mbs_host>/images/imagination.jpg
  6.  Stop <bundle id>  Check the bundle status is

    resolved  Uninstall <bundle id>
  7.  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
  8. 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
  9. 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; } }
  10. 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; } }
  11. 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
  12. 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>
  13. 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; } } }
  14. 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, "");
  15. 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) { } }
  16.  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
  17.  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
  18.  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
  19.  EV bundle portion  Java.net.DatagramSocket ▪ SO_BROADCAST ▪ setBroadcast(boolean

    on) ▪ getBroadcast()  Java.net.DatagramPacket ▪ Use DatagramPacket(int port)