org.apache.excalibur.instrument.manager.http.server
Class AbstractSocketServer

java.lang.Object
  extended by org.apache.avalon.framework.logger.AbstractLogEnabled
      extended by org.apache.excalibur.instrument.AbstractLogEnabledInstrumentable
          extended by org.apache.excalibur.instrument.manager.http.server.AbstractLogEnabledInstrumentableStartable
              extended by org.apache.excalibur.instrument.manager.http.server.AbstractSocketServer
All Implemented Interfaces:
java.lang.Runnable, org.apache.avalon.framework.activity.Startable, org.apache.avalon.framework.logger.LogEnabled, org.apache.excalibur.instrument.Instrumentable
Direct Known Subclasses:
HTTPServer

abstract class AbstractSocketServer
extends AbstractLogEnabledInstrumentableStartable

Version:
$Revision: 1.6 $
Author:
Avalon Development Team

Field Summary
private  int m_backlog
          The backlog to assign to the server socket.
private  java.net.InetAddress m_bindAddr
          The address to bind the port server to.
private  org.apache.excalibur.instrument.CounterInstrument m_instrumentConnects
          Number of times that the server socket is connected to.
private  org.apache.excalibur.instrument.CounterInstrument m_instrumentDisconnects
          Number of times that a connection socket disconnects.
private  org.apache.excalibur.instrument.ValueInstrument m_instrumentOpenSockets
          Number of sockets that are connected at any given time.
private  java.util.List m_openSockets
          Used to track the number of open sockets.
private  int m_port
          The port to listen on for connections.
private  java.lang.Object m_semaphore
          Semaphore used to synchronize actions contained in this class.
private  java.net.ServerSocket m_serverSocket
          Reference to the ServerSocket.
private  long m_shutdownTimeout
          The time in ms after the component starts to shutdown that sockets will have to shutdown on their own before they are closed.
private  int m_soTimeout
          The SO_TIMEOUT to use for client sockets.
private  boolean m_started
          Flag which keeps track of when the server has been started.
 
Fields inherited from interface org.apache.excalibur.instrument.Instrumentable
EMPTY_INSTRUMENT_ARRAY, EMPTY_INSTRUMENTABLE_ARRAY
 
Constructor Summary
AbstractSocketServer(int port, java.net.InetAddress bindAddress)
          Creates a new AbstractSocketServer.
 
Method Summary
protected  int getSoTimeout()
           
protected abstract  void handleSocket(java.net.Socket socket)
          Handle a newly connected socket.
private  void handleSocketInner(java.net.Socket socket)
          Keeps track of instrumentation related to the life of a socket as well as handles any errors encountered while handling the socket in the user code.
protected  void runner()
          Runner method that will be called when the component is started.
 void start()
          Starts the runner thread.
protected  void stopRunner()
          Called when the component is being stopped, the isStopping method will always return true when this method is called.
 
Methods inherited from class org.apache.excalibur.instrument.manager.http.server.AbstractLogEnabledInstrumentableStartable
isStopping, run, stop
 
Methods inherited from class org.apache.excalibur.instrument.AbstractLogEnabledInstrumentable
addChildInstrumentable, addInstrument, getChildInstrumentables, getInstrumentableName, getInstruments, setInstrumentableName
 
Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_semaphore

private java.lang.Object m_semaphore
Semaphore used to synchronize actions contained in this class. This is to avoid unintended problems with synchronization performed by subclasses this is important here because this is a server and actions from one thread can cause a call out through a socket into another thread and cause deadlocks.


m_port

private int m_port
The port to listen on for connections.


m_backlog

private int m_backlog
The backlog to assign to the server socket.


m_bindAddr

private java.net.InetAddress m_bindAddr
The address to bind the port server to. Null for any address.


m_soTimeout

private int m_soTimeout
The SO_TIMEOUT to use for client sockets.


m_shutdownTimeout

private long m_shutdownTimeout
The time in ms after the component starts to shutdown that sockets will have to shutdown on their own before they are closed.


m_started

private boolean m_started
Flag which keeps track of when the server has been started.


m_serverSocket

private java.net.ServerSocket m_serverSocket
Reference to the ServerSocket.


m_openSockets

private java.util.List m_openSockets
Used to track the number of open sockets.


m_instrumentConnects

private org.apache.excalibur.instrument.CounterInstrument m_instrumentConnects
Number of times that the server socket is connected to.


m_instrumentOpenSockets

private org.apache.excalibur.instrument.ValueInstrument m_instrumentOpenSockets
Number of sockets that are connected at any given time.


m_instrumentDisconnects

private org.apache.excalibur.instrument.CounterInstrument m_instrumentDisconnects
Number of times that a connection socket disconnects.

Constructor Detail

AbstractSocketServer

public AbstractSocketServer(int port,
                            java.net.InetAddress bindAddress)
Creates a new AbstractSocketServer.

Parameters:
port - The port on which the server will listen.
bindAddress - The address on which the server will listen for connections.
Method Detail

start

public void start()
           throws java.lang.Exception
Starts the runner thread.

Specified by:
start in interface org.apache.avalon.framework.activity.Startable
Overrides:
start in class AbstractLogEnabledInstrumentableStartable
Throws:
java.lang.Exception - If there are any problems.

stopRunner

protected void stopRunner()
                   throws java.lang.Exception
Called when the component is being stopped, the isStopping method will always return true when this method is called.

Overrides:
stopRunner in class AbstractLogEnabledInstrumentableStartable
Throws:
java.lang.Exception - If there are any problems

runner

protected void runner()
Runner method that will be called when the component is started. The method must monitor the isStopping() method and make sure that it returns in a timely manner when the isStopping() method returns true.

Specified by:
runner in class AbstractLogEnabledInstrumentableStartable

handleSocket

protected abstract void handleSocket(java.net.Socket socket)
Handle a newly connected socket. The implementation need not worry about closing the socket.

Parameters:
socket - Newly connected Socket to be handled.

handleSocketInner

private void handleSocketInner(java.net.Socket socket)
Keeps track of instrumentation related to the life of a socket as well as handles any errors encountered while handling the socket in the user code.

Parameters:
socket - The socket to be handled.

getSoTimeout

protected int getSoTimeout()