ecologylab.services.distributed.impl
Class NIOServerBackend

java.lang.Object
  extended by ecologylab.generic.Debug
      extended by ecologylab.services.distributed.impl.NIOCore
          extended by ecologylab.services.distributed.impl.NIONetworking
              extended by ecologylab.services.distributed.impl.NIOServerBackend
All Implemented Interfaces:
StartAndStoppable, NetworkingConstants, ServerConstants, java.lang.Runnable

public class NIOServerBackend
extends NIONetworking
implements ServerConstants

The backend portion of the NIO Server, which handles low-level communication with clients. Re-written based on the Rox Java NIO Tutorial (http://rox-xmlrpc.sourceforge.net/niotut/index.html).

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

Field Summary
protected  java.net.ServerSocket[] incomingConnectionSockets
           
 
Fields inherited from class ecologylab.services.distributed.impl.NIONetworking
byteBufferPool, connectionCount, objectRegistry, shuttingDown, translationSpace
 
Fields inherited from class ecologylab.services.distributed.impl.NIOCore
portNumber, selector
 
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, DECODER, ENCODER, HTTP_HEADER_LINE_DELIMITER, HTTP_HEADER_TERMINATOR, MAX_HTTP_HEADER_LENGTH, MAX_PACKET_SIZE_BYTES, MAX_PACKET_SIZE_CHARACTERS, UNIQUE_IDENTIFIER_STRING
 
Constructor Summary
protected NIOServerBackend(int portNumber, java.net.InetAddress[] hostAddresses, NIOServerFrontend sAP, TranslationSpace requestTranslationSpace, Scope<?> objectRegistry, int idleSocketTimeout)
           
 
Method Summary
 void acceptFinished(java.nio.channels.SelectionKey key)
          Queues a request to change key's interest operations back to READ.
protected  void acceptKey(java.nio.channels.SelectionKey key)
          Optional operation.
protected  void acceptReady(java.nio.channels.SelectionKey key)
           
protected  void checkAndDropIdleKeys()
          Checks all of the current keys to see if they have been idle for too long and drops them if they have.
protected  void close()
           
protected  void connectReady(java.nio.channels.SelectionKey key)
           
protected  java.lang.String generateSessionToken(java.net.Socket incomingSocket)
          Generates a unique identifier String for the given socket, based upon actual ports used and ip addresses with a hash.
 java.net.InetAddress[] getHostAddresses()
          Gets all host addresses associated with this server.
protected  void invalidateKey(java.nio.channels.SelectionKey key, boolean permanent)
           
protected  void processReadData(java.lang.Object sessionId, java.nio.channels.SelectionKey sk, java.nio.ByteBuffer bytes, int bytesRead)
          This method is called whenever bytes have been read from a socket.
protected  void readFinished(java.nio.channels.SelectionKey key)
           
protected  void removeBadConnections(java.nio.channels.SelectionKey key)
           
 void setPendingInvalidate(java.nio.channels.SocketChannel socket, boolean permanent)
           
 
Methods inherited from class ecologylab.services.distributed.impl.NIONetworking
acquireByteBufferFromPool, connectionTerminated, enqueueBytesForWriting, readReady, terminationAction, writeKey, writeReady
 
Methods inherited from class ecologylab.services.distributed.impl.NIOCore
connectFinished, getPortNumber, invalidateKey, queueForAccept, queueForConnect, queueForRead, queueForWrite, run, setPendingInvalidate, start, stop, writeFinished
 
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

incomingConnectionSockets

protected java.net.ServerSocket[] incomingConnectionSockets
Constructor Detail

NIOServerBackend

protected NIOServerBackend(int portNumber,
                           java.net.InetAddress[] hostAddresses,
                           NIOServerFrontend sAP,
                           TranslationSpace requestTranslationSpace,
                           Scope<?> objectRegistry,
                           int idleSocketTimeout)
                    throws java.io.IOException,
                           java.net.BindException
Throws:
java.io.IOException
java.net.BindException
Method Detail

getHostAddresses

public java.net.InetAddress[] getHostAddresses()
Gets all host addresses associated with this server.

Returns:

checkAndDropIdleKeys

protected void checkAndDropIdleKeys()
Checks all of the current keys to see if they have been idle for too long and drops them if they have.

Specified by:
checkAndDropIdleKeys in class NIOCore

acceptKey

protected final void acceptKey(java.nio.channels.SelectionKey key)
Description copied from class: NIONetworking
Optional operation. Called when a key has been marked for accepting. This method should be implemented by servers, but clients should leave this blank, unless they are also acting as servers (accepting incoming connections).

Specified by:
acceptKey in class NIONetworking

removeBadConnections

protected void removeBadConnections(java.nio.channels.SelectionKey key)
Specified by:
removeBadConnections in class NIOCore
See Also:
ecologylab.services.distributed.impl.NIONetworking#removeBadConnections()

invalidateKey

protected void invalidateKey(java.nio.channels.SelectionKey key,
                             boolean permanent)
Specified by:
invalidateKey in class NIOCore
See Also:
ecologylab.services.distributed.impl.NIONetworking#invalidateKey(java.nio.channels.SocketChannel, boolean)

generateSessionToken

protected java.lang.String generateSessionToken(java.net.Socket incomingSocket)
Generates a unique identifier String for the given socket, based upon actual ports used and ip addresses with a hash.

Parameters:
incomingSocket -
Returns:

close

protected void close()
Overrides:
close in class NIOCore

processReadData

protected void processReadData(java.lang.Object sessionId,
                               java.nio.channels.SelectionKey sk,
                               java.nio.ByteBuffer bytes,
                               int bytesRead)
                        throws BadClientException
Description copied from class: NIONetworking
This method is called whenever bytes have been read from a socket. There is no guaranty that the bytes will be a valid or complete message, nor is there a guaranty about what said bytes encode. Implementations should be prepared to handle incomplete messages, multiple messages, or malformed messages in this method.

Specified by:
processReadData in class NIONetworking
Parameters:
sessionId - the id being use for this session.
bytes - the bytes read from the SocketChannel.
bytesRead - the number of bytes in the bytes array.
Throws:
BadClientException - if the client from which the bytes were read has transmitted something inappropriate, such as data too large for a buffer or a possibly malicious message.

acceptReady

protected void acceptReady(java.nio.channels.SelectionKey key)
Specified by:
acceptReady in class NIOCore
See Also:
NIOCore.acceptReady(java.nio.channels.SelectionKey)

connectReady

protected void connectReady(java.nio.channels.SelectionKey key)
Specified by:
connectReady in class NIOCore
See Also:
NIOCore.connectReady(java.nio.channels.SelectionKey)

readFinished

protected void readFinished(java.nio.channels.SelectionKey key)
Specified by:
readFinished in class NIOCore
See Also:
NIOCore.readFinished(java.nio.channels.SelectionKey)

setPendingInvalidate

public void setPendingInvalidate(java.nio.channels.SocketChannel socket,
                                 boolean permanent)
Parameters:
socket -
permanent -

acceptFinished

public void acceptFinished(java.nio.channels.SelectionKey key)
Description copied from class: NIOCore
Queues a request to change key's interest operations back to READ. This method is automatically called after acceptReady(SelectionKey) in the main operating loop.

Specified by:
acceptFinished in class NIOCore
See Also:
NIOCore.acceptFinished(java.nio.channels.SelectionKey)