Reconsile changes accross Client utility classes
This commit is contained in:
parent
8f0ccbfa36
commit
9b1faa6ce1
@ -1,5 +1,7 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.multiplayer.client;
|
package ch.unibas.dmi.dbis.cs108.multiplayer.client;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.multiplayer.protocol.NoLegalProtocolCommandStringFoundException;
|
||||||
|
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
@ -35,10 +37,21 @@ public class Client {
|
|||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
while (socket.isConnected()) {
|
while (socket.isConnected()) {
|
||||||
String msg = sc.nextLine();
|
String msg = sc.nextLine();
|
||||||
String encodedMsg = encodeMessage(msg);
|
String encodedMsg = "";
|
||||||
out.write(encodedMsg);
|
try {
|
||||||
out.newLine();
|
encodedMsg = encodeMessage(msg);
|
||||||
out.flush();
|
} 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) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -53,7 +66,7 @@ public class Client {
|
|||||||
* @param msg the msg to be encoded.
|
* @param msg the msg to be encoded.
|
||||||
* @return Message encoded adhering to the NTtB Protocoll.
|
* @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);
|
NTtBProtocolParser pp = new NTtBProtocolParser(this);
|
||||||
return pp.parseMsg(msg);
|
return pp.parseMsg(msg);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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.multiplayer.protocol.NightTrainProtocol;
|
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.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@ -23,9 +24,11 @@ public class InputToProtocolMap {
|
|||||||
encoding = new HashMap<>(builder);
|
encoding = new HashMap<>(builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String encode(String toEncode) {
|
public static String encode(String toEncode) throws NoLegalProtocolCommandStringFoundException {
|
||||||
if (legalClientInput.contains(toEncode)) {
|
if (legalClientInput.contains(toEncode)) {
|
||||||
return encoding.get(toEncode).toString();
|
return encoding.get(toEncode).toString();
|
||||||
|
} else {
|
||||||
|
throw new NoLegalProtocolCommandStringFoundException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.multiplayer.client;
|
package ch.unibas.dmi.dbis.cs108.multiplayer.client;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.multiplayer.protocol.NoLegalProtocolCommandStringFoundException;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
@ -18,34 +20,26 @@ public class NTtBProtocolParser implements ProtocolParser {
|
|||||||
this.caller = caller;
|
this.caller = caller;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public String parseMsg(String msg) {
|
public String parseMsg(String msg) throws NoLegalProtocolCommandStringFoundException, EmptyClientInputException{
|
||||||
Scanner sc = new Scanner(msg);
|
Scanner sc = new Scanner(msg);
|
||||||
String parsedMsg;
|
|
||||||
|
|
||||||
ArrayList<String> input = new ArrayList<>();
|
ArrayList<String> input = new ArrayList<>();
|
||||||
|
String parsedMsg = buildProtocolMsg(input);
|
||||||
|
|
||||||
while(sc.hasNext()){
|
while(sc.hasNext()){
|
||||||
input.add(sc.next());
|
input.add(sc.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
parsedMsg = buildProtocolMsg(input);
|
|
||||||
} catch (EmptyClientInputException e) {
|
|
||||||
return e.getExceptionMsg();
|
|
||||||
//TODO Where do we log this?
|
|
||||||
}
|
|
||||||
|
|
||||||
return parsedMsg;
|
return parsedMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String buildProtocolMsg(ArrayList<String> input) throws EmptyClientInputException{
|
private String buildProtocolMsg(ArrayList<String> input) throws EmptyClientInputException, NoLegalProtocolCommandStringFoundException {
|
||||||
//TODO
|
//TODO
|
||||||
if(emptyClientInput(input)){
|
if(emptyClientInput(input)){
|
||||||
throw new EmptyClientInputException(caller);
|
throw new EmptyClientInputException(caller);
|
||||||
}
|
}
|
||||||
StringBuilder s = new StringBuilder(); //friendly little helper
|
StringBuilder s = new StringBuilder(); //friendly little helper
|
||||||
s.append(legalCommands.get(input.get(0)));
|
s.append(legalCommands.encode(input.get(0)));
|
||||||
if (containsParameters(input)) {
|
if (containsParameters(input)) {
|
||||||
int size = input.size();
|
int size = input.size();
|
||||||
for(int i = 1; i < size; i++) {
|
for(int i = 1; i < size; i++) {
|
||||||
|
|||||||
@ -7,5 +7,5 @@ public interface ProtocolParser {
|
|||||||
* @param msg the message to be parsed
|
* @param msg the message to be parsed
|
||||||
* @return a String message formatted for the specific protocol
|
* @return a String message formatted for the specific protocol
|
||||||
*/
|
*/
|
||||||
String parseMsg(String msg);
|
String parseMsg(String msg) throws Exception;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user