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

Slides:



Advertisements
Similar presentations
CS Network Programming CS 3331 Fall CS 3331 Outline Socket programming Remote method invocation (RMI)
Advertisements

Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
SOCKET PROGRAMMING WITH MOBILE SOCKET CLIENT DEARTMENT OF COMPUTER SCIENCE IOWA STATE UNIVERSITY.
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
Io package as Java’s basic I/O system continue’d.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Ryerson University CPS Distributing Computing with Java.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Critical Reference An occurrence of a variable v is defined to be critical reference: a. if it is assigned to in one process and has an occurrence in another.
11-1 Pipes. Learning Objectives ● This module will help you... – Understand key JXTA pipe concepts – Understand how pipes work – Gain familiarity with.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
14-1 JXTA for J2ME Learning Objectives ● This module will help you... – Learn about the architecture of JXTA for J2ME – Learn the JXTA for J2ME.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
© Amir Kirsh Java Networking Written by Amir Kirsh.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
9-1 JXTA Discovery Learning Objectives This module will help you... – Understand how JXTA advertisements are published, cached, and.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
Dynamic Architectures (Component Reconfiguration) with Fractal.
13-1 Monitoring and Metering Learning Objectives ● This module will help you... – Understand the JXTA monitoring and metering functionality – Gain.
Peer-to-Peer Connections using JXTA 情報科学部ディジタルメディア学 科 n00k1042 山崎 剛.
Server-Side Java Mapping Copyright © ZeroC, Inc. Ice Programming with Java 6. Server-Side Java Mapping.
ECEG Group. iSpace Room Before iSpace Room After.
System Programming Practical Session 4: Concurrency / Safety.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
Java Programming II Java Network (I) Java Programming II.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
10-1 JXTA Messages Learning Objectives ● This module will help you... – Develop applications and services which exchange messages either through.
Netprog Java RMI1 Remote Method Invocation.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Network Programming Communication between processes Many approaches:
Java 13. Networking public class SumTest {
Stock Market Quotes – Socket Version
Echo Networking COMP
Threads in Java Two ways to start a thread
Peergroups.
RADE new features via JAVA
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Threads Chate Patanothai.
Clients and Servers 19-Nov-18.
null, true, and false are also reserved.
Introduction to Java Programming
Clients and Servers 1-Dec-18.
Networking.
Units with – James tedder
JavaScript Reserved Words
Module 2 - Part 1 Variables, Assignment, and Data Types
Distributed Computing
Clients and Servers 19-Jul-19.
Clients and Servers 13-Sep-19.
Java Chapter 5 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

15-1 Service Wrapper Example

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

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

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)

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

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

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); }

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);

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); }

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", " ", 9100, 50, 0, 0);

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

15-1 Start JXTA and the Printer's Pipe /** * Start the NetPeerGroup, create the input pipe and wait for print jobs * 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

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...

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-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)

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; }

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

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

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

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"); }

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); }

15-1 End – Service Wrapper Example