Add: partial JavaDOC to RMIServer
This commit is contained in:
@@ -15,19 +15,47 @@ import java.util.Map;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
|
||||
import java.rmi.*;
|
||||
|
||||
|
||||
/**
|
||||
* Server RMI. Exposes a method to join the game and one to execute an event.
|
||||
*/
|
||||
public class RMIServer implements IGameServer {
|
||||
|
||||
/** Server game's controller */
|
||||
private GameController controller;
|
||||
|
||||
/** Server game's model */
|
||||
private Game model;
|
||||
|
||||
/** RMI registry */
|
||||
private Registry registry;
|
||||
|
||||
/** RMI port */
|
||||
private int nPort;
|
||||
|
||||
/** Map containing the associations between a player's username and its callback */
|
||||
private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>();
|
||||
|
||||
/** Queue containing the events to be applied to the game model */
|
||||
BlockingQueue<NetworkEvent> actionQueue;
|
||||
|
||||
/**
|
||||
* List containing the usernames of joined players.
|
||||
* {@link LimitedList}'s limit defines at which size the list calls its action. The limit can be set using {@link LimitedList#setLimit(int)}.
|
||||
*/
|
||||
private LimitedList<String> playerList;
|
||||
|
||||
// Costruttore
|
||||
|
||||
/**
|
||||
* Class constructor that initializes the attributes.
|
||||
* @param controller The game controller
|
||||
* @param nPort The RMI port
|
||||
* @param actionQueue The action queue
|
||||
* @param playerList The player's usernames list
|
||||
* @throws RemoteException if an RMI error occurs
|
||||
*/
|
||||
public RMIServer(GameController controller, int nPort, BlockingQueue<NetworkEvent> actionQueue,LimitedList<String> playerList) throws RemoteException {
|
||||
this.controller = controller;
|
||||
this.nPort = nPort;
|
||||
@@ -36,26 +64,29 @@ public class RMIServer implements IGameServer {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Metodi esposti RMI
|
||||
public void setController (GameController controller) {
|
||||
this.controller = controller;
|
||||
}
|
||||
// RMI's exposed methods
|
||||
/**
|
||||
* Allows a player to join the game.
|
||||
* If the desired number of player is invalid, the request is rejected.
|
||||
* If this is the first player, a new game model is created and passed to the controller. Additionally, the playerList's limit is set.
|
||||
* Then, if the controller successfully adds the player, the username is added to {@link #playerList} and {@link #clients}.
|
||||
* @param username The player's name
|
||||
* @param preferredInt The desired number of players
|
||||
* @param callback The client's callback interface
|
||||
* @return true if the player successfully joined the game, false otherwise
|
||||
*/
|
||||
@Override
|
||||
public boolean joinGame(String username, int preferredInt,IClientCallback callback) {
|
||||
synchronized (controller) {
|
||||
if(playerList.size()==0 && (preferredInt<2||preferredInt>5))
|
||||
public boolean joinGame(String username, int preferredInt, IClientCallback callback) {
|
||||
if (preferredInt<2 || preferredInt>5) {
|
||||
return false;
|
||||
if (controller.addPlayer(username)) {
|
||||
if(playerList.size()==0)
|
||||
{
|
||||
Game game = new Game(preferredInt);
|
||||
controller.setModel(game);
|
||||
}
|
||||
synchronized (controller) {
|
||||
if(playerList.isEmpty()){
|
||||
model = new Game(preferredInt);
|
||||
controller.setModel(model);
|
||||
playerList.setLimit(preferredInt);
|
||||
}
|
||||
if (controller.addPlayer(username)) {
|
||||
playerList.add(username);
|
||||
clients.put(username, callback);
|
||||
return true;
|
||||
@@ -69,9 +100,6 @@ public class RMIServer implements IGameServer {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Metodi interni del server
|
||||
public void notifyAll(NetworkEvent action) throws RemoteException {
|
||||
for (IClientCallback cb : clients.values()) {
|
||||
@@ -85,9 +113,6 @@ public class RMIServer implements IGameServer {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Metodi per avviare server RMI
|
||||
public boolean start() {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user