Skip to content

Commit

Permalink
WebsocketTransport: ensure that the timer exists always, not only whe…
Browse files Browse the repository at this point in the history
…n the activity timer is running, since the timer is now also used for WsClient event callbacks
  • Loading branch information
paddybyers committed Nov 11, 2019
1 parent e71c7eb commit 9bd0885
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions lib/src/main/java/io/ably/lib/transport/WebSocketTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,24 +260,36 @@ public void run() {
});
}

private void dispose() {
private synchronized void dispose() {
/* dispose timer */
if(timer != null) {
try {
timer.cancel();
timer = null;
}
} catch(IllegalStateException e) {}
}

private void flagActivity() {
private synchronized void flagActivity() {
lastActivityTime = System.currentTimeMillis();
connectionManager.setLastActivity(lastActivityTime);
if (activityTimerTask == null && connectionManager.maxIdleInterval != 0) {
/* No timer currently running because previously there was no
* maxIdleInterval configured, but now there is a
* maxIdleInterval configured. Call checkActivity so a timer
* gets started. This happens when flagActivity gets called
* just after processing the connect message that configures
* maxIdleInterval. */
checkActivity();
}
}

private void checkActivity() {
private synchronized void checkActivity() {
long timeout = connectionManager.maxIdleInterval;
if (timeout == 0) {
Log.v(TAG, "checkActivity: infinite timeout");
timer = null;
return;
}
if(activityTimerTask != null) {
/* timer already running */
return;
}
timeout += connectionManager.ably.options.realtimeRequestTimeout;
Expand All @@ -288,15 +300,15 @@ private void checkActivity() {
* of inactivity. Schedule a new timer for that long after the
* last activity time. */
Log.v(TAG, "checkActivity: ok");
schedule(new TimerTask() {
schedule((activityTimerTask = new TimerTask() {
public void run() {
try {
checkActivity();
} catch(Throwable t) {
Log.e(TAG, "Unexpected exception in activity timer handler", t);
}
}
}, next - now);
}), next - now);
} else {
/* Timeout has been reached. Close the connection. */
Log.e(TAG, "No activity for " + timeout + "ms, closing connection");
Expand All @@ -308,11 +320,13 @@ private void schedule(TimerTask task) {
schedule(task, 0);
}

private void schedule(TimerTask task, long delay) {
try {
timer.schedule(task, delay);
} catch(IllegalStateException ise) {
Log.e(TAG, "Unexpected exception scheduling activity timer", ise);
private synchronized void schedule(TimerTask task, long delay) {
if(timer != null) {
try {
timer.schedule(task, delay);
} catch(IllegalStateException ise) {
Log.e(TAG, "Unexpected exception scheduling activity timer", ise);
}
}
}

Expand All @@ -321,6 +335,7 @@ private void schedule(TimerTask task, long delay) {
***************************/

private Timer timer = new Timer();
private TimerTask activityTimerTask = null;
private long lastActivityTime;
}

Expand Down

0 comments on commit 9bd0885

Please sign in to comment.