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,133 +1,153 @@
|
|||||||
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;
|
||||||
import java.util.HashSet;
|
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 BufferedWriter out;
|
|
||||||
private BufferedReader in;
|
|
||||||
private Socket socket;
|
|
||||||
Scanner sc;
|
|
||||||
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();
|
|
||||||
|
|
||||||
/**
|
private String clientUserName;
|
||||||
* Implements the login logik in client-server
|
private BufferedWriter out;
|
||||||
* architecture.
|
private BufferedReader in;
|
||||||
* @param socket the socket on which to make the connection.
|
private Socket socket;
|
||||||
*/
|
Scanner sc;
|
||||||
public ClientHandler(Socket socket) {
|
public ServerPinger serverPinger;
|
||||||
try {
|
public static HashSet<ClientHandler> connectedClients = new HashSet<>();
|
||||||
this.socket = socket;
|
public static HashSet<ClientHandler> lobby = new HashSet<>();
|
||||||
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
public static HashSet<ClientHandler> ghostClients = new HashSet<>();
|
||||||
this.in = new BufferedReader((new InputStreamReader((socket.getInputStream()))));
|
private ClientMsgDecoder clientMsgDecoder = new ClientMsgDecoder();
|
||||||
this.clientUserName = in.readLine();
|
|
||||||
connectedClients.add(this);
|
/**
|
||||||
broadcastMessage("SERVER: " + clientUserName + " has joined the Server");
|
* Implements the login logic in client-server architecture.
|
||||||
} catch (IOException e) {
|
*
|
||||||
e.printStackTrace();
|
* @param socket the socket on which to make the connection.
|
||||||
closeEverything(socket, in, out);
|
*/
|
||||||
|
public ClientHandler(Socket socket) {
|
||||||
|
try {
|
||||||
|
this.socket = socket;
|
||||||
|
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||||
|
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();
|
||||||
|
closeEverything(socket, in, out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Getters:
|
||||||
|
public BufferedWriter getOut() {
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BufferedReader getIn() {
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Socket getSocket() {
|
||||||
|
return socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
/**
|
||||||
|
* The main logic of the client handler.
|
||||||
|
* Since every client is put on a string this is where
|
||||||
|
* most interactions between client and server are held
|
||||||
|
*/
|
||||||
|
public void run() {
|
||||||
|
String msg;
|
||||||
|
while (socket.isConnected()) {
|
||||||
|
try {
|
||||||
|
msg = in.readLine();
|
||||||
|
JServerProtocolParser.parse(msg, this);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
closeEverything(socket, in, out);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClientUserName() {
|
||||||
|
return clientUserName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void broadcastMessage(String msg) {
|
||||||
|
for (ClientHandler client : connectedClients) {
|
||||||
|
try {
|
||||||
|
if (!client.clientUserName.equals((clientUserName))) {
|
||||||
|
client.out.write(msg);
|
||||||
|
} else {
|
||||||
|
client.out.write("Message: **" + msg + "** sent!");
|
||||||
}
|
}
|
||||||
|
client.out.newLine();
|
||||||
|
client.out.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
closeEverything(socket, in, out);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Getters:
|
public void sendMsgToClient(String msg) {
|
||||||
public BufferedWriter getOut() {
|
try {
|
||||||
return out;
|
out.write(msg);
|
||||||
|
out.newLine();
|
||||||
|
out.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public BufferedReader getIn() {
|
public void removeClientHandler() {
|
||||||
return in;
|
connectedClients.remove(this);
|
||||||
|
broadcastMessage("SERVER: " + clientUserName + " has left the server");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void closeEverything(Socket socket, BufferedReader in, BufferedWriter out) {
|
||||||
|
removeClientHandler();
|
||||||
|
try {
|
||||||
|
if (in != null) {
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
if (out != null) {
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
if (socket != null) {
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static HashSet<ClientHandler> getConnectedClients() {
|
public void decodeMsg(String msg) {
|
||||||
return connectedClients;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static HashSet<ClientHandler> getLobby() {
|
}
|
||||||
return lobby;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static HashSet<ClientHandler> getGhostClients() {
|
|
||||||
return ghostClients;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ClientMsgDecoder getClientMsgDecoder() {
|
|
||||||
return clientMsgDecoder;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Setters
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
/**
|
|
||||||
* The main logic of the client handler.
|
|
||||||
* Since every client is put on a string this is where
|
|
||||||
* most interactions between client and server are held
|
|
||||||
*/
|
|
||||||
public void run() {
|
|
||||||
String msg;
|
|
||||||
while(socket.isConnected()) {
|
|
||||||
try {
|
|
||||||
msg = in.readLine();
|
|
||||||
JServerProtocolParser.parse(msg, this);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
closeEverything(socket, in, out);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getClientUserName() {
|
|
||||||
return clientUserName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void broadcastMessage(String msg) {
|
|
||||||
for (ClientHandler client : connectedClients) {
|
|
||||||
try {
|
|
||||||
if(!client.clientUserName.equals((clientUserName))) {
|
|
||||||
client.out.write(msg);
|
|
||||||
} else {
|
|
||||||
client.out.write("Message: **" + msg + "** sent!");
|
|
||||||
}
|
|
||||||
client.out.newLine();
|
|
||||||
client.out.flush();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
closeEverything(socket, in ,out);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeClientHandler() {
|
|
||||||
connectedClients.remove(this);
|
|
||||||
broadcastMessage("SERVER: " + clientUserName + " has left the server");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void closeEverything(Socket socket, BufferedReader in, BufferedWriter out) {
|
|
||||||
removeClientHandler();
|
|
||||||
try {
|
|
||||||
if (in != null) {
|
|
||||||
in.close();
|
|
||||||
}
|
|
||||||
if (out != null) {
|
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
if (socket != null) {
|
|
||||||
socket.close();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void decodeMsg(String msg){
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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");
|
||||||
|
|||||||
Reference in New Issue
Block a user