ecologylab.services.distributed.impl
Class NIONetworking

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

public abstract class NIONetworking
extends NIOCore

Handles backend, low-level communication between distributed programs, using NIO. This is the basis for servers for handling network communication.

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

Field Summary
protected  ByteBufferPool byteBufferPool
           
protected  int connectionCount
           
protected  Scope<?> objectRegistry
          Provides a context for request processing.
protected  boolean shuttingDown
           
protected  TranslationSpace translationSpace
          Space that defines mappings between xml names, and Java class names, for request messages.
 
Fields inherited from class ecologylab.services.distributed.impl.NIOCore
portNumber, selector
 
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 NIONetworking(java.lang.String networkIdentifier, int portNumber, TranslationSpace translationSpace, Scope<?> objectRegistry)
          Creates a Services Server Base.
 
Method Summary
protected abstract  void acceptKey(java.nio.channels.SelectionKey key)
          Optional operation.
 java.nio.ByteBuffer acquireByteBufferFromPool()
          Retrieves a ByteBuffer object from this's pool of ByteBuffers.
protected  void connectionTerminated()
          Remove the argument passed in from the set of connections we know about.
 void enqueueBytesForWriting(java.nio.channels.SelectionKey socketKey, java.nio.ByteBuffer data)
          Queue up bytes to send on a particular socket.
protected abstract  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 readReady(java.nio.channels.SelectionKey key)
           
protected  void terminationAction()
          This defines the actions that server needs to perform when the client ends unexpected way.
protected  void writeKey(java.nio.channels.SelectionKey key)
          Writes the bytes from pendingWrites that belong to key.
protected  void writeReady(java.nio.channels.SelectionKey key)
           
 
Methods inherited from class ecologylab.services.distributed.impl.NIOCore
acceptFinished, acceptReady, checkAndDropIdleKeys, close, connectFinished, connectReady, getPortNumber, invalidateKey, invalidateKey, queueForAccept, queueForConnect, queueForRead, queueForWrite, readFinished, removeBadConnections, 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

shuttingDown

protected boolean shuttingDown

translationSpace

protected TranslationSpace translationSpace
Space that defines mappings between xml names, and Java class names, for request messages.


objectRegistry

protected Scope<?> objectRegistry
Provides a context for request processing.


connectionCount

protected int connectionCount

byteBufferPool

protected ByteBufferPool byteBufferPool
Constructor Detail

NIONetworking

protected NIONetworking(java.lang.String networkIdentifier,
                        int portNumber,
                        TranslationSpace translationSpace,
                        Scope<?> objectRegistry)
                 throws java.io.IOException
Creates a Services Server Base. Sets internal variables, but does not bind the port. Port binding is to be handled by sublcasses.

Parameters:
portNumber - the port number to use for communicating.
translationSpace - the TranslationSpace to use for incoming messages; if this is null, uses DefaultServicesTranslations instead.
objectRegistry - Provides a context for request processing; if this is null, creates a new ObjectRegistry.
Throws:
java.io.IOException - if an I/O error occurs while trying to open a Selector from the system.
Method Detail

readReady

protected void readReady(java.nio.channels.SelectionKey key)
                  throws ClientOfflineException,
                         BadClientException
Specified by:
readReady in class NIOCore
Throws:
ClientOfflineException
BadClientException
See Also:
NIOCore.readReady(java.nio.channels.SelectionKey)

writeReady

protected void writeReady(java.nio.channels.SelectionKey key)
                   throws java.io.IOException
Specified by:
writeReady in class NIOCore
Throws:
java.io.IOException
See Also:
NIOCore.writeReady(java.nio.channels.SelectionKey)

enqueueBytesForWriting

public void enqueueBytesForWriting(java.nio.channels.SelectionKey socketKey,
                                   java.nio.ByteBuffer data)
Queue up bytes to send on a particular socket. This method is typically called by some outside context manager, that has produced an encoded message to send out.

Parameters:
socketKey -
data -

writeKey

protected void writeKey(java.nio.channels.SelectionKey key)
                 throws java.io.IOException
Writes the bytes from pendingWrites that belong to key.

Parameters:
key -
Throws:
java.io.IOException

acceptKey

protected abstract void acceptKey(java.nio.channels.SelectionKey key)
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).

Parameters:
key -
Throws:
javax.naming.OperationNotSupportedException

connectionTerminated

protected void connectionTerminated()
Remove the argument passed in from the set of connections we know about.


processReadData

protected abstract void processReadData(java.lang.Object sessionId,
                                        java.nio.channels.SelectionKey sk,
                                        java.nio.ByteBuffer bytes,
                                        int bytesRead)
                                 throws BadClientException
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.

Parameters:
sessionId - the id being use for this session.
sc - the SocketChannel from which the bytes originated.
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.

terminationAction

protected void terminationAction()
This defines the actions that server needs to perform when the client ends unexpected way. Detail implementations will be in subclasses.


acquireByteBufferFromPool

public java.nio.ByteBuffer acquireByteBufferFromPool()
Retrieves a ByteBuffer object from this's pool of ByteBuffers. Typically used by a ContextManager to store bytes that will be later enqueued to write (and thus released by that method).

Returns: