Add: Added TCP Client Handling Logic in TCPClient.java + Refactoring.

This commit is contained in:
GabrieleRadice
2026-04-19 19:05:44 +02:00
parent 5758aa2cc9
commit 2572f8f387
22 changed files with 74 additions and 279 deletions
@@ -1,4 +1,4 @@
package it.polimi.ingsw.gc14.Network.TCP;
package it.polimi.ingsw.gc14.Network;
public enum EventType {
ADD_PLAYER,
@@ -1,7 +1,7 @@
package it.polimi.ingsw.gc14.Network.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
@@ -1,7 +1,7 @@
package it.polimi.ingsw.gc14.Network.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
@@ -1,7 +1,7 @@
package it.polimi.ingsw.gc14.Network.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
@@ -1,7 +1,7 @@
package it.polimi.ingsw.gc14.Network.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
@@ -1,7 +1,7 @@
package it.polimi.ingsw.gc14.Network.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
@@ -1,7 +1,7 @@
package it.polimi.ingsw.gc14.Network.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
@@ -1,7 +1,7 @@
package it.polimi.ingsw.gc14.Network.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
@@ -1,7 +1,7 @@
package it.polimi.ingsw.gc14.Network.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
@@ -1,37 +1,69 @@
package it.polimi.ingsw.gc14.Network.TCP.Client;
import it.polimi.ingsw.gc14.View.IView;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer;
import java.io.*;
import java.net.*;
public class TCPClient implements Serializable {
public TCPClient(IView view) {
IView iView = view;
public class TCPClient implements Serializable{
Socket communicationSocket = null;
ObjectInputStream socketReceive;
ObjectOutputStream socketSend;
GameController controller;
String hostname;
int port;
public TCPClient(GameController controller, String hostname, int port){
this.controller = controller;
this.hostname = hostname;
this.port = port;
}
public static void main(String[] args) {
String hostName = "127.0.0.1";
int portNumber = 5200;
Socket communicationSocket = null;
try {
communicationSocket = new Socket(hostName, portNumber);
public boolean start(String user){
try{
communicationSocket = new Socket(hostname, port);
socketSend = new ObjectOutputStream(communicationSocket.getOutputStream());
socketReceive = new ObjectInputStream(communicationSocket.getInputStream());
socketSend.writeObject(new AddPlayer(user));
if(communicationSocket.getInputStream().read() == -1){
return false;
}
else{
Thread listener = new Thread(() -> ReceiveMessage());
listener.start();
return true;
}
}
catch(Exception e){
return false;
}
}
private void ReceiveMessage(){
while(true){
try{
((NetworkEvent)(socketReceive.readObject())).apply(controller);
}
catch(IOException e){
e.printStackTrace();
}
catch(ClassNotFoundException e){
throw new RuntimeException(e);
}
return;
}
}
private void SendEvent(NetworkEvent event){
try{
socketSend.writeObject(event);
}
catch (IOException e) {
System.err.println(e.toString() + " " + hostName);
System.exit(1);
}
PrintWriter out = null;
BufferedReader in = null;
try {
out = new PrintWriter(communicationSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(communicationSocket.getInputStream()));
} catch (IOException e) {
System.err.println(e.toString() + " " + hostName);
System.exit(1);
}
String userInput = "";
while (true) {
e.printStackTrace();
}
}
@@ -1,10 +0,0 @@
package it.polimi.ingsw.gc14.Network.TCP;
import it.polimi.ingsw.gc14.Controller.GameController;
import java.io.Serializable;
public abstract class NetworkEvent implements Serializable {
public abstract boolean apply(GameController gameController);
}
@@ -1,26 +0,0 @@
package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable;
public class AddPlayer extends NetworkEvent implements Serializable {
private String username;
private EventType eventType;
public AddPlayer(String username) {
this.username = username;
this.eventType = EventType.ADD_PLAYER;
}
@Override
public boolean apply(GameController gameController)
{
return gameController.addPlayer(username);
}
public String apply(IView gameController)
{
return gameController.toString();
}
}
@@ -1,29 +0,0 @@
package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable;
public class DrawLowerBuildingCard extends NetworkEvent implements Serializable{
private String username;
private EventType eventType;
private int pos;
public DrawLowerBuildingCard(String username, int pos){
this.username = username;
this.eventType = EventType.DRAW_LOWER_BUILD;
this.pos = pos;
}
@Override
public boolean apply(GameController gameController){
return gameController.drawLowerBuildingCard(username, pos);
}
public String apply(IView gameController){
return gameController.toString();
}
}
@@ -1,29 +0,0 @@
package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable;
public class DrawLowerTribeCard extends NetworkEvent implements Serializable{
private String username;
private EventType eventType;
private int pos;
public DrawLowerTribeCard(String username, int pos){
this.username = username;
this.eventType = EventType.DRAW_LOWER_TRIBE;
this.pos = pos;
}
@Override
public boolean apply(GameController gameController){
return gameController.drawLowerTribeCard(username, pos);
}
public String apply(IView gameController){
return gameController.toString();
}
}
@@ -1,29 +0,0 @@
package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable;
public class DrawUpperBuildingCard extends NetworkEvent implements Serializable{
private String username;
private EventType eventType;
private int pos;
public DrawUpperBuildingCard(String username, int pos){
this.username = username;
this.eventType = EventType.DRAW_UPPER_BUILD;
this.pos = pos;
}
@Override
public boolean apply(GameController gameController){
return gameController.drawUpperBuildingCard(username, pos);
}
public String apply(IView gameController){
return gameController.toString();
}
}
@@ -1,29 +0,0 @@
package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable;
public class DrawUpperTribeCard extends NetworkEvent implements Serializable{
private String username;
private EventType eventType;
private int pos;
public DrawUpperTribeCard(String username, int pos){
this.username = username;
this.eventType = EventType.DRAW_UPPER_TRIBE;
this.pos = pos;
}
@Override
public boolean apply(GameController gameController){
return gameController.drawUpperTribeCard(username, pos);
}
public String apply(IView gameController){
return gameController.toString();
}
}
@@ -1,29 +0,0 @@
package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable;
public class PickOptionalBuildingCard extends NetworkEvent implements Serializable{
private String username;
private EventType eventType;
private int pos;
public PickOptionalBuildingCard(String username, int pos){
this.username = username;
this.eventType = EventType.PICK_OPTIONAL_BUILD;
this.pos = pos;
}
@Override
public boolean apply(GameController gameController){
return gameController.pickOptionalBuildingCard(username, pos);
}
public String apply(IView gameController){
return gameController.toString();
}
}
@@ -1,29 +0,0 @@
package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable;
public class PickOptionalTribeCard extends NetworkEvent implements Serializable{
private String username;
private EventType eventType;
private int pos;
public PickOptionalTribeCard(String username, int pos){
this.username = username;
this.eventType = EventType.PICK_OPTIONAL_TRIBE;
this.pos = pos;
}
@Override
public boolean apply(GameController gameController){
return gameController.pickOptionalTribeCard(username, pos);
}
public String apply(IView gameController){
return gameController.toString();
}
}
@@ -1,29 +0,0 @@
package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Network.TCP.EventType;
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable;
public class SlotChoice extends NetworkEvent implements Serializable{
private String username;
private EventType eventType;
private int pos;
public SlotChoice(String username, int pos){
this.username = username;
this.eventType = EventType.SLOT_CHOICE;
this.pos = pos;
}
@Override
public boolean apply(GameController gameController){
return gameController.slotChoice(username, pos);
}
public String apply(IView gameController){
return gameController.toString();
}
}
@@ -1,8 +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 it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.EventType;
import java.io.*;
import java.net.*;
@@ -40,7 +40,7 @@ public class ClientHandler implements Runnable {
}
while(true){
try{
input = (NetworkEvent)(in.readObject());
input = (NetworkEvent) (in.readObject());
if(input.apply(gameController)){
server.broadcastUpdate(input);
}
@@ -1,7 +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 it.polimi.ingsw.gc14.Network.NetworkEvent;
import java.io.*;
import java.net.*;
@@ -41,9 +41,13 @@ public class TCPServer {
try{
clientSocket = serverTCP.accept();
if(!gameController.addPlayer(clientSocket.getInputStream().toString()) || ConnectedPlayers > gameController.getModel().getNPlayers()){
clientSocket.getOutputStream().write((int)(-1));
clientSocket.close();
System.out.println("Invalid parameters. Connection terminated.\n");
}
else{
clientSocket.getOutputStream().write((int)(1));
}
// gestione di ADD_PLAYER
}
catch (IOException e){
@@ -1,7 +1,5 @@
package it.polimi.ingsw.gc14.View;
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
public interface IView {
// --- Setup ---
void showWelcome();