Presentation is loading. Please wait.

Presentation is loading. Please wait.

15-1 Service Wrapper Example. 15-1 Learning Objectives ● This module will help you... – Understand how to create your own advertisement – Understand how.

Similar presentations


Presentation on theme: "15-1 Service Wrapper Example. 15-1 Learning Objectives ● This module will help you... – Understand how to create your own advertisement – Understand how."— Presentation transcript:

1 15-1 Service Wrapper Example

2 15-1 Learning Objectives ● This module will help you... – Understand how to create your own advertisement – Understand how to register new advertisement – Understand how to use module advertisements – Understand how to discover modules

3 15-1 Print Service Tutorial ● Illustrates how to wrap a JXTA service around a traditional service, such as an lpr printer – Create an advertisement describing the printer – Create module advertisements for the printer – Publish the advertisements – Discover the service – Print to the service

4 15-1 “JXTA-ify” a Service Part #1 – Create a service advertisement describing the printer service (printer name, global position, IP address, port, and capabilities) – Wait for print jobs – Open a connection to the printer – Send print job to the printer ● Part #2 (java, C) – Discover the printer module advertisement – Extract the printer advertisement – Resolve the printer's pipe – Send a print job(s)

5 15-1 Print Service Tutorial Overview Module Impl Adv Printer Adv Module Class Adv Module Spec Adv Module Impl Adv Printer Adv InputPipe OutputPipe Spooler ServicePrint Client

6 15-1 “JXTA-ify” a Service ● Part #1 – Create a service advertisement describing the printer service (printer name, global position, IP address, port, and capabilities) – Wait for print jobs – Open a connection to the printer – Send print job to the printer

7 15-1 Creating the Printer's Advertisement /** * A LprPrinterAdvertisement describes an lpr printer advertisement. Its main * purpose is to formally document the existence of a printer */ public abstract class LprPrinterAdvertisement extends Advertisement implements Cloneable { private String description = null; private ID id = null; private String ipAddress = null; private String location = null; private String manufacturer = null; private String model = null; private String name = null; private PipeID pipe = null; // default lpr socket port private int port = 9100; private String resolution = null; private String type = null; private double longitude=0; private double latitude=-1; private int altitude=0;....... public class LprPrinterAdvImpl extends LprPrinterAdvertisement { /** * Constructor for the LprPrinterAdvImpl object */ public LprPrinterAdvImpl(Element root) { initialize(root); }

8 15-1 Create Printer's Advertisement public LprPrinterAdvertisement createNewPrinter(PeerGroup group, String name, String desc, String Model, String ipAddress, int port, double latitude, double longitude, int altitude) { try { DiscoveryService discovery = group.getDiscoveryService(); // use a codat id for the printer CodatID pid = IDFactory.newCodatID(group.getPeerGroupID()); // create the pipe id this printer is going to be attached to PipeID pipe = IDFactory.newPipeID(group.getPeerGroupID()); // the printer advertisement String moduleSpecURI = null; LprPrinterAdvImpl adv = new LprPrinterAdvImpl(); adv.setName(name); adv.setDescription(desc); adv.setIPAddress(ipAddress); adv.setPort(port); adv.setAltitude(altitude); adv.setLatitude(latitude); adv.setLongitude(longitude); adv.setPipeID(pipe); adv.setPrinterID(pid);

9 15-1 Create Printer's Advertisement (cont.) // module class id for this print service URL mcURL = IDFactory.jxtaURL(mcURI); ModuleClassID mcID = (ModuleClassID) IDFactory.fromURL(mcURL); // module spec id for this print service URL msURL = IDFactory.jxtaURL(msURI); ModuleSpecID msID = (ModuleSpecID) IDFactory.fromURL(msURL); ModuleManager modMgr = ModuleManager.getModuleManager(group); modMgr.createServiceAdvertisement(group, name, desc, msURI, //no module code null, mcURI, mcID, msID, (Advertisement) adv, discovery.DEFAULT_LIFETIME, discovery.DEFAULT_EXPIRATION); return adv; } catch (MalformedURLException badID) { throw new IllegalArgumentException("Bad MC ID"); } catch (UnknownServiceException badID) { throw new IllegalArgumentException("Unusable MC ID"); } catch (IOException ie) { throw new RuntimeException("failed to publish Advertisements", ie); }

