The Meta-Environment API

toolbus.communication
Class SocketIOHandler

java.lang.Object
  extended by toolbus.communication.SocketIOHandler
All Implemented Interfaces:
IIOHandler

public class SocketIOHandler
extends java.lang.Object
implements IIOHandler

This class handles all communication with remote applications through sockets. It uses NIO / asynchroneous I/O. It will use 64k of staticly allocated native memory for I/O buffers.

Author:
Arnold Lankamp

Nested Class Summary
protected static class SocketIOHandler.OperationTermPair
          A structure that links a term with an operation.
 
Field Summary
private  aterm.pure.binary.BinaryReader binaryReader
           
private  aterm.pure.binary.BinaryWriter binaryWriter
           
private  int blockLength
           
private static byte BLOCKLENGTHBYTES
           
private static int BUFFERSIZE
           
private  AbstractConnectionHandler connectionHandler
           
private  IDataHandler dataHandler
           
private  boolean doneWithBlock
           
static byte END_OPC
           
private  boolean expectingDisconnect
           
private  java.nio.ByteBuffer lengthReadBuffer
           
private  java.nio.ByteBuffer lengthWriteBuffer
           
private  byte operation
           
private static byte OPERATIONBYTES
           
private  java.nio.ByteBuffer operationReadBuffer
           
private  java.nio.ByteBuffer readBuffer
           
private  java.nio.channels.SocketChannel socketChannel
           
private  java.nio.ByteBuffer writeBuffer
           
private  toolbus.util.collections.RotatingQueue<SocketIOHandler.OperationTermPair> writeQueue
           
 
Constructor Summary
SocketIOHandler(IDataHandler dataHandler, AbstractConnectionHandler connectionHandler, java.nio.channels.SocketChannel socketChannel)
          Constructor.
 
Method Summary
 void exceptionOccured()
          Notifies the I/O handler that an exception has occured so it can take the nessacary steps.
private  void forcedWrite(java.nio.ByteBuffer buffer)
          Forces the writing of the content of the given buffer to the socket channel.
private  void handleDisconnect()
          Handles the received disconnect.
 boolean hasMoreToWrite()
          Checks if there is data left that needs to be written.
protected  void read()
          This method triggers the reading of data from the socket.
private  boolean readFromChannel(java.nio.ByteBuffer buffer)
          Reads bytes from the socket channel and inserts them into the given byte buffer.
 void receive(byte op, aterm.ATerm aTerm)
          Receives a term from the remote location.
private  boolean receiveBlockLength()
          Reads the data block length from the socket channel (2 bytes, little endian encoded).
private  boolean receiveDataBlock()
          Reads the data from the current data block from the socket channel.
private  boolean receiveOperation()
          Reads the operation byte from the socket channel.
 void send(byte op, aterm.ATerm aTerm)
          Adds the term to the list of terms that needs to be send.
private  void sendBlockLength(int length)
          Writes the block length to the socket channel.
private  boolean sendDataBlock()
          Writes a block of data to the socket channel.
private  void sendOperation(byte op)
          Writes the operation byte to the stream.
 void shutDown()
          Shuts down this ioHandler and it's associated datahandler.
 void terminate()
          Terminates the connection with the in this socket I/O handler contained socket channel in an orderly manner.
protected  void write()
          This method triggers the writing of data from the socket.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

END_OPC

public static final byte END_OPC
See Also:
Constant Field Values

OPERATIONBYTES

private static final byte OPERATIONBYTES
See Also:
Constant Field Values

BLOCKLENGTHBYTES

private static final byte BLOCKLENGTHBYTES
See Also:
Constant Field Values

BUFFERSIZE

private static final int BUFFERSIZE
See Also:
Constant Field Values

operationReadBuffer

private final java.nio.ByteBuffer operationReadBuffer

lengthReadBuffer

private final java.nio.ByteBuffer lengthReadBuffer

readBuffer

private final java.nio.ByteBuffer readBuffer

lengthWriteBuffer

private final java.nio.ByteBuffer lengthWriteBuffer

writeBuffer

private final java.nio.ByteBuffer writeBuffer

socketChannel

private final java.nio.channels.SocketChannel socketChannel

connectionHandler

private final AbstractConnectionHandler connectionHandler

dataHandler

private final IDataHandler dataHandler

expectingDisconnect

private boolean expectingDisconnect

binaryReader

private aterm.pure.binary.BinaryReader binaryReader

operation

private byte operation

blockLength

private int blockLength

