File: IPHeartBeat.cpp

package info (click to toggle)
buteo-syncfw 0.11.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,704 kB
  • sloc: cpp: 18,406; xml: 396; sh: 34; makefile: 20
file content (155 lines) | stat: -rw-r--r-- 4,156 bytes parent folder | download
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * This file is part of buteo-syncfw package
 *
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 *
 * Contact: Sateesh Kavuri <sateesh.kavuri@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */
#include "IPHeartBeat.h"
#include <QStringList>
#include "LogMacros.h"

using namespace Buteo;

IPHeartBeat::IPHeartBeat(QObject *aParent)
    :  QObject(aParent)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
}

IPHeartBeat::~IPHeartBeat()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    removeAllWaits();
}

void IPHeartBeat::removeAllWaits()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    QStringList profNames;

    QMapIterator<QString, BeatStruct> iter(iBeatsWaiting);
    while (iter.hasNext()) {
        iter.next();
        profNames.append(iter.key());
    }

    for (int i = 0; i < profNames.size(); i++) {
        removeWait(profNames[i]);
    }
}

void IPHeartBeat::removeWait(const QString &aProfName)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    if (iBeatsWaiting.contains(aProfName) == false)
        return;

    BeatStruct &tmp = iBeatsWaiting[aProfName];

    delete tmp.sockNotifier;

    iphb_discard_wakeups(tmp.iphbHandle);
    iphb_close(tmp.iphbHandle);

    iBeatsWaiting.remove(aProfName);
}

bool IPHeartBeat::getProfNameFromFd(int aSockFd, QString &aProfName)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    bool ret = false;

    QMapIterator<QString, BeatStruct> iter(iBeatsWaiting);

    while (iter.hasNext()) {
        iter.next();
        const BeatStruct &tmp = iter.value();
        if (tmp.sockfd == aSockFd) {
            aProfName = iter.key();
            ret = true;
            break;
        }
    }

    return ret;
}

bool IPHeartBeat::setHeartBeat(const QString &aProfName, ushort aMinWaitTime, ushort aMaxWaitTime)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    if ((aMinWaitTime > aMaxWaitTime) || aProfName.isEmpty())
        return false;

    qCDebug(lcButeoMsyncd) << "setHeartBeat(), profile name = " << aProfName;

    if (iBeatsWaiting.contains(aProfName) == true) {
        qCDebug(lcButeoMsyncd) << "Profile already in waiting... No new beat";
        return true; //returing 'true' - no immediate sync request to be sent.
    }

    iphb_t iphbHandle = iphb_open(nullptr);

    if (iphbHandle == 0) {
        qCDebug(lcButeoMsyncd) << "iphb_open() failed.... No IP heartbeat available";
        return false;
    }

    int sockfd = iphb_get_fd(iphbHandle);

    if (sockfd < 0) {
        qCDebug(lcButeoMsyncd) << "iphb_get_fd() failed.... No IP heartbeat";
        iphb_close(iphbHandle);
        return false;
    }

    BeatStruct &newBeat = iBeatsWaiting[aProfName];
    newBeat.iphbHandle = iphbHandle;
    newBeat.sockfd = sockfd;
    newBeat.sockNotifier = new QSocketNotifier(sockfd, QSocketNotifier::Read, 0);

    if (iphb_wait(iphbHandle, aMinWaitTime, aMaxWaitTime, 0) == -1) {
        qCDebug(lcButeoMsyncd) << "iphb_wait() failed .... No IP heartbeat";
        removeWait(aProfName);
        return false;
    }

    connect(newBeat.sockNotifier, SIGNAL(activated(int)), this, SLOT(internalBeatTriggered(int)));

    qCDebug(lcButeoMsyncd) << "IP Heartbeat set for profile";

    return true;
}

void IPHeartBeat::internalBeatTriggered(int aSockFd)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    QString profName;

    if (getProfNameFromFd(aSockFd, profName) == true) {
        removeWait(profName);
        qCDebug(lcButeoMsyncd) << "Emitting IP  Heartbeat, profile name = " << profName;
        emit onHeartBeat(profName);
    }
}