java.lang.Object
java.rmi.server.RemoteObject
java.rmi.server.RemoteServer
java.rmi.server.UnicastRemoteObject
it.polimi.ingsw.am13.network.rmi.LobbyRMI
- All Implemented Interfaces:
LobbyRMIIF
,Serializable
,Remote
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
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
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 Summary
FieldsModifier and TypeFieldDescriptionprivate final Lobby
Stored instance of lobby (only for not to keep callingLobby.getInstance()
)private final Map
<PlayerLobby, GameListenerServerRMI> Map between players and their corresponding listeners.Fields inherited from class java.rmi.server.RemoteObject
ref
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
createRoom
(GameListenerClientRMIIF playerListener, int nPlayers) Creates a new Room, for now populated only with the specified player.getRooms()
void
joinRoom
(int gameId, GameListenerClientRMIIF playerListener) Adds a players to an existing room, specified by the given gameId.void
leaveRoom
(PlayerLobby player) Removes a players from the existing room (specified by the given gameId) they joined.void
reconnectPlayer
(GameListenerClientRMIIF playerListener) Reconnects a disconnected player for the already started game they took part in.Methods inherited from class java.rmi.server.UnicastRemoteObject
clone, exportObject, exportObject, exportObject, exportObject, exportObject, unexportObject
Methods inherited from class java.rmi.server.RemoteServer
getClientHost, getLog, setLog
-
Field Details
-
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
Stored instance of lobby (only for not to keep callingLobby.getInstance()
)
-
-
Constructor Details
-
LobbyRMI
- Throws:
RemoteException
-
-
Method Details
-
getRooms
- Specified by:
getRooms
in interfaceLobbyRMIIF
- 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 interfaceLobbyRMIIF
- Parameters:
playerListener
- Client listener of the first player who creates the game which will start in the futurenPlayers
- 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 lobbyRemoteException
-
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 interfaceLobbyRMIIF
- Parameters:
gameId
- Id of the room the player wants to joinplayerListener
- 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 fullRemoteException
-
leaveRoom
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 interfaceLobbyRMIIF
- Parameters:
player
- Player to remove from that room- Throws:
LobbyException
- If the specified player is not in any existing roomsRemoteException
-
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 interfaceLobbyRMIIF
- Parameters:
playerListener
- Client listener of the player to reconnect- Throws:
LobbyException
- If the given player is not among players of any started gameConnectionException
- If the player was already connectedGameStatusException
- if any of the methods called directly or indirectly by this method are called in wrong game phase (generic error, should not happen)RemoteException
-