Class RMIServer

All Implemented Interfaces:
IGameServer, Serializable, Remote

public class RMIServer extends UnicastRemoteObject implements IGameServer
RMI server responsible for handling remote client connections, receiving player actions, and propagating game updates.

The server manages player registration, reconnection handling, heartbeat monitoring, and the forwarding of client requests to the shared network event queue.

See Also:
  • Field Details

    • host

      private final String host
      Hostname or IP address exposed by the RMI runtime (java.rmi.server.hostname).
    • controller

      private final GameController controller
      Server-side game controller; all model access is synchronized on this object.
    • nPort

      private final int nPort
      Port on which the RMI registry is bound.
    • clients

      private final Map<String, IClientCallback> clients
      username → callback
    • watchdogs

      private final Map<String, RMIHeartbeat> watchdogs
      username → watchdog. One watchdog per connected player, mirrors pendingHeartbeat / per-socket HeartbeatHandler in the TCP stack.
    • actionQueue

      private final BlockingQueue<NetworkEvent> actionQueue
      Shared queue to which player actions are added for sequential processing.
    • playerList

      private final LimitedMap<String,Boolean> playerList
      Shared map tracking each player's connection status (true = online).
  • Constructor Details

    • RMIServer

      public RMIServer(GameController controller, int nPort, BlockingQueue<NetworkEvent> actionQueue, LimitedMap<String,Boolean> playerList, String host) throws RemoteException
      Constructs an RMI server with the required game and network components.
      Parameters:
      controller - the game controller used to manage the game logic.
      nPort - the port used by the RMI registry.
      actionQueue - the queue containing incoming network events.
      playerList - the map storing the connection status of the players.
      host - the hostname or IP address exposed by the RMI server.
      Throws:
      RemoteException - if the remote object cannot be exported.
  • Method Details

    • joinGame

      public ErrorType joinGame(String username, int preferredInt, IClientCallback callback) throws RemoteException
      Adds a player to the game and registers the callback used by the server to notify the corresponding RMI client.

      After a successful join an RMIHeartbeat watchdog is created and started for the new player — mirrors creating a HeartbeatHandler in TCPServer.acceptHeartbeat().

      Specified by:
      joinGame in interface IGameServer
      Parameters:
      username - the username chosen by the player.
      preferredInt - the desired number of players proposed by this client.
      callback - the remote callback associated with the client.
      Returns:
      null on success; an ErrorType value on rejection.
      Throws:
      RemoteException - if an RMI communication error occurs.
    • ping

      public void ping(String username) throws RemoteException
      Receives a heartbeat ping from the client. Mirrors the server reading PING and replying PONG in HeartbeatHandler.run().
      Specified by:
      ping in interface IGameServer
      Parameters:
      username - the username of the pinging client.
      Throws:
      RemoteException - if an RMI communication error occurs.
    • notifyAll

      public void notifyAll(NetworkEvent action)
      Notifies connected RMI clients of a new network event.

      If the event does not represent an error, it is sent to all registered clients. If it represents an error, it is sent only to the client that requested the action. Communication errors are logged without interrupting the notification process.

      Parameters:
      action - the network event to send to the clients.
    • notifyAll

      public void notifyAll(MiniModel model)
      Notifies all connected RMI clients of a new game model.

      The updated mini model is sent to each registered client callback. If a communication error occurs for a client, the exception is logged without interrupting the notification of the remaining clients.

      Parameters:
      model - the updated mini model to send to the connected clients.
    • drawUpperTribeCard

      public void drawUpperTribeCard(String playerUsername, int pos)
      Requests to draw a tribe card from the upper list. Creates a NetworkEvent and sends it through the network client.
      Specified by:
      drawUpperTribeCard in interface IGameServer
      Parameters:
      playerUsername - the name of the player performing the action
      pos - the index of the card to draw
    • drawLowerTribeCard

      public void drawLowerTribeCard(String playerUsername, int pos)
      Requests to draw a tribe card from the lower list. Creates a NetworkEvent and sends it through the network client.
      Specified by:
      drawLowerTribeCard in interface IGameServer
      Parameters:
      playerUsername - the name of the player performing the action
      pos - the index of the card to draw
    • drawUpperBuildingCard

      public void drawUpperBuildingCard(String playerUsername, int pos)
      Requests to draw a building card from the upper list. Creates a NetworkEvent and sends it through the network client.
      Specified by:
      drawUpperBuildingCard in interface IGameServer
      Parameters:
      playerUsername - the name of the player performing the action
      pos - the index of the card to draw
    • drawLowerBuildingCard

      public void drawLowerBuildingCard(String playerUsername, int pos)
      Requests to draw a building card from the lower list. Creates a NetworkEvent and sends it through the network client.
      Specified by:
      drawLowerBuildingCard in interface IGameServer
      Parameters:
      playerUsername - the name of the player performing the action
      pos - the index of the card to draw
    • skipTurn

      public void skipTurn(String playerUsername)
      Requests to skip the turn. This action is available only when the player cannot draw any tribe card, but still can buy some buildings.
      Specified by:
      skipTurn in interface IGameServer
      Parameters:
      playerUsername - the name of the player performing the action
    • slotChoice

      public void slotChoice(String playerUsername, int pos)
      Used to perform the slot choice action for the specified player at the specified position.
      Specified by:
      slotChoice in interface IGameServer
      Parameters:
      playerUsername - the name of the player performing the action
      pos - the index of the selected slot
    • totemChoice

      public void totemChoice(String playerUsername, String totems) throws RemoteException
      Enqueues a totem choice event for the specified player.
      Specified by:
      totemChoice in interface IGameServer
      Parameters:
      playerUsername - the username of the player choosing the totem.
      totems - the name of the chosen totem.
      Throws:
      RemoteException - if the RMI call fails.
    • start

      public void start()
      Starts the RMI server and binds it to the configured registry.

      The method configures the server hostname, creates the RMI registry on the specified port, and registers this server instance under the RMIGameServer name.

    • startWatchdog

      private void startWatchdog(String username)
      Creates and starts an RMIHeartbeat for the specified player.
      Parameters:
      username - the username of the player monitored by the heartbeat watchdog.
    • disconnectPlayer

      public void disconnectPlayer(String username)
      Triggers a voluntary disconnection for the specified player by stopping their heartbeat watchdog.
      Specified by:
      disconnectPlayer in interface IGameServer
      Parameters:
      username - the username of the player to disconnect.
    • disconnectedUsernames

      private static ArrayList<String> disconnectedUsernames(Game game)
      Returns usernames of players currently marked as disconnected in the given game.