Class TcpTransport
- java.lang.Object
- 
- jadex.platform.service.transport.tcp.TcpTransport
 
- 
- All Implemented Interfaces:
- ITransport<java.nio.channels.SocketChannel>
 
 public class TcpTransport extends java.lang.Object implements ITransport<java.nio.channels.SocketChannel> The selector thread waits for NIO events and issues the appropriate actions for asynchronous sending and receiving as data becomes available.
- 
- 
Field SummaryFields Modifier and Type Field Description protected ITransportHandler<java.nio.channels.SocketChannel>handlerThe transport handler, e.g.protected intmaxmsgsizeMaximum size a message is allowed to have (including header).static intPRIORITYPriority of transport.protected booleanrunningFlag indicating the thread should be running (set to false for shutdown).protected java.nio.channels.SelectorselectorThe NIO selector.protected booleanshutdownFlag indicating the transport has been shut down..protected java.util.List<java.lang.Runnable>tasksThe tasks enqueued from external threads.protected IDaemonThreadPoolServicetpsDaemon thread pool.protected java.util.Map<java.nio.channels.SocketChannel,java.util.List<Tuple2<java.nio.ByteBuffer,Future<java.lang.Integer>>>>writetasksThe write tasks of data waiting to be written to a connection.
 - 
Constructor SummaryConstructors Constructor Description TcpTransport(int maxmsgsize)Creates the transport
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcloseConnection(java.nio.channels.SelectionKey key, java.lang.Exception e, boolean remove)Perform close operations on a channel as identified by the given key.voidcloseConnection(java.nio.channels.SocketChannel sc)Perform close operations on a connection.IFuture<java.nio.channels.SocketChannel>createConnection(java.lang.String address, IComponentIdentifier target)Create a connection to a given address.java.lang.StringgetProtocolName()Get the protocol name.protected voidhandleAccept(java.nio.channels.SelectionKey key)Accept a connection request from a client.protected voidhandleConnect(java.nio.channels.SelectionKey key)Complete a connection request to a server.protected voidhandleRead(java.nio.channels.SelectionKey key)Read data from a connection.protected voidhandleWrite(java.nio.channels.SelectionKey key)Write data to a connection.voidinit(ITransportHandler<java.nio.channels.SocketChannel> handler)Initialize the transport.IFuture<java.lang.Integer>openPort(int port)Open a server socket.protected voidschedule(java.lang.Runnable task)Schedule a task on the thread.IFuture<java.lang.Integer>sendMessage(java.nio.channels.SocketChannel sc, byte[] header, byte[] body)Send bytes using the given connection.voidshutdown()Set the running flag to false to gracefully terminate the thread.
 
- 
- 
- 
Field Detail- 
PRIORITYpublic static final int PRIORITY Priority of transport.- See Also:
- Constant Field Values
 
 - 
handlerprotected ITransportHandler<java.nio.channels.SocketChannel> handler The transport handler, e.g. for delivering received messages.
 - 
runningprotected boolean running Flag indicating the thread should be running (set to false for shutdown).
 - 
shutdownprotected boolean shutdown Flag indicating the transport has been shut down..
 - 
maxmsgsizeprotected int maxmsgsize Maximum size a message is allowed to have (including header).
 - 
selectorprotected java.nio.channels.Selector selector The NIO selector.
 - 
tasksprotected java.util.List<java.lang.Runnable> tasks The tasks enqueued from external threads.
 - 
writetasksprotected java.util.Map<java.nio.channels.SocketChannel,java.util.List<Tuple2<java.nio.ByteBuffer,Future<java.lang.Integer>>>> writetasks The write tasks of data waiting to be written to a connection.
 - 
tpsprotected IDaemonThreadPoolService tps Daemon thread pool.
 
- 
 - 
Method Detail- 
initpublic void init(ITransportHandler<java.nio.channels.SocketChannel> handler) Initialize the transport. To be called once, before any other method.- Specified by:
- initin interface- ITransport<java.nio.channels.SocketChannel>
- Parameters:
- handler- The transport handler with callback methods.
 
 - 
shutdownpublic void shutdown() Set the running flag to false to gracefully terminate the thread.- Specified by:
- shutdownin interface- ITransport<java.nio.channels.SocketChannel>
 
 - 
getProtocolNamepublic java.lang.String getProtocolName() Get the protocol name.- Specified by:
- getProtocolNamein interface- ITransport<java.nio.channels.SocketChannel>
 
 - 
openPortpublic IFuture<java.lang.Integer> openPort(int port) Open a server socket. Must not be called while the thread is running.- Specified by:
- openPortin interface- ITransport<java.nio.channels.SocketChannel>
- Parameters:
- port- The (positive) port or 0 for any port.
- Returns:
- A future holding the actual port, once the server is running.
 
 - 
createConnectionpublic IFuture<java.nio.channels.SocketChannel> createConnection(java.lang.String address, IComponentIdentifier target) Create a connection to a given address.- Specified by:
- createConnectionin interface- ITransport<java.nio.channels.SocketChannel>
- Parameters:
- address- The target platform's address.
- target- The target identifier to maybe perform authentication of the connection.
- Returns:
- A future containing the connection when succeeded.
 
 - 
closeConnectionpublic void closeConnection(java.nio.channels.SocketChannel sc) Perform close operations on a connection. Potentially cleans up key attachments as well.- Specified by:
- closeConnectionin interface- ITransport<java.nio.channels.SocketChannel>
 
 - 
sendMessagepublic IFuture<java.lang.Integer> sendMessage(java.nio.channels.SocketChannel sc, byte[] header, byte[] body) Send bytes using the given connection.- Specified by:
- sendMessagein interface- ITransport<java.nio.channels.SocketChannel>
- Parameters:
- sc- The connection.
- header- The message header.
- body- The message body.
- Returns:
- A future indicating success.
 
 - 
closeConnectionprotected void closeConnection(java.nio.channels.SelectionKey key, java.lang.Exception e, boolean remove)Perform close operations on a channel as identified by the given key. Potentially cleans up key attachments as well.- Parameters:
- e- The exception, if any.
- remove- Notify the handler to remove the connection (should only be called for connections known to the handler).
 
 - 
scheduleprotected void schedule(java.lang.Runnable task) Schedule a task on the thread. Can be called on arbitrary threads.
 - 
handleAcceptprotected void handleAccept(java.nio.channels.SelectionKey key) Accept a connection request from a client.
 - 
handleConnectprotected void handleConnect(java.nio.channels.SelectionKey key) Complete a connection request to a server.
 - 
handleReadprotected void handleRead(java.nio.channels.SelectionKey key) Read data from a connection.
 - 
handleWriteprotected void handleWrite(java.nio.channels.SelectionKey key) Write data to a connection.
 
- 
 
-