Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigsaw / http / mux / MuxHttpHandler.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigsaw/http/mux/MuxHttpHandler.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigsaw/http/mux/MuxHttpHandler.java
new file mode 100644 (file)
index 0000000..dd6878e
--- /dev/null
@@ -0,0 +1,79 @@
+// MuxHttpHandler.java\r
+// $Id: MuxHttpHandler.java,v 1.1 2010/06/15 12:24:58 smhuang Exp $  \r
+// (c) COPYRIGHT MIT and INRIA, 1997.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigsaw.http.mux;\r
+\r
+import java.io.IOException;\r
+\r
+import org.w3c.util.ThreadCache;\r
+\r
+import org.w3c.www.mux.MuxProtocolHandler;\r
+import org.w3c.www.mux.MuxSession;\r
+\r
+import org.w3c.jigsaw.http.httpd;\r
+\r
+public class MuxHttpHandler implements MuxProtocolHandler {\r
+    protected httpd         server  = null;\r
+    protected int           cid     = -1;\r
+\r
+    protected int clientcount = 0;\r
+    protected int maxclient   = 50;\r
+\r
+    protected ThreadCache   threadcache = null;\r
+\r
+    protected MuxClient freelist = null;\r
+\r
+    private final synchronized MuxClient createClient() {\r
+       clientcount++;\r
+       return new MuxClient(server, this, ++cid);\r
+    }\r
+\r
+    protected synchronized void markIdle(MuxClient client) {\r
+       client.next = freelist;\r
+       freelist    = client;\r
+       notifyAll();\r
+    }\r
+\r
+    protected synchronized MuxClient getClient() {\r
+       MuxClient client = null;\r
+       while ( true ) {\r
+           if ( freelist != null ) {\r
+               // Free client already available:\r
+               client   = freelist;\r
+               freelist = client.next;\r
+               break;\r
+           } else if ( clientcount+1 < maxclient ) {\r
+               // We're allowed to create new clients\r
+               client = createClient();\r
+               break;\r
+           } else {\r
+               // Wait for a free client\r
+               try {\r
+                   wait();\r
+               } catch (InterruptedException ex) {\r
+               }\r
+           }\r
+       }\r
+       return client;\r
+    }\r
+\r
+    public void initialize(MuxSession session) \r
+       throws IOException\r
+    {\r
+       // Find an idle MuxClient, bind and run it:\r
+       MuxClient client = getClient();\r
+       client.bind(session);\r
+       threadcache.getThread(client, true);\r
+    }\r
+\r
+    public MuxHttpHandler(httpd server) {\r
+       this.server      = server;\r
+       this.threadcache = new ThreadCache("mux-clients");\r
+       this.threadcache.setCachesize(10);\r
+       this.threadcache.setThreadPriority(server.getClientThreadPriority());\r
+       this.threadcache.initialize();\r
+    }\r
+\r
+}\r