deleted unused classes

work on:
NTtBFormatMsg
ClientMsgDecoder
Notice: no correct response is generated yet!
This commit is contained in:
Sebastian Lenzlinger 2022-03-25 14:44:47 +01:00
parent 002e9a5e57
commit 3575c2010c
15 changed files with 135 additions and 49 deletions

View File

@ -1,8 +1,5 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Client; package ch.unibas.dmi.dbis.cs108.Multiplayer.Client;
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBFormatMsg;
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.ProtocolMessage;
public interface ProtocolParser { public interface ProtocolParser {
/** /**
* Takes a String from client input and parses into * Takes a String from client input and parses into

View File

@ -1,13 +1,43 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol; package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
public abstract class NTtBFormatMsg extends ProtocolMessage{ import java.util.LinkedList;
import java.util.Queue;
private String message; public class NTtBFormatMsg {
public NTtBFormatMsg(String msg) { private String msgToClient;
this.message = msg; private NightTrainProtocol.NTtBCommands command;
private final Queue<String> parameters; //TODO maybe use array?
public NTtBFormatMsg(String msgToClient, NightTrainProtocol.NTtBCommands command, Queue<String> parameters) {
this.msgToClient = msgToClient;
this.command = command;
this.parameters = parameters;
} }
public boolean isCorrectlyFormatted(String msg){ return false;} public NTtBFormatMsg() {
public String getMessage(){return this.message;} this.msgToClient = "";
this.command = null;
this.parameters = new LinkedList<>();
}
public String getMessage() {
return msgToClient;
}
public NightTrainProtocol.NTtBCommands getCommand() {
return command;
}
public Queue<String> getParameters() {
return parameters;
}
public void setMsgToClient(String msgToClient) {
this.msgToClient = msgToClient;
}
public void setCommand(NightTrainProtocol.NTtBCommands command) {
this.command = command;
}
} }

View File

@ -14,4 +14,5 @@
/** /**
Server Commands: Server Commands:
* MSGRS: "Message recieved": Paramaters: a string detailing to the client that and what the server recieved as command. * MSGRS: "Message recieved": Paramaters: a string detailing to the client that and what the server recieved as command.
* SEROR: Server had an error. (used for debugging)
*/ */

View File

@ -11,16 +11,56 @@ for this purpose.
*/ */
public class NightTrainProtocol { public class NightTrainProtocol {
public static HashMap<String, NTtBCommands> stringNTtBCommandsHashMap = new HashMap<>(); //TODO: initialite the fields
public static ProtocolValidator protocolValidator;
public static HashSet<String> legalStrings; private static HashMap<String, NTtBCommands> stringNTtBCommandsHashMap = initializeMapping();
private static ProtocolValidator protocolValidator;
private static HashSet<String> legalStrings = new HashSet<>(stringNTtBCommandsHashMap.keySet());
public enum NTtBCommands { public enum NTtBCommands {
//Client Commands //Client Commands
CRTGM, CHATA, CHATW, CHATG, LEAVG, JOING, VOTEG, QUITS, LISTP, CUSRN, CRTGM, CHATA, CHATW, CHATG, LEAVG, JOING, VOTEG, QUITS, LISTP, CUSRN,
//Server Responses //Server Responses
MSGRS; MSGRS, SEROR;
}
//Allowes to associate strings with the enum objects. the enum fields are easier for switch statements. private static HashMap<String, NTtBCommands> initializeMapping(){
HashMap<String, NTtBCommands> map = new HashMap<>();
for(NTtBCommands cmd: NTtBCommands.values()) {
map.put(cmd.toString(), cmd);
}
return map;
}
//getters & setters
public static HashMap<String, NTtBCommands> getStringNTtBCommandsHashMap() {
return stringNTtBCommandsHashMap;
}
public static HashSet<String> getLegalStrings() {
return legalStrings;
}
//Utility Methods:
/**
* Validates a given string is a valid representation
* of a protocol command
* @param cmd, the string command to be validated
* @return true if <code>cmd</code> is a valid command
*/
public boolean isLegalCmdString(String cmd) {
return legalStrings.contains(cmd);
}
public NTtBCommands getCmdEnumObject(String cmd) throws NoLegalProtocolCommandStringFoundException {
if(isLegalCmdString(cmd)){
return stringNTtBCommandsHashMap.get(cmd);
} else {
throw new NoLegalProtocolCommandStringFoundException();
}
} }
//TODO analyize what methods are needed
} }

View File

@ -0,0 +1,4 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
public class NoLegalProtocolCommandStringFoundException extends Exception {
}

View File

@ -2,5 +2,5 @@ package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
public interface ProtocolDecoder { public interface ProtocolDecoder {
public String decodeMsg(String msg); public NTtBFormatMsg decodeMsg(String msg);
} }

View File

@ -1,6 +0,0 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
public class ProtocolMessage {
//TODO ProtocolMessage Implementation
//TODO definition of abilities and properties of class
}

View File

@ -1,4 +0,0 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.methods;
public class Chat {
}

View File

@ -1,4 +0,0 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.methods;
public class NewGame {
}

View File

@ -1,4 +0,0 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.methods;
public class QUITS {
}

View File

@ -1,4 +0,0 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.methods;
public class VOTEG {
}

View File

@ -46,7 +46,7 @@ public class ClientHandler implements Runnable {
try { try {
msg = in.readLine(); msg = in.readLine();
response = clientMsgDecoder.decodeMsg(msg); //The response of the server to the clients message response = clientMsgDecoder.decodeMsg(msg).getMessage(); //The response of the server to the clients message
out.write(response); out.write(response);
out.newLine(); out.newLine();
out.flush(); out.flush();

View File

@ -1,24 +1,35 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Server; package ch.unibas.dmi.dbis.cs108.Multiplayer.Server;
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBFormatMsg;
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NightTrainProtocol; import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NightTrainProtocol;
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NoLegalProtocolCommandStringFoundException;
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.ProtocolDecoder; import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.ProtocolDecoder;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Queue;
public class ClientMsgDecoder implements ProtocolDecoder { public class ClientMsgDecoder implements ProtocolDecoder {
private NightTrainProtocol.NTtBCommands protocol; private NightTrainProtocol protocol;
@Override @Override
public String decodeMsg(String msg) { //TODO this method IS NOT FINNISHED. @return is not correct as of now!
public NTtBFormatMsg decodeMsg(String msg) {
List<String> msgTokens = tokenizeMsg(msg); //List where we'll put the string tokens seperated by $. List<String> msgTokens = tokenizeMsg(msg); //List where we'll put the string tokens seperated by $.
String cmd; //The command token String cmd; //The command token
NightTrainProtocol.NTtBCommands cmdObject;
Queue<String> parameters;
NTtBFormatMsg util = new NTtBFormatMsg();
cmd = serverResponseBuilder(msgTokens);
cmdObject = getCommandConstant(cmd);
util.setCommand(cmdObject);
try{ try{
cmd = getCommand(msgTokens); cmd = getCommandStringToken(msgTokens);
} catch (NoCommandTokenException e) { } catch (NoCommandTokenException e) {
//TODO: decide what to do here. How can we catch this smartly and where do we send it? //TODO: decide what to do here. How can we catch this smartly and where do we send it?
System.out.println(("ClientMsgDecoder cannot find a command token")); System.out.println(("ClientMsgDecoder cannot find a command token"));
e.printStackTrace(System.out); e.printStackTrace(System.out);
return"ERROR$command token not found"; //TODO This is a very unelegant solution. return new NTtBFormatMsg("ERROR$NoCommandTokenException caught!", null, null);
} }
return null; return null;
@ -29,12 +40,33 @@ public class ClientMsgDecoder implements ProtocolDecoder {
* to client * to client
*/ */
private String serverResponseBuilder(List<String> msgTokens){ private String serverResponseBuilder(List<String> msgTokens){
StringBuilder sb = new StringBuilder();
//assumes not empty list!
NightTrainProtocol.NTtBCommands cmd = getCommandConstant(msgTokens.get(0));
sb.append("Server msg: ");
sb.append("Command *" + cmd.toString() + "* recieved");
return null; return null;
} }
private String getCommand(List<String> msgTokens) throws NoCommandTokenException { //Uses the NightTrainProtocol classes utility method
private boolean isLegalCmdString(String cmd) {
return protocol.isLegalCmdString(cmd);
}
private String getCommandStringToken(List<String> msgTokens) throws NoCommandTokenException {
return msgTokens.get(0); return msgTokens.get(0);
} }
private NightTrainProtocol.NTtBCommands getCommandConstant(String stringToken) {
try{
return protocol.getCmdEnumObject(stringToken);
}catch (NoLegalProtocolCommandStringFoundException e) {
e.printStackTrace();
e.getMessage();
} finally {
return NightTrainProtocol.NTtBCommands.SEROR;
}
}
//Creates tokens from the clientMsg and puts them in a list //Creates tokens from the clientMsg and puts them in a list
private List<String> tokenizeMsg(String msg) { private List<String> tokenizeMsg(String msg) {
@ -45,7 +77,7 @@ public class ClientMsgDecoder implements ProtocolDecoder {
* This method should implement the initiation * This method should implement the initiation
* of server agency according to client msg * of server agency according to client msg
*/ */
private @interface serverActionBuilder { private Queue<String> serverActionBuilder() {
//TODO implement what should happen server side(vote/chat/quit etc) return new LinkedList<String>();
} }
} }

View File

@ -0,0 +1,8 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Server;
public class InSessionLogik {
Server server;
public InSessionLogik(Server server) {
this.server = server;
}
}

View File

@ -1,4 +0,0 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.Server;
public class sessionLogik {
}