The Meta-Environment API

toolbus.adapter
Class ToolConnectionHandler

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

public class ToolConnectionHandler
extends AbstractConnectionHandler
implements java.lang.Runnable

This class handles the opening and closing of the tool's connection with the ToolBus and provides access to the multiplexers that are handling network I/O.

Author:
Arnold Lankamp

Field Summary
private static int HANDSHAKEBUFFERSIZE
           
private  SocketReadWriteMultiplexer socketReadWriteMultiplexer
           
 
Constructor Summary
ToolConnectionHandler(ToolBridge toolBridge, java.net.InetAddress host, int port)
          Constructor.
 
Method Summary
 void closeConnection(java.nio.channels.SocketChannel socketChannel)
          Closes the connection associated with the given socket channel.
 IReadMultiplexer getReadMultiplexer()
          Returns a reference to the read multiplexer.
 IWriteMultiplexer getWriteMultiplexer()
          Returns a reference to the write multiplexer.
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()
          Starts the multiplexer.
private  void shakeHands(java.nio.channels.SocketChannel socketChannel, ToolBridge toolBridge)
          Negotiates with the ToolBus (about some stuff) and checks if the tool adheres to the expected interface.
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

socketReadWriteMultiplexer

private final SocketReadWriteMultiplexer socketReadWriteMultiplexer
Constructor Detail

ToolConnectionHandler

public ToolConnectionHandler(ToolBridge toolBridge,
                             java.net.InetAddress host,
                             int port)
Constructor.

Parameters:
toolBridge - The tool bridge that requested the starting of this tool connection.
host - The adress where the ToolBus is running.
port - The port the ToolBus is running on.
Method Detail

run

public void run()
Starts the multiplexer.

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

getReadMultiplexer

public IReadMultiplexer 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 IWriteMultiplexer 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()

shakeHands

private void shakeHands(java.nio.channels.SocketChannel socketChannel,
                        ToolBridge toolBridge)
                 throws java.io.IOException
Negotiates with the ToolBus (about some stuff) and checks if the tool adheres to the expected interface.

Parameters:
socketChannel - The socket channel that we need to use to perform the handshake.
toolBridge - The tool bridge that provides access to the tool.
Throws:
java.io.IOException - Thrown when something goes wrong with the connection during the handshake.

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