Main Page   Class Hierarchy   Compound List   File List   Compound Members  

Client.java

Go to the documentation of this file.
00001 import java.io.*;
00002 
00003 import org.objectweb.jonathan.apis.kernel.Kernel;
00004 import org.objectweb.jonathan.apis.kernel.Context;
00005 import org.objectweb.jonathan.apis.kernel.JonathanException;
00006 import org.objectweb.jonathan.apis.presentation.Marshaller;
00007 import org.objectweb.jonathan.apis.presentation.UnMarshaller;
00008 import org.objectweb.jonathan.apis.presentation.MarshallerFactory;
00009 import org.objectweb.jonathan.apis.protocols.Session_High;
00010 import org.objectweb.jonathan.apis.protocols.Session_Low;
00011 import org.objectweb.jonathan.apis.protocols.ip.IpSessionIdentifier;
00012 import org.objectweb.jonathan.apis.protocols.ip.TcpIpConnectionMgr;
00013 
00014 import org.objectweb.jonathan.libs.resources.JScheduler;
00015 import org.objectweb.jonathan.libs.resources.JSchedulerFactory;
00016 import org.objectweb.jonathan.libs.resources.JChunkFactory;
00017 import org.objectweb.jonathan.libs.resources.JChunkFactoryFactory;
00018 import org.objectweb.jonathan.libs.resources.tcpip.JConnectionMgr;
00019 import org.objectweb.jonathan.libs.protocols.tcpip.TcpIpProtocol;
00020 
00021 import org.objectweb.david.libs.presentation.portable.CDRMarshallerFactory;
00022 
00023 /*
00024  * This class implements the program of a client of the Echo service.
00025  * The client sends an input string and gets that string back, together
00026  * with the value of a counter maintained by the server and incremented
00027  * at each call. Depending on the service, the value of the counter may
00028  * be shared between the clients or private to each client.
00029  */
00030  
00031 class ClientSession implements Session_Low {
00032    static MarshallerFactory marshaller_factory;
00033    
00034    BufferedReader reader;
00035    
00036    ClientSession(BufferedReader reader) {
00037       this.reader = reader;
00038    }
00039 
00040    public void send(JonathanException e,Session_High session) {
00041       System.out.println("Exception received.");
00042       e.printStackTrace();
00043    }
00044 
00045    public  void send(UnMarshaller unmarshaller,Session_High session) {
00046       try {
00047          String fromServer,input;
00048 
00049          System.out.print("Client: ");
00050          System.out.flush();
00051          input = reader.readLine();
00052          Marshaller marshaller = marshaller_factory.newMarshaller();
00053          session.prepare(marshaller);
00054          marshaller.writeString8(input);
00055          session.send(marshaller);
00056          if (input.equalsIgnoreCase("quit")) {
00057              session.close();
00058              System.exit(0);
00059          }
00060          fromServer = unmarshaller.readString8();
00061          unmarshaller.close();
00062          System.out.println("Server: " + fromServer);
00063       } catch (Exception e) {
00064          e.printStackTrace();
00065          session.close();
00066       }
00067    }
00068 }
00069 
00070 public class Client {
00071    public static void main (String[] args) {
00072       try {
00073          BufferedReader reader =
00074             new BufferedReader(new InputStreamReader(System.in),1);
00075          System.out.print("Host ? ");
00076          System.out.flush();
00077          String host = reader.readLine(); 
00078          if (host.equals("")) {
00079              host = "localhost"; // defaults to local host
00080          }
00081          System.out.print("Port number ? ");
00082          System.out.flush();
00083          int port = Integer.parseInt(reader.readLine());
00084 
00085      // Initializing factories
00086          Context context = Kernel.newConfiguration(Client.class);
00087          JScheduler scheduler = 
00088              (JScheduler) context.getValue("/jonathan/JScheduler/instance",'/');
00089          JChunkFactory cf = 
00090              (JChunkFactory) context.getValue("/jonathan/JChunkFactory/instance",'/');
00091          CDRMarshallerFactory marshaller_factory =
00092             new CDRMarshallerFactory(cf);
00093          ClientSession.marshaller_factory = marshaller_factory;
00094         
00095      // Initializing TCP/IP protocol
00096      TcpIpConnectionMgr connection_mgr = (TcpIpConnectionMgr)
00097             context.getValue("/jonathan/JConnectionMgr/instance",'/');
00098          TcpIpProtocol protocol =
00099              new TcpIpProtocol(context, connection_mgr,
00100                                scheduler,cf,marshaller_factory);
00101 
00102          // preparing parameters for server connection
00103          IpSessionIdentifier participant = 
00104              protocol.newSessionIdentifier(host,port);
00105 
00106          // creating client-side session and connecting to server
00107          Session_High session = participant.bind(new ClientSession(reader));
00108 
00109          Object lock = new Object();
00110          synchronized (lock) {
00111             lock.wait();
00112          }
00113       } catch (Exception e) {
00114          e.printStackTrace();
00115       }
00116    }
00117 }

Generated at Thu Feb 21 18:32:19 2002 for SharedStateEcho by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001