Working on JClientProtocolParser
This commit is contained in:
parent
ac11bc4991
commit
146ff4ea82
@ -1,5 +1,6 @@
|
||||
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 java.net.Socket;
|
||||
@ -13,6 +14,7 @@ public class Client {
|
||||
private BufferedReader in;
|
||||
private BufferedWriter out;
|
||||
public String userName;
|
||||
public ClientPinger clientPinger;
|
||||
|
||||
public Client(Socket socket, String userName) {
|
||||
try {
|
||||
@ -26,6 +28,7 @@ public class Client {
|
||||
this.out.write(getUsername());
|
||||
this.out.newLine();
|
||||
this.out.flush();
|
||||
clientPinger = new ClientPinger(this.out, this.socket);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
closeEverything(socket, in, out);
|
||||
@ -72,12 +75,13 @@ public class Client {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
String chatMsg;
|
||||
|
||||
while (socket.isConnected()) {
|
||||
try {
|
||||
chatMsg = in.readLine();
|
||||
System.out.println(chatMsg);
|
||||
parse(chatMsg);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
closeEverything(socket, in, out);
|
||||
@ -88,6 +92,21 @@ public class Client {
|
||||
}).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) {
|
||||
//TODO Correctly closing a clients connection
|
||||
//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.chatListener();
|
||||
client.sendMessage();
|
||||
Thread cP = new Thread(client.clientPinger);
|
||||
System.out.println("im here");
|
||||
cP.start();
|
||||
} catch (UnknownHostException e) {
|
||||
System.out.println("Invalid host IP");
|
||||
} catch (IOException e) {
|
||||
|
||||
@ -18,23 +18,16 @@ public class JClientProtocolParser {
|
||||
}
|
||||
System.out.println(header);
|
||||
switch (header) {
|
||||
|
||||
case "CPING":
|
||||
h.sendMsgToClient("PINGB");
|
||||
case "SPING":
|
||||
c.sendMsgToServer("PINGB");
|
||||
System.out.println("got ping!"); //todo:delete
|
||||
return;
|
||||
break;
|
||||
case "PINGB":
|
||||
h.serverPinger.setGotPingBack(true);
|
||||
c.clientPinger.setGotPingBack(true);
|
||||
System.out.println("got pingback!"); //todo: delete
|
||||
return;
|
||||
case "QUITS":
|
||||
h.closeEverything(h.getSocket(), h.getIn(), h.getOut());
|
||||
return;
|
||||
break;
|
||||
default:
|
||||
System.out.println("Received unknown command");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -43,10 +43,11 @@ public class ClientPinger implements Runnable {
|
||||
System.out.println("Connection regained!");
|
||||
}
|
||||
} else {
|
||||
isConnected = false;
|
||||
System.out.println("Lost connection. Waiting to reconnect...");
|
||||
if (isConnected) {
|
||||
isConnected = false;
|
||||
System.out.println("Lost connection. Waiting to reconnect...");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
isConnected = false; //in case the socket accidentally disconnects (can this happen?)
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
@ -43,8 +43,10 @@ public class ServerPinger implements Runnable {
|
||||
System.out.println("Connection regained!");
|
||||
}
|
||||
} else {
|
||||
isConnected = false;
|
||||
System.out.println("Lost connection. Waiting to reconnect...");
|
||||
if (isConnected) {
|
||||
isConnected = false;
|
||||
System.out.println("Lost connection. Waiting to reconnect...");
|
||||
}
|
||||
}
|
||||
}
|
||||
isConnected = false; //in case the socket accidentally disconnects (can this happen?)
|
||||
|
||||
@ -18,4 +18,5 @@
|
||||
* SEROR: Server had an error. (used for debugging)
|
||||
* SPING: Ping from server to client;
|
||||
* NOCMD: Co command found.
|
||||
* CHATM: Incoming Chat message.
|
||||
*/
|
||||
@ -21,18 +21,18 @@ public class JServerProtocolParser {
|
||||
switch (header) {
|
||||
case "CHATA":
|
||||
h.broadcastMessage(msg.substring(6));
|
||||
return;
|
||||
break;
|
||||
case "CPING":
|
||||
h.sendMsgToClient("PINGB");
|
||||
System.out.println("got ping!"); //todo:delete
|
||||
return;
|
||||
break;
|
||||
case "PINGB":
|
||||
h.serverPinger.setGotPingBack(true);
|
||||
System.out.println("got pingback!"); //todo: delete
|
||||
return;
|
||||
break;
|
||||
case "QUITS":
|
||||
h.closeEverything(h.getSocket(), h.getIn(), h.getOut());
|
||||
return;
|
||||
break;
|
||||
default:
|
||||
System.out.println("Received unknown command");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user