atg.server.tcp
Class RequestServer

java.lang.Object
  extended by atg.nucleus.logging.VariableArgumentApplicationLoggingImpl
      extended by atg.nucleus.GenericService
          extended by atg.nucleus.TimedOperationService
              extended by atg.server.tcp.RequestServer
All Implemented Interfaces:
NameContextBindingListener, NameContextElement, NameResolver, AdminableService, ApplicationLogging, atg.nucleus.logging.ApplicationLoggingSender, atg.nucleus.logging.TraceApplicationLogging, atg.nucleus.logging.VariableArgumentApplicationLogging, ComponentNameResolver, Service, ServiceListener, java.util.EventListener
Direct Known Subclasses:
InvalidationRelay

public class RequestServer
extends TimedOperationService

This is the base class for Nucleus services that handle TCP requests. This will create a set of RequestServerHandler objects to handle incoming connections. This class understands the ServerImpl interface, in that it knows how to start and stop itself.

This class also keeps basic statistics about its operation, including total number of requests served, current number of requests being served, and average time spent on each request.

Subclasses should override the "createHandler" method to create a new subclass of RequestServerHandler.

The server can be configured to run in "standby" or "one-shot" mode. In "standby" mode, a number of handler threads are created at startup and kept around to handle requests. That number is specified by the property "handlerCount". In "one-shot" mode, handlers are created whenever a request comes in and destroyed after the request is complete. No handlers are kept around in this case. This mode is triggered by setting "handlerCount" to 0.


Field Summary
static java.lang.String CLASS_VERSION
          Class version string
static int DEFAULT_BACKLOG
          The default number of connections that get queued up to a "connect" system call.
protected  java.lang.String mAllowedConnection
          Set to a regular expression pattern of ip addresses allowed to connect
protected  atg.core.util.MiniRegex mAllowedConnectionMatcher
          The regular expression matcher for ip addresses
protected  int mBacklog
          The backlog, or 0 if default
protected  boolean mEnabled
          If false, prevent this server from starting up
protected  int mHandlerCount
          The number of handlers
protected  java.net.InetAddress mHostAddr
          The host address of the server
protected  java.lang.String mHostName
          The hostname of the server
protected  int mLocalPort
          This is the actual port we are listening on.
protected  int mPort
          The port this will use to listen for connections
protected  atg.service.portregistry.PortRegistry mPortRegistry
          The PortRegistry to whom this will report its port usage
protected  int mReadTimeout
          Maximum number of seconds (not millis) to block while reading input.
 
Fields inherited from class atg.nucleus.GenericService
SERVICE_INFO_KEY
 
Fields inherited from interface atg.nucleus.logging.TraceApplicationLogging
DEFAULT_LOG_TRACE_STATUS
 
Fields inherited from interface atg.nucleus.logging.ApplicationLogging
DEFAULT_LOG_DEBUG_STATUS, DEFAULT_LOG_ERROR_STATUS, DEFAULT_LOG_INFO_STATUS, DEFAULT_LOG_WARNING_STATUS
 
Constructor Summary
RequestServer()
          Constructs a new RequestServer
 
Method Summary
 java.net.Socket acceptConnection()
          Accepts and returns one connection.
 void acceptConnections()
          Repeatedly accepts connections and spins off handlers to handle those connections.
 RequestServerHandler createAndStartHandler()
          Constructs and starts a new RequestServerHandler without any socket.
 RequestServerHandler createAndStartHandlerIfNeeded(RequestServerHandler handler, int deathType)
          Called when a RequestServerHandler changes state, either from being turned into a zombie or from see if a new handler should be started socket.
protected  RequestServerHandler createHandler(java.lang.ThreadGroup pGroup, java.lang.String pThreadName, java.net.Socket pSocket)
          Constructs a new RequestServerHandler.
