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.BlockingQueue;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
|
||||||
import java.rmi.*;
|
import java.rmi.*;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Server RMI. Exposes a method to join the game and one to execute an event.
|
||||||
|
*/
|
||||||
public class RMIServer implements IGameServer {
|
public class RMIServer implements IGameServer {
|
||||||
|
|
||||||
|
/** Server game's controller */
|
||||||
private GameController controller;
|
private GameController controller;
|
||||||
|
|
||||||
|
/** Server game's model */
|
||||||
|
private Game model;
|
||||||
|
|
||||||
|
/** RMI registry */
|
||||||
private Registry registry;
|
private Registry registry;
|
||||||
|
|
||||||
|
/** RMI port */
|
||||||
private int nPort;
|
private int nPort;
|
||||||
|
|
||||||
|
/** Map containing the associations between a player's username and its callback */
|
||||||
private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>();
|
private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
/** Queue containing the events to be applied to the game model */
|
||||||
BlockingQueue<NetworkEvent> actionQueue;
|
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;
|
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 {
|
public RMIServer(GameController controller, int nPort, BlockingQueue<NetworkEvent> actionQueue,LimitedList<String> playerList) throws RemoteException {
|
||||||
this.controller = controller;
|
this.controller = controller;
|
||||||
this.nPort = nPort;
|
this.nPort = nPort;
|
||||||
@@ -36,26 +64,29 @@ public class RMIServer implements IGameServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// RMI's exposed methods
|
||||||
|
/**
|
||||||
|
* Allows a player to join the game.
|
||||||
|
* If the desired number of player is invalid, the request is rejected.
|
||||||
// Metodi esposti RMI
|
* If this is the first player, a new game model is created and passed to the controller. Additionally, the playerList's limit is set.
|
||||||
public void setController (GameController controller) {
|
* Then, if the controller successfully adds the player, the username is added to {@link #playerList} and {@link #clients}.
|
||||||
this.controller = controller;
|
* @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
|
@Override
|
||||||
public boolean joinGame(String username, int preferredInt,IClientCallback callback) {
|
public boolean joinGame(String username, int preferredInt, IClientCallback callback) {
|
||||||
|
if (preferredInt<2 || preferredInt>5) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
synchronized (controller) {
|
synchronized (controller) {
|
||||||
if(playerList.size()==0 && (preferredInt<2||preferredInt>5))
|
if(playerList.isEmpty()){
|
||||||
return false;
|
model = new Game(preferredInt);
|
||||||
|
controller.setModel(model);
|
||||||
|
playerList.setLimit(preferredInt);
|
||||||
|
}
|
||||||
if (controller.addPlayer(username)) {
|
if (controller.addPlayer(username)) {
|
||||||
if(playerList.size()==0)
|
|
||||||
{
|
|
||||||
Game game = new Game(preferredInt);
|
|
||||||
controller.setModel(game);
|
|
||||||
playerList.setLimit(preferredInt);
|
|
||||||
}
|
|
||||||
playerList.add(username);
|
playerList.add(username);
|
||||||
clients.put(username, callback);
|
clients.put(username, callback);
|
||||||
return true;
|
return true;
|
||||||
@@ -69,9 +100,6 @@ public class RMIServer implements IGameServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Metodi interni del server
|
// Metodi interni del server
|
||||||
public void notifyAll(NetworkEvent action) throws RemoteException {
|
public void notifyAll(NetworkEvent action) throws RemoteException {
|
||||||
for (IClientCallback cb : clients.values()) {
|
for (IClientCallback cb : clients.values()) {
|
||||||
@@ -85,9 +113,6 @@ public class RMIServer implements IGameServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Metodi per avviare server RMI
|
// Metodi per avviare server RMI
|
||||||
public boolean start() {
|
public boolean start() {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user