sg.edu.nus.util
Class AbstractPooledSocketServer

java.lang.Object
  extended by sg.edu.nus.util.AbstractPooledSocketServer
All Implemented Interfaces:
java.lang.Runnable, PooledSocketServer
Direct Known Subclasses:
AbstractEventManager

public abstract class AbstractPooledSocketServer
extends java.lang.Object
implements java.lang.Runnable, PooledSocketServer

This class defines the default implementation of the PooledSocketServer, by wrapping the java.net.ServerSocket.

The AbstractPooledSocketServer is responsible for initializing a set of AbstractPooledSocketHandlers, each of which is responsible for processing the incoming socket requests.

An example for creating your own pooled socket server:

 public class MyPooledSocketServer extends AbstractPooledSocketServer
 {
        // declare socket handlers
        protected MyPooledSocketHandler[] eventHandler;
        
        // constructor
        public MyPooledSocketServer(int port, int maxConn)
        {
                super(port, maxConn);
                this.gui = gui;
        }
 
        // setup handlers
        public void setupHandlers()
        {
                this.eventHandlers = new MyPooledSocketHandler[this.maxConn];
                for (int i = 0; i < maxConn; i++)
                {
                        eventHandlers[i] = new MyPooledSocketHandler();
                        eventHandlers[i].registerActionListeners();
                        new Thread(eventHandlers[i], "Event Handler " + i).start();
                }
        }
 
        // stop server
        public void stop()
        {
                ....
        }
 }
 

Version:
1.0 2005-12-29
Author:
Xu Linhao
See Also:
PooledSocketServer, AbstractPooledSocketHandler

Constructor Summary
AbstractPooledSocketServer(int port, int maxConn)
          Construct AbstractPooledSocketServer with specified parameters.
 
Method Summary
 void acceptConnections()
          Accept any incoming socket request from remote users and dispatch it to a sleeping PooledSocketHanlder.
 boolean isAlive()
          Returns true if AbstractPooledSocketServer is still running.
 void run()
          Initiate ServerSocket that are prepared for accepting any incoming socket request and all AbstractPooledSocketHandlers that are prepared for processing the socket request.
abstract  void setupHandlers()
          Initiate a set of AbstractPooledSocketHandlers, the number of which is specified by the parameter maxConn.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface sg.edu.nus.util.PooledSocketServer
stop
 

Constructor Detail

AbstractPooledSocketServer

public AbstractPooledSocketServer(int port,
                                  int maxConn)
Construct AbstractPooledSocketServer with specified parameters.

Parameters:
port - the port used for running ServerSocket
maxConn - the maximum connections that can be handled simultaneously
Method Detail

acceptConnections

public void acceptConnections()
Description copied from interface: PooledSocketServer
Accept any incoming socket request from remote users and dispatch it to a sleeping PooledSocketHanlder.

Specified by:
acceptConnections in interface PooledSocketServer

setupHandlers

public abstract void setupHandlers()
Initiate a set of AbstractPooledSocketHandlers, the number of which is specified by the parameter maxConn.

Specified by:
setupHandlers in interface PooledSocketServer

isAlive

public boolean isAlive()
Returns true if AbstractPooledSocketServer is still running.

Returns:
true if AbstractPooledSocketServer is still running; otherwise, return false

run

public void run()
Initiate ServerSocket that are prepared for accepting any incoming socket request and all AbstractPooledSocketHandlers that are prepared for processing the socket request.

Specified by:
run in interface java.lang.Runnable