sg.edu.nus.util
Class AbstractPooledSocketHandler

java.lang.Object
  extended by sg.edu.nus.util.AbstractPooledSocketHandler
All Implemented Interfaces:
java.lang.Runnable, PooledSocketHandler
Direct Known Subclasses:
EventDispatcherAdapter

public abstract class AbstractPooledSocketHandler
extends java.lang.Object
implements java.lang.Runnable, PooledSocketHandler

The class defines the default implementation of AbstractPooledSocketHandler.

All AbstractPooledSocketHandler will be blocked when there is no socket request in the pool of the socket request; and will be woke up when any socket request comes in and is inserted into the pool. To stop AbstractPooledSocketHandler, just set the stop signal as true.

AbstractPooledSocketServer is responsible for listening network requests from remote users and then passing the socket request to the pool.

An example to creating your own socket handler:

 public class MyPooledSocketHandler extends AbstractPooledSocketHandler
 {
        // used for getting input and output stream of the socket request
        private ObjectInputStream  ois;
        private ObjectOutputStream oos;
 
        // handle socket requests
        public void handleConnection()
        {
                // get input stream
                ois = new ObjectInputStream(connection.getInputStream());
                oos = new ObjectOutputStream(connection.getOutputStream());
                
                // process data stream
                ...             
 
                // close input and output stream
                ois.close();
                oos.close();
        }
 }
 

Version:
1.0 2006-1-12
Author:
Xu Linhao
See Also:
PooledSocketHandler, AbstractPooledSocketServer

Constructor Summary
AbstractPooledSocketHandler()
          Construct AbstractPooledSocketHandler.
 
Method Summary
static void processRequest(java.net.Socket requestToHandle)
          Accept any socket request from AbstractPooledSocketServer, and then insert the socke request to the end of the pool and notify all sleeped AbstractPooledSocketHandler to process it.
 void run()
          When a socket request comes in, all sleeped AbstractPooledSocketHandler will be woken up to handle with it.
 void stop()
          Set the stop flag as true.
static void stopAllHandlers()
          To stop all AbstractPooledSocketHandlers, the PooledSocketServer needs to close all socket requests that are stored in the pool.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface sg.edu.nus.util.PooledSocketHandler
handleConnection
 

Constructor Detail

AbstractPooledSocketHandler

public AbstractPooledSocketHandler()
Construct AbstractPooledSocketHandler.

Method Detail

processRequest

public static void processRequest(java.net.Socket requestToHandle)
Accept any socket request from AbstractPooledSocketServer, and then insert the socke request to the end of the pool and notify all sleeped AbstractPooledSocketHandler to process it.

Parameters:
requestToHandle - the incoming socket request to be processed

stopAllHandlers

public static void stopAllHandlers()
To stop all AbstractPooledSocketHandlers, the PooledSocketServer needs to close all socket requests that are stored in the pool.


run

public void run()
When a socket request comes in, all sleeped AbstractPooledSocketHandler will be woken up to handle with it. If no socket request arrives and the stop flag is false, then AbstractPooledSocketHandler will be blocked to wait for a socket request appearing.

Specified by:
run in interface java.lang.Runnable

stop

public void stop()
Set the stop flag as true.

Specified by:
stop in interface PooledSocketHandler