master-server-game-server-interaction

Master Server maintain the snapshot of All Game Servers. In snapshot it maintain app, rooms, users of that game server. As the master servers is started it try to connect the game servers as specified in MasterConfig.json which contain ip address and port of all Game Servers. Once connected to the game servers it start to monitor game server.

GameServer to MasterServer

AppWarpS2 Game Server interact with Master Server using IMasterServer and to get event from master server MasterServerListener interface is given. User need to add listener to IMasterServer in order to register to get notification from master server.

IMasterServer imasterserver = MasterServer.getInstance().getMasterServerManager();
imasterserver.addListener(<class implementing MasterServerListener>);

MasterServerListener

public interface MasterServerListener {

    /*
     * Invoked when a master server connect to game server
     * 
     * @param: Adrress : address of master server 
     */
    public void onConnected(Address address);

    /*
     * Invoked when a master server disconnected to game server
     * 
     * @parms: Address: address of the master server
     */
    public void onDisconnected(Address address);

    /*
     * Invoked when game server received a message from master server
     * 
     * @param: Address: address of the master server
     * message: message received from the master server
     */
    public void onMessage(Address address, byte[] message);

}

IMasterServer

public interface IMasterServer {

    /*
     * return true if game server is connected to mater server
     */
    public boolean isConnected();

    /**
     * add your listener object on which call backs will be invoked when a
     * master server is connected or disconnected from this game server or
     * message from the master server is received.
     *
     * @param listener
     */
    public void addListener(MasterServerListener listener);

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

    /**
     * send custom update message to master 
     *
     * @param message
     */
    public void sendMessage(byte[] message);

}

MasterServer to GameServer

On Master Server we need to register GameServerEventsListener to get notification from Game Servers. To do this we get IGameServerManager from MasterServer class.

IGameServerManager manager = MasterServer.getInstance().getGameServerManager();

IGameServerManager

public interface IGameServerManager {

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

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

    /*
     * return the list of all game servers connected to the master server.
     */
    public ArrayList<IGameServer> getServers();        
}

IGameServer

public interface IGameServer {

    /*
     * returns the address of the connected game server
     */
    public Address getAddress();

    /*
     * returns status if game server is connected or not
     */
    public boolean connected();

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

    /*
     * returns the list of all zones the game server has.
     */
    public ArrayList<IZone> getZones();

}

IZone

public interface IZone {

    /*
     * @return list of all users
     */
    public ArrayList<String> getUsernames();

    /**
     * @return list of room-id
     */
    public ArrayList<String> getRoomIds();

    /**
     * @return App Key
     */
    public String getAppKey();
}

GameServerEventsListener

public interface GameServerEventsListener {

    /*
     * Invoked when game servers is connected to master server.
     */
    public void gameServerConnected(IGameServer server);

    /*
     * Invoked when game servers get disconnected from master server.
     */
    public void gameServerDisconnected(IGameServer server);

    /*
     * Invoked when game servers receive custom message from master server
     */
    public void receivedMessage(IGameServer source, byte[] message);

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