Main Page   Class Hierarchy   Compound List   File List   Compound Members  

Server.java

Go to the documentation of this file.
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.ip.TcpIpConnectionMgr;
00012 import org.objectweb.jonathan.apis.resources.Scheduler;
00013 import org.objectweb.jonathan.apis.resources.ChunkFactory;
00014 
00015 import org.objectweb.jonathan.libs.resources.JSchedulerFactory;
00016 import org.objectweb.jonathan.libs.resources.JChunkFactoryFactory;
00017 import org.objectweb.jonathan.libs.resources.tcpip.JConnectionMgrFactory;
00018 import org.objectweb.jonathan.libs.protocols.tcpip.TcpIpProtocol;
00019 
00020 import org.objectweb.david.libs.presentation.portable.CDRMarshallerFactory;
00021 
00029 class OwnSession {
00030    private int counter = 0;
00031    SrvProtocol protocol;
00032    static MarshallerFactory marshaller_factory;
00033    
00034    OwnSession(SrvProtocol protocol) {
00035       this.protocol = protocol;
00036    }
00037    
00038    synchronized void send(UnMarshaller unmarshaller, Session_High sender) {
00039          try {
00040             String theOutput = null;
00041             String theInput = unmarshaller.readString8();
00042             counter++;
00043             if (theInput.equalsIgnoreCase("quit")) {
00044                 System.out.println("client closing session");
00045                 sender.close ();
00046                 return;
00047             } else {
00048                 theOutput = counter + ": " + theInput;
00049             }
00050             unmarshaller.close();
00051             Marshaller marshaller = marshaller_factory.newMarshaller();
00052             sender.prepare(marshaller);
00053             marshaller.writeString8(theOutput);
00054             sender.send(marshaller);
00055          } catch (JonathanException e) {
00056                 System.out.println("EXCEPTION");
00057                 e.printStackTrace();
00058          }
00059     }  
00060 }
00061 
00068 class SrvProtocol implements Session_Low {
00069    Hashtable own_sessions;
00070    
00071    SrvProtocol() {
00072       own_sessions = new Hashtable();
00073    }
00074 
00075    public void send(JonathanException e,Session_High session) {
00076       System.out.println("Exception received.");
00077       e.printStackTrace();
00078    }
00079 
00080    public void send(UnMarshaller message, Session_High sender) {
00081       OwnSession session = null;
00082       synchronized (this) {
00083          session = (OwnSession) own_sessions.get(sender);
00084          if (session == null) { // new client, unknown up to now
00085                                 // create session for this client
00086              session = new OwnSession(this); 
00087              own_sessions.put(sender,session);
00088          }
00089       }
00090       // forward the message to the session associated with client
00091       session.send(message,sender);
00092    }
00093 
00094    void unregister(Session_High sender) {
00095       own_sessions.remove(sender);
00096    }
00097 }
00098 
00099 
00100 public class Server {
00101    public static void main (String[] args) {
00102       try {
00103          Context kernel_context = Kernel.newConfiguration(Server.class);
00104          ChunkFactory cf = (ChunkFactory)
00105             kernel_context.getValue("/jonathan/JChunkFactory/instance",'/');
00106          Scheduler scheduler = (Scheduler) 
00107              kernel_context.getValue("/jonathan/JScheduler/instance",'/');
00108          MarshallerFactory marshaller_factory =
00109             new CDRMarshallerFactory(cf);
00110          OwnSession.marshaller_factory = marshaller_factory;
00111          TcpIpConnectionMgr connection_mgr = (TcpIpConnectionMgr)
00112             kernel_context.getValue("/jonathan/JConnectionMgr/instance",'/');
00113          TcpIpProtocol protocol =
00114             new TcpIpProtocol(kernel_context,connection_mgr,
00115                               scheduler,cf,marshaller_factory);
00116          protocol.newProtocolGraph().export(new SrvProtocol());
00117          Object lock = new Object();
00118          synchronized (lock) {
00119             lock.wait();
00120          }
00121       } catch (Exception e) {
00122          e.printStackTrace();
00123       }
00124    }
00125 }

Generated at Thu Feb 21 18:38:11 2002 for PerClientEcho by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001