writeQueue

private final toolbus.util.collections.RotatingQueue<SocketIOHandler.OperationTermPair> writeQueue

binaryWriter

private aterm.pure.binary.BinaryWriter binaryWriter

doneWithBlock

private boolean doneWithBlock
Constructor Detail

SocketIOHandler

public SocketIOHandler(IDataHandler dataHandler,
                       AbstractConnectionHandler connectionHandler,
                       java.nio.channels.SocketChannel socketChannel)
Constructor.

Parameters:
dataHandler - The data handler that is associated with this socket I/O handler.
connectionHandler - The connection handler that created this socket I/O handler.
socketChannel - The socket channel that will be used by this socket I/O handler.
Method Detail

send

public void send(byte op,
                 aterm.ATerm aTerm)
Adds the term to the list of terms that needs to be send. The terms will be send in the same order as they arrived at this method.

Specified by:
send in interface IIOHandler
Parameters:
op - The operation associated with the term we need to send.
aTerm - The term that needs to be send.
See Also:
IIOHandler.send(byte, ATerm)

receive

public void receive(byte op,
                    aterm.ATerm aTerm)
Description copied from interface: IIOHandler
Receives a term from the remote location.

Specified by:
receive in interface IIOHandler
Parameters:
op - The operation associated with the term we received.
aTerm - The term that was received.
See Also:
IIOHandler.receive(byte, ATerm)

read

protected void read()
This method triggers the reading of data from the socket. Partial reads are possible, it will continue where it left off. IMPORTANT: Do not call this method concurrently; it will lead to undefined behaviour.


receiveOperation

private boolean receiveOperation()
Reads the operation byte from the socket channel.

Returns:
False if we received a disconnect, true otherwise.

receiveBlockLength

private boolean receiveBlockLength()
Reads the data block length from the socket channel (2 bytes, little endian encoded).

Returns:
False if we received a disconnect, true otherwise.

receiveDataBlock

private boolean receiveDataBlock()
Reads the data from the current data block from the socket channel.

Returns:
False if we received a disconnect, true otherwise.

readFromChannel

private boolean readFromChannel(java.nio.ByteBuffer buffer)
Reads bytes from the socket channel and inserts them into the given byte buffer.

Parameters:
buffer - The buffer to insert the received data into.
Returns:
False if we received a disconnect, true otherwise.

write

protected void write()
This method triggers the writing of data from the socket. Partial writes are possible, it will continue where it left off. IMPORTANT: Do not call this method concurrently; it will lead to undefined behaviour.


sendOperation

private void sendOperation(byte op)
Writes the operation byte to the stream.

Parameters:
op - The operation byte that indicates what the upcoming data package represents.

sendBlockLength

private void sendBlockLength(int length)
Writes the block length to the socket channel.

Parameters:
length - The length of the block of data that will be written.

sendDataBlock

private boolean sendDataBlock()
Writes a block of data to the socket channel.

Returns:
True if the entire block of data has been written to the socket channel, false otherwise.

forcedWrite

private void forcedWrite(java.nio.ByteBuffer buffer)
Forces the writing of the content of the given buffer to the socket channel. Use this method for writing small amounts of data (no more then a few bytes) that need to be send in one piece.

Parameters:
buffer - The buffer to write to.

hasMoreToWrite

public boolean hasMoreToWrite()
Checks if there is data left that needs to be written.

Returns:
True if there is data left that needs to be written; false otherwise.

terminate

public void terminate()
Terminates the connection with the in this socket I/O handler contained socket channel in an orderly manner. All data that was queued for writing will be written to the socket before the end message will be send.

Specified by:
terminate in interface IIOHandler
See Also:
IIOHandler.terminate()

shutDown

public void shutDown()
Description copied from interface: IIOHandler
Shuts down this ioHandler and it's associated datahandler. IMPORTANT: This method should not be called from outside the ioHandler. Manual invokation will lead to undefined behaviour.

Specified by:
shutDown in interface IIOHandler
See Also:
IIOHandler.shutDown()

exceptionOccured

public void exceptionOccured()
Description copied from interface: IIOHandler
Notifies the I/O handler that an exception has occured so it can take the nessacary steps.

Specified by:
exceptionOccured in interface IIOHandler
See Also:
IIOHandler.exceptionOccured()

handleDisconnect

private void handleDisconnect()
Handles the received disconnect. The proper close method on the connection handler will be called depending on whether or not we anticipated the disconnect.


The Meta-Environment API