feat: implemented handshake and sendMessage methods properly

This commit is contained in:
Yannik Bretschneider 2021-06-05 23:12:41 +02:00
parent 20bce266b1
commit c5e8bd6ece
2 changed files with 25 additions and 6 deletions

View File

@ -39,9 +39,9 @@ public class MarvelousServer extends WebSocketServer {
private final UserManager userManager; private final UserManager userManager;
public MarvelousServer(InetSocketAddress address, JSON json) { public MarvelousServer(InetSocketAddress address) {
super(address); super(address);
this.userManager = new UserManager(json); this.userManager = new UserManager();
} }
/** Practically No-Arg constructor for testing. <b>NEVER USE ANYWHERE ELSE!</b> /** Practically No-Arg constructor for testing. <b>NEVER USE ANYWHERE ELSE!</b>

View File

@ -3,8 +3,11 @@ package uulm.teamname.marvelous.server.netconnector;
import org.tinylog.Logger; import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.json.JSON; import uulm.teamname.marvelous.gamelibrary.json.JSON;
import uulm.teamname.marvelous.gamelibrary.json.ValidationUtility; import uulm.teamname.marvelous.gamelibrary.json.ValidationUtility;
import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
import uulm.teamname.marvelous.gamelibrary.messages.ErrorMessage; import uulm.teamname.marvelous.gamelibrary.messages.ErrorMessage;
import uulm.teamname.marvelous.gamelibrary.messages.client.*; import uulm.teamname.marvelous.gamelibrary.messages.client.*;
import uulm.teamname.marvelous.gamelibrary.messages.server.HelloClientMessage;
import uulm.teamname.marvelous.server.Server;
import uulm.teamname.marvelous.server.lobbymanager.Participant; import uulm.teamname.marvelous.server.lobbymanager.Participant;
import org.java_websocket.WebSocket; import org.java_websocket.WebSocket;
@ -42,13 +45,13 @@ public class UserManager {
private final JSON json; private final JSON json;
/** Constructs a new, empty UserManager */ /** Constructs a new, empty UserManager */
public UserManager(JSON json) { public UserManager() {
this.newUsers = new HashSet<>(); this.newUsers = new HashSet<>();
this.readyToConnect = new HashMap<>(); this.readyToConnect = new HashMap<>();
this.readyToReconnect = new HashMap<>(); this.readyToReconnect = new HashMap<>();
this.inGame = new HashMap<>(); this.inGame = new HashMap<>();
this.activeParticipants = new HashMap<>(); this.activeParticipants = new HashMap<>();
this.json = json; this.json = new JSON(Server.getCharacterConfig());
} }
/** Called on a new WebSocket connection. Places the WebSocket and its ResourceDescriptor in a HashMap. */ /** Called on a new WebSocket connection. Places the WebSocket and its ResourceDescriptor in a HashMap. */
@ -100,7 +103,7 @@ public class UserManager {
} }
} }
void handshake(WebSocket conn, HelloServerMessage message) { void handshake(WebSocket conn, HelloServerMessage message) { // TODO: Send helloClient
if (!newUsers.contains(conn)) { if (!newUsers.contains(conn)) {
Logger.debug("websocket {} sent HelloServerMessage outside of handshake", conn); Logger.debug("websocket {} sent HelloServerMessage outside of handshake", conn);
sendError(conn, "Invalid message, as Handshake is already completed"); sendError(conn, "Invalid message, as Handshake is already completed");
@ -109,6 +112,8 @@ public class UserManager {
Logger.info("Performing handshake with user '{}'", message.name); Logger.info("Performing handshake with user '{}'", message.name);
var answer = new HelloClientMessage();
SUID clientID = new SUID(message.name, message.deviceID); SUID clientID = new SUID(message.name, message.deviceID);
// check if client is reconnected // check if client is reconnected
@ -117,6 +122,7 @@ public class UserManager {
synchronized (readyToReconnect) { synchronized (readyToReconnect) {
readyToReconnect.put(conn, clientID); readyToReconnect.put(conn, clientID);
} }
answer.runningGame = true;
} else { } else {
Logger.trace("removing handshaking user from newUsers"); Logger.trace("removing handshaking user from newUsers");
synchronized (newUsers) { synchronized (newUsers) {
@ -126,7 +132,10 @@ public class UserManager {
synchronized (readyToConnect) { synchronized (readyToConnect) {
readyToConnect.put(conn, clientID); readyToConnect.put(conn, clientID);
} }
answer.runningGame = false;
} }
// Send the answer message with the previously set runningGame
sendMessage(conn, answer);
} }
void reconnectClient(WebSocket conn, ReconnectMessage message) { void reconnectClient(WebSocket conn, ReconnectMessage message) {
@ -154,6 +163,7 @@ public class UserManager {
} }
void assignLobby(WebSocket conn, PlayerReadyMessage message) { void assignLobby(WebSocket conn, PlayerReadyMessage message) {
// TODO: Assign a lobby
} }
void charactersSelected(WebSocket conn, CharacterSelectionMessage message) { void charactersSelected(WebSocket conn, CharacterSelectionMessage message) {
@ -162,12 +172,21 @@ public class UserManager {
void relayRequestMessage(Participant conn, RequestMessage message) { void relayRequestMessage(Participant conn, RequestMessage message) {
} }
public void sendMessage(WebSocket conn, BasicMessage message) {
var jsonRepresentingMessage = json.stringify(message);
if (jsonRepresentingMessage.isEmpty()) {
Logger.warn("Message {} could not be serialized!", message);
} else {
conn.send(jsonRepresentingMessage.get());
}
}
/** Sends an {@link uulm.teamname.marvelous.gamelibrary.messages.ErrorMessage} to the specified user. */ /** Sends an {@link uulm.teamname.marvelous.gamelibrary.messages.ErrorMessage} to the specified user. */
public void sendError(WebSocket conn, String error) { public void sendError(WebSocket conn, String error) {
Logger.debug("Sending error message '{}' to WebSocket {}", error, conn); Logger.debug("Sending error message '{}' to WebSocket {}", error, conn);
var errorMessage = new ErrorMessage(); var errorMessage = new ErrorMessage();
errorMessage.message = error; errorMessage.message = error;
conn.send(json.stringify(errorMessage).get()); sendMessage(conn, errorMessage);
} }
public void disconnectUser(WebSocket conn, boolean closedByRemote) { public void disconnectUser(WebSocket conn, boolean closedByRemote) {