350 likes | 611 Views
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:
E N D
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.
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.
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.
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); };
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");
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 ....");
Writing the Server Code (cont.) //REMOVE THE NEXT LINE FOR THE SIMPLER EXAMPLE msImpl.startReadThread(); orb.run(); } catch (Exception e) { e.printStackTrace(); } } }
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;
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(); }
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 } } }
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));
Writing the Message Server Implementation (cont) try { for (;;) { System.out.print("message > "); String msg = br.readLine(); msImpl.message(msg); } } catch (Exception e) { e.printStackTrace(); } } }
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");
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");
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(); } } }
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); } }
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
References • http://java.sun.com/j2se/1.4.2/docs/guide/idl/jidlExample3.html