Class LobbyRMI

All Implemented Interfaces:
LobbyRMIIF, Serializable, Remote

public class LobbyRMI extends UnicastRemoteObject implements LobbyRMIIF
Version of class representing lobby for the RMI connection. Hence this class is exposed to the network as RMI remote object. The methods who allow players to join a room/game must receive a GameListenerClientRMI already instantiated, so that the server can use them to give update to the corresponding clients via RMI

Controller used to manage multiple games, that is to handle the lobby of players who are playing or are in a room willing to start a game.
It stores the server game listeners of players in all the rooms. A player joining the lobby can create a new room, setting the number of player for the future game to start, or can join an existing room. (In the moment they join/create a room, they decide their nickname and their token) Until game is not actually started, the players can be added and removed. If the room remains empty, it is automatically deleted.
When the right number of players is reached, the game automatically starts, adn the associated GameController is created. The players in the room are given the remote object GameControllerRMI via RMI update- The players who were in the room in that moment are so the definite ones for that game and they cannot change. All the rooms and the already started games are associated to a unique gameId.
See Also:
  • Field Details

    • mapLis

      private final Map<PlayerLobby,GameListenerServerRMI> mapLis
      Map between players and their corresponding listeners. The listeners are specific for the RMI connection. Hence note that these are only part of the clients, in particular those who chose to communicate via RMI
    • lobby

      private final Lobby lobby
      Stored instance of lobby (only for not to keep calling Lobby.getInstance())
  • Constructor Details

  • Method Details

    • getRooms

      public List<RoomIF> getRooms() throws RemoteException
      Specified by:
      getRooms in interface LobbyRMIIF
      Returns:
      List of all rooms in the lobby, both the ones with game starter and not already started
      Throws:
      RemoteException
    • createRoom

      public void createRoom(GameListenerClientRMIIF playerListener, int nPlayers) throws LobbyException, RemoteException
      Creates a new Room, for now populated only with the specified player. The gameId is automatically found as the next suitable gameId, hence it is different from the other ones created before by the lobby In case of success, the player who created the room is notified.
      Specified by:
      createRoom in interface LobbyRMIIF
      Parameters:
      playerListener - Client listener of the first player who creates the game which will start in the future
      nPlayers - The number of players to start the game, chosen by the player who creates the room
      Throws:
      LobbyException - If the player has a nickName already chosen by another player in the lobby
      RemoteException
    • joinRoom

      public void joinRoom(int gameId, GameListenerClientRMIIF playerListener) throws LobbyException, RemoteException
      Adds a players to an existing room, specified by the given gameId. In case of success, the players in that room are notified, and receive an update for the created gameController, too If the room with the newly joined players if full, it makes the game start (and notify the players of this).
      Specified by:
      joinRoom in interface LobbyRMIIF
      Parameters:
      gameId - Id of the room the player wants to join
      playerListener - Client listener of the player to add to that room
      Throws:
      LobbyException - If the player has a nickName already chosen by another player in the lobby, or if the room with the given gameId does not exist, or if it exists but the room is already full
      RemoteException
    • leaveRoom

      public void leaveRoom(PlayerLobby player) throws LobbyException, RemoteException
      Removes a players from the existing room (specified by the given gameId) they joined. In case of success, it notifies the players in the room If the room becomes empty, it is automatically removed, as if it has never been created.
      Specified by:
      leaveRoom in interface LobbyRMIIF
      Parameters:
      player - Player to remove from that room
      Throws:
      LobbyException - If the specified player is not in any existing rooms
      RemoteException
    • reconnectPlayer

      public void reconnectPlayer(GameListenerClientRMIIF playerListener) throws LobbyException, RemoteException, ConnectionException, GameStatusException
      Reconnects a disconnected player for the already started game they took part in. Triggers the notification for other players, and the update for the gameController for the reconnected player.
      Specified by:
      reconnectPlayer in interface LobbyRMIIF
      Parameters:
      playerListener - Client listener of the player to reconnect
      Throws:
      LobbyException - If the given player is not among players of any started game
      ConnectionException - If the player was already connected
      GameStatusException - if any of the methods called directly or indirectly by this method are called in wrong game phase (generic error, should not happen)
      RemoteException