org.javagamesfactory.nioservers
Class StringBasedClient

java.lang.Object
  extended by org.javagamesfactory.nioservers.StringBasedClient

public class StringBasedClient
extends java.lang.Object

A simple blocking client class that uses NIO over TCP, making it easier to interface with / share code with NIO servers you write.

NB: there is no reason this class could not be made to be non-blocking, but it was chosen to be blocking more for convenience of writing game-clients, which are easier to write if you can assume all IO is blocking-on-send.


Field Summary
static int defaultByteBufferSize
          Default size of maximum outgoing buffer - this limits the length of the longest single message you can possibly send with this class
protected  org.apache.log4j.Logger logger
           
static int maximumMessagePrefixToLog
          The logger automatically logs ALL transmitted data as INFO, but will truncate to this many characters when displaying what it has sent - or will display complete strings if you set this to anything less than zero.
protected  iMessageProcessor messageProcessor
           
protected  java.nio.channels.SocketChannel sc
           
protected  java.lang.String serverHostname
           
protected  int serverPort
           
protected  java.lang.String title
           
 
Constructor Summary
StringBasedClient()
          Simple constructor - remember that you MUST also call the setServerHostname and setServerPort methods before calling connect
StringBasedClient(int bufferSize)
          Remember that you MUST also call the setServerHostname and setServerPort methods before calling connect
StringBasedClient(java.lang.String h, int p, int bufferSize)
          Fully configures the connector, so that you can safely immediately call connect
 
Method Summary
 void close()
          You should always manually call this method when you wish to disconnect from the server, as it will close down the internal Selector
 void connect()
          Attempts to connect to the remote server, configure the TCP socket to disable Nagle's algorithm (to reduce latency), and start a slave thread to receive incoming messages.
 java.net.InetAddress getRemoteInetAddress()
          Mainly for debugging - allows you to check exactly which remote machine this connector has connected to
 int getRemotePort()
          Mainly for debugging - allows you to check exactly which remote machine this connector has connected to
 java.lang.String getServerHostname()
          Convenience for if you want to interpret a Connector e.g after an error has been thrown
 int getServerPort()
          Convenience for if you want to interpret a Connector e.g after an error has been thrown
 boolean isConnected()
          Confirms whether this connector has connected at the socket-level to the remote machine
 void sendMessage(java.lang.String message)
          Blocking send messages to the server; not thread-safe!
protected  void sendStaggeredMessage(java.lang.String message)
          For testing only - deliberately splits messages into two pieces and sends them separately - this is VERY VERY useful for testing whether a JVM on the other end using NIO is correctly merging packets into complete messages (NB: even a NIO server in blocking mode will still occasionally receive split messages due to the internet)
 void setServerHostname(java.lang.String s)
          Configures the hostname that the connector will connect to
 void setServerPort(int i)
          Configures the port that the connector will connect to
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

maximumMessagePrefixToLog

public static int maximumMessagePrefixToLog
The logger automatically logs ALL transmitted data as INFO, but will truncate to this many characters when displaying what it has sent - or will display complete strings if you set this to anything less than zero.

Default value is 60 chars


defaultByteBufferSize

public static int defaultByteBufferSize
Default size of maximum outgoing buffer - this limits the length of the longest single message you can possibly send with this class


logger

protected org.apache.log4j.Logger logger

title

protected java.lang.String title

messageProcessor

protected iMessageProcessor messageProcessor

serverHostname

protected java.lang.String serverHostname

serverPort

protected int serverPort

sc

protected java.nio.channels.SocketChannel sc
Constructor Detail

StringBasedClient

public StringBasedClient()
Simple constructor - remember that you MUST also call the setServerHostname and setServerPort methods before calling connect

See Also:
setServerHostname(String), setServerPort(int)

StringBasedClient

public StringBasedClient(int bufferSize)
Remember that you MUST also call the setServerHostname and setServerPort methods before calling connect

Parameters:
bufferSize - size of outgoing buffer in bytes
See Also:
setServerHostname(String), setServerPort(int)

StringBasedClient

public StringBasedClient(java.lang.String h,
                         int p,
                         int bufferSize)
Fully configures the connector, so that you can safely immediately call connect

Parameters:
h - the hostname of the server to connect to
p - the port of the server to connect to
bufferSize - size of outgoing buffer in bytes
See Also:
connect()
Method Detail

isConnected

public boolean isConnected()
Confirms whether this connector has connected at the socket-level to the remote machine

Returns:
true if the SocketChannel has connected, false if otherwise

setServerHostname

public void setServerHostname(java.lang.String s)
Configures the hostname that the connector will connect to

Parameters:
s - target hostname

setServerPort

public void setServerPort(int i)
Configures the port that the connector will connect to

Parameters:
i - port in range 0-65535

getServerHostname

public java.lang.String getServerHostname()
Convenience for if you want to interpret a Connector e.g after an error has been thrown

Returns:
the hostname you configured this class with

getServerPort

public int getServerPort()
Convenience for if you want to interpret a Connector e.g after an error has been thrown

Returns:
the port you configured this class with

getRemoteInetAddress

public java.net.InetAddress getRemoteInetAddress()
Mainly for debugging - allows you to check exactly which remote machine this connector has connected to

Returns:
the address of the remote server this connector has connected to, or null if it has not yet successfully completed a connect() call
See Also:
connect()

getRemotePort

public int getRemotePort()
Mainly for debugging - allows you to check exactly which remote machine this connector has connected to

Returns:
the port of the remote server this connector has connected to, or -1 if it has not yet successfully completed a connect() call
See Also:
connect()

connect

public void connect()
             throws java.lang.Throwable
Attempts to connect to the remote server, configure the TCP socket to disable Nagle's algorithm (to reduce latency), and start a slave thread to receive incoming messages.

Before calling this method you MUST have configured the server hostname and port

Throws:
java.lang.Throwable - if anything at all goes wrong (lots could go wrong here!)
See Also:
setServerHostname(String), setServerPort(int)

close

public void close()
You should always manually call this method when you wish to disconnect from the server, as it will close down the internal Selector


sendMessage

public void sendMessage(java.lang.String message)
                 throws java.io.IOException
Blocking send messages to the server; not thread-safe!

Parameters:
message -
Throws:
java.io.IOException

sendStaggeredMessage

protected void sendStaggeredMessage(java.lang.String message)
                             throws java.io.IOException
For testing only - deliberately splits messages into two pieces and sends them separately - this is VERY VERY useful for testing whether a JVM on the other end using NIO is correctly merging packets into complete messages (NB: even a NIO server in blocking mode will still occasionally receive split messages due to the internet)

Parameters:
message -
Throws:
java.io.IOException