org.javagamesfactory.nioservers
Class ResponseListener

java.lang.Object
  extended by org.javagamesfactory.nioservers.ResponseListener
All Implemented Interfaces:
java.lang.Runnable

public class ResponseListener
extends java.lang.Object
implements java.lang.Runnable

FIXME: this class has the same bug that originally existed in the StringBasedServer - if it receives fewer than 4 bytes in the first read of a message, it will NOT correctly handle that (it needs the same workaround in SBS, i.e needs to check if the very first read reads fewer than 4 bytes, and if so, ignore it, reset one buffer, advance the other, and try again)

Worker thread that listens to a SocketChannel, receives incoming messages, decodes them, and then passes them on one-by-one as String instances to a specified other class of your choice.

Implements a protocol of:

MSG_LENGTH_IN_BYTES + MESSAGE
(there is no explicit delimiting of message start/end)


Field Summary
 int bufferSize
           
 
Constructor Summary
ResponseListener(java.lang.String t, iMessageProcessor mp, java.nio.channels.SocketChannel sc, java.nio.charset.Charset charset)
           
 
Method Summary
 void run()
          Continuously reads from the channel, with a blocking-read, until it receives a complete message, which it then passes to the message processor.
 void start()
          Starts a new thread for this listener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bufferSize

public int bufferSize
Constructor Detail

ResponseListener

public ResponseListener(java.lang.String t,
                        iMessageProcessor mp,
                        java.nio.channels.SocketChannel sc,
                        java.nio.charset.Charset charset)
Parameters:
t - human-readable title of this listener, to make it easier to distinguish between log messages and debugging
mp - processor that will handle any messages that this instance receives and parses into strings
sc - the low-level channel to read messages from
charset - the encoding that the low-level bytes are in so that this instance can decode them into chars
Method Detail

start

public void start()
Starts a new thread for this listener


run

public void run()
Continuously reads from the channel, with a blocking-read, until it receives a complete message, which it then passes to the message processor.

Format of messages is: [length-in-bytes] [message]

length is specified as a 32-bit integer, i.e. 4 bytes

Specified by:
run in interface java.lang.Runnable