diff --git a/java_pid21340.hprof b/java_pid21340.hprof new file mode 100644 index 0000000..5d5f257 Binary files /dev/null and b/java_pid21340.hprof differ diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/ClientPinger.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/ClientPinger.java index a5f764d..0d8885a 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/ClientPinger.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/ClientPinger.java @@ -5,62 +5,64 @@ import java.io.IOException; import java.net.Socket; /** - * Sends a ping to the server ("CPING") every 2 seconds and checks if it has gotten a pingback. - * The actual logging of the pingback (via the gotPingBack boolean) has to be done elsewhere, - * depends on how the client receives and parses messages. + * Sends a ping to the server ("CPING") every 2 seconds and checks if it has gotten a pingback. The + * actual logging of the pingback (via the gotPingBack boolean) has to be done elsewhere, depends on + * how the client receives and parses messages. */ -public class ClientPinger implements Runnable{ - private boolean gotPingBack; //should be set to true (via setGotPingBack) as soon as the client gets a pingback. - private boolean isConnected; //set to true unless the ClientPinger detects a connection loss. - BufferedWriter out; //the output of this client through which the pings are sent - private Socket socket; +public class ClientPinger implements Runnable { - /** - * @param socket the socket the Client is connected to which is used to end the thread if the connection is lost. - * @param out the output through which the pings are sent. - */ - public ClientPinger(BufferedWriter out, Socket socket) { + private boolean gotPingBack; //should be set to true when client gets a pingback. + private boolean isConnected; //set to true unless the ClientPinger detects a connection loss. + BufferedWriter out; //the output of this client through which the pings are sent + private Socket socket; + + /** + * @param socket the socket the Client is connected to which is used to end the thread if the + * connection is lost. + * @param out the output through which the pings are sent. + */ + public ClientPinger(BufferedWriter out, Socket socket) { + gotPingBack = false; + isConnected = true; + this.out = out; + this.socket = socket; + } + + @Override + public void run() { + try { + while (socket.isConnected()) { gotPingBack = false; - isConnected = true; - this.out = out; - this.socket = socket; - } - - @Override - public void run() { - try { - while (socket.isConnected()) { - gotPingBack = false; - out.write("CPING"); - out.newLine(); - out.flush(); - Thread.sleep(2000); - if (gotPingBack) { - if (!isConnected) { //if !isConnected, then the connection had been lost before. - isConnected = true; - System.out.println("Connection regained!"); - } - } else { - isConnected = false; - System.out.println("Lost connection. Waiting to reconnect..."); - } - - } - isConnected = false; //in case the socket accidentally disconnects (can this happen?) - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + out.write("CPING"); + out.newLine(); + out.flush(); + Thread.sleep(2000); + if (gotPingBack) { + if (!isConnected) { //if !isConnected, then the connection had been lost before. + isConnected = true; + System.out.println("Connection regained!"); + } + } else { + isConnected = false; + System.out.println("Lost connection. Waiting to reconnect..."); } - } - public void setGotPingBack(boolean gotPingBack) { - this.gotPingBack = gotPingBack; + } + isConnected = false; //in case the socket accidentally disconnects (can this happen?) + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); } + } - public boolean isConnected() { - return isConnected; - } + public void setGotPingBack(boolean gotPingBack) { + this.gotPingBack = gotPingBack; + } + + public boolean isConnected() { + return isConnected; + } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/ServerPinger.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/ServerPinger.java index fcc767e..f641010 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/ServerPinger.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/ServerPinger.java @@ -5,61 +5,63 @@ import java.io.IOException; import java.net.Socket; /** - * Sends a ping to the client ("SPING") every 2 seconds and checks if it has gotten a pingback. - * The actual logging of the pingback (via the gotPingBack boolean) has to be done elsewhere, - * depends on how the server receives and parses messages. + * Sends a ping to the client ("SPING") every 2 seconds and checks if it has gotten a pingback. The + * actual logging of the pingback (via the gotPingBack boolean) has to be done elsewhere, depends on + * how the server receives and parses messages. */ -public class ServerPinger implements Runnable{ - private boolean gotPingBack; //should be set to true (via setGotPingBack) as soon as the server gets a pingback. - private boolean isConnected; //set to true unless the ServerPinger detects a connection loss. - BufferedWriter out; //the output of this client through which the pings are sent - private Socket socket; +public class ServerPinger implements Runnable { - /** - * @param socket the socket the ClientHandler is connected to; used to end the thread if the connection is lost. - * @param out the output through which the pings are sent. - */ - public ServerPinger(BufferedWriter out, Socket socket) { + private boolean gotPingBack; //should be set to true (via setGotPingBack) as soon as the server gets a pingback. + private boolean isConnected; //set to true unless the ServerPinger detects a connection loss. + BufferedWriter out; //the output of this client through which the pings are sent + private Socket socket; + + /** + * @param socket the socket the ClientHandler is connected to; used to end the thread if the + * connection is lost. + * @param out the output through which the pings are sent. + */ + public ServerPinger(BufferedWriter out, Socket socket) { + gotPingBack = false; + isConnected = true; + this.out = out; + this.socket = socket; + } + + @Override + public void run() { + try { + while (socket.isConnected()) { gotPingBack = false; - isConnected = true; - this.out = out; - this.socket = socket; - } - - @Override - public void run() { - try { - while (socket.isConnected()) { - gotPingBack = false; - out.write("SPING"); - out.newLine(); - out.flush(); - Thread.sleep(2000); - if (gotPingBack) { - if (!isConnected) { //if !isConnected, then the connection had been lost before. - isConnected = true; - System.out.println("Connection regained!"); - } - } else { - isConnected = false; - System.out.println("Lost connection. Waiting to reconnect..."); - } - } - isConnected = false; //in case the socket accidentally disconnects (can this happen?) - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + out.write("SPING"); + out.newLine(); + out.flush(); + Thread.sleep(2000); + if (gotPingBack) { + if (!isConnected) { //if !isConnected, then the connection had been lost before. + isConnected = true; + System.out.println("Connection regained!"); + } + } else { + isConnected = false; + System.out.println("Lost connection. Waiting to reconnect..."); } + } + isConnected = false; //in case the socket accidentally disconnects (can this happen?) + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); } + } - public void setGotPingBack(boolean gotPingBack) { - this.gotPingBack = gotPingBack; - } + public void setGotPingBack(boolean gotPingBack) { + this.gotPingBack = gotPingBack; + } - public boolean isConnected() { - return isConnected; - } + public boolean isConnected() { + return isConnected; + } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/protocol/NTtB_Protocol_Definition.txt b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/protocol/NTtB_Protocol_Definition.txt index d74795a..e694e0f 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/protocol/NTtB_Protocol_Definition.txt +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/protocol/NTtB_Protocol_Definition.txt @@ -7,14 +7,14 @@ * LEAVG: leave a game * JOING: join a game * VOTEG: ghost voting who to infect - * VOTEH: humans voting whos the ghost - * QUITS: quit server/ leave servr + * VOTEH: humans voting who is the ghost + * QUITS: quit server/ leave server * LISTP: list players/clients in session with the Server * CPING: Ping from client to server. */ /** Server Commands: - * MSGRS: "Message recieved": Paramaters: a string detailing to the client that and what the server recieved as command. + * MSGRS: "Message received": Paramaters: a string detailing to the client that and what the server received as command. * SEROR: Server had an error. (used for debugging) * SPING: Ping from server to client; */ \ No newline at end of file diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/Server.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/Server.java index 4939f6b..31b72ba 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/Server.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/Server.java @@ -8,54 +8,54 @@ import java.util.Scanner; public class Server { - private static final int gamePort = 42069; - private HashSet connectedClients = new HashSet<>(); - private ServerSocket serverSocket; - Scanner sc = new Scanner(System.in); + private static final int gamePort = 42069; + private HashSet connectedClients = new HashSet<>(); + private ServerSocket serverSocket; + Scanner sc = new Scanner(System.in); + + public Server(ServerSocket serverSocket) { + this.serverSocket = serverSocket; + } + + public void startServer() { + try { + System.out.println("Port 42069 is open on " + this.serverSocket.getInetAddress()); + while (!serverSocket.isClosed()) { + Socket socket = serverSocket.accept(); + ClientHandler nextClient = new ClientHandler(socket); + Thread th = new Thread(nextClient); + connectedClients.add(nextClient); + th.start(); + + } + } catch (IOException e) { + e.printStackTrace(); - public Server(ServerSocket serverSocket) { - this.serverSocket = serverSocket; } + } - public void startServer() { - try { - System.out.println("Port 42069 is open on " + this.serverSocket.getInetAddress()); - while (!serverSocket.isClosed()) { - Socket socket = serverSocket.accept(); - ClientHandler nextClient = new ClientHandler(socket); - Thread th = new Thread(nextClient); - connectedClients.add(nextClient); - th.start(); - - } - } catch (IOException e) { - e.printStackTrace(); - - } + public void closeServerSocket() { + try { + if (serverSocket != null) { + serverSocket.close(); + } + } catch (IOException e) { + e.printStackTrace(); } + } - public void closeServerSocket() { - try { - if (serverSocket != null){ - serverSocket.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } + public static void main(String[] args) { + ServerSocket serverSocket = null; + try { + serverSocket = new ServerSocket(gamePort); + } catch (IOException e) { + e.printStackTrace(); } + Server server = new Server(serverSocket); + server.startServer(); + } - public static void main(String[] args) { - ServerSocket serverSocket = null; - try { - serverSocket = new ServerSocket(gamePort); - } catch (IOException e) { - e.printStackTrace(); - } - Server server = new Server(serverSocket); - server.startServer(); - } - - public static void broadcast(String msg){ - //TODO - } + public static void broadcast(String msg) { + //TODO + } }