ecologylab.services.distributed.server
Class DoubleThreadedNIOServer

java.lang.Object
  extended by ecologylab.generic.Debug
      extended by ecologylab.services.distributed.impl.NIOServerBase
          extended by ecologylab.services.distributed.server.DoubleThreadedNIOServer
All Implemented Interfaces:
StartAndStoppable, NetworkingConstants, ServerConstants, SessionObjects, Shutdownable, NIOServerFrontend, java.lang.Runnable
Direct Known Subclasses:
DoubleThreadedAuthNIOServer, HttpGetServer, HTTPPostServer, NIOLoggingServer

public class DoubleThreadedNIOServer
extends NIOServerBase
implements ServerConstants

A server that uses NIO and two threads (one for handling IO, the other for handling interfacing with messages). Automatically processes and responds to any client RequestMessages. Subclasses should generally override the generateContextManager hook method, so that they can use their own, specific ContextManager in place of the default.

Author:
Zachary O. Toups (toupsz@cs.tamu.edu)

Field Summary
protected  int maxPacketSize
           
 
Fields inherited from class ecologylab.services.distributed.impl.NIOServerBase
registry, translationSpace
 
Fields inherited from interface ecologylab.services.distributed.common.ServerConstants
GARBAGE_CONNECTION_CLEANUP_TIMEOUT, MAX_CONNECTIONS, MAXIMUM_TRANSMISSION_ERRORS
 
Fields inherited from interface ecologylab.services.distributed.common.NetworkingConstants
CHARACTER_ENCODING, CONTENT_LENGTH_STRING, CONTENT_LENGTH_STRING_LENGTH, ENCODER, HTTP_HEADER_LINE_DELIMITER, HTTP_HEADER_TERMINATOR, MAX_HTTP_HEADER_LENGTH, MAX_PACKET_SIZE_BYTES, MAX_PACKET_SIZE_CHARACTERS, UNIQUE_IDENTIFIER_STRING
 
Fields inherited from interface ecologylab.services.distributed.common.SessionObjects
BROWSER_SERVICES_CLIENT, BROWSER_SERVICES_TRANSLATIONS, LOGGING, MAIN_SHUTDOWNABLE, MAIN_START_AND_STOPPABLE, TOP_LEVEL
 
Constructor Summary
protected DoubleThreadedNIOServer(int portNumber, java.net.InetAddress[] inetAddresses, TranslationSpace requestTranslationSpace, Scope objectRegistry, int idleConnectionTimeout, int maxPacketSize)
           
protected DoubleThreadedNIOServer(int portNumber, java.net.InetAddress inetAddress, TranslationSpace requestTranslationSpace, Scope objectRegistry, int idleConnectionTimeout, int maxPacketSize)
           
 
Method Summary
protected static java.net.InetAddress[] addressToAddresses(java.net.InetAddress address)
           
protected  AbstractClientManager generateContextManager(java.lang.Object token, java.nio.channels.SelectionKey sk, TranslationSpace translationSpaceIn, Scope registryIn)
          Hook method to allow changing the ContextManager to enable specific extra functionality.
static DoubleThreadedNIOServer getInstance(int portNumber, java.net.InetAddress[] inetAddress, TranslationSpace requestTranslationSpace, Scope objectRegistry, int idleConnectionTimeout, int maxPacketSize)
           
static DoubleThreadedNIOServer getInstance(int portNumber, java.net.InetAddress inetAddress, TranslationSpace requestTranslationSpace, Scope objectRegistry, int idleConnectionTimeout, int maxPacketSize)
           
 AbstractClientManager invalidate(java.lang.Object sessionId, boolean permanent)
          Performs any internal actions that should be taken whenever a client is disconnected.
 boolean isRunning()
           
 void processRead(java.lang.Object sessionId, NIOServerBackend base, java.nio.channels.SelectionKey sk, java.nio.ByteBuffer bs, int bytesRead)
           
 boolean restoreContextManagerFromSessionId(java.lang.Object oldSessionId, AbstractClientManager newContextManager)
          Attempts to switch the ContextManager for a SocketChannel.
 void run()
           
 void shutdown()
          Causes this to start to shutdown, and fires a SERVER_SHUTTING_DOWN event to all listeners.
 void start()
           
 void stop()
           
 
Methods inherited from class ecologylab.services.distributed.impl.NIOServerBase
composeTranslations, composeTranslations, generateBackend, getBackend, getRegistry, getTranslationSpace
 
Methods inherited from class ecologylab.generic.Debug
classSimpleName, closeLoggingFile, debug, debug, debug, debug, debugA, debugA, debugA, debugI, debugI, debugI, error, error, getClassName, getClassName, getInteractive, getPackageName, getPackageName, getPackageName, initialize, level, level, level, logToFile, print, print, println, println, println, println, println, println, printlnA, printlnA, printlnA, printlnI, printlnI, printlnI, printlnI, setLoggingFile, show, show, superString, toggleInteractive, toString, toString, warning, warning, weird, weird
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

maxPacketSize

protected int maxPacketSize
Constructor Detail

DoubleThreadedNIOServer

protected DoubleThreadedNIOServer(int portNumber,
                                  java.net.InetAddress[] inetAddresses,
                                  TranslationSpace requestTranslationSpace,
                                  Scope objectRegistry,
                                  int idleConnectionTimeout,
                                  int maxPacketSize)
                           throws java.io.IOException,
                                  java.net.BindException
Throws:
java.io.IOException
java.net.BindException

DoubleThreadedNIOServer

protected DoubleThreadedNIOServer(int portNumber,
                                  java.net.InetAddress inetAddress,
                                  TranslationSpace requestTranslationSpace,
                                  Scope objectRegistry,
                                  int idleConnectionTimeout,
                                  int maxPacketSize)
                           throws java.io.IOException,
                                  java.net.BindException
Throws:
java.io.IOException
java.net.BindException
Method Detail

addressToAddresses

protected static java.net.InetAddress[] addressToAddresses(java.net.InetAddress address)

getInstance

public static DoubleThreadedNIOServer getInstance(int portNumber,
                                                  java.net.InetAddress[] inetAddress,
                                                  TranslationSpace requestTranslationSpace,
                                                  Scope objectRegistry,
                                                  int idleConnectionTimeout,
                                                  int maxPacketSize)
                                           throws java.io.IOException,
                                                  java.net.BindException
Throws:
java.io.IOException
java.net.BindException

getInstance

public static DoubleThreadedNIOServer getInstance(int portNumber,
                                                  java.net.InetAddress inetAddress,
                                                  TranslationSpace requestTranslationSpace,
                                                  Scope objectRegistry,
                                                  int idleConnectionTimeout,
                                                  int maxPacketSize)
                                           throws java.io.IOException,
                                                  java.net.BindException
Throws:
java.io.IOException
java.net.BindException

processRead

public void processRead(java.lang.Object sessionId,
                        NIOServerBackend base,
                        java.nio.channels.SelectionKey sk,
                        java.nio.ByteBuffer bs,
                        int bytesRead)
                 throws BadClientException
Specified by:
processRead in interface NIOServerFrontend
Throws:
BadClientException - See ecologylab.services.nio.servers.NIOServerFrontend#process(ecologylab.services.nio.NIOServerBackend, java.nio.channels.SocketChannel, byte[], int)

generateContextManager

protected AbstractClientManager generateContextManager(java.lang.Object token,
                                                       java.nio.channels.SelectionKey sk,
                                                       TranslationSpace translationSpaceIn,
                                                       Scope registryIn)
Hook method to allow changing the ContextManager to enable specific extra functionality.

Specified by:
generateContextManager in class NIOServerBase
Parameters:
token -
sc -
translationSpaceIn -
registryIn -
Returns:

run

public void run()
Specified by:
run in interface java.lang.Runnable

start

public void start()
Specified by:
start in interface StartAndStoppable
Overrides:
start in class NIOServerBase
See Also:
StartAndStoppable.start()

stop

public void stop()
Specified by:
stop in interface StartAndStoppable
Overrides:
stop in class NIOServerBase
See Also:
StartAndStoppable.stop()

shutdown

public void shutdown()
Description copied from interface: Shutdownable
Causes this to start to shutdown, and fires a SERVER_SHUTTING_DOWN event to all listeners.

Specified by:
shutdown in interface Shutdownable
See Also:
Shutdownable.shutdown()

invalidate

public AbstractClientManager invalidate(java.lang.Object sessionId,
                                        boolean permanent)
Description copied from interface: NIOServerFrontend
Performs any internal actions that should be taken whenever a client is disconnected.

Specified by:
invalidate in interface NIOServerFrontend
permanent - - indicates whether the NIOServerFrontend should destroy the context manager object associated with the given connection.
Returns:
the ContextManager object associated with sc that has been removed from the system.
See Also:
ecologylab.services.distributed.server.NIOServerFrontend#invalidate(java.lang.Object, ecologylab.services.distributed.impl.NIOServerBackend, java.nio.channels.SocketChannel)

restoreContextManagerFromSessionId

public boolean restoreContextManagerFromSessionId(java.lang.Object oldSessionId,
                                                  AbstractClientManager newContextManager)
Attempts to switch the ContextManager for a SocketChannel. oldId indicates the session id that was used for the connection previously (in order to find the correct ContextManager) and newContextManager is the recently-created (and now, no longer necessary) ContextManager for the connection.

Specified by:
restoreContextManagerFromSessionId in interface NIOServerFrontend
Parameters:
oldId -
newContextManager -
Returns:
true if the restore was successful, false if it was not.

isRunning

public boolean isRunning()
Returns:
status of server in boolean