ClientPinger and ServerPinger "merged" from the ping branch

This commit is contained in:
Jonas 2022-03-25 12:39:20 +01:00
parent 30986d2d86
commit 7f967f6818
6 changed files with 25 additions and 48 deletions

View File

@ -30,9 +30,21 @@ public class ClientPinger implements Runnable{
public void run() {
try {
while (socket.isConnected()) {
gotPingBack = false;
out.write("CPING");
out.newLine();
out.flush();
Thread.sleep(2000);
if (!gotPingBack) isConnected = false;
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) {

View File

@ -1,8 +0,0 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.helpers;
public class PingListener implements Runnable {
@Override
public void run() {
}
}

View File

@ -1,24 +0,0 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.helpers;
/**
* PingPong offers services to for listening and sending
* Pings to a communication partner.
* Runs on a Thread as not to disturb other communication channels.(Is this necessary?)
*/
public class PingPong implements PingPongInterface {
@Override
public void pingListener(String ping) {
}
@Override
public String pongSender() {
return null;
}
@Override
public void run() {
}
//TODO: Impl
}

View File

@ -1,6 +0,0 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.helpers;
public interface PingPongInterface extends Runnable{
void pingListener(String ping);
String pongSender();
}

View File

@ -1,8 +0,0 @@
package ch.unibas.dmi.dbis.cs108.Multiplayer.helpers;
public class PongSender implements Runnable{
@Override
public void run() {
}
}

View File

@ -30,9 +30,20 @@ public class ServerPinger implements Runnable{
public void run() {
try {
while (socket.isConnected()) {
gotPingBack = false;
out.write("SPING");
out.newLine();
out.flush();
Thread.sleep(2000);
if (!gotPingBack) isConnected = false;
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) {