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