diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java index 6ac06de..1a3b94f 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java @@ -1,6 +1,8 @@ package it.polimi.ingsw.gc14.Network.TCP.Server; import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; import java.io.*; import java.net.*; @@ -8,10 +10,12 @@ import java.util.List; public class ClientHandler implements Runnable { private Socket clientSocket; + private TCPServer server; public ObjectInputStream in = null; public ObjectOutputStream out = null; List clientHandlers; GameController gameController; + private EventType eventType; public Socket getClientSocket() { return clientSocket; @@ -29,12 +33,26 @@ public class ClientHandler implements Runnable { } private void clientLoop(){ - String s = ""; try{ - while ((s = in.readLine()) != null) { - synchronized(out){ - //do something + NetworkEvent input = null; + synchronized(in){ + in = new ObjectInputStream(clientSocket.getInputStream()); + } + while(true){ + try{ + input = (NetworkEvent)(in.readObject()); + if(input.apply(gameController)){ + server.broadcastUpdate(input); + } } + catch(java.io.IOException e){ + e.printStackTrace(); + } + catch (ClassNotFoundException e){ + throw new RuntimeException(e); + } + + } } catch (IOException e) { @@ -42,7 +60,7 @@ public class ClientHandler implements Runnable { } } - public void notifyEvent(Object event){ + public void notifyEvent(NetworkEvent event){ synchronized(out){ try{ out = new ObjectOutputStream(clientSocket.getOutputStream()); diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java index 0614f7b..38fafe1 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java @@ -1,6 +1,7 @@ package it.polimi.ingsw.gc14.Network.TCP.Server; import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; import java.io.*; import java.net.*; @@ -43,6 +44,7 @@ public class TCPServer { clientSocket.close(); System.out.println("Invalid parameters. Connection terminated.\n"); } + // gestione di ADD_PLAYER } catch (IOException e){ e.printStackTrace(); @@ -79,7 +81,7 @@ public class TCPServer { this.gameController = gameController; } - public void broadcastUpdate(Object event){ + public void broadcastUpdate(NetworkEvent event){ clientHandlers.forEach((x) -> x.notifyEvent(event)); } }