Fix: Fixed Initial Socket Connection Logic.
Add: "broadcastModel" Method In TCPServer.java, "notifyModel" Method In ClientHandler.java.
This commit is contained in:
@@ -22,13 +22,13 @@ public class TCPClient implements Serializable{
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public boolean start(String user){
|
||||
public boolean start(String user, int players){
|
||||
try{
|
||||
communicationSocket = new Socket(hostname, port);
|
||||
socketSend = new ObjectOutputStream(communicationSocket.getOutputStream());
|
||||
socketReceive = new ObjectInputStream(communicationSocket.getInputStream());
|
||||
|
||||
socketSend.writeObject(new AddPlayer(user));
|
||||
socketSend.writeObject(new AddPlayer(user, players));
|
||||
if(communicationSocket.getInputStream().read() == -1){
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package it.polimi.ingsw.gc14.Network.TCP.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.EventType;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
public class ClientHandler implements Runnable {
|
||||
private Socket clientSocket;
|
||||
@@ -14,17 +16,17 @@ public class ClientHandler implements Runnable {
|
||||
public ObjectInputStream in = null;
|
||||
public ObjectOutputStream out = null;
|
||||
List<ClientHandler> clientHandlers;
|
||||
GameController gameController;
|
||||
BlockingQueue<NetworkEvent> actionQueue;
|
||||
private EventType eventType;
|
||||
|
||||
public Socket getClientSocket() {
|
||||
return clientSocket;
|
||||
}
|
||||
|
||||
public ClientHandler(Socket clientSocket, List<ClientHandler> clientHandlers, GameController gameController) {
|
||||
public ClientHandler(Socket clientSocket, List<ClientHandler> clientHandlers, BlockingQueue<NetworkEvent> actionQueue) {
|
||||
this.clientSocket = clientSocket;
|
||||
this.clientHandlers = clientHandlers;
|
||||
this.gameController = gameController;
|
||||
this.actionQueue = actionQueue;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -41,7 +43,7 @@ public class ClientHandler implements Runnable {
|
||||
while(true){
|
||||
try{
|
||||
input = (NetworkEvent) (in.readObject());
|
||||
if(input.apply(gameController)){
|
||||
if(actionQueue.add(input)){
|
||||
server.broadcastUpdate(input);
|
||||
}
|
||||
}
|
||||
@@ -64,13 +66,26 @@ public class ClientHandler implements Runnable {
|
||||
synchronized(out){
|
||||
try{
|
||||
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
||||
out.writeObject(gameController.getModel());
|
||||
out.writeObject(event);
|
||||
}
|
||||
catch(IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void notifyModel(Game game){
|
||||
synchronized(out){
|
||||
try{
|
||||
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
||||
out.writeObject(game);
|
||||
}
|
||||
catch(IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package it.polimi.ingsw.gc14.Network.TCP.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.NetworkEvents.AddPlayer;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
@@ -9,6 +11,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
|
||||
public class TCPServer {
|
||||
int port = -1;
|
||||
int ConnectedPlayers = 0;
|
||||
@@ -42,24 +45,32 @@ public class TCPServer {
|
||||
|
||||
try{
|
||||
clientSocket = serverTCP.accept();
|
||||
if(!gameController.addPlayer(clientSocket.getInputStream().toString()) || ConnectedPlayers > gameController.getModel().getNPlayers()){
|
||||
ObjectInputStream clientSocketObj = new ObjectInputStream(clientSocket.getInputStream());
|
||||
NetworkEvent event = (NetworkEvent) clientSocketObj.readObject();
|
||||
|
||||
//sincronizzazione su gameController
|
||||
if(!(event instanceof AddPlayer) || (ConnectedPlayers >= gameController.getModel().getCurrentPlayerNumber() && gameController.getModel() != null)){
|
||||
clientSocket.getOutputStream().write((int)(-1));
|
||||
clientSocket.close();
|
||||
System.out.println("Invalid parameters. Connection terminated.\n");
|
||||
}
|
||||
else{
|
||||
actionQueue.add(event);
|
||||
clientSocket.getOutputStream().write((int)(1));
|
||||
}
|
||||
// gestione di ADD_PLAYER
|
||||
}
|
||||
catch (IOException e){
|
||||
catch(IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch(ClassNotFoundException e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
System.out.println("Accepted player: " + gameController.getModel().getPlayerByUsername(clientSocket.getInetAddress().toString()));
|
||||
|
||||
ConnectedPlayers++;
|
||||
ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, gameController);
|
||||
ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, actionQueue);
|
||||
clientHandlers.add(clientHandler);
|
||||
|
||||
//Sending model to clients
|
||||
@@ -91,4 +102,8 @@ public class TCPServer {
|
||||
public void broadcastUpdate(NetworkEvent event){
|
||||
clientHandlers.forEach((x) -> x.notifyEvent(event));
|
||||
}
|
||||
|
||||
public void broadcastModel(){
|
||||
clientHandlers.forEach((x) -> x.notifyModel(gameController.getModel()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user