001: // HttpServerState.java
002: // $Id: HttpServerState.java,v 1.10 2005/05/27 15:00:31 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.protocol.http;
007:
008: import java.util.Vector;
009: import java.util.Enumeration;
010:
011: class HttpServerState {
012: HttpServer server = null;
013: Vector conns = null;
014:
015: protected int state = 0;
016: protected HttpException ex = null;
017: protected int num_conn = 0;
018:
019: protected static final int PREINIT = 0;
020: protected static final int ERROR = 1;
021: protected static final int OK = 2;
022:
023: private static final boolean debug = false;
024:
025: // Vector allconns = new Vector(); (used for debug)
026:
027: final HttpServer getServer() {
028: return server;
029: }
030:
031: synchronized void incrConnectionCount() {
032: ++num_conn;
033: }
034:
035: synchronized void decrConnectionCount() {
036: --num_conn;
037: }
038:
039: synchronized int getConnectionCount() {
040: return num_conn;
041: }
042:
043: synchronized boolean notEnoughConnections() {
044: return (conns == null) || (conns.size() == 0);
045: }
046:
047: synchronized void registerConnection(HttpConnection conn) {
048: if (conns == null) {
049: conns = new Vector(4);
050: }
051: conns.addElement(conn);
052: }
053:
054: synchronized void unregisterConnection(HttpConnection conn) {
055: if (conns != null) {
056: conns.removeElement(conn);
057: }
058: }
059:
060: synchronized void deleteConnection(HttpConnection conn) {
061: if (conns != null) {
062: conns.removeElement(conn);
063: }
064: }
065:
066: synchronized boolean hasConnection() {
067: return (conns != null) && (conns.size() > 0);
068: }
069:
070: synchronized HttpConnection getConnection() {
071: if ((conns != null) && (conns.size() > 0)) {
072: Enumeration e = conns.elements();
073: HttpConnection conn = null;
074: while (e.hasMoreElements()) {
075: HttpConnection tmp_conn = (HttpConnection) e
076: .nextElement();
077: if (tmp_conn.mayReuse()) {
078: conn = tmp_conn;
079: conns.removeElement(conn);
080: break;
081: }
082: }
083: if (conn == null) {
084: conn = (HttpConnection) conns.elementAt(0);
085: conns.removeElementAt(0);
086: }
087: conn.cached = true;
088: return conn;
089: }
090: return null;
091: }
092:
093: public String toString() {
094: String tostring = "";
095: if (conns == null)
096: tostring = "null";
097: else if (conns.size() == 0)
098: tostring = "empty";
099: else {
100: for (int i = 0; i < conns.size(); i++) {
101: tostring += "["
102: + ((HttpConnection) conns.elementAt(i))
103: .toString() + "]";
104: }
105: }
106: return "" + num_conn + tostring;
107: }
108:
109: HttpServerState(HttpServer server) {
110: this.server = server;
111: }
112: }
|