com.threerings.nio.conman
Class Connection

java.lang.Object
  extended by com.threerings.nio.conman.Connection
All Implemented Interfaces:
NetEventHandler
Direct Known Subclasses:
PresentsConnection

public abstract class Connection
extends Object
implements NetEventHandler

Implements the net event handler interface to check for delinquency and manages a client connection. Subclasses must handle incoming data in NetEventHandler.handleEvent(long).


Field Summary
protected  SocketChannel _channel
           
protected  ConnectionManager _cmgr
           
protected  int _connectionId
           
protected static int _lastConnectionId
          The last connection id assigned.
protected  long _lastEvent
           
 SelectionKey selkey
          The key used by the NIO code to track this connection.
 
Constructor Summary
Connection()
           
 
Method Summary
 void asyncClose()
          Queues up a request to have this connection closed by the connection manager once all messages in its queue have been written to its target.
 void becameIdle()
          Called if the handler is deemed to be idle.
 boolean checkIdle(long idleStamp)
          Called to ensure that this channel has not been idle for longer than is possible in happily operating circumstances.
 void close()
          Closes this connection and unregisters it from the connection manager.
protected  void closeSocket()
          Closes the socket associated with this connection.
 void connectFailure(IOException ioe)
          Called when an outgoing socket experiences a connect failure.
 SocketChannel getChannel()
          Returns the non-blocking socket object used to construct this connection.
 int getConnectionId()
          Returns the connection's unique identifier.
 InetAddress getInetAddress()
          Returns the address associated with this connection or null if it has no underlying socket channel.
 void init(ConnectionManager cmgr, SocketChannel channel, long createStamp)
          Initializes a connection object with a socket and related info.
 boolean isClosed()
          Returns true if this connection is closed.
 void networkFailure(IOException ioe)
          Called when there is a failure reading or writing to this connection.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.threerings.nio.conman.NetEventHandler
handleEvent
 

Field Detail

selkey

public SelectionKey selkey
The key used by the NIO code to track this connection.


_cmgr

protected ConnectionManager _cmgr

_channel

protected SocketChannel _channel

_lastEvent

protected long _lastEvent

_connectionId

protected int _connectionId

_lastConnectionId

protected static int _lastConnectionId
The last connection id assigned.

Constructor Detail

Connection

public Connection()
Method Detail

init

public void init(ConnectionManager cmgr,
                 SocketChannel channel,
                 long createStamp)
          throws IOException
Initializes a connection object with a socket and related info.

Parameters:
cmgr - The connection manager with which this connection is associated.
channel - The socket channel from which we'll be reading messages.
createStamp - The time at which this connection was created.
Throws:
IOException

getConnectionId

public int getConnectionId()
Returns the connection's unique identifier.


getChannel

public SocketChannel getChannel()
Returns the non-blocking socket object used to construct this connection.


getInetAddress

public InetAddress getInetAddress()
Returns the address associated with this connection or null if it has no underlying socket channel.


isClosed

public boolean isClosed()
Returns true if this connection is closed.


close

public void close()
Closes this connection and unregisters it from the connection manager. This should only be called from the conmgr thread.


asyncClose

public void asyncClose()
Queues up a request to have this connection closed by the connection manager once all messages in its queue have been written to its target.


connectFailure

public void connectFailure(IOException ioe)
Called when an outgoing socket experiences a connect failure. The connection manager will have cleaned up the partial registration needed during the connect process, so we are only responsible for closing our socket.


networkFailure

public void networkFailure(IOException ioe)
Called when there is a failure reading or writing to this connection. We notify the connection manager and close ourselves down.


checkIdle

public boolean checkIdle(long idleStamp)
Description copied from interface: NetEventHandler
Called to ensure that this channel has not been idle for longer than is possible in happily operating circumstances.

Specified by:
checkIdle in interface NetEventHandler
Parameters:
idleStamp - if the handler's last event occurred more recently than this timestamp, it should return false, otherwise true.
Returns:
true if the handler is idle (in which case it will be closed shortly), false if it is not.

becameIdle

public void becameIdle()
Description copied from interface: NetEventHandler
Called if the handler is deemed to be idle. Should shutdown any associated connection and remove any registrations with the connection manager.

Specified by:
becameIdle in interface NetEventHandler

toString

public String toString()
Overrides:
toString in class Object

closeSocket

protected void closeSocket()
Closes the socket associated with this connection. This happens when we receive EOF, are requested to close down or when our connection fails.