org.dyndns.kwitte.ogc
Class ServerConnection

java.lang.Object
  extended byorg.dyndns.kwitte.ogc.ServerConnection

public final class ServerConnection
extends java.lang.Object

A connection to be used for someone who wants to register a game on the OGC server.

Usage example: You want to host a game and make other players know that there is a game where they are welcome to join using the default OGC server.

 Game game = new Game(
                 "TestGame", // name of the game
                 "0.0",      // version of the game
                 "Tina",     // name of the host
                 99,         // maximum number of players
                 5555        // port where clients can connect
             );
 ServerConnection sc = new ServerConnection(game);
 sc.connect();
 
Methods are thread-safe unless documented otherwise. Each message sent to or received from the server is logged at "org.dyndns.kwitte.ogc" at java.util.logging.Level.INFO.


Constructor Summary
ServerConnection(Game game)
          Creates a new ServerConnection to x.yz.to:11111 (the public OGC server).
ServerConnection(java.lang.String host, int port, Game game)
          Creates a new ServerConnection.
 
Method Summary
 void connect()
          Establishes the connection to the OGC server.
 void disconnect()
          Unregisters this local game from the OGC server.
 boolean isConnected()
          Returns true if and only if the connection is established.
 void setPlayers(int players)
          Set the number of players of the current game as it shall be shown at the OGC server.
 void setStatus(java.lang.String status)
          Set the status of the current game as it shall be shown at the OGC server.
 void setUpdateInterval(long milliseconds)
          Sets the time interval for the updates.
 void setVerbose(boolean verbose)
          Enables or disables output of send and receive protocol to stdout.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServerConnection

public ServerConnection(Game game)
Creates a new ServerConnection to x.yz.to:11111 (the public OGC server). The number of players is initialized with 0, the status with undef.

Parameters:
game - the name of the game for which a list of games is needed.
Throws:
java.lang.NullPointerException - when game == null.
See Also:
setPlayers(int), setStatus(String)

ServerConnection

public ServerConnection(java.lang.String host,
                        int port,
                        Game game)
Creates a new ServerConnection. The number of players is initialized with 0, the status with undef.

Parameters:
host - the IP or host of the OGC server.
port - the port of the OGC server. Do not mix it up with the port of the game which you register at the server!
game - a Game holding the constants of the local game. That are the values that never change during the game.
Throws:
java.lang.NullPointerException - if host == null or game == null.
See Also:
setPlayers(int), setStatus(String)
Method Detail

connect

public void connect()
             throws java.io.IOException
Establishes the connection to the OGC server. Blocks until an IO error occurs or the connection is closed.

Throws:
java.io.IOException - if an IO error occurs.
See Also:
disconnect()

disconnect

public void disconnect()
                throws java.io.IOException
Unregisters this local game from the OGC server.

Throws:
java.io.IOException - if an IO error occurs.
See Also:
connect()

setStatus

public void setStatus(java.lang.String status)
Set the status of the current game as it shall be shown at the OGC server. When this game is already registered at the OGC server using connect() it will be updated there.

Parameters:
status - a String with a maximum length of 6 which describes the status of the game.
Throws:
java.lang.NullPointerException - if status == null.
java.lang.IllegalArgumentException - if status.length() > 6.

setPlayers

public void setPlayers(int players)
Set the number of players of the current game as it shall be shown at the OGC server. When this game is already registered at the OGC server using connect() it will be updated there.

Parameters:
players - the new number of active players in the local game.

isConnected

public boolean isConnected()
Returns true if and only if the connection is established.

Returns:
connection status

setVerbose

public void setVerbose(boolean verbose)
Enables or disables output of send and receive protocol to stdout. Disabled by default.

Parameters:
verbose - true to enable output, false to disable it.

setUpdateInterval

public void setUpdateInterval(long milliseconds)
Sets the time interval for the updates. The OGC protocol requires a host to resend its status frequently. The default value is chosen in such a way that an OGC server should not delete the game from its list unless you disconnect. Anyway, you can change this value if you want. The new value will apply after the next update to the OGC server if you are already connected.

Parameters:
milliseconds - the resend interval in milliseconds.
Throws:
java.lang.IllegalArgumentException - if milliseconds < 0