com.threerings.presents.client
Class BlockingCommunicator

java.lang.Object
  extended by com.threerings.presents.client.Communicator
      extended by com.threerings.presents.client.BlockingCommunicator
Direct Known Subclasses:
ClientCommunicator

public class BlockingCommunicator
extends Communicator

The client performs all network I/O on separate threads (one for reading and one for writing). The communicator class encapsulates that functionality.

 Logon synopsis:

 Client.logon():
 - Calls Communicator.start()
 Communicator.start():
 - spawn Reader thread
 Reader.run():
 { - connect
   - authenticate
 } if either fail, notify observers of failed logon
 - start writer thread
 - notify observers that we're logged on
 - read loop
 Writer.run():
 - write loop
 


Nested Class Summary
protected  class BlockingCommunicator.DatagramReader
          Handles the general flow of reading datagrams.
protected  class BlockingCommunicator.DatagramWriter
          Handles the general flow of writing datagrams.
protected  class BlockingCommunicator.Reader
          The reader encapsulates the authentication and message reading process.
protected static class BlockingCommunicator.TerminationMessage
          This is used to terminate the writer threads.
protected  class BlockingCommunicator.Writer
          The writer encapsulates the message writing process.
 
Field Summary
protected  ByteBufferOutputStream _bout
          We use these to write our upstream datagrams.
protected  ByteBuffer _buf
          We use these to read our downstream datagrams.
protected  SocketChannel _channel
           
protected  DatagramChannel _datagramChannel
           
protected  BlockingCommunicator.DatagramReader _datagramReader
           
protected  BlockingCommunicator.DatagramWriter _datagramWriter
           
protected  Queue<UpstreamMessage> _dataq
           
protected  MessageDigest _digest
           
protected  FramedInputStream _fin
          We use this to frame our downstream messages.
protected  FramingOutputStream _fout
          We use this to frame our upstream messages.
protected  ClassLoader _loader
           
protected  Exception _logonError
           
protected  Queue<UpstreamMessage> _msgq
           
protected  ObjectInputStream _oin
           
protected  ObjectOutputStream _oout
           
protected  BlockingCommunicator.Reader _reader
           
protected  byte[] _secret
           
protected  Selector _selector
           
protected  DatagramSequencer _sequencer
           
protected  UnreliableObjectOutputStream _uout
           
protected  BlockingCommunicator.Writer _writer
           
protected static int DATAGRAM_ATTEMPTS_PER_PORT
          The number of times per port to try to establish a datagram "connection".
protected static long DATAGRAM_RESPONSE_WAIT
          The number of milliseconds to wait for a response datagram.
 
Fields inherited from class com.threerings.presents.client.Communicator
_client, _lastWrite, _omgr
 
Constructor Summary
BlockingCommunicator(Client client)
          Creates a new communicator instance which is associated with the supplied client.
 
Method Summary
protected  void closeChannel()
          Closes the socket channel that we have open to the server.
protected  void closeDatagramChannel()
          Closes the datagram channel.
protected  void connectionClosed()
          Callback called by the reader if the server closes the other end of the connection.
protected  void connectionFailed(IOException ioe)
          Callback called by the reader or writer thread when something goes awry with our socket connection to the server.
protected  void datagramReaderDidExit()
          Callback called by the datagram reader thread when it goes away.
protected  void datagramWriterDidExit()
          Callback called by the datagram writer thread when it goes away.
protected  boolean debugLogMessages()
           
 long getLastWrite()
          Returns the time at which we last sent a packet to the server.
 boolean getTransmitDatagrams()
          Checks whether we should transmit datagrams.
 void gotBootstrap()
          Notifies the communicator that the client has received its bootstrap data.
 void logoff()
          Delivers a logoff notification to the server and shuts down the network connection.
 void logon()
          Logs on to the server and initiates our full-duplex message exchange.
protected  void logonSucceeded(AuthResponseData data)
          Called when the authentication process completes successfully.
protected  void openChannel(InetAddress host)
           
 void postMessage(UpstreamMessage msg)
          Queues up the specified message for delivery upstream.
protected  int readDatagram(ByteBuffer buf)
          Reads a datagram into the supplied buffer.
protected  void readerDidExit()
          Callback called by the reader thread when it goes away.
protected  boolean readFrame()
          Reads a frame from the socket.
protected  DownstreamMessage receiveDatagram()
          Reads a datagram from the socket (blocking until a datagram has arrived).
protected  DownstreamMessage receiveMessage()
          Reads a new message from the socket (blocking until a message has arrived).
protected  void sendDatagram(UpstreamMessage msg)
          Sends a datagram over the datagram socket.
protected  void sendMessage(UpstreamMessage msg)
          Writes the supplied message to the socket.
 void setClassLoader(ClassLoader loader)
          Configures this communicator with a custom class loader to be used when reading and writing objects over the network.
protected  void throttleOutgoingMessage()
          Throttles an outgoing message operation in a thread-safe manner.
protected  int writeDatagram(ByteBuffer buf)
          Writes the datagram contained in the supplied buffer.
protected  int writeMessage(ByteBuffer buf)
          Writes the message contained in the supplied buffer.
protected  void writerDidExit()
          Callback called by the writer thread when it goes away.
 
Methods inherited from class com.threerings.presents.client.Communicator
clientCleanup, gotAuthResponse, notifyClientObservers, processMessage, updateWriteStamp
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_reader

protected BlockingCommunicator.Reader _reader

_writer

protected BlockingCommunicator.Writer _writer

_datagramReader

protected BlockingCommunicator.DatagramReader _datagramReader

_datagramWriter

