summaryrefslogtreecommitdiffstats
blob: 95da0518ed6bee3b22bc0b568d75ae9bd4bb2ad2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */
/*
 * 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);
        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);
    }

    public void handleDataRead(SafeByteArray data) {
        writeDataToParentLayer(data);
    }

    private void handleTimerTick() {
        if (timer == null) {
            return;
        }
        timer.stop();
        writeDataToChildLayer(new SafeByteArray(" "));
        timer.start();
    }

    public void setActive() {
        isActive = true;
        timer.start();
    }

    public void setInactive() {
        timer.stop();
        isActive = false;
    }

    public boolean getIsActive() {
        return isActive;
    }

    private boolean isActive;
    private Timer timer;

    private final SignalConnection onTickConnection;
}