TTTO.MasterServer
Class MasterServerClient

java.lang.Object
  extended by TTTO.MasterServer.MasterServerClient

public class MasterServerClient
extends java.lang.Object

The client used to communicate with the Master server

Author:
emi-lind

Nested Class Summary
private  class MasterServerClient.ServerReader
           
 
Field Summary
private  java.net.Socket connection
           
static java.lang.String DEFAULT_HOST
           
static int DEFAULT_PORT
           
private  java.io.BufferedReader in
           
private  Room[] lastUpdatedRoomList
           
private  java.io.PrintWriter out
           
private  MasterServerClient.ServerReader serverReader
           
 
Constructor Summary
MasterServerClient()
           
 
Method Summary
 boolean connectToMasterServer(java.lang.String aHost, int aPort)
          Connects to a server.
 java.lang.String getConnectionStatus()
           
 Room[] getGameRoomList()
          Asks master server for the latest game room list.
 java.lang.String getMasterServerIP()
          Gets the master servers ip-address
 int getMasterServerPort()
          Gets the master servers port
 boolean isConnected()
          Checks if connected
private  void populateRoomList(java.lang.String[] aRooms)
          Transform a string array of room fields into Room objects and fill the roomList with new rooms.
 void registerGameRoom(java.lang.String aGameName, int aPort)
          Registers a game to the master server so other users can see it.
private  void sendMessage(java.lang.String aCommand, java.lang.String[] aData)
          Sends a message to the master server with array of data.
 void unregisterGameRoom(java.lang.String aGameName, int aPort)
          Unregisters a game from from the master server
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_HOST

public static final java.lang.String DEFAULT_HOST
See Also:
Constant Field Values

DEFAULT_PORT

public static final int DEFAULT_PORT
See Also:
Constant Field Values

connection

private java.net.Socket connection

in

private java.io.BufferedReader in

out

private java.io.PrintWriter out

serverReader

private MasterServerClient.ServerReader serverReader

lastUpdatedRoomList

private Room[] lastUpdatedRoomList
Constructor Detail

MasterServerClient

public MasterServerClient()
Method Detail

getGameRoomList

public Room[] getGameRoomList()
Asks master server for the latest game room list. The method locks until the game list is received.

Returns:
latest updated game list.

registerGameRoom

public void registerGameRoom(java.lang.String aGameName,
                             int aPort)
Registers a game to the master server so other users can see it. Make sure client is connected by calling isConnected() before invoking this method.

Parameters:
aGameName - Desired game name
aGameName - on which port the game server is located

unregisterGameRoom

public void unregisterGameRoom(java.lang.String aGameName,
                               int aPort)
Unregisters a game from from the master server

Parameters:
aGameName - The rooms name
aGameName - the rooms port

sendMessage

private void sendMessage(java.lang.String aCommand,
                         java.lang.String[] aData)
Sends a message to the master server with array of data. Data can be null in case you just want to invoke a command. Valid commands are those found in MasterServer. '#' and ':' chars will get removed from the data

Parameters:
aCommand - The command
aData - The data to be sent. Can be null

getConnectionStatus

public java.lang.String getConnectionStatus()

connectToMasterServer

public boolean connectToMasterServer(java.lang.String aHost,
                                     int aPort)
Connects to a server.

Parameters:
aHost - target server
aPort - target port
Returns:
A socket if connection could be made. Else null

isConnected

public boolean isConnected()
Checks if connected

Returns:
true if connected

getMasterServerIP

public java.lang.String getMasterServerIP()
Gets the master servers ip-address

Returns:
address as string

getMasterServerPort

public int getMasterServerPort()
Gets the master servers port

Returns:
the port

populateRoomList

private void populateRoomList(java.lang.String[] aRooms)
Transform a string array of room fields into Room objects and fill the roomList with new rooms. If aRooms is null then the list will stay null

Parameters:
aRooms - The rooms to fill