protected BlockingCommunicator.DatagramWriter _datagramWriter

_channel

protected SocketChannel _channel

_msgq

protected Queue<UpstreamMessage> _msgq

_selector

protected Selector _selector

_datagramChannel

protected DatagramChannel _datagramChannel

_dataq

protected Queue<UpstreamMessage> _dataq

_logonError

protected Exception _logonError

_fout

protected FramingOutputStream _fout
We use this to frame our upstream messages.


_oout

protected ObjectOutputStream _oout

_fin

protected FramedInputStream _fin
We use this to frame our downstream messages.


_oin

protected ObjectInputStream _oin

_bout

protected ByteBufferOutputStream _bout
We use these to write our upstream datagrams.


_uout

protected UnreliableObjectOutputStream _uout

_digest

protected MessageDigest _digest

_secret

protected byte[] _secret

_buf

protected ByteBuffer _buf
We use these to read our downstream datagrams.


_sequencer

protected DatagramSequencer _sequencer

_loader

protected ClassLoader _loader

DATAGRAM_ATTEMPTS_PER_PORT

protected static final int DATAGRAM_ATTEMPTS_PER_PORT
The number of times per port to try to establish a datagram "connection".

See Also:
Constant Field Values

DATAGRAM_RESPONSE_WAIT

protected static final long DATAGRAM_RESPONSE_WAIT
The number of milliseconds to wait for a response datagram.

See Also:
Constant Field Values
Constructor Detail

BlockingCommunicator

public BlockingCommunicator(Client client)
Creates a new communicator instance which is associated with the supplied client.

Method Detail

logon

public void logon()
Description copied from class: Communicator
Logs on to the server and initiates our full-duplex message exchange.

Specified by:
logon in class Communicator

logoff

public void logoff()
Description copied from class: Communicator
Delivers a logoff notification to the server and shuts down the network connection. Also causes all communication threads to terminate.

Specified by:
logoff in class Communicator

gotBootstrap

public void gotBootstrap()
Description copied from class: Communicator
Notifies the communicator that the client has received its bootstrap data.

Specified by:
gotBootstrap in class Communicator

postMessage

public void postMessage(UpstreamMessage msg)
Description copied from class: Communicator
Queues up the specified message for delivery upstream.

Specified by:
postMessage in class Communicator

setClassLoader

public void setClassLoader(ClassLoader loader)
Description copied from class: Communicator
Configures this communicator with a custom class loader to be used when reading and writing objects over the network.

Specified by:
setClassLoader in class Communicator

getLastWrite

public long getLastWrite()
Description copied from class: Communicator
Returns the time at which we last sent a packet to the server.

Overrides:
getLastWrite in class Communicator

getTransmitDatagrams

public boolean getTransmitDatagrams()
Description copied from class: Communicator
Checks whether we should transmit datagrams.

Overrides:
getTransmitDatagrams in class Communicator

logonSucceeded

protected void logonSucceeded(AuthResponseData data)
Description copied from class: Communicator
Called when the authentication process completes successfully. Derived classes can override this method and complete any initialization that need wait for authentication success.

Overrides:
logonSucceeded in class Communicator

connectionFailed

protected void connectionFailed(IOException ioe)
Callback called by the reader or writer thread when something goes awry with our socket connection to the server.


connectionClosed

protected void connectionClosed()
Callback called by the reader if the server closes the other end of the connection.


readerDidExit

protected void readerDidExit()
Callback called by the reader thread when it goes away.


writerDidExit

protected void writerDidExit()
Callback called by the writer thread when it goes away.


closeChannel

protected void closeChannel()
Closes the socket channel that we have open to the server. Called by either readerDidExit() or writerDidExit() whichever is called last.


datagramReaderDidExit

protected void datagramReaderDidExit()
Callback called by the datagram reader thread when it goes away.


datagramWriterDidExit

protected void datagramWriterDidExit()
Callback called by the datagram writer thread when it goes away.


closeDatagramChannel

protected void closeDatagramChannel()
Closes the datagram channel.


sendMessage

protected void sendMessage(UpstreamMessage msg)
                    throws IOException
Writes the supplied message to the socket.

Throws:
IOException

writeMessage

protected int writeMessage(ByteBuffer buf)
                    throws IOException
Writes the message contained in the supplied buffer.

Returns:
the number of bytes written.
Throws:
IOException

sendDatagram

protected void sendDatagram(UpstreamMessage msg)
                     throws IOException
Sends a datagram over the datagram socket.

Throws:
IOException

writeDatagram

protected int writeDatagram(ByteBuffer buf)
                     throws IOException
Writes the datagram contained in the supplied buffer.

Returns:
the number of bytes written.
Throws:
IOException

receiveMessage

protected DownstreamMessage receiveMessage()
                                    throws IOException
Reads a new message from the socket (blocking until a message has arrived).

Throws:
IOException

readFrame

protected boolean readFrame()
                     throws IOException
Reads a frame from the socket.

Returns:
true if a complete frame is available, false if more needs to be read.
Throws:
IOException

receiveDatagram

protected DownstreamMessage receiveDatagram()
                                     throws IOException
Reads a datagram from the socket (blocking until a datagram has arrived).

Throws:
IOException

readDatagram

protected int readDatagram(ByteBuffer buf)
                    throws IOException
Reads a datagram into the supplied buffer.

Returns:
the number of bytes read.
Throws:
IOException

openChannel

protected void openChannel(InetAddress host)
                    throws IOException
Throws:
IOException

debugLogMessages

protected boolean debugLogMessages()

throttleOutgoingMessage

protected void throttleOutgoingMessage()
Throttles an outgoing message operation in a thread-safe manner.