asynctcp
Class Connection

java.lang.Object
  extended by asynctcp.Connection
Direct Known Subclasses:
IncomingConnection, OutgoingConnection

public abstract class Connection
extends java.lang.Object

An interface shared by all connections, both locally and remotely established.

Author:
Michael Parker

Nested Class Summary
static class Connection.ConnectionState
          An enumeration over all possible connection states.
 
Method Summary
 java.lang.Object attach(java.lang.Object obj)
          Attaches the given object to this connection.
 java.lang.Object attachment()
          Retrieves the attachment for this connection.
 Connection.ConnectionState disconnect()
          Attempts to close this connection if it is not closing or closed already.
 Connection.ConnectionState getConnectionState()
          Returns the current state of this connection.
 ConnectionListener getListener()
          Retrieves the listener for this connection.
 ASyncTCPMessageQueue getMessageQueue()
          Returns the MessageQueue object associated with this connection.
 AddressPortPair getRemoteHost()
          Returns the IP address and port number at which this connection terminates.
abstract  boolean isLocalConnection()
          Returns whether this connection was locally or remotely established.
abstract  Connection.ConnectionState reconnect(long timeout_ms)
          If this connection was established locally, meaning isLocalConnection returns true, then this method attempts to reestablish the connection.
 ConnectionListener setListener(ConnectionListener _cl)
          Sets cl as the listener on this connection.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getRemoteHost

public AddressPortPair getRemoteHost()
Returns the IP address and port number at which this connection terminates. If this connection is not established, null is returned.

Returns:
the destination IP address and port number

attach

public java.lang.Object attach(java.lang.Object obj)
Attaches the given object to this connection. An attached object may later be retrieved via the attachment method. Only one object may be attached at a time; invoking this method causes any previous attachment to be discarded. The current attachment may be discarded by attaching null.

Parameters:
obj - the object to be attached; may be null
Returns:
the previous attachment, if any; otherwise null

attachment

public java.lang.Object attachment()
Retrieves the attachment for this connection.

Returns:
the object currently attached to this connection, or null if there is no attachment

getConnectionState

public Connection.ConnectionState getConnectionState()
Returns the current state of this connection.

Returns:
the current connection state

disconnect

public Connection.ConnectionState disconnect()
Attempts to close this connection if it is not closing or closed already. Note that this ASyncTCP instance will deliver all enqueued messages before finally closing. If the current state is either DISCONNECTING or DISCONNECTED, then invoking this method has no effect. If the current state is CONNECTING, then the connection attempt is cancelled immdiately. If the current state is CONNECTED, then this connection enters the DISCONNECTING state until all enqueued messages are sent.

Returns:
the new state of the connection

reconnect

public abstract Connection.ConnectionState reconnect(long timeout_ms)
If this connection was established locally, meaning isLocalConnection returns true, then this method attempts to reestablish the connection. If the connection state is currently DISCONNECTING because the queue of this connection is still being emptied, then invoking this method cancels the disconnect and returns the state to CONNECTED. If the connection state is currently DISCONNECTED, then this method will begin establishing a new connection entirely, and change the connection state to CONNECTING. In any other state, invoking this method has no effect.

If this connection was established remotely, and has a state of DISCONNECTING because it is still emptying its message queue, then invoking this method simply cancels the disconnect and returns the state to CONNECTED. In any other state, invoking this method has no effect.

Parameters:
timeout_ms - the maximum amount of time, in milliseconds, before it is assumed that the connection attempt has failed
Returns:
the new state of the connection

isLocalConnection

public abstract boolean isLocalConnection()
Returns whether this connection was locally or remotely established.

Returns:
true if this connection was locally established, false otherwise

getMessageQueue

public ASyncTCPMessageQueue getMessageQueue()
Returns the MessageQueue object associated with this connection.

Returns:
the associated message queue

getListener

public ConnectionListener getListener()
Retrieves the listener for this connection.

Returns:
the current listener for this connection, or null if there is no listener

setListener

public ConnectionListener setListener(ConnectionListener _cl)
Sets cl as the listener on this connection. The listener can later be retrieved via the getListener method. Only one listener may be set at a time; invoking this method causes any previous listener to be discarded. The current listener may be discarded by passing in null.

Parameters:
_cl - the new listener for this connection
Returns:
the previous listener, if any; otherwise null