ClientPinger and ServerPinger "merged" from the ping branch
This commit is contained in:
parent
30986d2d86
commit
7f967f6818
@ -30,9 +30,21 @@ public class ClientPinger implements Runnable{
|
|||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
while (socket.isConnected()) {
|
while (socket.isConnected()) {
|
||||||
|
gotPingBack = false;
|
||||||
out.write("CPING");
|
out.write("CPING");
|
||||||
|
out.newLine();
|
||||||
|
out.flush();
|
||||||
Thread.sleep(2000);
|
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?)
|
isConnected = false; //in case the socket accidentally disconnects (can this happen?)
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|||||||
@ -1,8 +0,0 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.Multiplayer.helpers;
|
|
||||||
|
|
||||||
public class PingListener implements Runnable {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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
|
|
||||||
}
|
|
||||||
@ -1,6 +0,0 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.Multiplayer.helpers;
|
|
||||||
|
|
||||||
public interface PingPongInterface extends Runnable{
|
|
||||||
void pingListener(String ping);
|
|
||||||
String pongSender();
|
|
||||||
}
|
|
||||||
@ -1,8 +0,0 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.Multiplayer.helpers;
|
|
||||||
|
|
||||||
public class PongSender implements Runnable{
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -30,9 +30,20 @@ public class ServerPinger implements Runnable{
|
|||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
while (socket.isConnected()) {
|
while (socket.isConnected()) {
|
||||||
|
gotPingBack = false;
|
||||||
out.write("SPING");
|
out.write("SPING");
|
||||||
|
out.newLine();
|
||||||
|
out.flush();
|
||||||
Thread.sleep(2000);
|
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?)
|
isConnected = false; //in case the socket accidentally disconnects (can this happen?)
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user