Finnished ClientMsgDecoder!

last thing todo is implement
the execution of the input.
This commit is contained in:
Sebastian Lenzlinger 2022-03-26 13:14:36 +01:00
parent 9b1faa6ce1
commit b2d725f4c3
12 changed files with 423 additions and 312 deletions

View File

@ -41,7 +41,7 @@
</td> </td>
<td> <td>
<div class="infoBox" id="duration"> <div class="infoBox" id="duration">
<div class="counter">0.009s</div> <div class="counter">0.013s</div>
<p>duration</p> <p>duration</p>
</div> </div>
</td> </td>
@ -76,7 +76,7 @@
</thead> </thead>
<tr> <tr>
<td class="success">testMain()</td> <td class="success">testMain()</td>
<td class="success">0.009s</td> <td class="success">0.013s</td>
<td class="success">passed</td> <td class="success">passed</td>
</tr> </tr>
</table> </table>
@ -89,7 +89,7 @@
<input id="line-wrapping-toggle" type="checkbox" autocomplete="off"/> <input id="line-wrapping-toggle" type="checkbox" autocomplete="off"/>
</label> </label>
</div>Generated by </div>Generated by
<a href="http://www.gradle.org">Gradle 6.9.2</a> at 06.03.2022, 13:30:14</p> <a href="http://www.gradle.org">Gradle 6.9.2</a> at 26.03.2022, 12:19:23</p>
</div> </div>
</div> </div>
</body> </body>

View File

@ -9,140 +9,142 @@ import java.util.Scanner;
public class Client { public class Client {
private Socket socket; private Socket socket;
private BufferedReader in; private BufferedReader in;
private BufferedWriter out; private BufferedWriter out;
public String userName; public String userName;
public Client(Socket socket, String userName) { public Client(Socket socket, String userName) {
try {
this.socket = socket;
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
this.in = new BufferedReader((new InputStreamReader((socket.getInputStream()))));
//TODO add the system based generated username here.
//TODO hide connecting logik(next 4 lines)
this.userName = userName;
this.out.write(getUsername());
this.out.newLine();
this.out.flush();
} catch (IOException e) {
e.printStackTrace();
closeEverything(socket, in, out);
}
}
public void sendMessage() {
try {
Scanner sc = new Scanner(System.in);
while (socket.isConnected()) {
String msg = sc.nextLine();
String encodedMsg = "";
try { try {
this.socket = socket; encodedMsg = encodeMessage(msg);
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); } catch (NoLegalProtocolCommandStringFoundException e) {
this.in = new BufferedReader((new InputStreamReader((socket.getInputStream())))); System.out.println("ERROR: no legal command found");
encodedMsg = "";
//TODO add the system based generated username here. } catch (EmptyClientInputException e) {
//TODO hide connecting logik(next 4 lines) //Maybe this exception shouldn't do anything.
this.userName = userName; } finally {
this.out.write(getUsername()); out.write(encodedMsg);
this.out.newLine(); out.newLine();
this.out.flush(); out.flush();
} catch (IOException e) {
e.printStackTrace();
closeEverything(socket, in, out);
} }
}
} catch (IOException e) {
e.printStackTrace();
closeEverything(socket, in, out);
} }
}
public void sendMessage() { /**
try { * Uses <code>NTtBProtocolParser</code> to turn Client input into the NTtB Protocol format. Must
Scanner sc = new Scanner(System.in); * be called before a client input is sent to the server.
while (socket.isConnected()) { *
String msg = sc.nextLine(); * @param msg the msg to be encoded.
String encodedMsg = ""; * @return Message encoded adhering to the NTtB Protocoll.
try { */
encodedMsg = encodeMessage(msg); private String encodeMessage(String msg)
} catch (NoLegalProtocolCommandStringFoundException e) { throws NoLegalProtocolCommandStringFoundException, EmptyClientInputException {
System.out.println("ERROR: no legal command found"); NTtBProtocolParser pp = new NTtBProtocolParser(this);
encodedMsg = ""; return pp.parseMsg(msg);
} catch (EmptyClientInputException e) { }
//Maybe this exception shouldn't do anything. //TODO implement decoding of server input
} finally { private String decodeServerMsg(String msg){return null;}
out.write(encodedMsg);
out.newLine();
out.flush();
}
/**
} * Listens for incoming messages
} catch (IOException e) { */
e.printStackTrace(); public void chatListener() {
closeEverything(socket, in, out);
}
}
/**
* 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) throws NoLegalProtocolCommandStringFoundException, EmptyClientInputException {
NTtBProtocolParser pp = new NTtBProtocolParser(this);
return pp.parseMsg(msg);
}
/**
* Listens for incoming messages
*/
public void chatListener() {
/*TODO: what type of decoding has to be done /*TODO: what type of decoding has to be done
think better about structure for incoming messages TODO how shall input be logged?
*/ */
//TODO how shall input be logged? new Thread(new Runnable() {
new Thread(new Runnable() { @Override
@Override public void run() {
public void run() { String chatMsg;
String chatMsg;
while(socket.isConnected()) { while (socket.isConnected()) {
try { try {
chatMsg = in.readLine(); chatMsg = in.readLine();
System.out.println(chatMsg); System.out.println(chatMsg);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
closeEverything(socket, in, out);
}
}
}
}).start();
}
public void closeEverything(Socket socket, BufferedReader in, BufferedWriter out) {
//TODO Correctly closing a clients connection
//TODO the server should be notified in a way so he can handle it cleanly
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
closeEverything(socket, in, out);
}
} }
}
}).start();
}
public void closeEverything(Socket socket, BufferedReader in, BufferedWriter out) {
//TODO Correctly closing a clients connection
//TODO the server should be notified in a way so he can handle it cleanly
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String hostname;
int port = 42069; //can be set via argument later if needed.
if (args.length < 1) {
System.out.println("Enter the host's IP address (or type localhost)");
hostname = sc.next();
} else {
hostname = args[0];
}
System.out.println("Choose a nickname: ");
String username = sc.next();
Socket socket;
try {
socket = new Socket(hostname, 42069);
Client client = new Client(socket, username);
client.chatListener();
client.sendMessage();
} catch (UnknownHostException e) {
System.out.println("Invalid host IP");
} catch (IOException e) {
e.printStackTrace();
} }
public static void main(String[] args) { }
Scanner sc = new Scanner(System.in);
String hostname;
int port = 42069; //can be set via argument later if needed.
if (args.length < 1) {
System.out.println("Enter the host's IP address (or type localhost)");
hostname = sc.next();
} else {
hostname = args[0];
}
System.out.println("Choose a nickname: ");
String username = sc.next();
Socket socket;
try {
socket = new Socket(hostname, 42069);
Client client = new Client(socket, username);
client.chatListener();
client.sendMessage();
} catch (UnknownHostException e) {
System.out.println("Invalid host IP");
} catch (IOException e) {
e.printStackTrace();
}
} public String getUsername() {
return userName;
public String getUsername() { }
return userName;
}
} }

