--- /dev/null
+// SampleMuxHandler.java\r
+// $Id: SampleMuxHandler.java,v 1.2 2010/06/15 17:53:12 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1996.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.www.mux;\r
+\r
+import java.io.IOException;\r
+import java.io.PrintStream;\r
+\r
+import java.util.Hashtable;\r
+\r
+public class SampleMuxHandler implements MuxStreamHandler {\r
+ /**\r
+ * Debug state.\r
+ */\r
+ private static final boolean debug = false;\r
+ /**\r
+ * Well known protocols - The echo protocol identifier.\r
+ */\r
+ public static final int ECHO = 7;\r
+ /**\r
+ * Well known protocols - The echo protocol identifier.\r
+ */\r
+ public static final int DISCARD = 9;\r
+\r
+ /**\r
+ * The sigle instance of that class.\r
+ */\r
+ protected static SampleMuxHandler sample = null;\r
+ /**\r
+ * The hashtable of accepted protocols.\r
+ */\r
+ protected Hashtable protocols = null;\r
+\r
+ /**\r
+ * Log an error.\r
+ * @param msg The message to log.\r
+ */\r
+\r
+ protected void log(String msg) {\r
+ System.out.println("[" + getClass().getName() + "]: "+msg);\r
+ }\r
+\r
+ /**\r
+ * Get an instance of that sample mux stream handler.\r
+ * @return A MuxStreamHandler conformant instance.\r
+ */\r
+\r
+ public static synchronized MuxStreamHandler getStreamHandler() {\r
+ // Of course, we should go through a factory, etc as usual:\r
+ if ( sample == null ) \r
+ sample = new SampleMuxHandler();\r
+ return sample;\r
+ }\r
+\r
+ /**\r
+ * Are we willing to speak the given protocol on the given session.\r
+ * @param stream The stream that received the new session.\r
+ * @param sessid The proposed session identifier.\r
+ * @param protid The protocol to be spoken on that session.\r
+ * @return A bolean, <strong>true</strong> if the session is accepted,\r
+ * <strong>false</strong> otherwise.\r
+ */\r
+\r
+ public boolean acceptSession(MuxStream stream, int sessid, int protid) {\r
+ Object o = protocols.get(new Integer(protid));\r
+ // Reject unknown protocols straight:\r
+ if ( o == null ) {\r
+ if ( debug )\r
+ System.out.println("Rejecting "+protid+" on "+sessid+".");\r
+ return false;\r
+ } else {\r
+ if ( debug )\r
+ System.out.println("Accepting "+protid+" on "+sessid+".");\r
+ return true;\r
+ } \r
+ }\r
+\r
+ /**\r
+ * Setup the appropriate protocol handler for that accepted session.\r
+ * @param session The newly accepted session.\r
+ */\r
+\r
+ public void notifySession(MuxSession session) {\r
+ int protid = session.getProtocolIdentifier();\r
+ Integer iprotid = new Integer(protid);\r
+ Object o = protocols.get(iprotid);\r
+ // This should not happen (except for some race conditions):\r
+ if ( o == null ) {\r
+ log("SampleMuxHandler: unknown protocol "+protid);\r
+ try {\r
+ session.shutdown();\r
+ } catch (Exception ex) {\r
+ }\r
+ }\r
+ // Find (or instantiate) appropriate protocol handler:\r
+ MuxProtocolHandler handler = null;\r
+ if ( o instanceof String ) {\r
+ String strcls = (String) o;\r
+ try {\r
+ Class c = Class.forName(strcls);\r
+ handler = (MuxProtocolHandler) c.newInstance();\r
+ //Added by Jeff Huang\r
+ //TODO: FIXIT\r
+ } catch (Exception ex) {\r
+ log("Instantiating handler for " + protid\r
+ + " of class \"" + strcls + "\" failed.");\r
+ ex.printStackTrace();\r
+ try {\r
+ session.shutdown();\r
+ } catch (IOException exex) {\r
+ }\r
+ }\r
+ } else if ( o instanceof MuxProtocolHandler ) {\r
+ handler = (MuxProtocolHandler) o;\r
+ } else {\r
+ log("SampleMuxHandler: unknown protocol "+protid);\r
+ try {\r
+ session.shutdown();\r
+ } catch (Exception ex) {\r
+ }\r
+ }\r
+ // We now have a handler, launch:\r
+ try {\r
+ handler.initialize(session);\r
+ } catch (Exception ex) {\r
+ log("Launching handler for " + protid\r
+ + " of class \"" + handler.getClass() + "\" failed.");\r
+ ex.printStackTrace();\r
+ try {\r
+ session.shutdown();\r
+ } catch (IOException exex) {\r
+ }\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Register a protocol handler for the given protocol identifier.\r
+ * This method register a class to handle all new connections for the \r
+ * given protocol identifier: each new connection will result in a new \r
+ * instance of the given class being created (the easy, but slow way).\r
+ * @param protid The protocol identifier.\r
+ * @param handler The name of the class to instantiate in order\r
+ * to get a suitable handler for that protocol.\r
+ * @see MuxProtocolHandler\r
+ */\r
+\r
+ public void registerHandler(int protid, String strhandler) {\r
+ protocols.put(new Integer(protid), strhandler);\r
+ }\r
+\r
+ /**\r
+ * Register an instantiated protocol handler for the given protocol id.\r
+ * This other method of registering protocol handler is faster then\r
+ * the previous one: it allows you to spare the instantiation of a protocol\r
+ * handler on each new sessions.\r
+ * <p>The given handler will be invoked for all new sessions willing to\r
+ * speak the advertized protocol.\r
+ * @param protid The protocol identifier.\r
+ * @param handler The instantiated protocol handler.\r
+ */\r
+\r
+ public void registerHandler(int protid, MuxProtocolHandler handler) {\r
+ protocols.put(new Integer(protid), handler);\r
+ }\r
+\r
+ /**\r
+ * Unregister any handler for that protocol.\r
+ * @param protid The identifier of the protocol to unregister.\r
+ */\r
+\r
+ public void unregisterHandler(int protid) {\r
+ protocols.remove(new Integer(protid));\r
+ }\r
+\r
+ /**\r
+ * Register default protocol handlers for that stream handler.\r
+ * This is the right method to override in order to either prevent\r
+ * well known protocols from being registered, or add new protocol\r
+ * handlers.\r
+ * <p>Default protocols registered by this class are:\r
+ * <dl>\r
+ * <dt>echo<dd>The echo protocol.\r
+ * <dt>discard<dd>The discard protocol.\r
+ * </dt>\r
+ */\r
+\r
+ public void registerDefaultHandlers() {\r
+ registerHandler(ECHO , "org.w3c.www.mux.handlers.Echo");\r
+ registerHandler(DISCARD, "org.w3c.www.mux.handlers.Discard");\r
+ }\r
+\r
+ public SampleMuxHandler() {\r
+ super();\r
+ // Initialize instance variables:\r
+ this.protocols = new Hashtable();\r
+ // Register default protocols:\r
+ registerDefaultHandlers();\r
+ }\r
+\r
+}\r
+\r
+\r