com.threerings.presents.server.net
Class PresentsConnection

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

public class PresentsConnection
extends Connection

Parses incoming network data into a stream of Message objects, sends messages to the client and adds datagram support to a connection.


Nested Class Summary
static interface PresentsConnection.MessageHandler
          Used with setMessageHandler(com.threerings.presents.server.net.PresentsConnection.MessageHandler).
 
Field Summary
protected  InetSocketAddress _datagramAddress
           
protected  DatagramChannel _datagramChannel
           
protected  byte[] _datagramSecret
           
protected  MessageDigest _digest
           
protected  FramedInputStream _fin
           
protected  PresentsConnection.MessageHandler _handler
           
protected  ClassLoader _loader
           
protected  ObjectInputStream _oin
           
protected  ObjectOutputStream _oout
           
protected  PresentsConnectionManager _pcmgr
           
protected  DatagramSequencer _sequencer
           
protected  boolean _transmitDatagrams
           
 
Fields inherited from class com.threerings.nio.conman.Connection
_channel, _cmgr, _connectionId, _lastConnectionId, _lastEvent, selkey
 
Constructor Summary
PresentsConnection()
           
 
Method Summary
 void clearMessageHandler()
          Clears out our message handler, causing any subsequent messages to be dropped on arrival.
protected  ObjectInputStream createObjectInputStream(InputStream src)
          Creates the object input stream used by this connection to communicate.
 InetSocketAddress getDatagramAddress()
          Returns the address to which datagrams should be sent or null if no datagram address has been established.
 DatagramChannel getDatagramChannel()
          Returns the channel through which datagrams should be sent or null if no datagram channel has been established.
protected  DatagramSequencer getDatagramSequencer()
          Returns a reference to the connection's datagram sequencer.
protected  ObjectInputStream getObjectInputStream()
          Returns the object input stream associated with this connection.
protected  ObjectOutputStream getObjectOutputStream(FramingOutputStream fout)
          Returns the object output stream associated with this connection (creating it if necessary).
 boolean getTransmitDatagrams()
          Checks whether we should transmit datagrams.
 void handleDatagram(InetSocketAddress source, DatagramChannel channel, ByteBuffer buf, long when)
          Processes a datagram sent to this connection.
 int handleEvent(long when)
          Called when a network event has occurred on this handler's source.
protected  void inheritStreams(PresentsConnection other)
          Instructs this connection to inherit its streams from the supplied connection object.
 void init(ConnectionManager cmgr, SocketChannel channel, long createStamp)
          Initializes the connection with its channel.
 void postMessage(Message msg)
          Posts a message for delivery to this connection.
 void setClassLoader(ClassLoader loader)
          Configures this connection with a custom class loader.
 void setDatagramSecret(String secret)
          Sets the secret string used to authenticate datagrams from the client.
 void setMessageHandler(PresentsConnection.MessageHandler handler)
          Instructs the connection to pass parsed messages on to this handler for processing.
protected  void setObjectOutputStream(ObjectOutputStream oout)
          Sets the object output stream used by this connection.
 void setTransmitDatagrams(boolean transmit)
          Sets whether we should transmit datagrams.
 
Methods inherited from class com.threerings.nio.conman.Connection
asyncClose, becameIdle, checkIdle, close, closeSocket, connectFailure, getChannel, getConnectionId, getInetAddress, isClosed, networkFailure, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_fin

protected FramedInputStream _fin

_oin

protected ObjectInputStream _oin

_oout

protected ObjectOutputStream _oout

_datagramAddress

protected InetSocketAddress _datagramAddress

_datagramChannel

protected DatagramChannel _datagramChannel

_datagramSecret

protected byte[] _datagramSecret

_transmitDatagrams

protected boolean _transmitDatagrams

_digest

protected MessageDigest _digest

_sequencer

protected DatagramSequencer _sequencer

_handler

protected PresentsConnection.MessageHandler _handler

_loader

protected ClassLoader _loader

_pcmgr

protected PresentsConnectionManager _pcmgr
Constructor Detail

PresentsConnection

public PresentsConnection()
Method Detail

init

public void init(ConnectionManager cmgr,
                 SocketChannel channel,
                 long createStamp)
          throws IOException
Initializes the connection with its channel. Must be called with a PresentsConnectionManager as cmgr.

Overrides:
init in class Connection
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

setMessageHandler

public void setMessageHandler(PresentsConnection.MessageHandler handler)
Instructs the connection to pass parsed messages on to this handler for processing. This should be done before the connection is turned loose to process messages.


clearMessageHandler

public void clearMessageHandler()
Clears out our message handler, causing any subsequent messages to be dropped on arrival. A log message is recorded for the dropped messages.


setClassLoader

public void setClassLoader(ClassLoader loader)
Configures this connection with a custom class loader.


setTransmitDatagrams

public void setTransmitDatagrams(boolean transmit)
Sets whether we should transmit datagrams.


getTransmitDatagrams

public boolean getTransmitDatagrams()
Checks whether we should transmit datagrams.


getDatagramAddress

public InetSocketAddress getDatagramAddress()
Returns the address to which datagrams should be sent or null if no datagram address has been established.


getDatagramChannel

public DatagramChannel getDatagramChannel()
Returns the channel through which datagrams should be sent or null if no datagram channel has been established.


setDatagramSecret

public void setDatagramSecret(String secret)
Sets the secret string used to authenticate datagrams from the client.


postMessage

public void postMessage(Message msg)
Posts a message for delivery to this connection. The message will be delivered by the conmgr thread as soon as it gets to it.


handleDatagram

public void handleDatagram(InetSocketAddress source,
                           DatagramChannel channel,
                           ByteBuffer buf,
                           long when)
Processes a datagram sent to this connection.


handleEvent

public int handleEvent(long when)
Description copied from interface: NetEventHandler
Called when a network event has occurred on this handler's source.

Returns:
the number of bytes read from the network as a result of handling this event.

getObjectInputStream

protected ObjectInputStream getObjectInputStream()
Returns the object input stream associated with this connection. This should only be used by the connection manager.


createObjectInputStream

protected ObjectInputStream createObjectInputStream(InputStream src)
Creates the object input stream used by this connection to communicate. This may be overridden by subclasses to create custom streams.


inheritStreams

protected void inheritStreams(PresentsConnection other)
Instructs this connection to inherit its streams from the supplied connection object. This is called by the connection manager when the time comes to pass streams from the authing connection to the running connection.


getObjectOutputStream

protected ObjectOutputStream getObjectOutputStream(FramingOutputStream fout)
Returns the object output stream associated with this connection (creating it if necessary). This should only be used by the connection manager.


setObjectOutputStream

protected void setObjectOutputStream(ObjectOutputStream oout)
Sets the object output stream used by this connection. This should only be called by the connection manager.


getDatagramSequencer

protected DatagramSequencer getDatagramSequencer()
Returns a reference to the connection's datagram sequencer. This should only be called by the connection manager.