1 callback. 2 client programs often react to changes or updates that occur in a server. for example,...

22
1 callback

Upload: phyllis-nelson

Post on 18-Jan-2018

214 views

Category:

Documents


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

1

callback

Page 2: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

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.

Page 3: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

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.

Page 4: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

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.

Page 5: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

5

Writing the IDL fileFor the example application, the file callback.idl

looks like this:

interface Listener {void message(in string msg);

};

interface MessageServer {void register(in Listener lt);

};

Page 6: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

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

Page 7: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

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

Page 8: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

8

Writing the Server Code (cont.) //REMOVE THE NEXT LINE FOR THE SIMPLER EXAMPLE

msImpl.startReadThread();

orb.run();

} catch (Exception e) { e.printStackTrace();}

}}

Page 9: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

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;

Page 10: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

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

Page 11: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

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

} }}

Page 12: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

12

Writing the Message Server Implementation (cont)

//EXCLUDE THIS CLASS FOR THE SIMPLER EXAMPLEclass ReadThread extends Thread {

MessageServerImpl msImpl = null;

public ReadThread(MessageServerImpl msImpl) {this.msImpl = msImpl;

}

public void run() {BufferedReader br = new BufferedReader( new InputStreamReader(System.in));

Page 13: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

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

}}

Page 14: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

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

Page 15: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

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

Page 16: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

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

}}

Page 17: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

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

}}

Page 18: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

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

Page 19: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

19

Server

Page 20: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

20

Client 1

Page 21: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

21

Client 2

Page 22: 1 callback. 2 Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need

22

References

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