game-client-and-master-server

AppWarp Load Balancing feature allow AppWarpS2 Game Client to communicate with AppWarpS2 Master Server. Here we will discuss Game Client to Master Server communication and vice versa.

AppWarpS2 Game Client to Master Server

AppWarpS2 client interact with master server using MasterClient. MasterRequestListener interface provide the result for the API call we make using MasterClient.

MasterClient

While initializing the the master client, you need to pass the master host and master port eg

MasterClient.initialize(HOST, PORT);

to receive callBack for API call we make using MasterClient we set MasterRequestListener to the MasterClient.

masterClient.addMasterServerRequestListener(<class implementing MasterRequestListener>);

API

initialize

Initializes the singleton instance of MasterClient with the developer credentials

public static byte initialize(String masterServerHost, int masterServerPort)

@parameters
masterServerHost: ip address where master server is running
masterServerPort: port number on which master server is running

@returns
WarpResponseResultCode

connect

Send a connect request to master server. Result of this API call will be received in onConnectDone of the MasterRequestListener.

public static void connect()

@parameters

@returns
void

disconnect

Send a disconnect request to master server. Result of this API call will be received in onDisconnectDone of the MasterRequestListener.

public static void disconnect()

@parameters

@returns
void

getAllServers

Send a GetAllServers request to master server. Result of this API call will be received in onGetAllServerDone of the MasterRequestListener.

public static void getAllServers ()

@parameters

@returns
void

sendCustomMessage

Send a CustomMessage request to master server.

public static void sendCustomMessage(byte[] message)

@parameters
message: message to be sent to MasterServer

@returns
void

MasterRequestListener

/**
* Invoked in response to a connect request
*/
public void onConnectDone(byte result);

/**
* Invoked in response to a disconnect request
* @param event
*/
public void onDisconnectDone(byte result);

/**
* Invoked in response of getAllServers request
* @param AllServerEvent
*/
public void onGetAllServerDone(AllServerEvent event);

/**
* Invoke when AppWarpS2 client receive custom message from MasterServer
* @param message
*/    
public void onCustomMessageReceived(byte[] message);

AllServerEvent

This event is raised when AppWarpS2 Client send GetAllServer request to master server. It is passed in the corresponding method of MasterRequestListener. It exposed following properties

result: Result of the API call servers: List of the servers.

public byte getResult()
public Server[] getServers()

Master Server to AppWarpS2 Client

Master Server can also communicate with the AppWarpS2 Game Client. To do this we first need to add corresponding listener to the master server.

IGameClientHandler handler = MasterServer.getInstance().getGameClientHandler();
handler.addListener(<class implementing GameClientEventsListener>);

IGameClientHandler

public interface IGameClientHandler {

    /*
     * add your listener object on which call backs will be invoked when a
     * game client is connected or disconnected from this master server 
     */
    public void addListener(GameClientEventsListener listener);

    /**
     * remove your listener object 
     *
     * @param listener
     */
    public void removeListener(GameClientEventsListener listener);    
}

GameClientEventsListener

GameClientEventsListener is used to get notification on master server about AppWarpS2 Client events. GameClientEventsListener notify for following events

public interface GameClientEventsListener {

    /*
     * Invoked when master server receive a custom mesage from client.
     */
    public void onCustomRequest(IGameClient source, byte[] message);

    /*
     * Invoked when AppWarpS2 Client is connected to the master server.
     */
    public void onClientConnected(IGameClient client);

    /*
     * Invoked when AppWarpS2 Client get disconnected from master server.
     */
    public void onClientDisconnected(IGameClient client);

    /*
     * Invoked with the timer running on master server
     */
    public void onTimerTick(long time);
}

IGameClient

IGameClient represents AppWarpS2 Client. IGameClient have following behaviours

public interface IGameClient {

    /*
     * To send custom message to the game client
     */
    public boolean sendMessage(byte[] message);

    /*
     * To get Address of the client which contain host and port the client.
     */
    public Address getAddress();

}