Commit 1c54937a authored by bogdanbear's avatar bogdanbear

Detect broken connections when they occur with a thread that wakes up every 2 seconds

parent 88d6bf09
......@@ -105,6 +105,29 @@ public class LocalGameLoop extends Thread{
LOGGER.info("Released relayer");
}
/**
* a separate thread which will verify periodically whether the network connection is still alive
* wakes up every 2 seconds, thus it is not a performance issue
*/
private Runnable BrokenConnectionVerifier=new Runnable() {
@Override
public void run() {
while (true) {
if (LocalGameLoop.getInstance().isPlaying) {
//System.out.println("Broken Connection verifier!");
if (getNetworkConnection().isBroken())
GameStarter.initiateBrokenConnectionMeasures();
}
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
public synchronized boolean getIsPlaying()
{
return isPlaying;
......@@ -132,6 +155,8 @@ public class LocalGameLoop extends Thread{
@Override
public void run() {
//start the thread which verifies if the connection is still alive
(new Thread(BrokenConnectionVerifier)).start();
LOGGER.info("LocalGameLoop Thread started+"+currentThread().getId());
while (true) {
//System.out.println("DummyLocalGameLoopThread, game is "+isPlaying);
......@@ -183,14 +208,11 @@ public class LocalGameLoop extends Thread{
/**
* Entities, relayers and objects related to the GameContent should not have a direct reference to the network connection
* this is why I insert here a sendEvent method
* it is inefficient to test whether the connection is alive here
*/
public void sendEvent(NetworkObject event)
{
if (getNetworkConnection().isBroken())
GameStarter.initiateBrokenConnectionMeasures();
else {
getNetworkConnection().sendEvent(event);
}
getNetworkConnection().sendEvent(event);
}
/**
......
......@@ -44,7 +44,8 @@ public class ClientConnection extends NetworkConnection{
/**
* Send an event to the server.
* The event is written on the socket.
* Still to be implemented - throwing exceptions when the connection is not open
* Exceptions are not implemented per se, they are included into isBroken(), which can be tested before trying to read
* or write(but that is not efficient)
* @param e the event to send; e has to be serializable and not empty
*/
@Override
......
......@@ -94,7 +94,11 @@ public abstract class NetworkConnection {
}
}
//public visibility
/**
* tests whether a thread encountered a problem and updated the flag of the connection as broken
* actively tests if the connection is not broken now
* @return a boolean representing whether the connection is broken or not
*/
public boolean isBroken() {
//attempt to send a test event first
//must not be sunchronized because broken might need to be updated when we attempt to send a test event
......@@ -104,6 +108,16 @@ public abstract class NetworkConnection {
return broken;
}
/**
* like isBroken(), but does not do an active test and just looks at whether a thread encountered a problem
* this could be an alternative to using isBroken and the timer
* by calling this every time before sending or receiving an event
* @return a boolean with the value of broken
*/
public boolean isSoftBroken(){
return broken;
}
//package visibility
synchronized void setBroken(boolean broken) {
this.broken = broken;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment