Reconsile changes accross Client utility classes

This commit is contained in:
Sebastian Lenzlinger 2022-03-26 11:35:51 +01:00
parent 8f0ccbfa36
commit 9b1faa6ce1
4 changed files with 29 additions and 19 deletions

View File

@ -1,5 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client;
import ch.unibas.dmi.dbis.cs108.multiplayer.protocol.NoLegalProtocolCommandStringFoundException;
import java.net.Socket;
import java.io.*;
import java.net.UnknownHostException;
@ -35,11 +37,22 @@ public class Client {
Scanner sc = new Scanner(System.in);
while (socket.isConnected()) {
String msg = sc.nextLine();
String encodedMsg = encodeMessage(msg);
String encodedMsg = "";
try {
encodedMsg = encodeMessage(msg);
} catch (NoLegalProtocolCommandStringFoundException e) {
System.out.println("ERROR: no legal command found");
encodedMsg = "";
} catch (EmptyClientInputException e) {
//Maybe this exception shouldn't do anything.
} finally {
out.write(encodedMsg);
out.newLine();
out.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
closeEverything(socket, in, out);
@ -53,7 +66,7 @@ public class Client {
* @param msg the msg to be encoded.
* @return Message encoded adhering to the NTtB Protocoll.
*/
private String encodeMessage(String msg) {
private String encodeMessage(String msg) throws NoLegalProtocolCommandStringFoundException, EmptyClientInputException {
NTtBProtocolParser pp = new NTtBProtocolParser(this);
return pp.parseMsg(msg);
}

View File

@ -1,6 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client;
import ch.unibas.dmi.dbis.cs108.multiplayer.protocol.NightTrainProtocol;
import ch.unibas.dmi.dbis.cs108.multiplayer.protocol.NoLegalProtocolCommandStringFoundException;
import java.util.HashMap;
import java.util.HashSet;
@ -23,9 +24,11 @@ public class InputToProtocolMap {
encoding = new HashMap<>(builder);
}
public static String encode(String toEncode) {
public static String encode(String toEncode) throws NoLegalProtocolCommandStringFoundException {
if (legalClientInput.contains(toEncode)) {
return encoding.get(toEncode).toString();
} else {
throw new NoLegalProtocolCommandStringFoundException();
}
}

View File

@ -1,5 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client;
import ch.unibas.dmi.dbis.cs108.multiplayer.protocol.NoLegalProtocolCommandStringFoundException;
import java.util.ArrayList;
import java.util.Scanner;
@ -18,34 +20,26 @@ public class NTtBProtocolParser implements ProtocolParser {
this.caller = caller;
}
@Override
public String parseMsg(String msg) {
public String parseMsg(String msg) throws NoLegalProtocolCommandStringFoundException, EmptyClientInputException{
Scanner sc = new Scanner(msg);
String parsedMsg;
ArrayList<String> input = new ArrayList<>();
String parsedMsg = buildProtocolMsg(input);
while(sc.hasNext()){
input.add(sc.next());
}
try {
parsedMsg = buildProtocolMsg(input);
} catch (EmptyClientInputException e) {
return e.getExceptionMsg();
//TODO Where do we log this?
}
return parsedMsg;
}
private String buildProtocolMsg(ArrayList<String> input) throws EmptyClientInputException{
private String buildProtocolMsg(ArrayList<String> input) throws EmptyClientInputException, NoLegalProtocolCommandStringFoundException {
//TODO
if(emptyClientInput(input)){
throw new EmptyClientInputException(caller);
}
StringBuilder s = new StringBuilder(); //friendly little helper
s.append(legalCommands.get(input.get(0)));
s.append(legalCommands.encode(input.get(0)));
if (containsParameters(input)) {
int size = input.size();
for(int i = 1; i < size; i++) {

View File

@ -7,5 +7,5 @@ public interface ProtocolParser {
* @param msg the message to be parsed
* @return a String message formatted for the specific protocol
*/
String parseMsg(String msg);
String parseMsg(String msg) throws Exception;
}