Working on JClientProtocolParser

This commit is contained in:
Jonas 2022-03-27 11:54:01 +02:00
parent ac11bc4991
commit 146ff4ea82
6 changed files with 41 additions and 22 deletions

View File

@ -1,5 +1,6 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client; package ch.unibas.dmi.dbis.cs108.multiplayer.client;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ClientPinger;
import ch.unibas.dmi.dbis.cs108.multiplayer.protocol.NoLegalProtocolCommandStringFoundException; import ch.unibas.dmi.dbis.cs108.multiplayer.protocol.NoLegalProtocolCommandStringFoundException;
import java.net.Socket; import java.net.Socket;
@ -13,6 +14,7 @@ public class Client {
private BufferedReader in; private BufferedReader in;
private BufferedWriter out; private BufferedWriter out;
public String userName; public String userName;
public ClientPinger clientPinger;
public Client(Socket socket, String userName) { public Client(Socket socket, String userName) {
try { try {
@ -26,6 +28,7 @@ public class Client {
this.out.write(getUsername()); this.out.write(getUsername());
this.out.newLine(); this.out.newLine();
this.out.flush(); this.out.flush();
clientPinger = new ClientPinger(this.out, this.socket);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
closeEverything(socket, in, out); closeEverything(socket, in, out);
@ -72,12 +75,13 @@ public class Client {
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); parse(chatMsg);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
closeEverything(socket, in, out); closeEverything(socket, in, out);
@ -88,6 +92,21 @@ public class Client {
}).start(); }).start();
} }
public void sendMsgToServer(String msg) {
try {
out.write(msg);
out.newLine();
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void parse(String msg) {
JClientProtocolParser.parse(msg, this);
}
public void closeEverything(Socket socket, BufferedReader in, BufferedWriter out) { public void closeEverything(Socket socket, BufferedReader in, BufferedWriter out) {
//TODO Correctly closing a clients connection //TODO Correctly closing a clients connection
//TODO the server should be notified in a way so he can handle it cleanly //TODO the server should be notified in a way so he can handle it cleanly
@ -124,6 +143,9 @@ public class Client {
Client client = new Client(socket, username); Client client = new Client(socket, username);
client.chatListener(); client.chatListener();
client.sendMessage(); client.sendMessage();
Thread cP = new Thread(client.clientPinger);
System.out.println("im here");
cP.start();
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
System.out.println("Invalid host IP"); System.out.println("Invalid host IP");
} catch (IOException e) { } catch (IOException e) {

View File

@ -18,23 +18,16 @@ public class JClientProtocolParser {
} }
System.out.println(header); System.out.println(header);
switch (header) { switch (header) {
case "SPING":
case "CPING": c.sendMsgToServer("PINGB");
h.sendMsgToClient("PINGB");
System.out.println("got ping!"); //todo:delete System.out.println("got ping!"); //todo:delete
return; break;
case "PINGB": case "PINGB":
h.serverPinger.setGotPingBack(true); c.clientPinger.setGotPingBack(true);
System.out.println("got pingback!"); //todo: delete System.out.println("got pingback!"); //todo: delete
return; break;
case "QUITS":
h.closeEverything(h.getSocket(), h.getIn(), h.getOut());
return;
default: default:
System.out.println("Received unknown command"); System.out.println("Received unknown command");
} }
} }
} }
}

View File

@ -43,10 +43,11 @@ public class ClientPinger implements Runnable {
System.out.println("Connection regained!"); System.out.println("Connection regained!");
} }
} else { } else {
if (isConnected) {
isConnected = false; isConnected = false;
System.out.println("Lost connection. Waiting to reconnect..."); System.out.println("Lost connection. Waiting to reconnect...");
} }
}
} }
isConnected = false; //in case the socket accidentally disconnects (can this happen?) isConnected = false; //in case the socket accidentally disconnects (can this happen?)
} catch (InterruptedException e) { } catch (InterruptedException e) {

View File

@ -43,10 +43,12 @@ public class ServerPinger implements Runnable {
System.out.println("Connection regained!"); System.out.println("Connection regained!");
} }
} else { } else {
if (isConnected) {
isConnected = false; isConnected = false;
System.out.println("Lost connection. Waiting to reconnect..."); System.out.println("Lost connection. Waiting to reconnect...");
} }
} }
}
isConnected = false; //in case the socket accidentally disconnects (can this happen?) isConnected = false; //in case the socket accidentally disconnects (can this happen?)
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -18,4 +18,5 @@
* SEROR: Server had an error. (used for debugging) * SEROR: Server had an error. (used for debugging)
* SPING: Ping from server to client; * SPING: Ping from server to client;
* NOCMD: Co command found. * NOCMD: Co command found.
* CHATM: Incoming Chat message.
*/ */

View File

@ -21,18 +21,18 @@ public class JServerProtocolParser {
switch (header) { switch (header) {
case "CHATA": case "CHATA":
h.broadcastMessage(msg.substring(6)); h.broadcastMessage(msg.substring(6));
return; break;
case "CPING": case "CPING":
h.sendMsgToClient("PINGB"); h.sendMsgToClient("PINGB");
System.out.println("got ping!"); //todo:delete System.out.println("got ping!"); //todo:delete
return; break;
case "PINGB": case "PINGB":
h.serverPinger.setGotPingBack(true); h.serverPinger.setGotPingBack(true);
System.out.println("got pingback!"); //todo: delete System.out.println("got pingback!"); //todo: delete
return; break;
case "QUITS": case "QUITS":
h.closeEverything(h.getSocket(), h.getIn(), h.getOut()); h.closeEverything(h.getSocket(), h.getIn(), h.getOut());
return; break;
default: default:
System.out.println("Received unknown command"); System.out.println("Received unknown command");
} }