protected  java.net.ServerSocket createServerSocket(int pPort, int pBacklog, java.net.InetAddress pHostAddr)
          Create a ServerSocket used by the RequestServer.
 void doStartService()
          If the server is enabled this will start the server running, and create the minimum number of clients.
 void doStopService()
          This will stop the server from running by shutting down the server socket.
 int getAcceptorPriorityDelta()
          Gets the acceptor thread priority delta.
 int getActiveHandlerCount()
          Returns the number of handlers currently handling requests.
 java.lang.String getAllowedConnection()
          Returns the AllowedConnection property.
 int getBacklog()
          Returns the backlog that will be set to the server socket.
 boolean getCloseSocketWhenStopped()
          Close the listener socket when this server is stopped?
 atg.server.tcp.ConnectionAcceptor getConnectionAcceptor()
          Returns the connection acceptor used to acquire connected clients for the handler threads.
 boolean getEnabled()
          Returns the Enabled property.
 int getHandlerCount()
          Returns the number of handlers that will be used to listen for requests.
 java.net.InetAddress getHostAddr()
          Returns the host address of the server
 java.lang.String getHostName()
          Returns the hostname of the server
 int getLocalPort()
          Returns the port this server is listening on.
 int getMaxZombieHandlerCount()
          Returns the maximum number of handlers that can be in a killed state.
 int getNewHandlerRequestCount()
          Returns the number of handlers which have been requested, but have not yet started.
 int getPort()
          Returns the port this server is listening on.
 atg.service.portregistry.PortRegistry getPortRegistry()
          Returns the PortRegistry to whom this will report which port this is using.
 int getReadTimeout()
          Gets the maximum number of seconds (not millis) to block when reading input.
 RequestServerHandler[] getRequestHandlers()
          Returns an array of the RequestHandler objects running in the server.
protected  atg.server.tcp.RequestServerSocket getRequestServerSocket()
           
 int getRunningHandlerCount()
          Returns the total number of handlers, including those that are currently handling connections, and those that are waiting for connections.
 javax.net.ServerSocketFactory getServerSocketFactory()
          Returns property ServerSocketFactory
 long getWaitForRequestHandlersToStop()
          Gets the amount of time in milliseconds to wait until all request handlers have completed, during doStopService()
 int getWaitingHandlerCount()
          Returns the number of handlers waiting for new requests.
 int getZombieHandlerCount()
          Returns the number of handlers which have been killed, but have not yet handled the ThreadDeath.
 boolean isOutOfMemoryDetected()
          Was an out of memory exception detected?
 boolean isSuccessfulServerStartup()
          get SuccessfulServerStartup
 boolean isUseServerSocketFactory()
          Returns property UseServerSocketFactory
 void setAcceptorPriorityDelta(int pDelta)
          Sets the acceptor thread priority delta.
 void setAllowedConnection(java.lang.String pAllowedConnection)
          Sets a regular expression against which connecting hosts are matched to see if they are allowed to connect.
 void setBacklog(int pBacklog)
          Sets the backlog that will be set to the server socket.
 void setCloseSocketWhenStopped(boolean pClose)
          Close the listener socket when this server is stopped?
 void setConnectionAcceptor(atg.server.tcp.ConnectionAcceptor pAcceptor)
          Changes the connection acceptor used to acquire connected clients for the handler threads.
 void setEnabled(boolean pEnabled)
          Sets the Enabled flag.
 void setHandlerCount(int pHandlerCount)
          Sets the number of handlers that will be used to listen for requests.
 void setHostAddr(java.net.InetAddress pHostAddr)
          Sets the host address of the server
 void setHostName(java.lang.String pHostName)
          Sets the host name explicitly.
 void setMaxZombieHandlerCount(int pMaxZombieHandlerCount)
          Sets the maximum number of handlers that can be in a killed state.
 void setOutOfMemoryDetected(boolean pOutOfMemoryDetected)
          Was an out of memory exception detected?
 void setPort(int pPort)
          Sets the port this uses to listen for connections.
 void setPortRegistry(atg.service.portregistry.PortRegistry pPortRegistry)
          Sets the PortRegistry to whom this will report which port this is using.
 void setReadTimeout(int pReadTimeout)
          Sets the maximum number of seconds (not millis) to block when reading input.
 void setServerSocketFactory(javax.net.ServerSocketFactory pServerSocketFactory)
          Sets property ServerSocketFactory
 void setSocketReadTimeout(java.net.Socket pSocket)
          Attempt to set the read timeout for the specified socket.
 void setSuccessfulServerStartup(boolean pSuccessfulServerStartup)
          set SuccessfulServerStartup
 void setUseServerSocketFactory(boolean pUseServerSocketFactory)
          Sets property UseServerSocketFactory
 void setWaitForRequestHandlersToStop(long pWait)
          Sets the amount of time in milliseconds to wait until all request handlers have completed, during doStopService()
 void startServer()
          This will start the server running, and create the minimum number of handlers.
 
Methods inherited from class atg.nucleus.TimedOperationService
getAverageRequestHandlingTime, getHandledRequestCount, getRequestStartTime, getTotalRequestHandlingTime, isKeepingStatistics, notifyHandledRequest, resetStatistics, setKeepingStatistics
 
Methods inherited from class atg.nucleus.GenericService
addLogListener, createAdminServlet, getAbsoluteName, getAdminServlet, getLoggingForVlogging, getLogListenerCount, getLogListeners, getName, getNameContext, getNucleus, getRoot, getServiceConfiguration, getServiceInfo, isLoggingDebug, isLoggingError, isLoggingInfo, isLoggingTrace, isLoggingWarning, isRunning, logDebug, logDebug, logDebug, logError, logError, logError, logInfo, logInfo, logInfo, logTrace, logTrace, logTrace, logWarning, logWarning, logWarning, nameContextElementBound, nameContextElementUnbound, removeLogListener, resolveName, resolveName, resolveName, resolveName, sendLogEvent, setLoggingDebug, setLoggingError, setLoggingInfo, setLoggingTrace, setLoggingWarning, setNucleus, setServiceInfo, startService, stopService
 
Methods inherited from class atg.nucleus.logging.VariableArgumentApplicationLoggingImpl
vlogDebug, vlogDebug, vlogDebug, vlogDebug, vlogError, vlogError, vlogError, vlogError, vlogInfo, vlogInfo, vlogInfo, vlogInfo, vlogTrace, vlogTrace, vlogTrace, vlogTrace, vlogWarning, vlogWarning, vlogWarning, vlogWarning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS_VERSION

public static java.lang.String CLASS_VERSION
Class version string


DEFAULT_BACKLOG

public static final int DEFAULT_BACKLOG
The default number of connections that get queued up to a "connect" system call. On UNIX systems, this seems to be ignored but it is necessary for NT. The default in the socket implementation is 50 which has been demonstrated to be way too low.

See Also:
Constant Field Values

mPort

protected int mPort
The port this will use to listen for connections


mLocalPort

protected int mLocalPort
This is the actual port we are listening on. It is only different from the Port if Port is set to 0 (i.e. choose an available port)


mHostName

protected java.lang.String mHostName
The hostname of the server


mHostAddr

protected java.net.InetAddress mHostAddr
The host address of the server


mHandlerCount

protected int mHandlerCount
The number of handlers


mBacklog

protected int mBacklog
The backlog, or 0 if default


mPortRegistry

protected atg.service.portregistry.PortRegistry mPortRegistry
The PortRegistry to whom this will report its port usage


mEnabled

protected boolean mEnabled
If false, prevent this server from starting up


mAllowedConnection

protected java.lang.String mAllowedConnection
Set to a regular expression pattern of ip addresses allowed to connect


mAllowedConnectionMatcher

protected atg.core.util.MiniRegex mAllowedConnectionMatcher
The regular expression matcher for ip addresses


mReadTimeout

protected int mReadTimeout
Maximum number of seconds (not millis) to block while reading input.

Constructor Detail

RequestServer

public RequestServer()
Constructs a new RequestServer

Method Detail

getRequestServerSocket

protected atg.server.tcp.RequestServerSocket getRequestServerSocket()

createAndStartHandler

public RequestServerHandler createAndStartHandler()
Constructs and starts a new RequestServerHandler without any socket. determine a unique name for the Thread running the handler.


createHandler

protected RequestServerHandler createHandler(java.lang.ThreadGroup pGroup,
                                             java.lang.String pThreadName,
                                             java.net.Socket pSocket)
Constructs a new RequestServerHandler. Subclasses should override this to create their own subclasses of RequestServerHandler.

Parameters:
pGroup - the ThreadGroup to which the new handler should belong
pThreadName - the name that should be assigned to the handler's thread.
pSocket - the socket that the handler should use to perform its one-shot request. If null, then the handler should obtain sockets by calling "acceptConnection".

createAndStartHandlerIfNeeded

public RequestServerHandler createAndStartHandlerIfNeeded(RequestServerHandler handler,
                                                          int deathType)
Called when a RequestServerHandler changes state, either from being turned into a zombie or from see if a new handler should be started socket. determine a unique name for the Thread running the handler.


acceptConnection

