00001 import java.util.*;
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.SessionIdentifier;
00012 import org.objectweb.jonathan.apis.resources.Job;
00013 import org.objectweb.jonathan.apis.protocols.ip.TcpIpConnectionMgr;
00014 import org.objectweb.jonathan.libs.resources.JScheduler;
00015 import org.objectweb.jonathan.libs.resources.JChunkFactory;
00016 import org.objectweb.jonathan.libs.resources.JChunkFactoryFactory;
00017 import org.objectweb.jonathan.libs.resources.JSchedulerFactory;
00018 import org.objectweb.jonathan.libs.resources.tcpip.JConnectionMgr;
00019 import org.objectweb.jonathan.libs.resources.tcpip.JConnectionMgrFactory;
00020
00021 import org.objectweb.jonathan.libs.protocols.tcpip.TcpIpProtocol;
00022 import org.objectweb.david.libs.presentation.portable.CDRMarshallerFactory;
00023
00030 class SharedServerSession implements Session_Low {
00031
00032 SharedServerSession() {
00033 }
00034
00035 static MarshallerFactory marshaller_factory;
00036 private int counter;
00037 private Object lock = new Object();
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 sender) {
00045 try {
00046 String theOutput = null;
00047 String theInput = unmarshaller.readString8();
00048 synchronized (lock) {
00049 counter++;
00050 }
00051 if (theInput.equalsIgnoreCase("quit")) {
00052 System.out.println("client closing session");
00053 sender.close ();
00054 return;
00055 } else {
00056 theOutput = counter + ": " + theInput;
00057 }
00058 unmarshaller.close();
00059 Marshaller marshaller = marshaller_factory.newMarshaller();
00060 sender.prepare(marshaller);
00061 marshaller.writeString8(theOutput);
00062 sender.send(marshaller);
00063 } catch (JonathanException e) {
00064 System.out.println("EXCEPTION");
00065 e.printStackTrace();
00066 }
00067 }
00068 }
00069
00070 public class Server {
00071 public static void main (String[] args) {
00072 try {
00073 int port = 0;
00074 if (args.length != 0) {
00075 port = Integer.parseInt(args [0]);
00076 }
00077 Context context = Kernel.newConfiguration(Server.class);
00078 JScheduler scheduler =
00079 (JScheduler) context.getValue("/jonathan/JScheduler/instance",'/');
00080 JChunkFactory cf =
00081 (JChunkFactory) context.getValue("/jonathan/JChunkFactory/instance",'/');
00082 MarshallerFactory marshaller_factory =
00083 new CDRMarshallerFactory(cf);
00084 SharedServerSession.marshaller_factory = marshaller_factory;
00085 TcpIpConnectionMgr connection_mgr =
00086 (TcpIpConnectionMgr) context.getValue("/jonathan/JConnectionMgr/instance",'/');
00087 TcpIpProtocol protocol =
00088 new TcpIpProtocol(context, connection_mgr, scheduler,cf,marshaller_factory);
00089
00090
00091 SessionIdentifier session_id =
00092 protocol.newProtocolGraph(port).export(new SharedServerSession());
00093
00094
00095 System.out.println("Server ready");
00096 Object lock = new Object();
00097 synchronized (lock) {
00098 lock.wait();
00099 }
00100 } catch (Exception e) {
00101 e.printStackTrace();
00102 }
00103 }
00104 }
00105