diff options
| -rw-r--r-- | src/com/isode/stroke/streamstack/WhitespacePingLayer.java | 38 |
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 | */ |
| 10 | package com.isode.stroke.streamstack; | 10 | package com.isode.stroke.streamstack; |
| 11 | 11 | ||
| 12 | import java.util.logging.Logger; | ||
| 13 | |||
| 12 | import com.isode.stroke.base.SafeByteArray; | 14 | import com.isode.stroke.base.SafeByteArray; |
| 13 | import com.isode.stroke.network.Timer; | 15 | import com.isode.stroke.network.Timer; |
| 14 | import com.isode.stroke.network.TimerFactory; | 16 | import com.isode.stroke.network.TimerFactory; |
| 17 | import com.isode.stroke.signals.SignalConnection; | ||
| 15 | import com.isode.stroke.signals.Slot; | 18 | import com.isode.stroke.signals.Slot; |
| 16 | 19 | ||
| 17 | public class WhitespacePingLayer extends StreamLayer { | 20 | public 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 | } |
Swift