|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.javagamesfactory.nioservers.StringBasedClient
public class StringBasedClient
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 |
---|
public static int maximumMessagePrefixToLog
Default value is 60 chars
public static int defaultByteBufferSize
protected org.apache.log4j.Logger logger
protected java.lang.String title
protected iMessageProcessor messageProcessor
protected java.lang.String serverHostname
protected int serverPort
protected java.nio.channels.SocketChannel sc
Constructor Detail |
---|
public StringBasedClient()
setServerHostname(String)
,
setServerPort(int)
public StringBasedClient(int bufferSize)
bufferSize
- size of outgoing buffer in bytessetServerHostname(String)
,
setServerPort(int)
public StringBasedClient(java.lang.String h, int p, int bufferSize)
h
- the hostname of the server to connect top
- the port of the server to connect tobufferSize
- size of outgoing buffer in bytesconnect()
Method Detail |
---|
public boolean isConnected()
public void setServerHostname(java.lang.String s)
s
- target hostnamepublic void setServerPort(int i)
i
- port in range 0-65535public java.lang.String getServerHostname()
public int getServerPort()
public java.net.InetAddress getRemoteInetAddress()
connect()
public int getRemotePort()
connect()
public void connect() throws java.lang.Throwable
Before calling this method you MUST have configured the server hostname and port
java.lang.Throwable
- if anything at all goes wrong (lots could go wrong here!)setServerHostname(String)
,
setServerPort(int)
public void close()
public void sendMessage(java.lang.String message) throws java.io.IOException
message
-
java.io.IOException
protected void sendStaggeredMessage(java.lang.String message) throws java.io.IOException
message
-
java.io.IOException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |