@@ -8,7 +8,7 @@ public class ClientController {
|
||||
|
||||
private Game localModel;
|
||||
public GameController localController;
|
||||
private final IView view;
|
||||
public final IView view;
|
||||
|
||||
public ClientController(IView view,Game localModel) {
|
||||
this.view = view;
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package it.polimi.ingsw.gc14;
|
||||
|
||||
import javafx.application.Application;
|
||||
|
||||
public class Launcher {
|
||||
public static void main(String[] args) {
|
||||
Application.launch(HelloApplication.class, args);
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -43,7 +43,7 @@ public class ShamanicRitual extends EventCard {
|
||||
* If all players have the same amount of icons, they all gain and lose Prestige Points.
|
||||
*
|
||||
* Buildings influence:
|
||||
* Building 2: if you have fewer icons than all other players, you don't lose Prestige Points
|
||||
* Building 2: if you have the lowest number of icons, you don't lose Prestige Points
|
||||
* Building 5: during the Shamanic Ritual, you gain 3 icons
|
||||
* Building 6: if you have more icons than any other player, you gain double of Prestige Points
|
||||
*
|
||||
@@ -87,7 +87,6 @@ public class ShamanicRitual extends EventCard {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates and returns a copy of this ShamanicRitual card.
|
||||
*
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import javafx.event.Event;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@@ -9,12 +10,12 @@ public abstract class NetworkEvent implements Serializable {
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
public NetworkEvent()
|
||||
{
|
||||
|
||||
}
|
||||
public NetworkEvent(String username) {
|
||||
protected EventType eventType;
|
||||
public EventType getEventType() {return eventType;}
|
||||
protected NetworkEvent(String username, EventType eventType) {
|
||||
this.username = username;
|
||||
this.eventType = eventType;
|
||||
}
|
||||
|
||||
public abstract boolean apply(GameController gameController);
|
||||
}
|
||||
|
||||
@@ -8,10 +8,8 @@ import it.polimi.ingsw.gc14.View.IView;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class AddPlayer extends NetworkEvent implements Serializable {
|
||||
private EventType eventType;
|
||||
public AddPlayer(String username) {
|
||||
this.username = username;
|
||||
this.eventType = EventType.ADD_PLAYER;
|
||||
super(username, EventType.ADD_PLAYER);
|
||||
}
|
||||
@Override
|
||||
public boolean apply(GameController gameController)
|
||||
|
||||
@@ -8,13 +8,10 @@ import it.polimi.ingsw.gc14.View.IView;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class DrawLowerBuildingCard extends NetworkEvent implements Serializable{
|
||||
|
||||
private EventType eventType;
|
||||
private int pos;
|
||||
|
||||
public DrawLowerBuildingCard(String username, int pos){
|
||||
this.username = username;
|
||||
this.eventType = EventType.DRAW_LOWER_BUILD;
|
||||
super(username, EventType.DRAW_LOWER_BUILD);
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class DrawLowerTribeCard extends NetworkEvent implements Serializable{
|
||||
private EventType eventType;
|
||||
private int pos;
|
||||
|
||||
public DrawLowerTribeCard(String username, int pos){
|
||||
this.username = username;
|
||||
this.eventType = EventType.DRAW_LOWER_TRIBE;
|
||||
super(username, EventType.DRAW_LOWER_TRIBE);
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class DrawUpperBuildingCard extends NetworkEvent implements Serializable{
|
||||
private EventType eventType;
|
||||
private int pos;
|
||||
|
||||
public DrawUpperBuildingCard(String username, int pos){
|
||||
this.username = username;
|
||||
this.eventType = EventType.DRAW_UPPER_BUILD;
|
||||
super(username, EventType.DRAW_UPPER_BUILD);
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class DrawUpperTribeCard extends NetworkEvent implements Serializable{
|
||||
private EventType eventType;
|
||||
private int pos;
|
||||
|
||||
public DrawUpperTribeCard(String username, int pos){
|
||||
this.username = username;
|
||||
this.eventType = EventType.DRAW_UPPER_TRIBE;
|
||||
super(username, EventType.DRAW_UPPER_TRIBE);
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
|
||||
+1
-3
@@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class PickOptionalBuildingCard extends NetworkEvent implements Serializable{
|
||||
private EventType eventType;
|
||||
private int pos;
|
||||
|
||||
public PickOptionalBuildingCard(String username, int pos){
|
||||
this.username = username;
|
||||
this.eventType = EventType.PICK_OPTIONAL_BUILD;
|
||||
super(username, EventType.PICK_OPTIONAL_BUILD);
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class PickOptionalTribeCard extends NetworkEvent implements Serializable{
|
||||
private EventType eventType;
|
||||
private int pos;
|
||||
|
||||
public PickOptionalTribeCard(String username, int pos){
|
||||
this.username = username;
|
||||
this.eventType = EventType.PICK_OPTIONAL_TRIBE;
|
||||
super(username, EventType.PICK_OPTIONAL_TRIBE);
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class SlotChoice extends NetworkEvent implements Serializable {
|
||||
private EventType eventType;
|
||||
private int pos;
|
||||
|
||||
public SlotChoice(String username, int pos) {
|
||||
this.username = username;
|
||||
this.eventType = EventType.SLOT_CHOICE;
|
||||
super(username, EventType.SLOT_CHOICE);
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package it.polimi.ingsw.gc14.Network.RMI.Client;
|
||||
import it.polimi.ingsw.gc14.Controller.ClientController;
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
@@ -23,9 +24,9 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa
|
||||
@Override
|
||||
public void onAction(NetworkEvent event) throws RemoteException {
|
||||
event.apply(clientController.localController); // delega tutto al controller
|
||||
//clientController.view.update(); TODO
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onError(String message) throws RemoteException {
|
||||
clientController.onError(message);
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Client;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.ClientController;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
import it.polimi.ingsw.gc14.View.IView;
|
||||
|
||||
public interface GameClient {
|
||||
boolean connect( String username,ClientController clientController );
|
||||
void doEvent(NetworkEvent event) throws Exception;
|
||||
}
|
||||
@@ -1,15 +1,12 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Client;
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.ClientController;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
import it.polimi.ingsw.gc14.Network.RMI.Server.*;
|
||||
import it.polimi.ingsw.gc14.View.IView;
|
||||
import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer;
|
||||
|
||||
public class RMIClient implements GameClient {
|
||||
public class RMIClient {
|
||||
|
||||
private final String host;
|
||||
private final int port;
|
||||
@@ -20,7 +17,7 @@ public class RMIClient implements GameClient {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public boolean connect(String username,ClientController clientController) {
|
||||
// 1. Connettiti al registry
|
||||
try {
|
||||
@@ -49,8 +46,8 @@ public class RMIClient implements GameClient {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doEvent( NetworkEvent event) throws Exception {
|
||||
|
||||
public void doEvent(NetworkEvent event) throws Exception {
|
||||
stub.doEvent(event);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Client;
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Common;
|
||||
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
+1
-2
@@ -1,6 +1,5 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Server;
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Common;
|
||||
|
||||
import it.polimi.ingsw.gc14.Network.RMI.Client.IClientCallback;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
|
||||
import java.rmi.*;
|
||||
@@ -1,9 +0,0 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Server;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
public interface IRMIServer {
|
||||
|
||||
void add(Integer number) throws RemoteException;
|
||||
void reset() throws RemoteException;
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Server;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Network.RMI.Client.IClientCallback;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvents.DrawUpperBuildingCard;
|
||||
|
||||
import java.rmi.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.*;
|
||||
|
||||
public class RMIGameController implements IGameServer {
|
||||
|
||||
private final GameController controller;
|
||||
private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>();
|
||||
|
||||
public RMIGameController(GameController controller) {
|
||||
this.controller = controller;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean joinGame(String username, IClientCallback callback) {
|
||||
if(controller.addPlayer(username))
|
||||
{
|
||||
clients.put(username, callback);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean doEvent(NetworkEvent event) throws RemoteException {
|
||||
boolean result = event.apply(controller);
|
||||
if (!result) {
|
||||
notifyError(event.getUsername(),"Mossa non valida");
|
||||
} else {
|
||||
notifyAll(event);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void notifyAll(NetworkEvent action) throws RemoteException {
|
||||
for (IClientCallback cb : clients.values()) {
|
||||
cb.onAction(action);
|
||||
}
|
||||
}
|
||||
private void notifyAll(Game model) throws RemoteException {
|
||||
for (IClientCallback cb : clients.values()) {
|
||||
cb.onGameInit(model);
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyError(String username, String message) throws RemoteException {
|
||||
IClientCallback cb = clients.get(username);
|
||||
if (cb != null) cb.onError(message);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,92 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Server;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback;
|
||||
import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class RMIServer {
|
||||
|
||||
private final RMIGameController controller;
|
||||
import java.rmi.*;
|
||||
|
||||
public class RMIServer implements IGameServer {
|
||||
|
||||
private GameController controller;
|
||||
private Registry registry;
|
||||
private int nPort;
|
||||
public RMIServer(RMIGameController controller, int nPort) {
|
||||
private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
// Costruttore
|
||||
public RMIServer(GameController controller, int nPort) throws RemoteException {
|
||||
this.controller = controller;
|
||||
this.nPort = nPort;
|
||||
}
|
||||
|
||||
|
||||
public boolean start() throws Exception {
|
||||
|
||||
|
||||
|
||||
// Metodi esposti RMI
|
||||
public void setController (GameController controller) {
|
||||
this.controller = controller;
|
||||
}
|
||||
@Override
|
||||
public boolean joinGame(String username, IClientCallback callback) {
|
||||
if(controller.addPlayer(username))
|
||||
{
|
||||
clients.put(username, callback);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public boolean doEvent(NetworkEvent event) throws RemoteException {
|
||||
boolean result = event.apply(controller);
|
||||
if (!result) {
|
||||
notifyError(event.getUsername(),"Mossa non valida");
|
||||
} else {
|
||||
notifyAll(event);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Metodi interni del server
|
||||
private void notifyAll(NetworkEvent action) throws RemoteException {
|
||||
for (IClientCallback cb : clients.values()) {
|
||||
cb.onAction(action);
|
||||
}
|
||||
}
|
||||
private void notifyAll(Game model) throws RemoteException {
|
||||
for (IClientCallback cb : clients.values()) {
|
||||
cb.onGameInit(model);
|
||||
}
|
||||
}
|
||||
private void notifyError(String username, String message) throws RemoteException {
|
||||
IClientCallback cb = clients.get(username);
|
||||
if (cb != null) cb.onError(message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Metodi per avviare server RMI
|
||||
public boolean start() {
|
||||
try {
|
||||
IGameServer stub = (IGameServer) UnicastRemoteObject.exportObject(controller, 0);
|
||||
registry = LocateRegistry.createRegistry(nPort);
|
||||
registry.rebind("RMIGameServer", stub);
|
||||
registry.rebind("RMIGameServer", this);
|
||||
System.out.println("RMI Server avviato sulla porta "+nPort);
|
||||
return true;
|
||||
}
|
||||
@@ -31,11 +95,17 @@ public class RMIServer {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void stop() throws Exception {
|
||||
public boolean stop() {
|
||||
try {
|
||||
registry.unbind("RMIGameServer");
|
||||
UnicastRemoteObject.unexportObject(controller, true);
|
||||
UnicastRemoteObject.unexportObject(this, true);
|
||||
System.out.println("RMI Server fermato");
|
||||
return true;
|
||||
} catch (RemoteException | NotBoundException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package it.polimi.ingsw.gc14;
|
||||
|
||||
|
||||
public class ServerLauncher {
|
||||
public static void main(String[] args){
|
||||
// TODO
|
||||
// Deve avere una coda con gli eventi.
|
||||
// Il server RMI e il server TCP quando ricevono un doEvent devono aggiungere l'evento alla coda condivisa
|
||||
// Questa classe esegue gli eventi nella coda e chiama il notify all e notify error sia RMI che TCP
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user