feud.clientserver
Class SimpleServer

java.lang.Object
  extended byfeud.clientserver.SimpleServer

public class SimpleServer
extends java.lang.Object

As the name indicates, this is a simple server class. Once started, it opens a TCP-socket for clients to connect as well as an (optional) UDP datagram socket (if possible) that broadcasts this server's ip and port for allowing to implement a simple server browser.
The communication between this server and its clients is done via DataContainers, which are simple Wrappers for primitive values (and Strings) into byte arrays.


Field Summary
static int DEFAULT_PORT
          The default tcp port
static int UDP_DEFAULT_PORT
          The default UDP port
 
Constructor Summary
SimpleServer(int tcpPort, int udpPort, boolean doBroadcast, java.lang.String serverName)
          Creates a new Server.
 
Method Summary
 void addListener(DataTransferListener sl)
          Adds a new DataTransferListener to the server.
 void addLoginListener(ClientLoginListener ctl)
          Adds a new ClientLoginListener to the server.
 void addLogoutListener(ClientLogoutListener ctl)
          Adds a new ClientLogoutListener to the server.
 void broadcast(DataContainer dc)
          Adds a DataContainer to the broadcast-queue.
 void shutDown()
          Shuts down the server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PORT

public static final int DEFAULT_PORT
The default tcp port

See Also:
Constant Field Values

UDP_DEFAULT_PORT

public static final int UDP_DEFAULT_PORT
The default UDP port

See Also:
Constant Field Values
Constructor Detail

SimpleServer

public SimpleServer(int tcpPort,
                    int udpPort,
                    boolean doBroadcast,
                    java.lang.String serverName)
Creates a new Server. When creating a two server, up to two threads are being spawned. One for listening on the tcp socket for clients to connect and one for broadcasting the server's data via UDP.

Parameters:
tcpPort - The tcp port to which a client can connect with this server. This port has to be open and unused or otherwise, the server won't start.
udpPort - the udp port for broadcasting the server's ip and port (and player count). If this port is not available, the broadcasting thread will terminate, but the server will run anyway.
doBroadcast - If true, the broadcasting thread will be spawned. Otherwise, it won't.
serverName - The name of the server. Should be unique but doesn't have to.
Method Detail

addListener

public void addListener(DataTransferListener sl)
Adds a new DataTransferListener to the server. A DataTransferListener will be notified in case of data being transfered from the client to the server.

Parameters:
sl - the listener

addLogoutListener

public void addLogoutListener(ClientLogoutListener ctl)
Adds a new ClientLogoutListener to the server. This listener will be notified if a client logs out from this server.

Parameters:
ctl - the listener

addLoginListener

public void addLoginListener(ClientLoginListener ctl)
Adds a new ClientLoginListener to the server. This listener will be notified if a client logs in on this server.

Parameters:
ctl - the listener

shutDown

public void shutDown()
Shuts down the server. This shut down is semi-hard, i.e. all server threads and such will be terminated correctly, but there's no waiting for the clients to be informed about this. The clients have to take care of a "lost" server themselves.


broadcast

public void broadcast(DataContainer dc)
Adds a DataContainer to the broadcast-queue. Each container added to this queue is ensured to be send once to all clients known to the server at the time that this method is being called (unless the client hasn't died or logout, of course).
This has nothing to do with the data broadcast via UDP that the ServerBroadcast is doing. This is a tcp "broadcast" to all known clients.

Parameters:
dc - the DataContainer that should be enqueued