ecologylab.services.distributed.impl
Class NIOCore

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

public abstract class NIOCore
extends Debug
implements StartAndStoppable, NetworkingConstants

Provides core functionality for NIO-based servers or clients. This class is Runnable and StartAndStoppable; it's run method automatically handles interest-switching on a selector's keys, as well as calling appropriate abstract methods whenever interest ops are selected. Subclasses are required to configure their own selector.

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

Field Summary
protected  int portNumber
           
protected  java.nio.channels.Selector 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 NIOCore(java.lang.String networkingIdentifier, int portNumber)
          Instantiates a new NIOCore object.
 
Method Summary
abstract  void acceptFinished(java.nio.channels.SelectionKey key)
          Queues a request to change key's interest operations back to READ.
protected abstract  void acceptReady(java.nio.channels.SelectionKey key)
           
protected abstract  void checkAndDropIdleKeys()
          Check for timeout on all allocated keys; deallocate those that are hanging around, but no longer in use.
protected  void close()
           
 void connectFinished(java.nio.channels.SelectionKey key)
          Queues a request to change key's interest operations back to READ.
protected abstract  void connectReady(java.nio.channels.SelectionKey key)
           
 int getPortNumber()
           
protected abstract  void invalidateKey(java.nio.channels.SelectionKey key, boolean permanent)
           
protected  void invalidateKey(java.nio.channels.SocketChannel chan)
          Shut down the connection associated with this SelectionKey.
protected  void queueForAccept(java.nio.channels.SelectionKey key)
           
protected  void queueForConnect(java.nio.channels.SelectionKey key)
           
protected  void queueForRead(java.nio.channels.SelectionKey key)
           
protected  void queueForWrite(java.nio.channels.SelectionKey key)
           
protected abstract  void readFinished(java.nio.channels.SelectionKey key)
           
protected abstract  void readReady(java.nio.channels.SelectionKey key)
           
protected abstract  void removeBadConnections(java.nio.channels.SelectionKey key)
           
 void run()
          THIS METHOD SHOULD NOT BE CALLED DIRECTLY! Proper use of this method is through the start / stop methods.
 void setPendingInvalidate(java.nio.channels.SelectionKey key, boolean permanent)
          Sets up a pending invalidate command for the given input.
 void start()
           
 void stop()
           
protected  void writeFinished(java.nio.channels.SelectionKey key)
          Queues a request to change key's interest operations back to READ.
protected abstract  void writeReady(java.nio.channels.SelectionKey key)
           
 
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

selector

protected java.nio.channels.Selector selector

portNumber

protected int portNumber
Constructor Detail

NIOCore

protected NIOCore(java.lang.String networkingIdentifier,
                  int portNumber)
           throws java.io.IOException
Instantiates a new NIOCore object.

Parameters:
networkingIdentifier - the name to identify this object when its thread is created.
portNumber - the port number that this object will use for network communications.
Throws:
java.io.IOException - if an I/O error occurs while trying to open a Selector from the system.
Method Detail

run

public final void run()
THIS METHOD SHOULD NOT BE CALLED DIRECTLY! Proper use of this method is through the start / stop methods. Main run method. Performs a loop of changing the mode (read/write) for each socket, if requested, then checks for and performs appropriate I/O for each socket that is ready. Ends when running is set to false (through the stop method).

Specified by:
run in interface java.lang.Runnable
See Also:
Runnable.run()

acceptReady

protected abstract void acceptReady(java.nio.channels.SelectionKey key)
Parameters:
key -

connectReady

protected abstract void connectReady(java.nio.channels.SelectionKey key)
Parameters:
key -

readFinished

protected abstract void readFinished(java.nio.channels.SelectionKey key)
Parameters:
key -

readReady

protected abstract void readReady(java.nio.channels.SelectionKey key)
                           throws ClientOfflineException,
                                  BadClientException
Parameters:
key -
Throws:
ClientOfflineException
BadClientException

acceptFinished

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

Parameters:
key -

connectFinished

public void connectFinished(java.nio.channels.SelectionKey key)
Queues a request to change key's interest operations back to READ. This method is automatically called after connectReady(SelectionKey) in the main operating loop.

Parameters:
key -

writeFinished

protected void writeFinished(java.nio.channels.SelectionKey key)
Queues a request to change key's interest operations back to READ. This method is automatically called after writeReady(SelectionKey) in the main operating loop. Perform any actions necessary after all data has been written from the outgoing queue to the client for this key. This is a hook method so that subclasses can provide specific functionality (such as, for example, invalidating the connection once the data has been sent.

Parameters:
key - - the SelectionKey that is finished writing.

removeBadConnections

protected abstract void removeBadConnections(java.nio.channels.SelectionKey key)

setPendingInvalidate

public void setPendingInvalidate(java.nio.channels.SelectionKey key,
                                 boolean permanent)
Sets up a pending invalidate command for the given input.

Parameters:
chan - - the SocketChannel to invalidate.

invalidateKey

protected void invalidateKey(java.nio.channels.SocketChannel chan)
Shut down the connection associated with this SelectionKey. Subclasses should override to do your own housekeeping, then call super.invalidateKey(SelectionKey) to utilize the functionality here.

Parameters:
chan - The SocketChannel that needs to be shut down.

invalidateKey

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

start

public void start()
Specified by:
start in interface StartAndStoppable

stop

public void stop()
Specified by:
stop in interface StartAndStoppable

close

protected void close()

checkAndDropIdleKeys

protected abstract void checkAndDropIdleKeys()
Check for timeout on all allocated keys; deallocate those that are hanging around, but no longer in use.


writeReady

protected abstract void writeReady(java.nio.channels.SelectionKey key)
                            throws java.io.IOException
Parameters:
key -
Throws:
java.io.IOException

queueForAccept

protected void queueForAccept(java.nio.channels.SelectionKey key)

queueForConnect

protected void queueForConnect(java.nio.channels.SelectionKey key)

queueForRead

protected void queueForRead(java.nio.channels.SelectionKey key)

queueForWrite

protected void queueForWrite(java.nio.channels.SelectionKey key)

getPortNumber

public int getPortNumber()
Returns:
the port number the server is listening on.