001: // SampleMuxHandler.java
002: // $Id: SampleMuxHandler.java,v 1.5 2000/08/16 21:38:02 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.mux;
007:
008: import java.io.IOException;
009: import java.io.PrintStream;
010:
011: import java.util.Hashtable;
012:
013: public class SampleMuxHandler implements MuxStreamHandler {
014: /**
015: * Debug state.
016: */
017: private static final boolean debug = false;
018: /**
019: * Well known protocols - The echo protocol identifier.
020: */
021: public static final int ECHO = 7;
022: /**
023: * Well known protocols - The echo protocol identifier.
024: */
025: public static final int DISCARD = 9;
026:
027: /**
028: * The sigle instance of that class.
029: */
030: protected static SampleMuxHandler sample = null;
031: /**
032: * The hashtable of accepted protocols.
033: */
034: protected Hashtable protocols = null;
035:
036: /**
037: * Log an error.
038: * @param msg The message to log.
039: */
040:
041: protected void log(String msg) {
042: System.out.println("[" + getClass().getName() + "]: " + msg);
043: }
044:
045: /**
046: * Get an instance of that sample mux stream handler.
047: * @return A MuxStreamHandler conformant instance.
048: */
049:
050: public static synchronized MuxStreamHandler getStreamHandler() {
051: // Of course, we should go through a factory, etc as usual:
052: if (sample == null)
053: sample = new SampleMuxHandler();
054: return sample;
055: }
056:
057: /**
058: * Are we willing to speak the given protocol on the given session.
059: * @param stream The stream that received the new session.
060: * @param sessid The proposed session identifier.
061: * @param protid The protocol to be spoken on that session.
062: * @return A bolean, <strong>true</strong> if the session is accepted,
063: * <strong>false</strong> otherwise.
064: */
065:
066: public boolean acceptSession(MuxStream stream, int sessid,
067: int protid) {
068: Object o = protocols.get(new Integer(protid));
069: // Reject unknown protocols straight:
070: if (o == null) {
071: if (debug)
072: System.out.println("Rejecting " + protid + " on "
073: + sessid + ".");
074: return false;
075: } else {
076: if (debug)
077: System.out.println("Accepting " + protid + " on "
078: + sessid + ".");
079: return true;
080: }
081: }
082:
083: /**
084: * Setup the appropriate protocol handler for that accepted session.
085: * @param session The newly accepted session.
086: */
087:
088: public void notifySession(MuxSession session) {
089: int protid = session.getProtocolIdentifier();
090: Integer iprotid = new Integer(protid);
091: Object o = protocols.get(iprotid);
092: // This should not happen (except for some race conditions):
093: if (o == null) {
094: log("SampleMuxHandler: unknown protocol " + protid);
095: try {
096: session.shutdown();
097: } catch (Exception ex) {
098: }
099: }
100: // Find (or instantiate) appropriate protocol handler:
101: MuxProtocolHandler handler = null;
102: if (o instanceof String) {
103: String strcls = (String) o;
104: try {
105: Class c = Class.forName(strcls);
106: handler = (MuxProtocolHandler) c.newInstance();
107: } catch (Exception ex) {
108: log("Instantiating handler for " + protid
109: + " of class \"" + strcls + "\" failed.");
110: ex.printStackTrace();
111: try {
112: session.shutdown();
113: } catch (IOException exex) {
114: }
115: }
116: } else if (o instanceof MuxProtocolHandler) {
117: handler = (MuxProtocolHandler) o;
118: } else {
119: log("SampleMuxHandler: unknown protocol " + protid);
120: try {
121: session.shutdown();
122: } catch (Exception ex) {
123: }
124: }
125: // We now have a handler, launch:
126: try {
127: handler.initialize(session);
128: } catch (Exception ex) {
129: log("Launching handler for " + protid + " of class \""
130: + handler.getClass() + "\" failed.");
131: ex.printStackTrace();
132: try {
133: session.shutdown();
134: } catch (IOException exex) {
135: }
136: }
137: }
138:
139: /**
140: * Register a protocol handler for the given protocol identifier.
141: * This method register a class to handle all new connections for the
142: * given protocol identifier: each new connection will result in a new
143: * instance of the given class being created (the easy, but slow way).
144: * @param protid The protocol identifier.
145: * @param handler The name of the class to instantiate in order
146: * to get a suitable handler for that protocol.
147: * @see MuxProtocolHandler
148: */
149:
150: public void registerHandler(int protid, String strhandler) {
151: protocols.put(new Integer(protid), strhandler);
152: }
153:
154: /**
155: * Register an instantiated protocol handler for the given protocol id.
156: * This other method of registering protocol handler is faster then
157: * the previous one: it allows you to spare the instantiation of a protocol
158: * handler on each new sessions.
159: * <p>The given handler will be invoked for all new sessions willing to
160: * speak the advertized protocol.
161: * @param protid The protocol identifier.
162: * @param handler The instantiated protocol handler.
163: */
164:
165: public void registerHandler(int protid, MuxProtocolHandler handler) {
166: protocols.put(new Integer(protid), handler);
167: }
168:
169: /**
170: * Unregister any handler for that protocol.
171: * @param protid The identifier of the protocol to unregister.
172: */
173:
174: public void unregisterHandler(int protid) {
175: protocols.remove(new Integer(protid));
176: }
177:
178: /**
179: * Register default protocol handlers for that stream handler.
180: * This is the right method to override in order to either prevent
181: * well known protocols from being registered, or add new protocol
182: * handlers.
183: * <p>Default protocols registered by this class are:
184: * <dl>
185: * <dt>echo<dd>The echo protocol.
186: * <dt>discard<dd>The discard protocol.
187: * </dt>
188: */
189:
190: public void registerDefaultHandlers() {
191: registerHandler(ECHO, "org.w3c.www.mux.handlers.Echo");
192: registerHandler(DISCARD, "org.w3c.www.mux.handlers.Discard");
193: }
194:
195: public SampleMuxHandler() {
196: super ();
197: // Initialize instance variables:
198: this .protocols = new Hashtable();
199: // Register default protocols:
200: registerDefaultHandlers();
201: }
202:
203: }
|