summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Clayton <alex.clayton@isode.com>2016-03-15 14:42:00 (GMT)
committerAlex Clayton <alex.clayton@isode.com>2016-03-15 16:16:26 (GMT)
commit1eddc7b6041678c47ae3e2e8e632419ae3b5e150 (patch)
tree2fb04d3ff4ab15c61fdf9474a00581a9f9813e5e
parentb1ac3f64bfa7924da8bdeaf01308259018fe5148 (diff)
downloadstroke-1eddc7b6041678c47ae3e2e8e632419ae3b5e150.zip
stroke-1eddc7b6041678c47ae3e2e8e632419ae3b5e150.tar.bz2
Add destroy method to WhitespacePingLayer.
Add a destroy method to the WhitespacePingLayer to act like the destructor introduced in swiften patch 'Stop timer during clean up of WhitespacePingLayer' (f377207cb896679b4eab9f6773d9d071700852ad). Test-information: Unit tests still pass ok. Change-Id: I4fca19c3d3b5f120102727db10b046904da848a4
-rw-r--r--src/com/isode/stroke/streamstack/WhitespacePingLayer.java38
1 files changed, 36 insertions, 2 deletions
diff --git a/src/com/isode/stroke/streamstack/WhitespacePingLayer.java b/src/com/isode/stroke/streamstack/WhitespacePingLayer.java
index 3a1805c..95da051 100644
--- a/src/com/isode/stroke/streamstack/WhitespacePingLayer.java
+++ b/src/com/isode/stroke/streamstack/WhitespacePingLayer.java
@@ -4,29 +4,58 @@
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) 2011, Isode Limited, London, England. 7 * Copyright (c) 2011-2016, Isode Limited, London, England.
8 * All rights reserved. 8 * All rights reserved.
9 */ 9 */
10package com.isode.stroke.streamstack; 10package com.isode.stroke.streamstack;
11 11
12import java.util.logging.Logger;
13
12import com.isode.stroke.base.SafeByteArray; 14import com.isode.stroke.base.SafeByteArray;
13import com.isode.stroke.network.Timer; 15import com.isode.stroke.network.Timer;
14import com.isode.stroke.network.TimerFactory; 16import com.isode.stroke.network.TimerFactory;
17import com.isode.stroke.signals.SignalConnection;
15import com.isode.stroke.signals.Slot; 18import com.isode.stroke.signals.Slot;
16 19
17public class WhitespacePingLayer extends StreamLayer { 20public class WhitespacePingLayer extends StreamLayer {
18 21
19 private static final int TIMEOUT_MILLISECONDS = 60000; 22 private static final int TIMEOUT_MILLISECONDS = 60000;
23
24 private final Logger logger = Logger.getLogger(this.getClass().getName());
20 25
21 public WhitespacePingLayer(TimerFactory timerFactory) { 26 public WhitespacePingLayer(TimerFactory timerFactory) {
22 isActive = false; 27 isActive = false;
23 timer = timerFactory.createTimer(TIMEOUT_MILLISECONDS); 28 timer = timerFactory.createTimer(TIMEOUT_MILLISECONDS);
24 timer.onTick.connect(new Slot() { 29 onTickConnection = timer.onTick.connect(new Slot() {
25 public void call() { 30 public void call() {
26 handleTimerTick(); 31 handleTimerTick();
27 } 32 }
28 }); 33 });
29 } 34 }
35
36 @Override
37 protected void finalize() throws Throwable {
38 try {
39 destroy();
40 }
41 finally {
42 super.finalize();
43 }
44 }
45
46 /**
47 * This replaces the C++ destructor. After calling this object should not be used again.
48 * If any methods are called after they may throw {@link NullPointerException}
49 */
50 public void destroy() {
51 if (isActive && timer != null) {
52 logger.finer("WhitespacePingLayer still active at destruction");
53 timer.stop();
54 }
55 onTickConnection.disconnect();
56 timer = null;
57 isActive = false;
58 }
30 59
31 public void writeData(SafeByteArray data) { 60 public void writeData(SafeByteArray data) {
32 writeDataToChildLayer(data); 61 writeDataToChildLayer(data);
@@ -37,6 +66,9 @@ public class WhitespacePingLayer extends StreamLayer {
37 } 66 }
38 67
39 private void handleTimerTick() { 68 private void handleTimerTick() {
69 if (timer == null) {
70 return;
71 }
40 timer.stop(); 72 timer.stop();
41 writeDataToChildLayer(new SafeByteArray(" ")); 73 writeDataToChildLayer(new SafeByteArray(" "));
42 timer.start(); 74 timer.start();
@@ -58,4 +90,6 @@ public class WhitespacePingLayer extends StreamLayer {
58 90
59 private boolean isActive; 91 private boolean isActive;
60 private Timer timer; 92 private Timer timer;
93
94 private final SignalConnection onTickConnection;
61} 95}