Implemented NTtBProtocolParser.java.
Still needs to be tested.
This commit is contained in:
parent
66628b07cd
commit
dd898c1742
@ -28,7 +28,8 @@ public class Client {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
while (socket.isConnected()) {
|
||||
String msg = sc.nextLine();
|
||||
out.write(userName + "says: " + msg);
|
||||
String encodedMsg = encodeMessage(msg);
|
||||
out.write(encodedMsg);
|
||||
out.newLine();
|
||||
out.flush();
|
||||
}
|
||||
@ -38,6 +39,18 @@ public class Client {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses <code>NTtBProtocolParser</code> to turn Client
|
||||
* input into the NTtB Protocol format.
|
||||
* Must be called before a client input is sent to the server.
|
||||
* @param msg the msg to be encoded.
|
||||
* @return Message encoded adhering to the NTtB Protocoll.
|
||||
*/
|
||||
private String encodeMessage(String msg) {
|
||||
NTtBProtocolParser pp = new NTtBProtocolParser(this);
|
||||
return pp.parseMsg(msg);
|
||||
}
|
||||
|
||||
public void chatListener() {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
@ -89,4 +102,8 @@ public class Client {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return userName;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,15 @@
|
||||
package ch.unibas.dmi.dbis.cs108.Multiplayer.Client;
|
||||
|
||||
public class EmptyClientInputException extends Exception {
|
||||
String exceptionMsg;
|
||||
Client whoDunIt;
|
||||
public EmptyClientInputException(Client whoDunIt) {
|
||||
this.whoDunIt = whoDunIt;
|
||||
this.exceptionMsg = whoDunIt.getUsername() + " tried to send an empty message";
|
||||
}
|
||||
|
||||
public String getExceptionMsg(){
|
||||
return exceptionMsg;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package ch.unibas.dmi.dbis.cs108.Multiplayer.Client;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBCommands;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class InputToProtocolMap extends HashMap<String, NTtBCommands> {
|
||||
public InputToProtocolMap(){
|
||||
super();
|
||||
this.put("chat", NTtBCommands.CHATA);
|
||||
this.put("cn", NTtBCommands.CUSRN);
|
||||
this.put("list", NTtBCommands.LISTP);
|
||||
this.put("exit", NTtBCommands.LEAVG);
|
||||
//TODO extend according to extended function
|
||||
}
|
||||
}
|
||||
@ -2,43 +2,86 @@ package ch.unibas.dmi.dbis.cs108.Multiplayer.Client;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBCommands;
|
||||
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBFormatMsg;
|
||||
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBParameter;
|
||||
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.ProtocolParser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Implements a protocol parser for the NTtB protocoll,
|
||||
* that transforms client input
|
||||
* into a server readable format.
|
||||
*/
|
||||
public class NTtBProtocolParser implements ProtocolParser {
|
||||
//TODO Possibly bad name, rename to clientMsgParser?
|
||||
public static HashMap<String, NTtBCommands> inputMapper;
|
||||
public final Client caller;
|
||||
public static InputToProtocolMap legalCommands = new InputToProtocolMap();
|
||||
|
||||
public NTtBProtocolParser(){
|
||||
this.inputMapper = new HashMap<>();
|
||||
this.inputMapper.put("chat",NTtBCommands.CHATG);
|
||||
|
||||
public NTtBProtocolParser(Client caller) {
|
||||
this.caller = caller;
|
||||
//TODO by far not done!
|
||||
}
|
||||
@Override
|
||||
public NTtBFormatMsg parseMsg(String msg) {
|
||||
public String parseMsg(String msg) {
|
||||
Scanner sc = new Scanner(msg);
|
||||
String parsedMsg;
|
||||
|
||||
ArrayList<String> input = new ArrayList<>();
|
||||
|
||||
while(sc.hasNext()){
|
||||
input.add(sc.next());
|
||||
}
|
||||
buildProtocolMsg(input);
|
||||
return null;
|
||||
//TODO needs to be finnished
|
||||
|
||||
try {
|
||||
parsedMsg = buildProtocolMsg(input);
|
||||
} catch (EmptyClientInputException e) {
|
||||
return e.getExceptionMsg();
|
||||
//TODO Where to we log this?
|
||||
}
|
||||
|
||||
return parsedMsg;
|
||||
}
|
||||
|
||||
private String buildProtocolMsg(ArrayList<String> input) {
|
||||
|
||||
private String buildProtocolMsg(ArrayList<String> input) throws EmptyClientInputException{
|
||||
//TODO
|
||||
String cmd = parseCmd(input.get(0));
|
||||
input.remove(0);
|
||||
return "";
|
||||
if(emptyClientInput(input)){
|
||||
throw new EmptyClientInputException(caller);
|
||||
}
|
||||
StringBuilder s = new StringBuilder(); //friendly little helper
|
||||
s.append(legalCommands.get(input.get(0)));
|
||||
if (containsParameters(input)) {
|
||||
int size = input.size();
|
||||
for(int i = 1; i < size; i++) {
|
||||
s.append("$");
|
||||
s.append(input.get(i).toLowerCase());
|
||||
}
|
||||
}
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
private String parseCmd(String s){
|
||||
//TODO
|
||||
return "";
|
||||
/**
|
||||
* Checks if input has parameters
|
||||
*
|
||||
* if the list size is smaller than 2, i.e.
|
||||
* not larger than 1, the input only contains
|
||||
* a command.
|
||||
*
|
||||
* @param input the tokenized input string.
|
||||
* @return true if input list is larger than 2.
|
||||
*/
|
||||
private boolean containsParameters(ArrayList<String> input) {
|
||||
return input.size() > 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if client input is empty
|
||||
* @param clientInput the clients input.
|
||||
* @return true if client didn't send any input besides whitespace
|
||||
*/
|
||||
private boolean emptyClientInput(ArrayList<String> clientInput) {
|
||||
return clientInput.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,14 @@
|
||||
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 {
|
||||
/**
|
||||
* Takes a String from client input and parses into
|
||||
* server readable message.
|
||||
* @param msg the message to be parsed
|
||||
* @return a String message formatted for the specific protocol
|
||||
*/
|
||||
String parseMsg(String msg);
|
||||
}
|
||||
@ -15,5 +15,5 @@ public enum NTtBCommands {
|
||||
* QUITS: quit server/ leave servr
|
||||
* LISTP: list players/clients in session with the Server
|
||||
*/
|
||||
CRTGM, CHATA, CHATW, CHATG, LEAVG, JOING, VOTEG, QUITS, LISTP
|
||||
CRTGM, CHATA, CHATW, CHATG, LEAVG, JOING, VOTEG, QUITS, LISTP, CUSRN
|
||||
}
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
package ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.Multiplayer.Protocol.NTtBFormatMsg;
|
||||
|
||||
public interface ProtocolParser {
|
||||
|
||||
ProtocolMessage parseMsg(String msg);
|
||||
}
|
||||
Reference in New Issue
Block a user