public java.net.Socket acceptConnection()
                                 throws DisallowedConnectionException
Accepts and returns one connection. If null is returned, then the server has shut down and all the handlers should exit.

Throws:
DisallowedConnectionException

acceptConnections

public void acceptConnections()
Repeatedly accepts connections and spins off handlers to handle those connections.


setPort

public void setPort(int pPort)
Sets the port this uses to listen for connections. Set this value to 0 if you want the server to choose an available port. You can then call getLocalPort to determine which port we are listening to.


getPort

public int getPort()
Returns the port this server is listening on. This is only different from the port if port is set to 0.


getLocalPort

public int getLocalPort()
Returns the port this server is listening on. This is only different from the port if port is set to 0.


getHostAddr

public java.net.InetAddress getHostAddr()
Returns the host address of the server


setHostAddr

public void setHostAddr(java.net.InetAddress pHostAddr)
Sets the host address of the server


setHostName

public void setHostName(java.lang.String pHostName)
Sets the host name explicitly. If this is not set, the default is chosen.


getHostName

public java.lang.String getHostName()
Returns the hostname of the server


setServerSocketFactory

public void setServerSocketFactory(javax.net.ServerSocketFactory pServerSocketFactory)
Sets property ServerSocketFactory


getServerSocketFactory

public javax.net.ServerSocketFactory getServerSocketFactory()
Returns property ServerSocketFactory


setUseServerSocketFactory

public void setUseServerSocketFactory(boolean pUseServerSocketFactory)
Sets property UseServerSocketFactory


isUseServerSocketFactory

public boolean isUseServerSocketFactory()
Returns property UseServerSocketFactory


setHandlerCount

public void setHandlerCount(int pHandlerCount)
Sets the number of handlers that will be used to listen for requests.


getMaxZombieHandlerCount

public int getMaxZombieHandlerCount()
Returns the maximum number of handlers that can be in a killed state. When the max value is reached, then new handlers will not be created until some of the zombies start dying off. A positive value means that we track zombie handlers up to the given number; a negative value means that we will continue to create new handlers as handlers are killed; a value of 0 means that we will not track zombie handlers at all, and rather will count threads as running until they actually die.


setMaxZombieHandlerCount

public void setMaxZombieHandlerCount(int pMaxZombieHandlerCount)
Sets the maximum number of handlers that can be in a killed state. When the max value is reached, then new handlers will not be created until some of the zombies start dying off. A positive value means that we track zombie handlers up to the given number; a negative value means that we will continue to create new handlers as handlers are killed; a value of 0 means that we will not track zombie handlers at all, and rather will count threads as running until they actually die.


getHandlerCount

public int getHandlerCount()
Returns the number of handlers that will be used to listen for requests.


setBacklog

public void setBacklog(int pBacklog)
Sets the backlog that will be set to the server socket. Setting to 0 sets to the default (512).


getBacklog

public int getBacklog()
Returns the backlog that will be set to the server socket. Setting to 0 sets to the default (512).


setPortRegistry

public void setPortRegistry(atg.service.portregistry.PortRegistry pPortRegistry)
Sets the PortRegistry to whom this will report which port this is using.


getPortRegistry

public atg.service.portregistry.PortRegistry getPortRegistry()
Returns the PortRegistry to whom this will report which port this is using.


setEnabled

public void setEnabled(boolean pEnabled)
Sets the Enabled flag. If the server is disabled. It does nothing. Right now, this flag is only looked at when the services starts.


getEnabled

public boolean getEnabled()
Returns the Enabled property.


setSuccessfulServerStartup

public void setSuccessfulServerStartup(boolean pSuccessfulServerStartup)
set SuccessfulServerStartup

Parameters:
pSuccessfulServerStartup - the SuccessfulServerStartup

isSuccessfulServerStartup

public boolean isSuccessfulServerStartup()
get SuccessfulServerStartup

Returns:
the SuccessfulServerStartup

setAllowedConnection

public void setAllowedConnection(java.lang.String pAllowedConnection)
Sets a regular expression against which connecting hosts are matched to see if they are allowed to connect.


getAllowedConnection

public java.lang.String getAllowedConnection()
Returns the AllowedConnection property.


setAcceptorPriorityDelta

public void setAcceptorPriorityDelta(int pDelta)
Sets the acceptor thread priority delta. A negative number means that the acceptor thread will run at a lower priority than the threads that are accepting new connections. This is generally the right thing.

