01: // MuxHttpHandler.java
02: // $Id: MuxHttpHandler.java,v 1.6 2000/08/16 21:37:42 ylafon Exp $
03: // (c) COPYRIGHT MIT and INRIA, 1997.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.jigsaw.http.mux;
07:
08: import java.io.IOException;
09:
10: import org.w3c.util.ThreadCache;
11:
12: import org.w3c.www.mux.MuxProtocolHandler;
13: import org.w3c.www.mux.MuxSession;
14:
15: import org.w3c.jigsaw.http.httpd;
16:
17: public class MuxHttpHandler implements MuxProtocolHandler {
18: protected httpd server = null;
19: protected int cid = -1;
20:
21: protected int clientcount = 0;
22: protected int maxclient = 50;
23:
24: protected ThreadCache threadcache = null;
25:
26: protected MuxClient freelist = null;
27:
28: private final synchronized MuxClient createClient() {
29: clientcount++;
30: return new MuxClient(server, this , ++cid);
31: }
32:
33: protected synchronized void markIdle(MuxClient client) {
34: client.next = freelist;
35: freelist = client;
36: notifyAll();
37: }
38:
39: protected synchronized MuxClient getClient() {
40: MuxClient client = null;
41: while (true) {
42: if (freelist != null) {
43: // Free client already available:
44: client = freelist;
45: freelist = client.next;
46: break;
47: } else if (clientcount + 1 < maxclient) {
48: // We're allowed to create new clients
49: client = createClient();
50: break;
51: } else {
52: // Wait for a free client
53: try {
54: wait();
55: } catch (InterruptedException ex) {
56: }
57: }
58: }
59: return client;
60: }
61:
62: public void initialize(MuxSession session) throws IOException {
63: // Find an idle MuxClient, bind and run it:
64: MuxClient client = getClient();
65: client.bind(session);
66: threadcache.getThread(client, true);
67: }
68:
69: public MuxHttpHandler(httpd server) {
70: this .server = server;
71: this .threadcache = new ThreadCache("mux-clients");
72: this .threadcache.setCachesize(10);
73: this.threadcache.setThreadPriority(server
74: .getClientThreadPriority());
75: this.threadcache.initialize();
76: }
77:
78: }
|