The Meta-Environment API

toolbus
Class SocketConnectionHandler

java.lang.Object
  extended by toolbus.communication.AbstractConnectionHandler
      extended by toolbus.SocketConnectionHandler
All Implemented Interfaces:
java.lang.Runnable

public class SocketConnectionHandler
extends AbstractConnectionHandler
implements java.lang.Runnable

This class handles the establishing and closing of all TCP/IP connections.

Author:
Arnold Lankamp

Field Summary
private  java.nio.ByteBuffer handShakeBuffer
           
private static int HANDSHAKEBUFFERSIZE
           
private  SocketReadMultiplexer readMultiplexer
           
private  boolean running
           
private  java.nio.channels.ServerSocketChannel serverSocketChannel
           
private  ToolBus toolbus
           
private  SocketWriteMultiplexer writeMultiplexer
           
 
Constructor Summary
SocketConnectionHandler(ToolBus toolbus)
          Constructor.
 
Method Summary
private  void acceptConnection()
          Accepts a connection with a remote application.
 void closeConnection(java.nio.channels.SocketChannel socketChannel)
          Closes the connection associated with the given socket channel.
 int getPort()
          Returns the port number the ToolBus is currently running on.
 SocketReadMultiplexer getReadMultiplexer()
          Returns a reference to the read multiplexer.
 SocketWriteMultiplexer getWriteMultiplexer()
          Returns a reference to the write multiplexer.
 void initialize()
          Initializes the connection handler.
 void initialize(int port)
          Initializes the connection handler on a user specified port.
 boolean isRunning()
          Checks if this handler is running or not.
private  aterm.ATerm readTermFromChannel(aterm.pure.PureFactory factory, java.nio.channels.SocketChannel socketChannel, java.nio.ByteBuffer byteBuffer)
          Reads a term from the given channel, using the given buffer.
 void run()
          Main loop of this handler.
private  ToolInstance shakeHands(java.nio.channels.SocketChannel socketChannel)
           
 void stopRunning()
          Stops the execution of this handler.
private  void writeTermToChannel(aterm.ATerm aTerm, java.nio.channels.SocketChannel socketChannel, java.nio.ByteBuffer byteBuffer)
          Transmits the term to the given socket channel, using the given buffer.
 
Methods inherited from class toolbus.communication.AbstractConnectionHandler
closeDueToDisconnect, closeDueToException
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HANDSHAKEBUFFERSIZE

private static final int HANDSHAKEBUFFERSIZE
See Also:
Constant Field Values

toolbus

private final ToolBus toolbus

readMultiplexer

private final SocketReadMultiplexer readMultiplexer

writeMultiplexer

private final SocketWriteMultiplexer writeMultiplexer

serverSocketChannel

private final java.nio.channels.ServerSocketChannel serverSocketChannel

handShakeBuffer

private final java.nio.ByteBuffer handShakeBuffer

running

private volatile boolean running
Constructor Detail

SocketConnectionHandler

public SocketConnectionHandler(ToolBus toolbus)
Constructor.

Parameters:
toolbus - A reference to the main class of the Toolbus.
Method Detail

initialize

public void initialize()
                throws java.io.IOException
Initializes the connection handler. Initialization MUST occur before executing the run method.

Throws:
java.io.IOException

initialize

public void initialize(int port)
                throws java.io.IOException
Initializes the connection handler on a user specified port. Initialization MUST occur before executing the run method.

Throws:
java.io.IOException

getPort

public int getPort()
Returns the port number the ToolBus is currently running on.

Returns:
The port number the ToolBus is currently running on.

getReadMultiplexer

public SocketReadMultiplexer getReadMultiplexer()
Description copied from class: AbstractConnectionHandler
Returns a reference to the read multiplexer.

Specified by:
getReadMultiplexer in class AbstractConnectionHandler
Returns:
A reference to the read multiplexer.
See Also:
AbstractConnectionHandler.getReadMultiplexer()

getWriteMultiplexer

public SocketWriteMultiplexer getWriteMultiplexer()
Description copied from class: AbstractConnectionHandler
Returns a reference to the write multiplexer.

Specified by:
getWriteMultiplexer in class AbstractConnectionHandler
Returns:
A reference to the write multiplexer.
See Also:
AbstractConnectionHandler.getWriteMultiplexer()

isRunning

public boolean isRunning()
Checks if this handler is running or not.

Returns:
True if this handler is running, false otherwise.

stopRunning

public void stopRunning()
Stops the execution of this handler. The running state will be set to false and the server socket will be closed.


run

public void run()
Main loop of this handler. It tries to accept any incoming connection attempt.

Specified by:
run in interface java.lang.Runnable

acceptConnection

private void acceptConnection()
                       throws java.io.IOException
Accepts a connection with a remote application.

Throws:
java.io.IOException - Thrown when something goes wrong while accepting the connection, registering it or instantiating the associated I/O handlers.

shakeHands

private ToolInstance shakeHands(java.nio.channels.SocketChannel socketChannel)
                         throws java.io.IOException,
                                ToolBusException
Throws:
java.io.IOException
ToolBusException

writeTermToChannel

private void writeTermToChannel(aterm.ATerm aTerm,
                                java.nio.channels.SocketChannel socketChannel,
                                java.nio.ByteBuffer byteBuffer)
                         throws java.io.IOException
Transmits the term to the given socket channel, using the given buffer.

Parameters:
aTerm - The term to write.
socketChannel - The channel to write to.
byteBuffer - The buffer to use.
Throws:
java.io.IOException - Thrown when something goes wrong while writing to the channel.

readTermFromChannel

private aterm.ATerm readTermFromChannel(aterm.pure.PureFactory factory,
                                        java.nio.channels.SocketChannel socketChannel,
                                        java.nio.ByteBuffer byteBuffer)
                                 throws java.io.IOException
Reads a term from the given channel, using the given buffer.

Parameters:
factory - The factory to use for parsing the term.
socketChannel - The channel to read from.
byteBuffer - The buffer to use.
Returns:
The term that was read.
Throws:
java.io.IOException - Thrown when something goes wrong while reading the term from the channel.

closeConnection

public void closeConnection(java.nio.channels.SocketChannel socketChannel)
Description copied from class: AbstractConnectionHandler
Closes the connection associated with the given socket channel. This method executes a controlled shutdown of the connection. The socket channel will be deregistered (if needed) and will be closed.

Specified by:
closeConnection in class AbstractConnectionHandler
Parameters:
socketChannel - The socket channel, who's associated socket should be closed.
See Also:
AbstractConnectionHandler.closeConnection(SocketChannel)

The Meta-Environment API