View File

@ -8,29 +8,29 @@ import java.util.HashSet;
public class InputToProtocolMap { public class InputToProtocolMap {
private static final HashMap<String, NightTrainProtocol.NTtBCommands> encoding; private static final HashMap<String, NightTrainProtocol.NTtBCommands> encoding;
private static final HashSet<String> legalClientInput; private static final HashSet<String> legalClientInput;
static { static {
//First add all legal commands to a map //First add all legal commands to a map
HashMap<String, NightTrainProtocol.NTtBCommands> builder = new HashMap<>(); HashMap<String, NightTrainProtocol.NTtBCommands> builder = new HashMap<>();
builder.put("chat", NightTrainProtocol.NTtBCommands.CHATA); builder.put("chat", NightTrainProtocol.NTtBCommands.CHATA);
builder.put("cn", NightTrainProtocol.NTtBCommands.CUSRN); builder.put("cn", NightTrainProtocol.NTtBCommands.CUSRN);
builder.put("list", NightTrainProtocol.NTtBCommands.LISTP); builder.put("list", NightTrainProtocol.NTtBCommands.LISTP);
builder.put("exit", NightTrainProtocol.NTtBCommands.LEAVG); builder.put("exit", NightTrainProtocol.NTtBCommands.LEAVG);
//TODO extend according to extended function //TODO extend according to extended function
//Initialize static final map and set //Initialize static final map and set
legalClientInput = new HashSet<>(builder.keySet()); legalClientInput = new HashSet<>(builder.keySet());
encoding = new HashMap<>(builder); encoding = new HashMap<>(builder);
} }
public static String encode(String toEncode) throws NoLegalProtocolCommandStringFoundException { 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 { } else {
throw new NoLegalProtocolCommandStringFoundException(); throw new NoLegalProtocolCommandStringFoundException();
}
} }
}
} }

View File

@ -6,70 +6,72 @@ import java.util.ArrayList;
import java.util.Scanner; import java.util.Scanner;
/** /**
* Implements a protocol parser for the NTtB protocoll, * Implements a protocol parser for the NTtB protocoll, that transforms client input into a server
* that transforms client input * readable format.
* into a server readable format.
*/ */
public class NTtBProtocolParser implements ProtocolParser { public class NTtBProtocolParser implements ProtocolParser {
//TODO Possibly bad name, rename to clientMsgParser?
public final Client caller; //TODO Possibly bad name, rename to clientMsgParser?
public static InputToProtocolMap legalCommands = new InputToProtocolMap(); public final Client caller;
public static InputToProtocolMap legalCommands = new InputToProtocolMap();
public NTtBProtocolParser(Client caller) { public NTtBProtocolParser(Client caller) {
this.caller = caller; this.caller = caller;
} }
@Override
public String parseMsg(String msg) throws NoLegalProtocolCommandStringFoundException, EmptyClientInputException{
Scanner sc = new Scanner(msg);
ArrayList<String> input = new ArrayList<>();
String parsedMsg = buildProtocolMsg(input);
while(sc.hasNext()){ @Override
input.add(sc.next()); public String parseMsg(String msg)
} throws NoLegalProtocolCommandStringFoundException, EmptyClientInputException {
Scanner sc = new Scanner(msg);
ArrayList<String> input = new ArrayList<>();
String parsedMsg = buildProtocolMsg(input);
return parsedMsg; while (sc.hasNext()) {
input.add(sc.next());
} }
return parsedMsg;
}
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.encode(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()); //parameters are always lower case (is that good?)
}
}
return s.toString();
}
/** private String buildProtocolMsg(ArrayList<String> input)
* Checks if input has parameters throws EmptyClientInputException, NoLegalProtocolCommandStringFoundException {
* //TODO
* if the list size is smaller than 2, i.e. if (emptyClientInput(input)) {
* not larger than 1, the input only contains throw new EmptyClientInputException(caller);
* 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;
} }
StringBuilder s = new StringBuilder(); //friendly little helper
s.append(legalCommands.encode(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()); //parameters are always lower case (is that good?)
}
}
return s.toString();
}
/** /**
* checks if client input is empty * Checks if input has parameters
* @param clientInput the clients input. * <p>
* @return true if client didn't send any input besides whitespace * if the list size is smaller than 2, i.e. not larger than 1, the input only contains a command.
*/ *
private boolean emptyClientInput(ArrayList<String> clientInput) { * @param input the tokenized input string.
return clientInput.isEmpty(); * @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();
}
} }

View File

@ -1,11 +1,12 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client; package ch.unibas.dmi.dbis.cs108.multiplayer.client;
public interface ProtocolParser { public interface ProtocolParser {
/**
* Takes a String from client input and parses into /**
* server readable message. * 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 * @param msg the message to be parsed
*/ * @return a String message formatted for the specific protocol
String parseMsg(String msg) throws Exception; */
String parseMsg(String msg) throws Exception;
} }

View File

@ -4,49 +4,47 @@ import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
/** /**
* This class defines what type the ClientMsgDecoder returns after decoding the message. * This class defines what type the ClientMsgDecoder returns after decoding the message. This is
* This is done so the output can be split into a response to the client and action * done so the output can be split into a response to the client and action in to the game logik.
* in to the game logik. * commands should map to methods(maybe classes) parameters map to method parameters
* commands should map to methods(maybe classes)
* parameters map to method parameters
*
*/ */
public class NTtBFormatMsg { public class NTtBFormatMsg {
private String msgToClient; private String msgToClient;
private NightTrainProtocol.NTtBCommands command; private NightTrainProtocol.NTtBCommands command;
private final Queue<String> parameters; //TODO maybe use array? private final String[] parameters; //TODO maybe use array?
public NTtBFormatMsg(String msgToClient, NightTrainProtocol.NTtBCommands command, Queue<String> parameters) { public NTtBFormatMsg(String msgToClient, NightTrainProtocol.NTtBCommands command,
this.msgToClient = msgToClient; String[] parameters) {
this.command = command; this.msgToClient = msgToClient;
this.parameters = parameters; this.command = command;
} this.parameters = parameters;
}
public NTtBFormatMsg() { public NTtBFormatMsg() {
this.msgToClient = ""; this.msgToClient = "";
this.command = null; this.command = null;
this.parameters = new LinkedList<>(); this.parameters = new String[]{""};
} }
public String getMessage() { public String getMessage() {
return msgToClient; return msgToClient;
} }
public NightTrainProtocol.NTtBCommands getCommand() { public NightTrainProtocol.NTtBCommands getCommand() {
return command; return command;
} }
public Queue<String> getParameters() { public String[] getParameters() {
return parameters; return parameters;
} }
public void setMsgToClient(String msgToClient) { protected void setMsgToClient(String msgToClient) {
this.msgToClient = msgToClient; this.msgToClient = msgToClient;
} }
public void setCommand(NightTrainProtocol.NTtBCommands command) { protected void setCommand(NightTrainProtocol.NTtBCommands command) {
this.command = command; this.command = command;
} }
} }

