001: // MuxClient.java
002: // $Id: MuxClient.java,v 1.6 2000/08/16 21:37:42 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.jigsaw.http.mux;
007:
008: import java.io.BufferedOutputStream;
009: import java.io.DataOutputStream;
010: import java.io.IOException;
011: import java.io.PrintStream;
012:
013: import java.net.InetAddress;
014:
015: import org.w3c.www.mux.MuxSession;
016:
017: import org.w3c.www.http.HttpMessage;
018:
019: import org.w3c.jigsaw.http.Client;
020: import org.w3c.jigsaw.http.Reply;
021: import org.w3c.jigsaw.http.Request;
022: import org.w3c.jigsaw.http.httpd;
023:
024: public class MuxClient extends Client implements Runnable {
025: /**
026: * The InetAddress of the session we are currently handling.
027: */
028: private InetAddress addr = null;
029: /**
030: * The MuxSession we are currently handling.
031: */
032: private MuxSession session = null;
033: /**
034: * The MuxHttpHandler that createed us.
035: */
036: private MuxHttpHandler handler = null;
037: /**
038: * MuxHttpHandler maintained klist of clients.
039: */
040: MuxClient next = null;
041: /**
042: * The thread powering that client connection.
043: */
044: protected Thread thread = null;
045:
046: protected boolean tryKeepConnection(Request request, Reply reply) {
047: reply.addConnection("close");
048: return false;
049: }
050:
051: /**
052: * Client implementation - Get the IP address of this client.
053: * @return An InetAddress instance, or <strong>null</strong> if the
054: * client is not currently running.
055: */
056:
057: public InetAddress getInetAddress() {
058: return addr;
059: }
060:
061: /**
062: * Run HTTP on the newly created mux session.
063: */
064:
065: public void run() {
066: try {
067: startConnection(session.getInputStream(),
068: (new DataOutputStream(new BufferedOutputStream(
069: session.getOutputStream()))));
070: } catch (Exception ex) {
071: System.out.println(this + ": erred !");
072: ex.printStackTrace();
073: }
074: }
075:
076: /**
077: * Client implementation - The current connection is now idle.
078: * We always close the mux session at that time, since creating a new
079: * mux session has nearly no overhead.
080: */
081:
082: protected boolean idleConnection() {
083: return true;
084: }
085:
086: /**
087: * Client implementation - The current connection is now in use.
088: * Nothing special done.
089: */
090:
091: protected void usedConnection() {
092: return;
093: }
094:
095: /**
096: * Client implementation - The current connection was terminated.
097: * We make sure the underlying mux session is closed properly, and
098: * terminate the underlying thread.
099: */
100:
101: protected void stopConnection() {
102: // Terminate the session properly:
103: try {
104: session.shutdown();
105: } catch (IOException ex) {
106: ex.printStackTrace();
107: }
108: session = null;
109: // Mark that MuxClient instance ready for re-used.
110: handler.markIdle(this );
111: }
112:
113: /**
114: * Bind that client to the given connection.
115: * @param session The mux session to handle.
116: */
117:
118: protected void bind(MuxSession session) throws IOException {
119: this .session = session;
120: this .addr = session.getInetAddress();
121: }
122:
123: /**
124: * Get the thread powering that client.
125: * @return A Thread instance, or <strong>null</strong>.
126: */
127:
128: protected Thread getThread() {
129: return thread;
130: }
131:
132: MuxClient(httpd server, MuxHttpHandler handler, int identifier) {
133: initialize(server, identifier);
134: this.handler = handler;
135: }
136:
137: }
|