diff options
author | Alex Clayton <alex.clayton@isode.com> | 2016-03-15 14:42:00 (GMT) |
---|---|---|
committer | Alex Clayton <alex.clayton@isode.com> | 2016-03-15 16:16:26 (GMT) |
commit | 1eddc7b6041678c47ae3e2e8e632419ae3b5e150 (patch) | |
tree | 2fb04d3ff4ab15c61fdf9474a00581a9f9813e5e /src/com | |
parent | b1ac3f64bfa7924da8bdeaf01308259018fe5148 (diff) | |
download | stroke-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
Diffstat (limited to 'src/com')
-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 @@ * See Documentation/Licenses/GPLv3.txt for more information. */ /* - * Copyright (c) 2011, Isode Limited, London, England. + * Copyright (c) 2011-2016, Isode Limited, London, England. * All rights reserved. */ package com.isode.stroke.streamstack; +import java.util.logging.Logger; + import com.isode.stroke.base.SafeByteArray; import com.isode.stroke.network.Timer; import com.isode.stroke.network.TimerFactory; +import com.isode.stroke.signals.SignalConnection; import com.isode.stroke.signals.Slot; public class WhitespacePingLayer extends StreamLayer { private static final int TIMEOUT_MILLISECONDS = 60000; + + private final Logger logger = Logger.getLogger(this.getClass().getName()); public WhitespacePingLayer(TimerFactory timerFactory) { isActive = false; timer = timerFactory.createTimer(TIMEOUT_MILLISECONDS); - timer.onTick.connect(new Slot() { + onTickConnection = timer.onTick.connect(new Slot() { public void call() { handleTimerTick(); } }); } + + @Override + protected void finalize() throws Throwable { + try { + destroy(); + } + finally { + super.finalize(); + } + } + + /** + * This replaces the C++ destructor. After calling this object should not be used again. + * If any methods are called after they may throw {@link NullPointerException} + */ + public void destroy() { + if (isActive && timer != null) { + logger.finer("WhitespacePingLayer still active at destruction"); + timer.stop(); + } + onTickConnection.disconnect(); + timer = null; + isActive = false; + } public void writeData(SafeByteArray data) { writeDataToChildLayer(data); @@ -37,6 +66,9 @@ public class WhitespacePingLayer extends StreamLayer { } private void handleTimerTick() { + if (timer == null) { + return; + } timer.stop(); writeDataToChildLayer(new SafeByteArray(" ")); timer.start(); @@ -58,4 +90,6 @@ public class WhitespacePingLayer extends StreamLayer { private boolean isActive; private Timer timer; + + private final SignalConnection onTickConnection; } |