Download a client

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
callback
1
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.
2
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.
3
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.
4
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);
};
5
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");
6
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 ....");
7
Writing the Server Code (cont.)
//REMOVE THE NEXT LINE FOR THE SIMPLER EXAMPLE
msImpl.startReadThread();
orb.run();
} catch (Exception e) {
e.printStackTrace();
}
}
}
8
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;
9
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();
}
10
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
}
}
}
11
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));
12
Writing the Message Server
Implementation (cont)
try {
for (;;) {
System.out.print("message > ");
String msg = br.readLine();
msImpl.message(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
13
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");
14
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#Message
Server"));
//Register listener reference (callback object) with MessageServer
msgServer.register(ref);
System.out.println("Listener registered with MessageServer");
15
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();
}
}
}
16
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);
}
}
17
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
18
Server
19
Client 1
20
Client 2
21
References
• http://java.sun.com/j2se/1.4.2/docs/guide/i
dl/jidlExample3.html
22