protocol is now fully in the protocol.java class

This commit is contained in:
Jonas 2022-04-08 13:03:32 +02:00
parent 1d9ee624fe
commit e348840169
5 changed files with 49 additions and 26 deletions

View File

@ -1,6 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client; package ch.unibas.dmi.dbis.cs108.multiplayer.client;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -8,17 +9,12 @@ public class JClientProtocolParser {
public static final Logger LOGGER = LogManager.getLogger(); public static final Logger LOGGER = LogManager.getLogger();
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
/**
* Got a request from the Server to vote which human to infect.
*/
public static final String GVOTR = "GVOTR";
/**
* Got a request from Server to vote which player to throw of the train
*/
public static final String HVOTR = "HVOTR";
/** /**
* Used by the client to parse an incoming protocol message. * Used by the client to parse an incoming protocol message.
* For documentation on the individual Protocol messages, view the Protocol.java
* class or hover over the commands (e.g. Protocol.chatMsgToAll) with your mouse
* in this class.
* *
* @param msg the encoded message that needs to be parsed * @param msg the encoded message that needs to be parsed
* @param c this Client(required so this method can access the Client's methods) * @param c this Client(required so this method can access the Client's methods)
@ -33,29 +29,24 @@ public class JClientProtocolParser {
e.printStackTrace(); e.printStackTrace();
} }
switch (header) { switch (header) {
case "SPING": case Protocol.pingFromServer:
//sends a pingback to the server
c.sendMsgToServer("PINGB"); c.sendMsgToServer(Protocol.pingBack);
break; break;
case "PINGB": case Protocol.pingBack:
//registers pingback from server
c.clientPinger.setGotPingBack(true); c.clientPinger.setGotPingBack(true);
break; break;
case "CHATM": case Protocol.printToClientConsole:
/* prints out incoming chat messages / announcements into the user's console.
* any string that follows CHATM$ is printed as is, so the message that follows
* already has to be formatted the way it should be shown to the client.
*/
System.out.println(msg.substring(6)); System.out.println(msg.substring(6));
break; break;
case "QUITC": case Protocol.serverConfirmQuit:
c.disconnectFromServer(); c.disconnectFromServer();
break; break;
case GVOTR: case Protocol.serverRequestsGhostVote:
System.out.println("Ghost received Vote request"); System.out.println("Ghost received Vote request");
//TODO(Seraina): How can be enforced, that clients won't vote otherwise? Trigger a methode here that listens to input //TODO(Seraina): How can be enforced, that clients won't vote otherwise? Trigger a methode here that listens to input
break; break;
case HVOTR: case Protocol.serverRequestsHumanVote:
System.out.println("Human received Vote request"); System.out.println("Human received Vote request");
//TODO(Seraina): How can be enforced, that clients won't vote otherwise? Trigger a methode here that listens to input //TODO(Seraina): How can be enforced, that clients won't vote otherwise? Trigger a methode here that listens to input
break; break;

View File

@ -39,7 +39,7 @@ public class ClientPinger implements Runnable {
Thread.sleep(20000); Thread.sleep(20000);
while (socket.isConnected() && !socket.isClosed()) { while (socket.isConnected() && !socket.isClosed()) {
gotPingBack = false; gotPingBack = false;
client.sendMsgToServer("CPING"); client.sendMsgToServer(Protocol.pingFromClient);
Thread.sleep(4000); Thread.sleep(4000);
if (gotPingBack) { if (gotPingBack) {
if (!isConnected) { //if !isConnected, then the connection had been lost before. if (!isConnected) { //if !isConnected, then the connection had been lost before.

View File

@ -63,4 +63,35 @@ public class Protocol {
*/ */
public static final String clientQuitRequest = "QUITS"; public static final String clientQuitRequest = "QUITS";
//SERVER TO CLIENT COMMANDS
/**
* todo: doc
* //sends a pingback to the server
*/
public static final String pingFromServer = "SPING";
/**
* prints out incoming chat messages / announcements into the user's console.
* any string that follows CHATM$ is printed as is, so the message that follows
* already has to be formatted the way it should be shown to the client.
*/
public static final String printToClientConsole = "CHATM";
/**
* todo:doc
*/
public static final String serverConfirmQuit = "QUITC";
/**
* todo:doc
*/
public static final String serverRequestsGhostVote = "GVOTR";
/**
* todo:doc
*/
public static final String serverRequestsHumanVote = "HVOTR";
} }

View File

@ -40,7 +40,7 @@ public class ServerPinger implements Runnable {
Thread.sleep(2000); Thread.sleep(2000);
while (socket.isConnected() && !socket.isClosed()) { while (socket.isConnected() && !socket.isClosed()) {
gotPingBack = false; gotPingBack = false;
c.sendMsgToClient("SPING"); c.sendMsgToClient(Protocol.pingFromServer);
Thread.sleep(4000); Thread.sleep(4000);
if (gotPingBack) { if (gotPingBack) {
if (!isConnected) { //if !isConnected, then the connection had been lost before. if (!isConnected) { //if !isConnected, then the connection had been lost before.

View File

@ -1,6 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.server; package ch.unibas.dmi.dbis.cs108.multiplayer.server;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ServerPinger; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ServerPinger;
import java.io.*; import java.io.*;
import java.net.InetAddress; import java.net.InetAddress;
@ -144,7 +145,7 @@ public class ClientHandler implements Runnable {
*/ */
public void broadcastChatMessage(String msg) { public void broadcastChatMessage(String msg) {
for (ClientHandler client : connectedClients) { for (ClientHandler client : connectedClients) {
client.sendMsgToClient("CHATM$" + clientUserName + ": " + msg); client.sendMsgToClient(Protocol.printToClientConsole + "$" + clientUserName + ": " + msg);
} }
} }
@ -159,7 +160,7 @@ public class ClientHandler implements Runnable {
public void broadcastAnnouncement(String msg) { public void broadcastAnnouncement(String msg) {
System.out.println(msg); System.out.println(msg);
for (ClientHandler client : connectedClients) { for (ClientHandler client : connectedClients) {
client.sendMsgToClient("CHATM$" + msg); client.sendMsgToClient(Protocol.printToClientConsole + "$" + msg);
} }
} }
@ -199,7 +200,7 @@ public class ClientHandler implements Runnable {
*/ */
public void removeClientOnLogout() { public void removeClientOnLogout() {
broadcastAnnouncement(getClientUserName() + " has left the server."); broadcastAnnouncement(getClientUserName() + " has left the server.");
sendMsgToClient("QUITC"); sendMsgToClient(Protocol.serverConfirmQuit); //todo: protocol
connectedClients.remove(this); connectedClients.remove(this);
disconnectClient(); disconnectClient();
} }