Implemented threading in Client

- GUI doesn't get blocked anymore
- Implemented client side protocol based control
- Client is now ready for player versus player
This commit is contained in:
2021-03-18 03:47:40 +01:00
parent 8299f680b0
commit f1fc88511b
15 changed files with 494 additions and 238 deletions

View File

@@ -1,6 +1,6 @@
package networking;
import game.TicTacToe_Server;
import res.TicTacToe_Server;
import logging.LogType;
import logging.ServerLogger;
@@ -11,6 +11,8 @@ import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;
public class SinglePlayerServer {
@@ -66,8 +68,6 @@ public class SinglePlayerServer {
outstreams.get(client).writeInt(200);
outstreams.get(client).flush();
clientNames.put(client, instreams.get(client).readUTF());
outstreams.get(client).writeUTF(serverSocket.getInetAddress().getHostAddress()+":"+serverSocket.getLocalPort());
outstreams.get(client).flush();
serverLogger.printLog(String.format("%s got connected", clientNames.get(client)), LogType.Log);
} else {
outstreams.get(client).writeInt(403);
@@ -84,6 +84,7 @@ public class SinglePlayerServer {
for (Socket client: clients.values()) {
try {
while (!client.isClosed()) {
//Get instruction
String message = instreams.get(client).readUTF();
serverLogger.printLog(message, clientNames.get(client), LogType.Message);
outstreams.get(client).writeInt(200);
@@ -102,64 +103,105 @@ public class SinglePlayerServer {
}
}
public void sendGameState(Socket client){
try {
outstreams.get(client).writeUTF(ticTacToe_server.getGameState());
outstreams.get(client).flush();
serverLogger.printLog("Sent gameState", clientNames.get(client), LogType.Log);
} catch (IOException e) {
e.printStackTrace();
}
}
public void gameFlow(String input, Socket client){
switch (input){
case "gameState":
try {
outstreams.get(client).writeUTF(ticTacToe_server.getGameState());
outstreams.get(client).flush();
serverLogger.printLog("Sent gameState", clientNames.get(client), LogType.Log);
break;
} catch (IOException e) {
e.printStackTrace();
}
case "ready":
gameFlow("gameState", client);
break;
case "update":
case "gameState":
sendGameState(client);
break;
case "clientMove":
try {
//Get position (X|Y)
String position = instreams.get(client).readUTF();
serverLogger.printLog(position, clientNames.get(client), LogType.Message);
//Send confirmation (2ßß)
outstreams.get(client).writeInt(200);
outstreams.get(client).flush();
serverLogger.printLog("Sent verification code", clientNames.get(client), LogType.Log);
boolean moveAllowed = ticTacToe_server.makeClientMove(position);
if (moveAllowed) {
String gameState = ticTacToe_server.getGameState();
outstreams.get(client).writeUTF(gameState);
outstreams.get(client).flush();
serverLogger.printLog(String.format("Sent gameState: %s", gameState), clientNames.get(client), LogType.Log);
break;
sendGameState(client);
} else {
outstreams.get(client).writeUTF(" ");
//send " "
outstreams.get(client).writeUTF("userInput");
outstreams.get(client).flush();
serverLogger.printLog("Requested userInput", LogType.Log);
serverLogger.printLog(String.format("Move is not allowed!"), clientNames.get(client), LogType.Error);
break;
}
} catch (IOException e) {
e.printStackTrace();
}
break;
case "computerMove":
ticTacToe_server.makeComputerMove();
sendGameState(client);
try {
outstreams.get(client).writeUTF("userInput");
outstreams.get(client).flush();
serverLogger.printLog("Requested userInput", LogType.Log);
} catch (IOException e) {
e.printStackTrace();
}
break;
case "serverType":
try {
outstreams.get(client).writeBoolean(true);
outstreams.get(client).flush();
serverLogger.printLog("Sent serverType", clientNames.get(client), LogType.Log);
} catch (IOException e) {
e.printStackTrace();
}
break;
case "isClientOne":
for (Map.Entry<Integer, Socket> entry : clients.entrySet()) {
if (Objects.equals(client, entry.getValue())) {
try {
outstreams.get(client).writeBoolean(entry.getKey() == 0);
outstreams.get(client).flush();
serverLogger.printLog("Sent isPlayerOne", clientNames.get(client), LogType.Log);
} catch (IOException e) {
e.printStackTrace();
}
} else {
serverLogger.printLog("Current client not in clients!", LogType.Error);
}
}
break;
case "gameEnded":
try {
boolean gameEnded = ticTacToe_server.gameEnded();
outstreams.get(client).writeBoolean(gameEnded);
if (gameEnded) {
//send coordinates
String coordinates = "";
for (Point point: ticTacToe_server.getWinCoordinates()) {
coordinates += point.x + ";" + point.y + ";";
}
//send winning fields
outstreams.get(client).writeUTF(coordinates);
break;
} else {
ticTacToe_server.makeServerMove();
String gameState = ticTacToe_server.getGameState();
outstreams.get(client).writeUTF(gameState);
outstreams.get(client).flush();
serverLogger.printLog(String.format("Sent gameState: %s", gameState), clientNames.get(client), LogType.Log);
break;
}
} catch (IOException e) {
e.printStackTrace();
}
break;
case "exit()":
try {
@@ -169,10 +211,10 @@ public class SinglePlayerServer {
instreams.get(client).close();
client.close();
serverLogger.printLog(String.format("%s closed the connection",clientNames.get(client)), LogType.Log);
break;
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}