implemented JServerProtocolParser, started writing JClientProtocolParser
This commit is contained in:
parent
80644a934c
commit
ac11bc4991
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
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 java.io.*;
|
||||
import java.net.Socket;
|
||||
@ -7,19 +8,21 @@ import java.util.HashSet;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ClientHandler implements Runnable {
|
||||
|
||||
private String clientUserName;
|
||||
private BufferedWriter out;
|
||||
private BufferedReader in;
|
||||
private Socket socket;
|
||||
Scanner sc;
|
||||
public ServerPinger serverPinger;
|
||||
public static HashSet<ClientHandler> connectedClients = new HashSet<>();
|
||||
public static HashSet<ClientHandler> lobby = new HashSet<>();
|
||||
public static HashSet<ClientHandler> ghostClients = new HashSet<>();
|
||||
private ClientMsgDecoder clientMsgDecoder = new ClientMsgDecoder();
|
||||
|
||||
/**
|
||||
* Implements the login logik in client-server
|
||||
* architecture.
|
||||
* Implements the login logic in client-server architecture.
|
||||
*
|
||||
* @param socket the socket on which to make the connection.
|
||||
*/
|
||||
public ClientHandler(Socket socket) {
|
||||
@ -29,6 +32,9 @@ public class ClientHandler implements Runnable {
|
||||
this.in = new BufferedReader((new InputStreamReader((socket.getInputStream()))));
|
||||
this.clientUserName = in.readLine();
|
||||
connectedClients.add(this);
|
||||
serverPinger = new ServerPinger(out, socket);
|
||||
Thread sP = new Thread(serverPinger);
|
||||
sP.start();
|
||||
broadcastMessage("SERVER: " + clientUserName + " has joined the Server");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@ -45,6 +51,10 @@ public class ClientHandler implements Runnable {
|
||||
return in;
|
||||
}
|
||||
|
||||
public Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
public static HashSet<ClientHandler> getConnectedClients() {
|
||||
return connectedClients;
|
||||
}
|
||||
@ -105,6 +115,16 @@ public class ClientHandler implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
public void sendMsgToClient(String msg) {
|
||||
try {
|
||||
out.write(msg);
|
||||
out.newLine();
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void removeClientHandler() {
|
||||
connectedClients.remove(this);
|
||||
broadcastMessage("SERVER: " + clientUserName + " has left the server");
|
||||
|
||||
@ -11,7 +11,7 @@ public class JServerProtocolParser {
|
||||
* @param h this ClientHandler (required so this method can access the ClientHandler's methods)
|
||||
*/
|
||||
public static void parse(String msg, ClientHandler h) {
|
||||
String header = "";
|
||||
String header = ""; //"header" is the first 5 characters.
|
||||
try {
|
||||
header = msg.substring(0, 5);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
@ -23,10 +23,15 @@ public class JServerProtocolParser {
|
||||
h.broadcastMessage(msg.substring(6));
|
||||
return;
|
||||
case "CPING":
|
||||
//todo: pingback
|
||||
h.sendMsgToClient("PINGB");
|
||||
System.out.println("got ping!"); //todo:delete
|
||||
return;
|
||||
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;
|
||||
default:
|
||||
System.out.println("Received unknown command");
|
||||
|
||||
Reference in New Issue
Block a user