@@ -122,7 +122,7 @@ public class RMIServer implements IGameServer {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a model game to all RMI clients.
|
* Sends a game model to all RMI clients.
|
||||||
* @param model The desired model
|
* @param model The desired model
|
||||||
*/
|
*/
|
||||||
public void notifyAll(Game model) throws RemoteException {
|
public void notifyAll(Game model) throws RemoteException {
|
||||||
|
|||||||
@@ -8,78 +8,111 @@ import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
|
||||||
public class TCPClient implements Serializable{
|
/**
|
||||||
Socket communicationSocket = null;
|
* Client TCP. Sends and receives messages with the TCP server.
|
||||||
|
*/
|
||||||
|
public class TCPClient {
|
||||||
|
|
||||||
|
/** Socket TCP */
|
||||||
|
Socket communicationSocket;
|
||||||
|
|
||||||
|
/** Input stream used receive objects from the server */
|
||||||
ObjectInputStream socketReceive;
|
ObjectInputStream socketReceive;
|
||||||
|
|
||||||
|
/** Output stream used to send objects to the server */
|
||||||
ObjectOutputStream socketSend;
|
ObjectOutputStream socketSend;
|
||||||
|
|
||||||
|
/** Client game's controller */
|
||||||
GameController controller;
|
GameController controller;
|
||||||
|
|
||||||
|
/** IP address of the server to connect to */
|
||||||
String hostname;
|
String hostname;
|
||||||
|
|
||||||
|
/** TCP port */
|
||||||
int port;
|
int port;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class constructor that initializes the attributes.
|
||||||
|
* @param controller The game controller
|
||||||
|
* @param hostname The IP address of the server
|
||||||
|
* @param port The TCP port of the server
|
||||||
|
*/
|
||||||
public TCPClient(GameController controller, String hostname, int port) {
|
public TCPClient(GameController controller, String hostname, int port) {
|
||||||
this.controller = controller;
|
this.controller = controller;
|
||||||
this.hostname = hostname;
|
this.hostname = hostname;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean start(String user, int players){
|
|
||||||
|
/**
|
||||||
|
* Starts the TCP connection with the server.
|
||||||
|
* Sends an {@link AddPlayer} event, if the server responds with {@code -1}, the connection is refused and the method returns {@code false}.
|
||||||
|
* Otherwise, a listener thread is started.
|
||||||
|
* @param user The username of the player
|
||||||
|
* @param proposedNPlayers The desired number of players for the game
|
||||||
|
* @return true if the connection is successful, false otherwise.
|
||||||
|
*/
|
||||||
|
public boolean start(String user, int proposedNPlayers) {
|
||||||
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, players));
|
sendEvent(new AddPlayer(user, proposedNPlayers));
|
||||||
if (communicationSocket.getInputStream().read() == -1) {
|
if (communicationSocket.getInputStream().read() == -1) {
|
||||||
|
System.out.println("Could not connect to server");
|
||||||
return false;
|
return false;
|
||||||
}
|
} else {
|
||||||
else{
|
Thread listener = new Thread(() -> receiveMessage());
|
||||||
Thread listener = new Thread(() -> ReceiveMessage());
|
|
||||||
listener.start();
|
listener.start();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
} catch (IOException e) {
|
||||||
catch(Exception e){
|
e.printStackTrace();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ReceiveMessage(){
|
|
||||||
|
/**
|
||||||
|
* Listens continuously for incoming objects from the server.
|
||||||
|
* - If the received object is a {@link NetworkEvent} flagged as an error, it is printed.
|
||||||
|
* - If the received object is a valid {@link NetworkEvent}, it is applied to the game controller.
|
||||||
|
* - If the received object is a {@link Game} model, the controller's model is set.
|
||||||
|
*/
|
||||||
|
private void receiveMessage() {
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
Object read = socketReceive.readObject();
|
Object read = socketReceive.readObject();
|
||||||
|
if (read instanceof NetworkEvent event) { //TODO: avoid instanceof
|
||||||
if (read instanceof NetworkEvent) { //TODO non fare con instanceof
|
|
||||||
NetworkEvent event = (NetworkEvent) read;
|
|
||||||
if (event.getIsError()) {
|
if (event.getIsError()) {
|
||||||
System.out.println(event.toString());
|
System.out.println(event);
|
||||||
} else {
|
} else {
|
||||||
event.apply(controller);
|
event.apply(controller);
|
||||||
//clientController.view.update(); TODO
|
//clientController.view.update(); TODO
|
||||||
}
|
}
|
||||||
|
} else if (read instanceof Game model) {
|
||||||
|
controller.setModel(model);
|
||||||
}
|
}
|
||||||
else if (read instanceof Game) {
|
} catch (IOException e) {
|
||||||
controller.setModel((Game) read);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
catch(IOException e){
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
} catch (ClassNotFoundException e) {
|
||||||
catch(ClassNotFoundException e){
|
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SendEvent(NetworkEvent event){
|
|
||||||
|
/**
|
||||||
|
* Sends a {@link NetworkEvent} to the server.
|
||||||
|
* @param event The NetworkEvent to send.
|
||||||
|
*/
|
||||||
|
private void sendEvent(NetworkEvent event) {
|
||||||
try {
|
try {
|
||||||
socketSend.writeObject(event);
|
socketSend.writeObject(event);
|
||||||
}
|
} catch (IOException e) {
|
||||||
catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,89 +1,105 @@
|
|||||||
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.Model.Game;
|
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 java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the TCP connection with a single client.
|
||||||
|
* Each instance runs on a dedicated thread and is responsible for receiving {@link NetworkEvent} from its client and adding them
|
||||||
|
* to the {@link #actionQueue}.
|
||||||
|
* It is also responsible to send events and game model updates back to the client.
|
||||||
|
*/
|
||||||
public class ClientHandler implements Runnable {
|
public class ClientHandler implements Runnable {
|
||||||
|
|
||||||
|
/** The TCP socket */
|
||||||
private Socket clientSocket;
|
private Socket clientSocket;
|
||||||
private TCPServer server;
|
|
||||||
public ObjectInputStream in = null;
|
/** Input stream used to receive objects from the client */
|
||||||
public ObjectOutputStream out = null;
|
public ObjectInputStream in;
|
||||||
|
|
||||||
|
/** Output stream used to send objects to the client */
|
||||||
|
public ObjectOutputStream out;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shared list of all client handlers.
|
||||||
|
* This handler removes itself from the list when disconnected.
|
||||||
|
*/
|
||||||
List<ClientHandler> clientHandlers;
|
List<ClientHandler> clientHandlers;
|
||||||
|
|
||||||
|
/** Queue containing the events to be applied to the game model */
|
||||||
BlockingQueue<NetworkEvent> actionQueue;
|
BlockingQueue<NetworkEvent> actionQueue;
|
||||||
private EventType eventType;
|
|
||||||
|
|
||||||
public Socket getClientSocket() {
|
|
||||||
return clientSocket;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class constructor that initializes the attributes.
|
||||||
|
* @param clientSocket The socket representing the client's TCP connection
|
||||||
|
* @param clientHandlers The shared list of all active client handlers
|
||||||
|
* @param actionQueue The queue containing incoming events
|
||||||
|
*/
|
||||||
public ClientHandler(Socket clientSocket, List<ClientHandler> clientHandlers, BlockingQueue<NetworkEvent> actionQueue) {
|
public ClientHandler(Socket clientSocket, List<ClientHandler> clientHandlers, BlockingQueue<NetworkEvent> actionQueue) {
|
||||||
this.clientSocket = clientSocket;
|
this.clientSocket = clientSocket;
|
||||||
this.clientHandlers = clientHandlers;
|
this.clientHandlers = clientHandlers;
|
||||||
this.actionQueue = actionQueue;
|
this.actionQueue = actionQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listens for incoming {@link NetworkEvent}s from the client and adds them to the {@link #actionQueue}.
|
||||||
|
* Upon disconnection, this handler removes itself from {@link #clientHandlers}.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
clientLoop();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void clientLoop(){
|
|
||||||
try {
|
try {
|
||||||
NetworkEvent input = null;
|
|
||||||
synchronized(in){
|
|
||||||
in = new ObjectInputStream(clientSocket.getInputStream());
|
in = new ObjectInputStream(clientSocket.getInputStream());
|
||||||
}
|
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
||||||
while (true) {
|
while (true) {
|
||||||
try{
|
NetworkEvent event = (NetworkEvent) in.readObject();
|
||||||
input = (NetworkEvent) (in.readObject());
|
if (!actionQueue.add(event)) {
|
||||||
if(!actionQueue.add(input)){
|
System.out.println("Error inserting action into queue");
|
||||||
System.out.println("An error occurred in inserting an action into queue");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(java.io.IOException e){
|
} catch (IOException e) {
|
||||||
|
clientHandlers.remove(this);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
} catch (ClassNotFoundException e) {
|
||||||
catch (ClassNotFoundException e){
|
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a {@link NetworkEvent} to the client.
|
||||||
|
* @param event The network event to send to the client.
|
||||||
|
*/
|
||||||
public void notifyEvent(NetworkEvent event) {
|
public void notifyEvent(NetworkEvent event) {
|
||||||
synchronized (out) {
|
synchronized (out) {
|
||||||
try {
|
try {
|
||||||
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
||||||
out.writeObject(event);
|
out.writeObject(event);
|
||||||
}
|
} catch (IOException e) {
|
||||||
catch(IOException e){
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends the current game model to this client.
|
||||||
|
* @param game The current state of the game to send to the client.
|
||||||
|
*/
|
||||||
public void notifyModel(Game game) {
|
public void notifyModel(Game game) {
|
||||||
synchronized (out) {
|
synchronized (out) {
|
||||||
try {
|
try {
|
||||||
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
||||||
out.writeObject(game);
|
out.writeObject(game);
|
||||||
}
|
} catch (IOException e) {
|
||||||
catch(IOException e){
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ 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.LimitedList;
|
import it.polimi.ingsw.gc14.LimitedList;
|
||||||
import it.polimi.ingsw.gc14.Model.Game;
|
import it.polimi.ingsw.gc14.Model.Game;
|
||||||
|
import it.polimi.ingsw.gc14.Network.EventType;
|
||||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||||
import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer;
|
import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer;
|
||||||
|
|
||||||
@@ -12,71 +13,120 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Server TCP. Accepts connections and manages all client handlers.
|
||||||
|
*/
|
||||||
public class TCPServer {
|
public class TCPServer {
|
||||||
int port = -1;
|
|
||||||
int ConnectedPlayers = 0;
|
/** TCP port */
|
||||||
ServerSocket serverTCP = null;
|
int port;
|
||||||
GameController gameController;
|
|
||||||
|
/** Number of currently connected clients */
|
||||||
|
int ConnectedPlayers;
|
||||||
|
|
||||||
|
/** Socket TCP */
|
||||||
|
ServerSocket socketTCP;
|
||||||
|
|
||||||
|
/** Server game's controller */
|
||||||
|
GameController controller;
|
||||||
|
|
||||||
|
/** Queue containing the events to be applied to the game model */
|
||||||
BlockingQueue<NetworkEvent> actionQueue;
|
BlockingQueue<NetworkEvent> actionQueue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List containing the usernames of joined players.
|
||||||
|
* {@link LimitedList}'s limit defines at which size the list calls its action. The limit can be set using {@link LimitedList#setLimit(int)}.
|
||||||
|
*/
|
||||||
private LimitedList<String> playerList;
|
private LimitedList<String> playerList;
|
||||||
|
|
||||||
|
/** List containing all client's handlers */
|
||||||
private List<ClientHandler> clientHandlers;
|
private List<ClientHandler> clientHandlers;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
private int getConnectedPlayers(){
|
* Class constructor that initializes the attributes.
|
||||||
return ConnectedPlayers;
|
* @param controller The game controller
|
||||||
|
* @param port The TCP port
|
||||||
|
* @param actionQueue The action queue
|
||||||
|
* @param playerList The player's usernames list
|
||||||
|
*/
|
||||||
|
public TCPServer(GameController controller, int port, BlockingQueue<NetworkEvent> actionQueue, LimitedList<String> playerList){
|
||||||
|
this.port = port;
|
||||||
|
this.ConnectedPlayers = 0;
|
||||||
|
this.socketTCP = null;
|
||||||
|
this.controller = controller;
|
||||||
|
this.actionQueue = actionQueue;
|
||||||
|
this.playerList = playerList;
|
||||||
|
this.clientHandlers = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the TCP server.
|
||||||
|
* If the first event is not AddPlayer, the request is rejected.
|
||||||
|
* If the desired number of player is invalid, the request is rejected.
|
||||||
|
*
|
||||||
|
* If this is the first player to connect, a new game model is created and passed to the controller. Additionally, the playerList's limit is set.
|
||||||
|
* If the controller successfully adds the player, the username is added to {@link #playerList} and the handler is added to {@link #clientHandlers}.
|
||||||
|
*
|
||||||
|
* If any error occurs, the server sends -1 back to the client. Otherwise, it sends 1.
|
||||||
|
*/
|
||||||
public void start(){
|
public void start(){
|
||||||
clientHandlers = new ArrayList<>();
|
|
||||||
|
|
||||||
try{
|
try{
|
||||||
serverTCP = new ServerSocket(port);
|
socketTCP = new ServerSocket(port);
|
||||||
}
|
}
|
||||||
catch (IOException e){
|
catch (IOException e){
|
||||||
System.out.println("Could not listen on port: " + port);
|
System.out.println("Could not start the server TCP on port: " + port);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
System.out.println("Listening on port: " + port);
|
System.out.println("Server TCP started on port: " + port);
|
||||||
|
Socket clientSocket;
|
||||||
|
|
||||||
while(true){
|
while(true){
|
||||||
Socket clientSocket = null;
|
|
||||||
|
|
||||||
try{
|
try{
|
||||||
clientSocket = serverTCP.accept();
|
clientSocket = socketTCP.accept();
|
||||||
ObjectInputStream clientSocketObj = new ObjectInputStream(clientSocket.getInputStream());
|
ObjectInputStream clientSocketObj = new ObjectInputStream(clientSocket.getInputStream());
|
||||||
NetworkEvent event = (NetworkEvent) clientSocketObj.readObject();
|
NetworkEvent event = (NetworkEvent) clientSocketObj.readObject();
|
||||||
|
|
||||||
synchronized (gameController){
|
if(!(event.getEventType() == EventType.ADD_PLAYER)){
|
||||||
if(!(event instanceof AddPlayer) || (gameController.getModel() != null && gameController.getModel().getCurrentPlayerNumber() >= gameController.getModel().getNPlayers())){
|
|
||||||
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{
|
||||||
synchronized (gameController) {
|
AddPlayer eventAddPlayer = (AddPlayer) event;
|
||||||
AddPlayer addPlayer = (AddPlayer) event;
|
if (eventAddPlayer.getProposedNPlayer() < 2 || eventAddPlayer.getProposedNPlayer() > 5) {
|
||||||
if(playerList.isEmpty() && (addPlayer.getProposedNPlayer() < 2 || addPlayer.getProposedNPlayer() > 5)){
|
|
||||||
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 if(gameController.addPlayer(addPlayer.getUsername())){
|
synchronized (controller) {
|
||||||
if (playerList.isEmpty()){
|
if (playerList.isEmpty()){
|
||||||
Game game = new Game(addPlayer.getProposedNPlayer());
|
Game model = new Game(eventAddPlayer.getProposedNPlayer());
|
||||||
gameController.setModel(game);
|
controller.setModel(model);
|
||||||
playerList.setLimit(addPlayer.getProposedNPlayer());
|
playerList.setLimit(eventAddPlayer.getProposedNPlayer());
|
||||||
}
|
}
|
||||||
playerList.add(addPlayer.getUsername());
|
if (controller.addPlayer(eventAddPlayer.getUsername())) {
|
||||||
|
playerList.add(eventAddPlayer.getUsername());
|
||||||
clientSocket.getOutputStream().write((int) (1));
|
clientSocket.getOutputStream().write((int) (1));
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
System.out.println("Accepted player: " + eventAddPlayer.getUsername());
|
||||||
|
ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, actionQueue);
|
||||||
|
clientHandlers.add(clientHandler);
|
||||||
|
ConnectedPlayers++;
|
||||||
|
|
||||||
|
Thread t = new Thread(clientHandler);
|
||||||
|
t.start();
|
||||||
|
} else {
|
||||||
|
clientSocket.getOutputStream().write((int) (-1));
|
||||||
|
clientSocket.close();
|
||||||
|
System.out.println("Player could not be added. Connection terminated.\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// gestione di ADD_PLAYER
|
|
||||||
}
|
}
|
||||||
catch(IOException e){
|
catch(IOException e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -84,44 +134,17 @@ public class TCPServer {
|
|||||||
catch(ClassNotFoundException e){
|
catch(ClassNotFoundException e){
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Accepted player: " + gameController.getModel().getPlayerByUsername(clientSocket.getInetAddress().toString()));
|
|
||||||
|
|
||||||
ConnectedPlayers++;
|
|
||||||
ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, actionQueue);
|
|
||||||
clientHandlers.add(clientHandler);
|
|
||||||
|
|
||||||
//Sending model to clients
|
|
||||||
if(ConnectedPlayers == gameController.getModel().getNPlayers()){
|
|
||||||
for (ClientHandler handler : clientHandlers) {
|
|
||||||
try {
|
|
||||||
synchronized(handler.out){
|
|
||||||
ObjectOutputStream socketTx = new ObjectOutputStream(handler.getClientSocket().getOutputStream());
|
|
||||||
socketTx.writeObject(gameController.getModel());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(IOException e){
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Thread t = new Thread(clientHandler);
|
|
||||||
t.start();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public TCPServer(GameController gameController, int port, BlockingQueue<NetworkEvent> actionQueue, LimitedList<String> players){
|
|
||||||
this.port = port;
|
|
||||||
this.gameController = gameController;
|
|
||||||
this.actionQueue = actionQueue;
|
|
||||||
this.playerList = players;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/** Sends an action to all TCP clients */
|
||||||
public void notifyAll(NetworkEvent event){
|
public void notifyAll(NetworkEvent event){
|
||||||
clientHandlers.forEach((x) -> x.notifyEvent(event));
|
clientHandlers.forEach((x) -> x.notifyEvent(event));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Sends a game model to all TCP clients */
|
||||||
public void notifyAll(Game model){
|
public void notifyAll(Game model){
|
||||||
clientHandlers.forEach((x) -> x.notifyModel(model));
|
clientHandlers.forEach((x) -> x.notifyModel(model));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user