Jumpi v1.2.0

org.jumpi.impl.connector.tcp
Class TcpConnection

java.lang.Object
  |
  +--org.jumpi.impl.connector.tcp.TcpConnection
All Implemented Interfaces:
Comparable, java.lang.Runnable, Task

public class TcpConnection
extends java.lang.Object
implements Task, Comparable

A TcpConnection represents a peer to peer Socket connection between TcpDestination endpoints.

The TcpConnection is referenced by the remote TcpDestination. There is no concept of client or server on a TcpConnection, both endpoints are equivalent. Accepting a Socket or creating a Socket is the responsibility of the TcpConnector. The Socket options, i.e. read timeout, linger etc, are determined by the TcpConnector's configuration.

Data can be sent to the remote TcpDestination through the TcpConnection. Data is continuously read from the remote TcpDestination by the TcpConnection's reader task. This task uses a dedicated thread to read data. Failure conditions when reading from or writing to the socket cause the TcpConnection to be closed by the TcpConnector. See run().

A last used timestamp is kept on record for the TcpDestination. This timestamp is used to control caching policies by the TcpConnector.


Constructor Summary
TcpConnection(TcpConnector tcp, Jumpi jumpi, java.lang.String taskId, TcpDestination dest, java.net.Socket sock, long flowcontrolTimeout)
          Creates a new TcpConnection object.
 
Method Summary
 void close()
          Close the TcpConnection, in doing so close the underlying Socket connection if it exists.
 int compareTo(java.lang.Object other)
          Ordering function of TcpConnections, based on the last used timestamp.
 TcpDestination getDestination()
          Return the remote peer TcpDestination to which this TcpConnection is associated.
 long getLastUsed()
          Return the timestamp of the last send or receive operation.
 java.lang.String getTaskId()
          A unique name for the task in the VM.
 boolean hasMatchedReceive()
          Returns true if at least one message has been successfully received by the TcpConnector.
 boolean isLongRunning()
          Returns true always indicating that the reader thread is used exclusively by the TcpConnection, potentially for long periods.
 boolean isSchedulable()
          The reader thread is scheduled to run the Task as long as the TcpConnection is not closed.
 void run()
          Read packeted data from the instance's Socket.
 void send(byte[] data)
          Send data over the Socket to the remote peer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TcpConnection

public TcpConnection(TcpConnector tcp,
                     Jumpi jumpi,
                     java.lang.String taskId,
                     TcpDestination dest,
                     java.net.Socket sock,
                     long flowcontrolTimeout)
Creates a new TcpConnection object.

Parameters:
tcp - the TcpConnector parent.
jumpi - the Jumpi instance parent of TcpConnector.
taskId - the unique taskId for the reader task.
dest - the remote peer TcpDestination.
sock - the Socket associated with the TcpConnection.
flowcontrolTimeout - the timeout in millis for matching a received message.
Method Detail

compareTo

public int compareTo(java.lang.Object other)
Ordering function of TcpConnections, based on the last used timestamp. This is used to determine which TcpConnections are least recently used and are to be considered first for discarding from a connection cache.

Specified by:
compareTo in interface Comparable
Parameters:
other - the other TcpConnection.
Returns:
-1 if less recently used, 0 equal, and 1 if more recently used than other.

getTaskId

public java.lang.String getTaskId()
A unique name for the task in the VM.

Specified by:
getTaskId in interface Task
Returns:
a unique name for the task in the VM.

getLastUsed

public long getLastUsed()
Return the timestamp of the last send or receive operation.

Returns:
the timestamp of the last send or receive operation.

send

public void send(byte[] data)
          throws java.lang.Exception
Send data over the Socket to the remote peer.

Parameters:
data - the raw data.
Throws:
java.lang.Exception - if any error condition occurs.

close

public void close()
Close the TcpConnection, in doing so close the underlying Socket connection if it exists. This method is called exclusively by TcpConnection.closeConnection()


isSchedulable

public boolean isSchedulable()
The reader thread is scheduled to run the Task as long as the TcpConnection is not closed.

Specified by:
isSchedulable in interface Task
Returns:
true if the Socket is not closed.

isLongRunning

public boolean isLongRunning()
Returns true always indicating that the reader thread is used exclusively by the TcpConnection, potentially for long periods.

Specified by:
isLongRunning in interface Task
Returns:
true always.

getDestination

public TcpDestination getDestination()
Return the remote peer TcpDestination to which this TcpConnection is associated.

Returns:
the remote peer TcpDestination.

run

public void run()
Read packeted data from the instance's Socket. Run is continually called as long as the TcpConnection is not closed. The following conditions can occur whilst reading from the Socket:

Specified by:
run in interface java.lang.Runnable

hasMatchedReceive

public boolean hasMatchedReceive()
Returns true if at least one message has been successfully received by the TcpConnector. Used to determine if the received data can match wildcard destinations or not.

Returns:
true if at least one message has been dispatched to the TcpConnector successfully, else false.

Jumpi v1.2.0

Copyright © 2003, Peter Jonathan Klauser.