001: package com.quadcap.net.server;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042:
043: import java.net.ServerSocket;
044: import java.net.Socket;
045:
046: import com.quadcap.util.Debug;
047:
048: /**
049: * Implements the main socket listening thread: we first obtain a block
050: * of idle workers (currently 4, should be configurable though.)
051: * then accept connections and pass them out until we need more workers.
052: *
053: * @author Stan Bailes
054: */
055: public class Acceptor implements Runnable {
056: Server server;
057: int port;
058: ServerSocket serverSocket;
059: boolean terminate = false;
060:
061: public Acceptor(Server server, int port, int queueDepth)
062: throws IOException {
063: this .server = server;
064: this .port = port;
065: this .serverSocket = new ServerSocket(port, queueDepth);
066: }
067:
068: public void run() {
069: while (!terminate) {
070: try {
071: Worker w = server.getIdleWorker();
072: Socket socket = null;
073: try {
074: socket = serverSocket.accept();
075: w.handle(socket, port);
076: } catch (Throwable e) {
077: Debug.print(e);
078: server.returnIdleWorker(w);
079: }
080: } catch (Throwable e) {
081: Debug.print(e);
082: terminate = true;
083: }
084: }
085: }
086:
087: static final int WCNT = 4;
088:
089: public void runxxx() {
090: Worker[] wv = new Worker[WCNT];
091: int valid = -1;
092: int pos = 0;
093: while (!terminate) {
094: try {
095: while (pos >= valid) {
096: valid = server.getIdleWorkers(wv);
097: pos = 0;
098: }
099: while (pos < valid && !terminate) {
100: Worker w = wv[pos++];
101: try {
102: Socket socket = serverSocket.accept();
103: w.handle(socket, port);
104: } catch (Throwable e) {
105: Debug.print(e);
106: server.returnIdleWorker(w);
107: }
108: }
109: } catch (Exception e) {
110: Debug.print(e);
111: terminate = true;
112: }
113: }
114: }
115:
116: public void stop() {
117: }
118: }
|