This property is only used by the DRP server in connection pooling mode.


getWaitForRequestHandlersToStop

public long getWaitForRequestHandlersToStop()
Gets the amount of time in milliseconds to wait until all request handlers have completed, during doStopService()


setWaitForRequestHandlersToStop

public void setWaitForRequestHandlersToStop(long pWait)
Sets the amount of time in milliseconds to wait until all request handlers have completed, during doStopService()


getAcceptorPriorityDelta

public int getAcceptorPriorityDelta()
Gets the acceptor thread priority delta. A negative number means that the acceptor thread will run at a lower priority than the threads that are accepting new connections. This is generally the right thing.

This property is only used by the DRP server in connection pooling mode.


setReadTimeout

public void setReadTimeout(int pReadTimeout)
Sets the maximum number of seconds (not millis) to block when reading input. 0 == indefinitely.

Parameters:
int - the maximum number of seconds

getReadTimeout

public int getReadTimeout()
Gets the maximum number of seconds (not millis) to block when reading input. 0 == indefinitely.

Returns:
int the number of seconds

getRunningHandlerCount

public int getRunningHandlerCount()
Returns the total number of handlers, including those that are currently handling connections, and those that are waiting for connections.


getActiveHandlerCount

public int getActiveHandlerCount()
Returns the number of handlers currently handling requests.


getZombieHandlerCount

public int getZombieHandlerCount()
Returns the number of handlers which have been killed, but have not yet handled the ThreadDeath.


getNewHandlerRequestCount

public int getNewHandlerRequestCount()
Returns the number of handlers which have been requested, but have not yet started.


getWaitingHandlerCount

public int getWaitingHandlerCount()
Returns the number of handlers waiting for new requests.


getCloseSocketWhenStopped

public boolean getCloseSocketWhenStopped()
Close the listener socket when this server is stopped?


setCloseSocketWhenStopped

public void setCloseSocketWhenStopped(boolean pClose)
Close the listener socket when this server is stopped?


getConnectionAcceptor

public atg.server.tcp.ConnectionAcceptor getConnectionAcceptor()
Returns the connection acceptor used to acquire connected clients for the handler threads.


setConnectionAcceptor

public void setConnectionAcceptor(atg.server.tcp.ConnectionAcceptor pAcceptor)
Changes the connection acceptor used to acquire connected clients for the handler threads.


isOutOfMemoryDetected

public boolean isOutOfMemoryDetected()
Was an out of memory exception detected?


setOutOfMemoryDetected

public void setOutOfMemoryDetected(boolean pOutOfMemoryDetected)
Was an out of memory exception detected?


getRequestHandlers

public RequestServerHandler[] getRequestHandlers()
Returns an array of the RequestHandler objects running in the server. This list is obtained by finding all of the Threads running in the RequestServer's ThreadGroup that are of type RequestHandler.


createServerSocket

protected java.net.ServerSocket createServerSocket(int pPort,
                                                   int pBacklog,
                                                   java.net.InetAddress pHostAddr)
                                            throws java.io.IOException
Create a ServerSocket used by the RequestServer. The ServerSocket may be created by a ServerSocketFactory if one is supplied by the serverSocketFactory property. Otherwise a new ServerSocket is created. ServerSockets are created by with the parameters supplied by pPort, pBackLog and pHostAddr.

Parameters:
pPort - port number on which to listen
pBacklog - the backlog of pending connection before rejection of connections
pHostAddr - host address of network interface where connections accepted
Returns:
a ServerSocket to be used by the RequestServer
Throws:
java.io.IOException - is an erro occured while creating the ServerSocket

startServer

public void startServer()
This will start the server running, and create the minimum number of handlers.


doStartService

public void doStartService()
                    throws ServiceException
If the server is enabled this will start the server running, and create the minimum number of clients.

Overrides:
doStartService in class GenericService
Throws:
ServiceException - if there was a problem

doStopService

public void doStopService()
                   throws ServiceException
This will stop the server from running by shutting down the server socket. Any handlers already handling requests will be allowed to finish.

Overrides:
doStopService in class GenericService
Throws:
ServiceException - if an error occurred during the operation

setSocketReadTimeout

public void setSocketReadTimeout(java.net.Socket pSocket)
Attempt to set the read timeout for the specified socket.

Parameters:
Socket - -- the socket whose read timeout to set