View File

@ -10,9 +10,11 @@
* VOTEH: humans voting whos the ghost * VOTEH: humans voting whos the ghost
* QUITS: quit server/ leave servr * QUITS: quit server/ leave servr
* LISTP: list players/clients in session with the Server * LISTP: list players/clients in session with the Server
* CPING: Ping from client to server.
*/ */
/** /**
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) * SEROR: Server had an error. (used for debugging)
* SPING: Ping from server to client;
*/ */

View File

@ -19,9 +19,9 @@ public class NightTrainProtocol {
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,CPING,
//Server Responses //Server Responses
MSGRS, SEROR; MSGRS, SEROR, SPING;
} }
private static HashMap<String, NTtBCommands> initializeMapping(){ private static HashMap<String, NTtBCommands> initializeMapping(){

View File

@ -1,5 +1,6 @@
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 java.io.*; import java.io.*;
import java.net.Socket; import java.net.Socket;
import java.util.HashSet; import java.util.HashSet;
@ -12,7 +13,7 @@ public class ClientHandler implements Runnable {
private Socket socket; private Socket socket;
Scanner sc; Scanner sc;
public static HashSet<ClientHandler> connectedClients = new HashSet<>(); public static HashSet<ClientHandler> connectedClients = new HashSet<>();
public static HashSet<ClientHandler> inGameClients = new HashSet<>(); public static HashSet<ClientHandler> lobby = new HashSet<>();
public static HashSet<ClientHandler> ghostClients = new HashSet<>(); public static HashSet<ClientHandler> ghostClients = new HashSet<>();
private ClientMsgDecoder clientMsgDecoder = new ClientMsgDecoder(); private ClientMsgDecoder clientMsgDecoder = new ClientMsgDecoder();
@ -35,19 +36,47 @@ public class ClientHandler implements Runnable {
} }
} }
//Getters:
public BufferedWriter getOut() {
return out;
}
public BufferedReader getIn() {
return in;
}
public static HashSet<ClientHandler> getConnectedClients() {
return connectedClients;
}
public static HashSet<ClientHandler> getLobby() {
return lobby;
}
public static HashSet<ClientHandler> getGhostClients() {
return ghostClients;
}
public ClientMsgDecoder getClientMsgDecoder() {
return clientMsgDecoder;
}
//Setters
@Override @Override
/** /**
* point of contact for client and server. * point of contact for client and server.
*/ */
public void run() { public void run() {
String msg; String msg;
String response; NTtBFormatMsg response;
while(socket.isConnected()) { while(socket.isConnected()) {
try { try {
msg = in.readLine(); msg = in.readLine();
response = clientMsgDecoder.decodeMsg(msg).getMessage(); //The response of the server to the clients message response = clientMsgDecoder.decodeMsg(msg); //The response of the server to the clients message
out.write(response); out.write(response.getMessage());
out.newLine(); out.newLine();
out.flush(); out.flush();
//TODO if merely an acknowledgement is sent back to the client, how does the client recieve game updates? //TODO if merely an acknowledgement is sent back to the client, how does the client recieve game updates?

View File

@ -6,78 +6,74 @@ import ch.unibas.dmi.dbis.cs108.multiplayer.protocol.NoLegalProtocolCommandStrin
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.LinkedList;
import java.util.List;
import java.util.Queue; import java.util.Queue;
public class ClientMsgDecoder implements ProtocolDecoder { public class ClientMsgDecoder implements ProtocolDecoder {
private NightTrainProtocol protocol;
@Override private NightTrainProtocol protocol;
//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 $.
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{
cmd = getCommandStringToken(msgTokens);
} catch (NoCommandTokenException e) {
//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"));
e.printStackTrace(System.out);
return new NTtBFormatMsg("ERROR$NoCommandTokenException caught!", null, null);
}
return null; @Override
//TODO this method IS NOT FINNISHED. @return is not correct as of now!
public NTtBFormatMsg decodeMsg(String msg) {
//Declare needed variables
String[] msgTokens; //List where we'll put the string tokens seperated by $.
String ackMsg; //The command token
String[] parameters;
NightTrainProtocol.NTtBCommands cmdObject;
//Initalize fields for return object
msgTokens = tokenizeMsg(msg);
ackMsg = serverResponseBuilder(msgTokens);
parameters = new String[msgTokens.length-1];
cmdObject = getCommandConstant(msgTokens[0]);
return new NTtBFormatMsg(ackMsg, cmdObject, parameters);
}
/*
* Builds the servers response message
* to client
*/
private String serverResponseBuilder(String[] msgTokens) {
StringBuilder sb = new StringBuilder();
//assumes not empty list!
NightTrainProtocol.NTtBCommands cmd = getCommandConstant(msgTokens[0]);
sb.append("SERVER: ");
sb.append("Command *" + cmd.toString() + "* recieved!");
return sb.toString();
}
//Uses the NightTrainProtocol classes utility method
private boolean isLegalCmdString(String cmd) {
return protocol.isLegalCmdString(cmd);
}
private String getCommandStringToken(String[] msgTokens) throws NoCommandTokenException {
return msgTokens[0];
}
private NightTrainProtocol.NTtBCommands getCommandConstant(String stringToken) {
try {
return protocol.getCmdEnumObject(stringToken);
} catch (NoLegalProtocolCommandStringFoundException e) {
e.printStackTrace();
e.getMessage();
} finally {
return NightTrainProtocol.NTtBCommands.SEROR;
} }
/* }
* Builds the servers response message
* to client
*/
private String serverResponseBuilder(List<String> msgTokens){
StringBuilder sb = new StringBuilder();
//assumes not empty list!
NightTrainProtocol.NTtBCommands cmd = getCommandConstant(msgTokens.get(0));
sb.append("SERVER: ");
sb.append("Command *" + cmd.toString() + "* recieved!");
return sb.toString(); //Creates tokens from the clientMsg and puts them in a list
} //TODO what side effects could be here?
private String[] tokenizeMsg(String msg) {
return msg.split("$");
}
//Uses the NightTrainProtocol classes utility method /*
private boolean isLegalCmdString(String cmd) { * This method should implement the initiation
return protocol.isLegalCmdString(cmd); * of server agency according to client msg
} */
private String getCommandStringToken(List<String> msgTokens) throws NoCommandTokenException { private Queue<String> serverActionBuilder() {
return msgTokens.get(0); return new LinkedList<String>();
} }
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
private List<String> tokenizeMsg(String msg) {
return null;
}
/*
* This method should implement the initiation
* of server agency according to client msg
*/
private Queue<String> serverActionBuilder() {
return new LinkedList<String>();
}
} }

View File

@ -0,0 +1,73 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.server.cmd.methods;
import ch.unibas.dmi.dbis.cs108.multiplayer.protocol.NTtBFormatMsg;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
import java.io.IOException;
/**
* This Class implements actually acting on the clients
* messages.
*/
public class CommandExecuter {
ClientHandler caller;
public static void execute(NTtBFormatMsg msg) {
switch (msg.getCommand()) {
case CRTGM:
break;
case CHATA:
broadcastClientMsg(msg.getParameters());
break;
case CHATG:
//TODO
break;
case LEAVG:
//TODO
break;
case JOING:
//TODO
break;
case VOTEG:
//TODO
break;
case QUITS:
quitServer();
break;
case CHATW:
wisper(msg.getParameters());
break;
case LISTP:
//TODO
break;
case CUSRN:
changeNickname(msg.getParameters());
break;
case CPING:
pongS();
break;
case MSGRS:
//TODO
break;
case SEROR:
//TODO
break;
case SPING:
pongC();
break;
}
}
/**
* boradcast chat message to everyone
* @param parameters should only have one entry i.e.
* parameters.length == 1
* should be true;
*/
private static void broadcastClientMsg(String[] parameters) throws IOException {
for(ClientHandler clients: ClientHandler.connectedClients) {
clients.getOut().write(parameters[0]);
}
}
}

View File

@ -0,0 +1,8 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.server.cmd.methods;
public interface msgToMethod {
void quit();
}