implemented JServerProtocolParser, started writing JClientProtocolParser

This commit is contained in:
Jonas 2022-03-27 10:41:37 +02:00
parent 80644a934c
commit ac11bc4991
3 changed files with 183 additions and 118 deletions

View File

@ -0,0 +1,40 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
public class JClientProtocolParser {
/**
* Used by the client to parse an incoming protocol message.
* @param msg the encoded message that needs to be parsed
* @param c this Client(required so this method can access the Client's methods)
*/
public static void parse(String msg, Client c) {
String header = ""; //"header" is the first 5 characters.
try {
header = msg.substring(0, 5);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
System.out.println(header);
switch (header) {
case "CPING":
h.sendMsgToClient("PINGB");
System.out.println("got ping!"); //todo:delete
return;
case "PINGB":
h.serverPinger.setGotPingBack(true);
System.out.println("got pingback!"); //todo: delete
return;
case "QUITS":
h.closeEverything(h.getSocket(), h.getIn(), h.getOut());
return;
default:
System.out.println("Received unknown command");
}
}
}
}

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.helpers.ServerPinger;
import ch.unibas.dmi.dbis.cs108.multiplayer.protocol.NTtBFormatMsg; import ch.unibas.dmi.dbis.cs108.multiplayer.protocol.NTtBFormatMsg;
import java.io.*; import java.io.*;
import java.net.Socket; import java.net.Socket;
@ -7,19 +8,21 @@ import java.util.HashSet;
import java.util.Scanner; import java.util.Scanner;
public class ClientHandler implements Runnable { public class ClientHandler implements Runnable {
private String clientUserName; private String clientUserName;
private BufferedWriter out; private BufferedWriter out;
private BufferedReader in; private BufferedReader in;
private Socket socket; private Socket socket;
Scanner sc; Scanner sc;
public ServerPinger serverPinger;
public static HashSet<ClientHandler> connectedClients = new HashSet<>(); public static HashSet<ClientHandler> connectedClients = new HashSet<>();
public static HashSet<ClientHandler> lobby = 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();
/** /**
* Implements the login logik in client-server * Implements the login logic in client-server architecture.
* architecture. *
* @param socket the socket on which to make the connection. * @param socket the socket on which to make the connection.
*/ */
public ClientHandler(Socket socket) { public ClientHandler(Socket socket) {
@ -29,6 +32,9 @@ public class ClientHandler implements Runnable {
this.in = new BufferedReader((new InputStreamReader((socket.getInputStream())))); this.in = new BufferedReader((new InputStreamReader((socket.getInputStream()))));
this.clientUserName = in.readLine(); this.clientUserName = in.readLine();
connectedClients.add(this); connectedClients.add(this);
serverPinger = new ServerPinger(out, socket);
Thread sP = new Thread(serverPinger);
sP.start();
broadcastMessage("SERVER: " + clientUserName + " has joined the Server"); broadcastMessage("SERVER: " + clientUserName + " has joined the Server");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -45,6 +51,10 @@ public class ClientHandler implements Runnable {
return in; return in;
} }
public Socket getSocket() {
return socket;
}
public static HashSet<ClientHandler> getConnectedClients() { public static HashSet<ClientHandler> getConnectedClients() {
return connectedClients; return connectedClients;
} }
@ -72,7 +82,7 @@ public class ClientHandler implements Runnable {
*/ */
public void run() { public void run() {
String msg; String msg;
while(socket.isConnected()) { while (socket.isConnected()) {
try { try {
msg = in.readLine(); msg = in.readLine();
JServerProtocolParser.parse(msg, this); JServerProtocolParser.parse(msg, this);
@ -91,7 +101,7 @@ public class ClientHandler implements Runnable {
public void broadcastMessage(String msg) { public void broadcastMessage(String msg) {
for (ClientHandler client : connectedClients) { for (ClientHandler client : connectedClients) {
try { try {
if(!client.clientUserName.equals((clientUserName))) { if (!client.clientUserName.equals((clientUserName))) {
client.out.write(msg); client.out.write(msg);
} else { } else {
client.out.write("Message: **" + msg + "** sent!"); client.out.write("Message: **" + msg + "** sent!");
@ -100,11 +110,21 @@ public class ClientHandler implements Runnable {
client.out.flush(); client.out.flush();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
closeEverything(socket, in ,out); closeEverything(socket, in, out);
} }
} }
} }
public void sendMsgToClient(String msg) {
try {
out.write(msg);
out.newLine();
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void removeClientHandler() { public void removeClientHandler() {
connectedClients.remove(this); connectedClients.remove(this);
broadcastMessage("SERVER: " + clientUserName + " has left the server"); broadcastMessage("SERVER: " + clientUserName + " has left the server");
@ -127,7 +147,7 @@ public class ClientHandler implements Runnable {
} }
} }
public void decodeMsg(String msg){ public void decodeMsg(String msg) {
} }
} }

View File

@ -11,7 +11,7 @@ public class JServerProtocolParser {
* @param h this ClientHandler (required so this method can access the ClientHandler's methods) * @param h this ClientHandler (required so this method can access the ClientHandler's methods)
*/ */
public static void parse(String msg, ClientHandler h) { public static void parse(String msg, ClientHandler h) {
String header = ""; String header = ""; //"header" is the first 5 characters.
try { try {
header = msg.substring(0, 5); header = msg.substring(0, 5);
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
@ -23,10 +23,15 @@ public class JServerProtocolParser {
h.broadcastMessage(msg.substring(6)); h.broadcastMessage(msg.substring(6));
return; return;
case "CPING": case "CPING":
//todo: pingback h.sendMsgToClient("PINGB");
System.out.println("got ping!"); //todo:delete
return; return;
case "PINGB": case "PINGB":
//todo: register pingback h.serverPinger.setGotPingBack(true);
System.out.println("got pingback!"); //todo: delete
return;
case "QUITS":
h.closeEverything(h.getSocket(), h.getIn(), h.getOut());
return; return;
default: default:
System.out.println("Received unknown command"); System.out.println("Received unknown command");