dd.net
Class NetClient

java.lang.Object
  extended bydd.net.NetClient
All Implemented Interfaces:
Chat, Client, java.lang.Runnable
Direct Known Subclasses:
TurnClient

public class NetClient
extends java.lang.Object
implements java.lang.Runnable, Client, Chat

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:

This 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.

Author:
Eric Scharff

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

ALIVE_PERIOD

public static final long ALIVE_PERIOD
Number of milliseconds between AYT calls

See Also:
Constant Field Values

DEFAULT_SERVER

public static final java.lang.String DEFAULT_SERVER
Name of the default server.

See Also:
Constant Field Values

DISCOVERY_URL

public static final java.lang.String DISCOVERY_URL
URL to initiate server discovery.

See Also:
Constant Field Values

uiClient

protected UI uiClient
Constructor Detail

NetClient

public NetClient()
          throws java.io.IOException
Creates a new client connecting to the default server and port. If the connection to the server was successful, a new thread to handle messages from the server will start immediately.


NetClient

public NetClient(java.lang.String serverName)
          throws java.io.IOException

NetClient

public NetClient(java.lang.String serverName,
                 int port)
          throws java.io.IOException
Method Detail

startRunning

public void startRunning()
Description copied from interface: Client
Begin operating of the client. This method may start an additional thread which handles incoming or outgoing network connections. This method should be called once in order to begin the client's operation.

Specified by:
startRunning in interface Client

stopRunning

public void stopRunning()
Description copied from interface: Client
Ends operation of the client. This method will terminate any network connections or other server handling, and should clean up any used resources. This method may abruptly terminate the client or server, so it is advistable to avoid using this call and instead rely on a message that handles a clean disconnection.

Specified by:
stopRunning in interface Client

isRunning

public boolean isRunning()

getClientName

public java.lang.String getClientName()
Specified by:
getClientName in interface Client

setClientName

public void setClientName(java.lang.String name)
Specified by:
setClientName in interface Client

run

public void run()
Specified by:
run in interface java.lang.Runnable

handleMessage

public void handleMessage(java.lang.String message)
Description copied from interface: Client
Handle an incoming network message. Messages are typically one-line strings with a command name and arguments. The networking client will typically handle the message by looking at the command name and calling an appropriate handler, although the precise action taken when a message is received is up to the client. This method is called internally by the networking layer and is rarely called outside of the network code.

Specified by:
handleMessage in interface Client
Parameters:
message - the message that was received from the network
See Also:
Handler

getReader

public java.io.BufferedReader getReader()
Description copied from interface: Client
Returns the reader associated with the networking layer. The incoming command stream is a series of textual lines. For commands that span multiple lines, this method can be used to acquire the input after the message line. This reader will be pointing to the line after the current message line when 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.

Specified by:
getReader in interface Client
Returns:
the line-oriented character stream associated with this client

setChatClient

public void setChatClient(Chat client)

setUI

public void setUI(UI ui)

setGame

public void setGame(GameServer g)
Specified by:
setGame in interface Client

sendChatMessage

public void sendChatMessage(java.lang.String user,
                            java.lang.String message)
Description copied from interface: Chat
Sends a chat message over the network. This method should be implemented by chat servers. The expected result of this call is that the chat message will be broadcast appropriately to the other clients on the network.

Specified by:
sendChatMessage in interface Chat
Parameters:
user - name of the user who sent the message
message - chat message that to be sent

gotChatMessage

public void gotChatMessage(java.lang.String user,
                           java.lang.String message)
Description copied from interface: Chat
Called when a chat message is received. This method serves as notificaiton, a kind of event handler. When a chat message is sent over the network, this method should be called (by the network layer) to all clients that wish to be notified that a new chat message was sent. Typically the result of this call will be to prompt the user of a new incoming message.

Specified by:
gotChatMessage in interface Chat
Parameters:
user - name of the user who sent the message
message - chat message that was sent

sendMessage

public void sendMessage(java.lang.String message)
Description copied from interface: Client
Send an outgoing network message. Messages are typically one-line strings with a command name and arguments. This method is used to send an outgoing message to the client or server, and is the main low-level interface for generating outgoing network traffic.

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.

Specified by:
sendMessage in interface Client
Parameters:
message - the message that should be sent to the network

addHandler

public void addHandler(java.lang.String commandName,
                       Handler handler)

setupDefaultActions

public void setupDefaultActions()

discoverVenues

public static java.util.List discoverVenues()

waitUntilReady

public void waitUntilReady()

main

public static void main(java.lang.String[] args)