summaryrefslogtreecommitdiffstats
blob: 9a473f3acb38c6cefc7a00fcb03b14e3a14f4c4c (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
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/StreamStack/WhitespacePingLayer.h>

#include <boost/bind.hpp>

#include <Swiften/Base/Log.h>
#include <Swiften/Network/Timer.h>
#include <Swiften/Network/TimerFactory.h>

namespace Swift {

static const int TIMEOUT_MILLISECONDS = 60000;

WhitespacePingLayer::WhitespacePingLayer(TimerFactory* timerFactory) : isActive(false) {
    timer = timerFactory->createTimer(TIMEOUT_MILLISECONDS);
    timer->onTick.connect(boost::bind(&WhitespacePingLayer::handleTimerTick, this));
}

WhitespacePingLayer::~WhitespacePingLayer() {
    SWIFT_LOG_ASSERT(!isActive, debug) << "WhitespacePingLayer still active at destruction." << std::endl;
    if (isActive) {
        timer->stop();
    }
    timer->onTick.disconnect(boost::bind(&WhitespacePingLayer::handleTimerTick, this));
}

void WhitespacePingLayer::writeData(const SafeByteArray& data) {
    writeDataToChildLayer(data);
}

void WhitespacePingLayer::handleDataRead(const SafeByteArray& data) {
    writeDataToParentLayer(data);
}

void WhitespacePingLayer::handleTimerTick() {
    timer->stop();
    writeDataToChildLayer(createSafeByteArray(" "));
    timer->start();
}

void WhitespacePingLayer::setActive() {
    isActive = true;
    timer->start();
}

void WhitespacePingLayer::setInactive() {
    timer->stop();
    isActive = false;
}

}