10 15-1 Discover/Create Printer Adv. private LprPrinterAdvertisement findOrCreatePrinter(PeerGroup group) { try { enum = discovery.getLocalAdvertisements(discovery.ADV, "MSID", msURI); } catch (IOException ie) {} if (enum != null && enum.hasMoreElements()) { System.out.println("Looking in local cache"); while (enum.hasMoreElements()) { Advertisement adv = (Advertisement) enum.nextElement(); if (adv instanceof ModuleImplAdvertisement) { ModuleManager modMgr = ModuleManager.getModuleManager(group); try { padv = modMgr.getServiceAdvertisement(group, (ModuleImplAdvertisement) adv, LprPrinterAdvertisement.getAdvertisementType()); } catch (Exception e) { continue; } if (padv != null && padv instanceof LprPrinterAdvertisement) { System.out.println("Found Printer Advertisement"); return (LprPrinterAdvertisement) padv; } // could not find the printer's advertisement, recreate it System.out.println("Creating new Printer Advertisements"); return createNewPrinter(group, "Xerox workSet", "Postscript Duplex Printer", "Xerox workSet", "192.9.200.1", 9100, 50, 0, 0);

11 15-1 public class Spooler implements PipeMsgListener, Runnable { // Module Class ID private final static String mcURI = "urn:jxta:uuid- A3E3952F55D448DBBC66948F8C0DB04105"; // Module Spec ID String msURI = "urn:jxta:uuid-A3E3952F55D448DBBC66948F8C0DB041E9 0B8F31D111491DAF32AB5AD441940B06"; /** * Constructor for the Spooler object */ public Spooler() { } Class Spooler

12 15-1 Start JXTA and the Printer's Pipe /** * Start the NetPeerGroup, create the input pipe and wait for print jobs * * @param args command line arguments */ public static void main(String[] args) { Spooler spooler = new Spooler(); spooler.startJxta(); //Register LprPrinterAdvertisement with the AdvertisementFactory AdvertisementFactory.registerAdvertisementInstance( LprPrinterAdvertisement.getAdvertisementType(), new LprPrinterAdvImpl.Instantiator()); //create the printer in the NetPeerGroup LprPrinterAdvertisement printerAdv = spooler.findOrCreatePrinter(spooler.netPeerGroup); //create the pipe Advertisement for this printer spooler.createPipeAdv(printerAdv); try { System.out.println("Starting the Printer Pipe"); // start listening on the input pipe, and // register spooler as the msg listener spooler.inputPipe = spooler.netPeerGroup. getPipeService().createInputPipe(spooler.pipeAdv, spooler); spooler.run(); } catch (IOException e) {} } Class Spooler

13 15-1 Class Spooler public void pipeMsgEvent(PipeMsgEvent event) { Message msg = null; try { // grab the message from the event msg = event.getMessage(); MessageElement printjob = msg.getMessageElement(null, jobTag); // push the print job on the queue queue.push(printjob, -1); } catch (InterruptedException e) {} } public void run() { System.out.println("Waiting for print jobs"); while (true) { try { // pop the print job off the queue MessageElement el = (MessageElement) queue.pop(0); if (el != null) { System.out.println("Received a print job"); print(inetAddress, port, el); } } catch (InterruptedException ie) {} } Wait for Print Jobs...

14 15-1 Class Spooler private void print(InetAddress inetAddress, int port, MessageElement data) { try { System.out.print("Printing..."); //open a connection to the lpr printer Socket socket = new Socket(inetAddress, port); OutputStream out = socket.getOutputStream(); data.sendToStream(out); out.flush(); socket.close(); System.out.println("done"); } catch (Exception e) {} }... and Print

15 15-1 “JXTA-ify” a Service ● Part #2 – Discover the printer module advertisement – Extract the printer advertisement – Resolve the printer's pipe – Send a print job(s)

16 15-1 Discover/Create Printer Adv. private PipeAdvertisement findPrinter() { // for brevity we issue a remote discovery followed by local discovery.getRemoteAdvertisements(null, discovery.ADV, "MSID", Spooler.msURI, 1, this); Enumeration enum = null; try { enum = discovery.getLocalAdvertisements(discovery.ADV, "MSID", Spooler.msURI); } catch (IOException ie) {} if (enum != null && enum.hasMoreElements()) { ModuleImplAdvertisement mia = null; System.out.println("Looking in local cache"); while (enum.hasMoreElements()) { Advertisement adv = (Advertisement) enum.nextElement(); if (adv instanceof ModuleImplAdvertisement) { PipeAdvertisement padv = getPipeAdvertisement( (ModuleImplAdvertisement) adv); if (padv == null) { continue; } return padv; } return null; }

17 15-1 public void discoveryEvent(DiscoveryEvent ev) { DiscoveryResponseMsg res = ev.getResponse(); Enumeration enum = res.getAdvertisements(); while (enum.hasMoreElements()) { adv = (Advertisement) enum.nextElement(); if (adv instanceof ModuleImplAdvertisement) { // create a pipe advertisement from the // printer's ModuleImplAdvertisement PipeAdvertisement padv = getPipeAdvertisement( (ModuleImplAdvertisement) adv); if (padv == null) { continue; } pipeAdv = padv; synchronized (lock) { // we found the printer notify lock.notify(); } Discover/Create Printer Advertisement

18 15-1 public static void main(String args[]) { Print printer = new Print(); printer.startJxta(); printer.pipeAdv = printer.findPrinter(); if (printer.pipeAdv != null) { try { //wait until we find the printer synchronized (printer.lock) { printer.lock.wait(); } } catch (InterruptedException ie) {} } // we found the printer try { pipe.createOutputPipe(printer.pipeAdv, printer); } catch (IOException e) { } } Discover/Create Printer Advertisement

19 15-1 “JXTA-ify” a Service JXTA-C shell command lpr_print.c – Resolve the printer's pipe – Send a print job(s)

20 15-1 lpr_print.c static void connect_to_printer () { Jxta_pipe_adv* adv = NULL; Jxta_status res; Jxta_pipe* pipe = NULL; Jxta_outputpipe* op = NULL; // obtain the printer's pipe advertisement adv = get_printer_pipe_adv (DEFAULT_TIMEOUT); if (adv != NULL) { res = jxta_pipe_service_timed_connect (pipe_service, adv, 30 * 1000 * 1000, NULL, &pipe); if (res != JXTA_SUCCESS) { printf("Cannot connect to printer\n"); return; } res = jxta_pipe_get_outputpipe (pipe, &op); if (res != JXTA_SUCCESS) { printf ("Cannot get printer's outputpipe\n"); return; } // send the printjob lpr_print(op, printjob, length); JXTA_OBJECT_RELEASE (adv); } else { printf ("Cannot retreive printer's advertisement\n"); }

21 15-1 lpr_print Static void lpr_print (Jxta_outputpipe* op, char* printjob, int length) { Jxta_message* msg = jxta_message_new(); Jxta_message_element* el = NULL; char* pt = NULL; Jxta_status res; JXTA_OBJECT_CHECK_VALID (op); JXTA_OBJECT_RELEASE (el); el = jxta_message_element_new_1 ((char*) JOBTAG, (char*) "application/octet-stream", printjob, length, NULL ); jxta_message_add_element (msg, el); JXTA_OBJECT_RELEASE (el); res = jxta_outputpipe_send (op, msg); if (res != JXTA_SUCCESS) { printf ("sending failed: %d\n", (int) res); } JXTA_OBJECT_CHECK_VALID (msg); JXTA_OBJECT_RELEASE (msg); }

22 15-1 End – Service Wrapper Example


Download ppt "15-1 Service Wrapper Example. 15-1 Learning Objectives ● This module will help you... – Understand how to create your own advertisement – Understand how."

Similar presentations


Ads by Google