1 / 22

callback

callback. callback. Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need to be updated with each stock price update on a stock market server. The client has two options in this scenario:

roden
Download Presentation

callback

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. callback

  2. callback • Client programs often react to changes or updates that occur in a server. • For example, a client graph or spreadsheet program might need to be updated with each stock price update on a stock market server. • The client has two options in this scenario: • Periodically ask for the stock price via a method request on the stock server. • This is known as the "polling" or "pull" model. • Ask to be notified by the server whenever a price change occurs. • This option is referred to as a "callback" or the "push" model.

  3. callback • The example in this lecture illustrates • how a client program can pass a callback object to a server, and • the server can then callback to notify changes to the client. • At this time, we have provided the code for an extension of a simple application. • Notes about simplifying the application are contained within the code.

  4. callback • This lecture provides the code for: • The IDL for an example program with a callback. • A server implementation that callsback to a client. • A client that sends a callback object reference to a server. • An implementation of the Listener. • An implementation of the MessageServer.

  5. Writing the IDL file For the example application, the file callback.idl looks like this: interface Listener { void message(in string msg); }; interface MessageServer { void register(in Listener lt); };

  6. Writing the Server Code import org.omg.CORBA.ORB; import org.omg.PortableServer.POA; import org.omg.PortableServer.POAHelper; import org.omg.CosNaming.NameComponent; import org.omg.CosNaming.NamingContext; import org.omg.CosNaming.NamingContextHelper; import java.util.Properties; public class Server { public static void main(String[] args) { try { //create and initialize the ORB Properties props = System.getProperties(); props.put("org.omg.CORBA.ORBInitialPort", "1050"); //Replace MyHost with the name of the host on which you are running the server props.put("org.omg.CORBA.ORBInitialHost", "<MyHost>"); ORB orb = ORB.init(args, props); System.out.println("Initialized ORB");

  7. Writing the Server Code (cont.) //Bind reference with NameService NamingContext namingContext = NamingContextHelper.narrow( orb.resolve_initial_references("NameService")); System.out.println("Resolved NameService"); NameComponent[] nc = { new NameComponent("MessageServer", "") }; namingContext.rebind(nc, msRef); //Activate rootpoa rootPOA.the_POAManager().activate(); //Start readthread and wait for incoming requests System.out.println("Server ready and running ....");

  8. Writing the Server Code (cont.) //REMOVE THE NEXT LINE FOR THE SIMPLER EXAMPLE msImpl.startReadThread(); orb.run(); } catch (Exception e) { e.printStackTrace(); } } }

  9. Writing the MessageServer Implementation • The file • registers new clients, • accepts messages, • then relays the messages to the registered clients. import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Vector; import java.util.Iterator; public class MessageServerImpl extends MessageServerPOA { private Vector clients = new Vector(); private ReadThread rt = null;

  10. Writing the Message Server Implementation (cont) public MessageServerImpl() { rt = new ReadThread(this); } public void register(Listener lt) { clients.add(lt); } public void startReadThread() { rt.start(); }

  11. Writing the Message Server Implementation (cont) public void message(String msg) { Iterator it = clients.iterator(); while (it.hasNext()) { Listener lt = (Listener) it.next(); lt.message(msg); //FOR THE SIMPLER EXAMPLE, ADD A SIMPLE //MESSAGE TO BE CALLED BACK, FOR EXAMPLE, //SLEEP FOR 30 SECONDS, THEN SEND THE TIME } } }

  12. Writing the Message Server Implementation (cont) //EXCLUDE THIS CLASS FOR THE SIMPLER EXAMPLE class ReadThread extends Thread { MessageServerImpl msImpl = null; public ReadThread(MessageServerImpl msImpl) { this.msImpl = msImpl; } public void run() { BufferedReader br = new BufferedReader( new InputStreamReader(System.in));

  13. Writing the Message Server Implementation (cont) try { for (;;) { System.out.print("message > "); String msg = br.readLine(); msImpl.message(msg); } } catch (Exception e) { e.printStackTrace(); } } }

  14. Writing the Client Code import java.util.Properties; import org.omg.CORBA.ORB; import org.omg.PortableServer.POA; import org.omg.PortableServer.POAHelper; import org.omg.CosNaming.NameComponent; import org.omg.CosNaming.NamingContext; import org.omg.CosNaming.NamingContextHelper; public class Client { public static void main(String[] args) { try { //initialize orb Properties props = System.getProperties(); props.put("org.omg.CORBA.ORBInitialPort", "1050"); //Replace MyHost with the name of the host on which you are running the server props.put("org.omg.CORBA.ORBInitialHost", "<MyHost>"); ORB orb = ORB.init(args, props); System.out.println("Initialized ORB");

  15. Writing the Client Code (cont) //Instantiate Servant and create reference POA rootPOA = POAHelper.narrow( orb.resolve_initial_references("RootPOA")); ListenerImpl listener = new ListenerImpl(); rootPOA.activate_object(listener); Listener ref = ListenerHelper.narrow( rootPOA.servant_to_reference(listener)); //Resolve MessageServer MessageServer msgServer = MessageServerHelper.narrow( orb.string_to_object("corbaname:iiop:1.2@localhost:1050#MessageServer")); //Register listener reference (callback object) with MessageServer msgServer.register(ref); System.out.println("Listener registered with MessageServer");

  16. Writing the Client Code //Activate rootpoa rootPOA.the_POAManager().activate(); //Wait for messages System.out.println("Wait for incoming messages"); orb.run(); } catch (Exception e) { e.printStackTrace(); } } }

  17. Writing the Listener Implementation • When the Listener identifies that a message has been received from the server, it displays the message on the client. • For the example application, the ListenerImpl.java file looks like the following example. public class ListenerImpl extends ListenerPOA { public void message(String msg) { System.out.println("Message from server : " + msg); } }

  18. Instructions for compiling and running the example • idlj -fall callback.idl • javac *.java • start orbd -ORBInitialPort 1050 • start java Server -ORBInitialPort 1050 -ORBInitialHost localhost • java Client -ORBInitialPort 1050 -ORBInitialHost localhost

  19. Server

  20. Client 1

  21. Client 2

  22. References • http://java.sun.com/j2se/1.4.2/docs/guide/idl/jidlExample3.html

More Related