Add: Finished Implementation For TCP Server.
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package it.polimi.ingsw.gc14.Network.TCP.Server;
|
package it.polimi.ingsw.gc14.Network.TCP.Server;
|
||||||
|
|
||||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
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.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
@@ -8,10 +10,12 @@ import java.util.List;
|
|||||||
|
|
||||||
public class ClientHandler implements Runnable {
|
public class ClientHandler implements Runnable {
|
||||||
private Socket clientSocket;
|
private Socket clientSocket;
|
||||||
|
private TCPServer server;
|
||||||
public ObjectInputStream in = null;
|
public ObjectInputStream in = null;
|
||||||
public ObjectOutputStream out = null;
|
public ObjectOutputStream out = null;
|
||||||
List<ClientHandler> clientHandlers;
|
List<ClientHandler> clientHandlers;
|
||||||
GameController gameController;
|
GameController gameController;
|
||||||
|
private EventType eventType;
|
||||||
|
|
||||||
public Socket getClientSocket() {
|
public Socket getClientSocket() {
|
||||||
return clientSocket;
|
return clientSocket;
|
||||||
@@ -29,12 +33,26 @@ public class ClientHandler implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void clientLoop(){
|
private void clientLoop(){
|
||||||
String s = "";
|
|
||||||
try{
|
try{
|
||||||
while ((s = in.readLine()) != null) {
|
NetworkEvent input = null;
|
||||||
synchronized(out){
|
synchronized(in){
|
||||||
//do something
|
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) {
|
catch (IOException e) {
|
||||||
@@ -42,7 +60,7 @@ public class ClientHandler implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void notifyEvent(Object event){
|
public void notifyEvent(NetworkEvent event){
|
||||||
synchronized(out){
|
synchronized(out){
|
||||||
try{
|
try{
|
||||||
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package it.polimi.ingsw.gc14.Network.TCP.Server;
|
package it.polimi.ingsw.gc14.Network.TCP.Server;
|
||||||
|
|
||||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||||
|
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
@@ -43,6 +44,7 @@ public class TCPServer {
|
|||||||
clientSocket.close();
|
clientSocket.close();
|
||||||
System.out.println("Invalid parameters. Connection terminated.\n");
|
System.out.println("Invalid parameters. Connection terminated.\n");
|
||||||
}
|
}
|
||||||
|
// gestione di ADD_PLAYER
|
||||||
}
|
}
|
||||||
catch (IOException e){
|
catch (IOException e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -79,7 +81,7 @@ public class TCPServer {
|
|||||||
this.gameController = gameController;
|
this.gameController = gameController;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void broadcastUpdate(Object event){
|
public void broadcastUpdate(NetworkEvent event){
|
||||||
clientHandlers.forEach((x) -> x.notifyEvent(event));
|
clientHandlers.forEach((x) -> x.notifyEvent(event));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user