|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectdd.net.NetClient
Base class for network client protocol. There are two classes responsible for the actual communication of messages from a game client to a game server. This is the implementation of the client part of that system. The communication is performed with TCP sockets that stay open for the duration of a game session. Unlike many request systems (like HTTP), both clients and servers can initiate requests. Therefore, both listen for commands from the other end and react accordingly.
The communication between client and server is predouminantly through
a line-oriented text protocol. A command is usually a single line
containing a command word and an arbitrary number of arguments. All
the clients and servers do is interpret the network message and call
the appropriate Handler
that is registered to accept that kind
of message.
The client simply keeps a connection to the server with which it is connected. It has now awareness of the other clients except through the protocol provided by the server.
A minimal client must respond to the following messages:
AYT
sent by the server to determine if the client
is still running. The response should be [yes]
hello
sent by the server in the negotiation process.
See the NetServer
documentation for a thorough explanation.bye
sent at the (successful) end of communication.
the client should exit shortly after receiving this messages, and it
cannot send any further messages to the serverThis class provides implementation of the behaviors described above as well as default debugging behaviors. To implement a custom client / server protocol, one can either subclass this class, or instantiate this class and provide it with Handler objects that perform the custom game protocol.
Field Summary | |
static long |
ALIVE_PERIOD
Number of milliseconds between AYT calls |
static java.lang.String |
DEFAULT_SERVER
Name of the default server. |
static java.lang.String |
DISCOVERY_URL
URL to initiate server discovery. |
protected UI |
uiClient
|
Constructor Summary | |
NetClient()
Creates a new client connecting to the default server and port. |
|
NetClient(java.lang.String serverName)
|
|
NetClient(java.lang.String serverName,
int port)
|
Method Summary | |
void |
addHandler(java.lang.String commandName,
Handler handler)
|
static java.util.List |
discoverVenues()
|
java.lang.String |
getClientName()
|
java.io.BufferedReader |
getReader()
Returns the reader associated with the networking layer. |
void |
gotChatMessage(java.lang.String user,
java.lang.String message)
Called when a chat message is received. |
void |
handleMessage(java.lang.String message)
Handle an incoming network message. |
boolean |
isRunning()
|
static void |
main(java.lang.String[] args)
|
void |
run()
|
void |
sendChatMessage(java.lang.String user,
java.lang.String message)
Sends a chat message over the network. |
void |
sendMessage(java.lang.String message)
Send an outgoing network message. |
void |
setChatClient(Chat client)
|
void |
setClientName(java.lang.String name)
|
void |
setGame(GameServer g)
|
void |
setUI(UI ui)
|
void |
setupDefaultActions()
|
void |
startRunning()
Begin operating of the client. |
void |
stopRunning()
Ends operation of the client. |
void |
waitUntilReady()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final long ALIVE_PERIOD
public static final java.lang.String DEFAULT_SERVER
public static final java.lang.String DISCOVERY_URL
protected UI uiClient
Constructor Detail |
public NetClient() throws java.io.IOException
public NetClient(java.lang.String serverName) throws java.io.IOException
public NetClient(java.lang.String serverName, int port) throws java.io.IOException
Method Detail |
public void startRunning()
Client
startRunning
in interface Client
public void stopRunning()
Client
stopRunning
in interface Client
public boolean isRunning()
public java.lang.String getClientName()
getClientName
in interface Client
public void setClientName(java.lang.String name)
setClientName
in interface Client
public void run()
run
in interface java.lang.Runnable
public void handleMessage(java.lang.String message)
Client
handleMessage
in interface Client
message
- the message that was received from the networkHandler
public java.io.BufferedReader getReader()
Client
Client.handleMessage(java.lang.String)
is called.
Do not close this stream. It is shared by all of the networking clients and will be disposed within the networking support code.
This reader may block if no input is available. Great care should be used when using this method. Use single-line commands whenever possible.
getReader
in interface Client
public void setChatClient(Chat client)
public void setUI(UI ui)
public void setGame(GameServer g)
setGame
in interface Client
public void sendChatMessage(java.lang.String user, java.lang.String message)
Chat
sendChatMessage
in interface Chat
user
- name of the user who sent the messagemessage
- chat message that to be sentpublic void gotChatMessage(java.lang.String user, java.lang.String message)
Chat
gotChatMessage
in interface Chat
user
- name of the user who sent the messagemessage
- chat message that was sentpublic void sendMessage(java.lang.String message)
Client
Although this interface is called the Client, both clients and servers can send messages. The semantics of this vary. Calling this method from a client sends a message to the server. On the server, this method is used to send a message back to a specific client.
sendMessage
in interface Client
message
- the message that should be sent to the networkpublic void addHandler(java.lang.String commandName, Handler handler)
public void setupDefaultActions()
public static java.util.List discoverVenues()
public void waitUntilReady()
public static void main(java.lang.String[] args)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |