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
00025
00026
00027
00028
00029
00030 class ClientSession implements Session_Low {
00031 static MarshallerFactory marshaller_factory;
00032
00033 BufferedReader reader;
00034
00035 ClientSession(BufferedReader reader) {
00036 this.reader = reader;
00037 }
00038
00039 public void send(JonathanException e,Session_High session) {
00040 System.out.println("Exception received.");
00041 e.printStackTrace();
00042 }
00043
00044 public void send(UnMarshaller unmarshaller,Session_High session) {
00045 try {
00046 String fromServer,input;
00047
00048 System.out.print("Client: ");
00049 System.out.flush();
00050 input = reader.readLine();
00051 Marshaller marshaller = marshaller_factory.newMarshaller();
00052 session.prepare(marshaller);
00053 marshaller.writeString8(input);
00054 session.send(marshaller);
00055 if (input.equalsIgnoreCase("quit")) {
00056 session.close();
00057 System.exit(0);
00058 }
00059 fromServer = unmarshaller.readString8();
00060 unmarshaller.close();
00061 System.out.println("Server: " + fromServer);
00062 } catch (Exception e) {
00063 e.printStackTrace();
00064 session.close();
00065 }
00066 }
00067 }
00068
00069 public class Client {
00070 public static void main (String[] args) {
00071 try {
00072 BufferedReader reader =
00073 new BufferedReader(new InputStreamReader(System.in),1);
00074 System.out.print("Host ? ");
00075 System.out.flush();
00076 String host = reader.readLine();
00077 if (host.equals("")) {
00078 host = "localhost";
00079 }
00080 System.out.print("Port number ? ");
00081 System.out.flush();
00082 int port = Integer.parseInt(reader.readLine());
00083
00084
00085 Context context = Kernel.newConfiguration(Client.class);
00086 JScheduler scheduler =
00087 (JScheduler) context.getValue("/jonathan/JScheduler/instance",'/');
00088 JChunkFactory cf =
00089 (JChunkFactory) context.getValue("/jonathan/JChunkFactory/instance",'/');
00090 CDRMarshallerFactory marshaller_factory =
00091 new CDRMarshallerFactory(cf);
00092 ClientSession.marshaller_factory = marshaller_factory;
00093
00094
00095 TcpIpConnectionMgr connection_mgr = (TcpIpConnectionMgr)
00096 context.getValue("/jonathan/JConnectionMgr/instance",'/');
00097 TcpIpProtocol protocol =
00098 new TcpIpProtocol(context, connection_mgr,
00099 scheduler,cf,marshaller_factory);
00100
00101
00102 IpSessionIdentifier participant =
00103 protocol.newSessionIdentifier(host,port);
00104
00105
00106 Session_High session = participant.bind(new ClientSession(reader));
00107
00108 Object lock = new Object();
00109 synchronized (lock) {
00110 lock.wait();
00111 }
00112 } catch (Exception e) {
00113 e.printStackTrace();
00114 }
00115 }
00116 }