summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Clayton <alex.clayton@isode.com>2018-03-16 14:12:08 (GMT)
committerAlex Clayton <alex.clayton@isode.com>2018-03-21 09:27:34 (GMT)
commit17a1e7662102eea20ddc104133e2e30a70fefdc1 (patch)
treeb5d75cd518fe6924bfa4b3a5908bc95b7faf4e34
parentbf4830bc9f51a27aba59bc5708caddb835e84789 (diff)
downloadstroke-17a1e7662102eea20ddc104133e2e30a70fefdc1.zip
stroke-17a1e7662102eea20ddc104133e2e30a70fefdc1.tar.bz2
Ensure JavaTimer.stop() cleans up any unresolved events
The code for stop() in JavaTimer read: @Override public void stop() { timer_.stop(); //FIXME: This needs to clear any remaining events out of the EventLoop queue. } Where as the equivalent swiften code in BoostTimer was: void BoostTimer::stop() { { std::unique_lock<std::mutex> lockTimer(timerMutex); shuttingDown = true; timer->cancel(); eventLoop->removeEventsFromOwner(shared_from_this()); } } This patch updates the java code to bring it inline with the swiften code. In short to make sure it removes any remaining events from the event loop when it is stopped. Test-information: Code Inspection. Unit test still pass. Ran update stroke against M-Link Console it ran ok. Change-Id: Idf92e92d002b8404547702d1c34738165e331810
-rw-r--r--src/com/isode/stroke/network/JavaTimer.java47
1 files changed, 25 insertions, 22 deletions
diff --git a/src/com/isode/stroke/network/JavaTimer.java b/src/com/isode/stroke/network/JavaTimer.java
index ac9e219..2baae3d 100644
--- a/src/com/isode/stroke/network/JavaTimer.java
+++ b/src/com/isode/stroke/network/JavaTimer.java
@@ -4,21 +4,20 @@
4 * See Documentation/Licenses/GPLv3.txt for more information. 4 * See Documentation/Licenses/GPLv3.txt for more information.
5 */ 5 */
6/* 6/*
7 * Copyright (c) 2010, Isode Limited, London, England. 7 * Copyright (c) 2010-2018, Isode Limited, London, England.
8 * All rights reserved. 8 * All rights reserved.
9 */ 9 */
10package com.isode.stroke.network; 10package com.isode.stroke.network;
11 11
12import java.util.Date;
13
14import com.isode.stroke.eventloop.Event; 12import com.isode.stroke.eventloop.Event;
15import com.isode.stroke.eventloop.EventLoop; 13import com.isode.stroke.eventloop.EventLoop;
14import com.isode.stroke.eventloop.EventOwner;
16 15
17class JavaTimer extends Timer { 16class JavaTimer extends Timer {
18 17
19 private class TimerRunnable implements Runnable { 18 private class TimerRunnable implements Runnable,EventOwner {
20 19
21 boolean running_ = true; 20 private boolean running_ = true;
22 private final EventLoop eventLoop_; 21 private final EventLoop eventLoop_;
23 private final long milliseconds_; 22 private final long milliseconds_;
24 23
@@ -28,25 +27,27 @@ class JavaTimer extends Timer {
28 } 27 }
29 28
30 public void run() { 29 public void run() {
31 long endTime = new Date().getTime() + milliseconds_; 30 long endTime = System.currentTimeMillis() + milliseconds_;
32 while (shouldEmit() && new Date().getTime() < endTime) { 31 while (shouldEmit() && System.currentTimeMillis() < endTime) {
33 try { 32 try {
34 long timeToWait = endTime - new Date().getTime(); 33 long timeToWait = endTime - System.currentTimeMillis();
35 if (timeToWait > 0) { 34 if (timeToWait > 0) {
36 Thread.sleep(milliseconds_); 35 Thread.sleep(timeToWait);
37 } 36 }
38 } catch (InterruptedException ex) { 37 } catch (InterruptedException ex) {
39 // Needs to be caught, but won't break out of the loop 38 // Needs to be caught, but won't break out of the loop
40 // unless end time reached or stop() has been called. 39 // unless end time reached or stop() has been called.
41 } 40 }
42 } 41 }
43 if (shouldEmit()) { 42 synchronized(this) {
44 eventLoop_.postEvent(new Event.Callback() { 43 if (shouldEmit()) {
45 public void run() { 44 eventLoop_.postEvent(new Event.Callback() {
46 onTick.emit(); 45 public void run() {
47 } 46 onTick.emit();
48 }); 47 }
49 } 48 },this);
49 }
50 }
50 } 51 }
51 52
52 53
@@ -56,6 +57,7 @@ class JavaTimer extends Timer {
56 57
57 public synchronized void stop() { 58 public synchronized void stop() {
58 running_ = false; 59 running_ = false;
60 timer_.eventLoop_.removeEventsFromOwner(this);
59 } 61 }
60 } 62 }
61 63
@@ -85,14 +87,15 @@ class JavaTimer extends Timer {
85 @Override 87 @Override
86 public void stop() { 88 public void stop() {
87 timer_.stop(); 89 timer_.stop();
88 //FIXME: This needs to clear any remaining events out of the EventLoop queue.
89 } 90 }
90 91
91 @Override 92 @Override
92 public String toString() { 93 public String toString() {
93 return "JavaTimer for " + timer_.milliseconds_ + 94 synchronized (timer_) {
94 " milliseconds " + 95 return "JavaTimer for " + timer_.milliseconds_ +
95 (timer_.running_ ? "running" : "not running"); 96 " milliseconds " +
97 (timer_.running_ ? "running" : "not running");
98 }
96 } 99 }
97 private final TimerRunnable timer_; 100 private final TimerRunnable timer_;
98} 101}