Merge pull request #43 from rubenpirreram/Server-Launcher
Server-Launcher
This commit is contained in:
@@ -11,6 +11,7 @@ import java.rmi.registry.LocateRegistry;
|
|||||||
import java.rmi.registry.Registry;
|
import java.rmi.registry.Registry;
|
||||||
import java.rmi.server.UnicastRemoteObject;
|
import java.rmi.server.UnicastRemoteObject;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
|
||||||
@@ -22,12 +23,14 @@ public class RMIServer implements IGameServer {
|
|||||||
private Registry registry;
|
private Registry registry;
|
||||||
private int nPort;
|
private int nPort;
|
||||||
private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>();
|
private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>();
|
||||||
|
BlockingQueue<NetworkEvent> actionQueue;
|
||||||
|
|
||||||
|
|
||||||
// Costruttore
|
// Costruttore
|
||||||
public RMIServer(GameController controller, int nPort) throws RemoteException {
|
public RMIServer(GameController controller, int nPort, BlockingQueue<NetworkEvent> actionQueue) throws RemoteException {
|
||||||
this.controller = controller;
|
this.controller = controller;
|
||||||
this.nPort = nPort;
|
this.nPort = nPort;
|
||||||
|
this.actionQueue = actionQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -49,13 +52,7 @@ public class RMIServer implements IGameServer {
|
|||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public boolean doEvent(NetworkEvent event) throws RemoteException {
|
public boolean doEvent(NetworkEvent event) throws RemoteException {
|
||||||
boolean result = event.apply(controller);
|
return actionQueue.offer(event);
|
||||||
if (!result) {
|
|
||||||
notifyError(event.getUsername(),"Mossa non valida");
|
|
||||||
} else {
|
|
||||||
notifyAll(event);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -63,17 +60,17 @@ public class RMIServer implements IGameServer {
|
|||||||
|
|
||||||
|
|
||||||
// Metodi interni del server
|
// Metodi interni del server
|
||||||
private void notifyAll(NetworkEvent action) throws RemoteException {
|
public void notifyAll(NetworkEvent action) throws RemoteException {
|
||||||
for (IClientCallback cb : clients.values()) {
|
for (IClientCallback cb : clients.values()) {
|
||||||
cb.onAction(action);
|
cb.onAction(action);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void notifyAll(Game model) throws RemoteException {
|
public void notifyAll(Game model) throws RemoteException {
|
||||||
for (IClientCallback cb : clients.values()) {
|
for (IClientCallback cb : clients.values()) {
|
||||||
cb.onGameInit(model);
|
cb.onGameInit(model);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void notifyError(String username, String message) throws RemoteException {
|
public void notifyError(String username, String message) throws RemoteException {
|
||||||
IClientCallback cb = clients.get(username);
|
IClientCallback cb = clients.get(username);
|
||||||
if (cb != null) cb.onError(message);
|
if (cb != null) cb.onError(message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,12 +7,14 @@ import java.io.*;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
|
||||||
public class TCPServer {
|
public class TCPServer {
|
||||||
int port = -1;
|
int port = -1;
|
||||||
int ConnectedPlayers = 0;
|
int ConnectedPlayers = 0;
|
||||||
ServerSocket serverTCP = null;
|
ServerSocket serverTCP = null;
|
||||||
GameController gameController;
|
GameController gameController;
|
||||||
|
BlockingQueue<NetworkEvent> actionQueue;
|
||||||
|
|
||||||
private List<ClientHandler> clientHandlers;
|
private List<ClientHandler> clientHandlers;
|
||||||
|
|
||||||
@@ -80,9 +82,10 @@ public class TCPServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private TCPServer(GameController gameController, int port){
|
public TCPServer(GameController gameController, int port, BlockingQueue<NetworkEvent> actionQueue){
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.gameController = gameController;
|
this.gameController = gameController;
|
||||||
|
this.actionQueue = actionQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void broadcastUpdate(NetworkEvent event){
|
public void broadcastUpdate(NetworkEvent event){
|
||||||
|
|||||||
@@ -1,11 +1,74 @@
|
|||||||
package it.polimi.ingsw.gc14;
|
package it.polimi.ingsw.gc14;
|
||||||
|
|
||||||
|
|
||||||
|
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.Server.RMIServer;
|
||||||
|
import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer;
|
||||||
|
|
||||||
|
import java.rmi.RemoteException;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
|
||||||
public class ServerLauncher {
|
public class ServerLauncher {
|
||||||
public static void main(String[] args){
|
BlockingQueue<NetworkEvent> actionQueue;
|
||||||
|
GameController gameController;
|
||||||
|
RMIServer serverRMI;
|
||||||
|
TCPServer serverTCP;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public ServerLauncher(BlockingQueue<NetworkEvent> actionQueue, GameController gameController, RMIServer serverRMI, TCPServer serverTCP) {
|
||||||
|
this.actionQueue = actionQueue;
|
||||||
|
this.serverRMI = serverRMI;
|
||||||
|
this.gameController = gameController;
|
||||||
|
this.serverTCP = serverTCP;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean doFirstEven() throws InterruptedException, RemoteException {
|
||||||
|
if(!actionQueue.isEmpty()) {
|
||||||
|
NetworkEvent event = actionQueue.take();
|
||||||
|
if(event.apply(gameController)) {
|
||||||
|
serverRMI.notifyAll(event);
|
||||||
|
serverTCP.broadcastUpdate(event);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
serverRMI.notifyError(event.getUsername(), "Mossa non valida"); // TODO Converrebbe mettere in network event un booleano che dice se è stato accettato e fare una notifyAll anche per errori
|
||||||
|
//serverTCP // TODO non esiste un notify error (guarda sopra)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main() throws RemoteException {
|
||||||
// TODO
|
// TODO
|
||||||
// Deve avere una coda con gli eventi.
|
// 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
|
// 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
|
// Questa classe esegue gli eventi nella coda e chiama il notify all e notify error sia RMI che TCP
|
||||||
|
|
||||||
|
BlockingQueue<NetworkEvent> actionQueue = new LinkedBlockingQueue<>();
|
||||||
|
Game model = new Game();
|
||||||
|
GameController gameController = new GameController(model);
|
||||||
|
RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue);
|
||||||
|
TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue);
|
||||||
|
|
||||||
|
ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP);
|
||||||
|
launcher.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
this.doFirstEven();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
break;
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user