dd.net
Class NetServer

java.lang.Object
  extended bydd.net.NetServer
All Implemented Interfaces:
java.lang.Runnable

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

Base class for network server 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 server 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 server keeps persistent references to each of the clients that are connected. Messages can be sent to specific clients or broadcast to all connected clients.

There is an implicit protocol implemented between the NetClient and NetServer. When either side sends the AYT message, the response should be the string [yes]. If an AYT response is not received, the client is assumed to be in an error state and disconnects. There is also a handshaking between client and server that goes as follows:

  1. Client connects to server
  2. Server sends a hello message in the form
        hello uniqueID
     
  3. Client uses this unique ID to form a unique client ID name
  4. Client sends a register message of the form
        register [uniqueID]
     
    (note the square brackets are part of the protocol, and are used to delemit the name of the client.

Alternatively, if the client is disconnected, register can be replaced with reconnect to inform the server that the client is replacing an existing client that terminated.

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 int DEFAULT_PORT
          Default TCP socket to use for client/server communication.
static java.lang.String DISCOVERY_URL
          Well known URL to query for list of servers.
 Logger log
          Log object used for debugging messages.
 
Constructor Summary
NetServer()
          Creates a new server listening on the default port.
NetServer(java.lang.String serverName)
           
NetServer(java.lang.String serverName, int port)
           
 
Method Summary
 void addClient(dd.net.NetServer.ServerClient client)
           
 void broadcast(java.lang.String message)
           
protected  java.util.Iterator getClientNames()
           
 java.util.Set getClients()
           
 GameServer getLobby()
           
 void handleReconnect(java.lang.String command, java.lang.String message, Client c)
           
 void handleRegister(java.lang.String command, java.lang.String message, Client c)
           
 boolean isReady()
           
 boolean isRunning()
           
static void main(java.lang.String[] args)
           
 boolean registerVenue(java.lang.String venueName)
           
 void removeClient(Client client)
           
 void run()
           
 void sendTo(java.lang.String user, java.lang.String message)
           
 void setGame(GameServer g)
           
 void stopRunning()
           
 void unregisterVenue()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

public Logger log
Log object used for debugging messages.


DISCOVERY_URL

public static final java.lang.String DISCOVERY_URL
Well known URL to query for list of servers.

See Also:
Constant Field Values

DEFAULT_PORT

public static final int DEFAULT_PORT
Default TCP socket to use for client/server communication.

See Also:
Constant Field Values
Constructor Detail

NetServer

public NetServer()
          throws java.io.IOException
Creates a new server listening on the default port. The server will create a new server socket (if possible) and a thread to handle connections. The thread will start immediately.


NetServer

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

NetServer

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

registerVenue

public boolean registerVenue(java.lang.String venueName)
                      throws java.io.IOException
Throws:
java.io.IOException

unregisterVenue

public void unregisterVenue()

run

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

addClient

public void addClient(dd.net.NetServer.ServerClient client)

setGame

public void setGame(GameServer g)

getLobby

public GameServer getLobby()

handleRegister

public void handleRegister(java.lang.String command,
                           java.lang.String message,
                           Client c)

handleReconnect

public void handleReconnect(java.lang.String command,
                            java.lang.String message,
                            Client c)

removeClient

public void removeClient(Client client)

getClientNames

protected java.util.Iterator getClientNames()

getClients

public java.util.Set getClients()

broadcast

public void broadcast(java.lang.String message)

sendTo

public void sendTo(java.lang.String user,
                   java.lang.String message)

isReady

public boolean isReady()

stopRunning

public void stopRunning()

isRunning

public boolean isRunning()

main

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