File: cluster.cpp

package info (click to toggle)
wfview 2.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,256 kB
  • sloc: cpp: 43,386; ansic: 3,196; sh: 32; xml: 29; makefile: 11
file content (341 lines) | stat: -rw-r--r-- 12,202 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "cluster.h"
#include "logcategories.h"


dxClusterClient::dxClusterClient(QObject* parent):
    QObject(parent)
{
    qInfo(logCluster()) << "starting dxClusterClient()";
}

dxClusterClient::~dxClusterClient()
{
    qInfo(logCluster()) << "closing dxClusterClient()";
    enableUdp(false);
    enableTcp(false);
#ifdef USESQL
    database db;
    db.close();
#else
    QMap<QString, spotData*>::iterator spot = allSpots.begin();
    while (spot != allSpots.end())
    {
        delete spot.value(); // Stop memory leak?
        spot = allSpots.erase(spot);
    }
#endif
}

void dxClusterClient::enableUdp(bool enable)
{
    udpEnable = enable;
    if (enable)
    {
        if (udpSocket == Q_NULLPTR)
        {
            udpSocket = new QUdpSocket(this);
            bool result = udpSocket->bind(QHostAddress::AnyIPv4, udpPort);
            qInfo(logCluster()) << "Starting udpSocket() on:" << udpPort << "Result:" << result;

            connect(udpSocket, SIGNAL(readyRead()), this, SLOT(udpDataReceived()), Qt::QueuedConnection);
        }
    }
    else {
        if (udpSocket != Q_NULLPTR)
        {
            qInfo(logCluster()) << "Stopping udpSocket() on:" << udpPort;

            udpSocket->disconnect();
            delete udpSocket;
            udpSocket = Q_NULLPTR;
        }
    }
}

void dxClusterClient::enableTcp(bool enable)
{
    tcpEnable = enable;
    if (enable)
    {
        //tcpRegex = QRegularExpression("^DX de ([a-z-|A-Z|0-9|#|/]+):\\s+([0-9|.]+)\\s+([a-z|A-Z|0-9|/]+)+\\s+(.*)\\s+(\\d{4}Z)");
        tcpRegex = QRegularExpression("^DX de ([A-Z|0-9|\\/\\-#]{3,}): +(\\d*.\\d{1,2}) +([A-Z|0-9|\\/]{3,}) +(.{1,})? +(\\d{4})Z");
        tcpRegex.setPatternOptions(QRegularExpression::CaseInsensitiveOption | QRegularExpression::MultilineOption);

        if (tcpSocket == Q_NULLPTR)
        {
            tcpSocket = new QTcpSocket(this);
            tcpSocket->connectToHost(tcpServerName, tcpPort);
            qInfo(logCluster()) << "Starting tcpSocket() on:" << tcpPort;
            emit sendOutput(QString("\nConnecting to %0 %1\n\n").arg(tcpServerName).arg(tcpPort));

            connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(tcpDataReceived()), Qt::QueuedConnection);
            connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(tcpDisconnected()));

            tcpCleanupTimer = new QTimer(this);
            tcpCleanupTimer->setInterval(1000 * 10); // Run once a minute
            connect(tcpCleanupTimer, SIGNAL(timeout()), this, SLOT(tcpCleanup()));
            tcpCleanupTimer->start();
            authenticated = false;
        }
    }
    else {
        if (tcpSocket != Q_NULLPTR)
        {
            sendTcpData(QString("bye\n"));
            qInfo(logCluster()) << "Disconnecting tcpSocket() on:" << tcpPort;
            emit sendOutput(QString("\nDisconnecting from %0 %1\n").arg(tcpServerName).arg(tcpPort));
            if (tcpCleanupTimer != Q_NULLPTR)
            {
                tcpCleanupTimer->stop();
                delete tcpCleanupTimer;
                tcpCleanupTimer = Q_NULLPTR;
            }
            tcpSocket->disconnect();
            delete tcpSocket;
            tcpSocket = Q_NULLPTR;
        }
    }
}

void dxClusterClient::udpDataReceived()
{
    QHostAddress sender;
    quint16 port;
    QByteArray datagram;
    datagram.resize(udpSocket->pendingDatagramSize());
    udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &port);

    if (udpSpotReader.setContent(datagram))
    {
        QDomElement spot = udpSpotReader.firstChildElement("spot");
        if (spot.nodeName() == "spot")
        {
            // This is a spot?
            QString action = spot.firstChildElement("action").text();
            if (action == "add") {
                spotData* data = new spotData();
                data->dxcall = spot.firstChildElement("dxcall").text();
                data->frequency = spot.firstChildElement("frequency").text().toDouble() / 1000.0;
                data->spottercall = spot.firstChildElement("spottercall").text();
                data->timestamp = QDateTime::fromString(spot.firstChildElement("timestamp").text(),"yyyy-MM-dd hh:mm:ss");
                data->mode = spot.firstChildElement("mode").text();
                data->comment = spot.firstChildElement("comment").text();

#ifdef USESQL
                database db = database();
                db.query(QString("DELETE from spots where dxcall='%1'").arg(data->dxcall));
                QString query = QString("INSERT INTO spots(type,spottercall,frequency,dxcall,mode,comment,timestamp) VALUES('%1','%2',%3,'%4','%5','%6','%7')\n")
                    .arg("UDP").arg(data->spottercall).arg(data->frequency).arg(data->dxcall).arg(data->mode).arg(data->comment).arg(data->timestamp.toString("yyyy-MM-dd hh:mm:ss"));
                db.query(query);
#else
                bool found = false;
                QMap<QString, spotData*>::iterator spot = allSpots.find(data->dxcall);
                while (spot != allSpots.end() && spot.key() == data->dxcall && spot.value()->frequency == data->frequency) {
                    found = true;
                    ++spot;
                }
                if (found == false) {
                    allSpots.insert(data->dxcall, data);
                }
#endif
                emit sendOutput(QString("<spot><action>add</action><dxcall>%1</dxcall><spottercall>%2</spottercall><frequency>%3</frequency><comment>%4</comment></spot>\n")
                    .arg(data->dxcall).arg(data->spottercall).arg(data->frequency).arg(data->comment));

            }
            else if (action == "delete")
            {
                QString dxcall = spot.firstChildElement("dxcall").text();
                double frequency = spot.firstChildElement("frequency").text().toDouble() / 1000.0;

#ifdef USESQL
                database db = database();
                QString query=QString("DELETE from spots where dxcall='%1' AND frequency=%2").arg(dxcall).arg(frequency);
                db.query(query);
                qInfo(logCluster()) << query;
#else
                QMap<QString, spotData*>::iterator spot = allSpots.find(dxcall);
                while (spot != allSpots.end() && spot.key() == dxcall && spot.value()->frequency == frequency) 
                {
                    delete spot.value(); // Stop memory leak?
                    spot = allSpots.erase(spot);
                }
#endif
                emit sendOutput(QString("<spot><action>delete</action><dxcall>%1</dxcall<frequency>%3</frequency></spot>\n")
                    .arg(dxcall).arg(frequency));
            }
            updateSpots();
        }
    }
}


void dxClusterClient::tcpDataReceived()
{
    QString data = QString(tcpSocket->readAll());

    emit sendOutput(data);
    if (!authenticated) {
        if (data.contains("login:") || data.contains("call:") || data.contains("callsign:")) {
            sendTcpData(QString("%1\n").arg(tcpUserName));
            return;
        }
        if (data.contains("password:")) {
            sendTcpData(QString("%1\n").arg(tcpPassword));
            return;
        }
        if (data.contains("Hello")) {
            authenticated = true;
            enableSkimmerSpots(skimmerSpots);
        }
    }
    else {
        QRegularExpressionMatchIterator i = tcpRegex.globalMatch(data);
        while (i.hasNext()) {
            QRegularExpressionMatch match = i.next();
            if (match.hasMatch()) {
                spotData* data = new spotData();
                data->spottercall = match.captured(1);
                data->frequency = match.captured(2).toDouble() / 1000.0;
                data->dxcall = match.captured(3);
                data->comment = match.captured(4).trimmed();
                data->timestamp = QDateTime::currentDateTimeUtc();

                qInfo() << "Got spot:" << data->dxcall;

#ifdef USESQL
                database db = database();
                db.query(QString("DELETE from spots where dxcall='%1'").arg(data->dxcall));
                QString query = QString("INSERT INTO spots(type,spottercall,frequency,dxcall,comment,timestamp) VALUES('%1','%2',%3,'%4','%5','%6')\n")
                    .arg("TCP").arg(data->spottercall).arg(data->frequency).arg(data->dxcall).arg(data->comment).arg(data->timestamp.toString("yyyy-MM-dd hh:mm:ss"));
                db.query(query);
#else
                bool found = false;
                QMap<QString, spotData*>::iterator spot = allSpots.find(data->dxcall);
                while (spot != allSpots.end() && spot.key() == data->dxcall && spot.value()->frequency == data->frequency) {
                    found = true;
                    ++spot;
                }
                if (found == false) {
                    allSpots.insert(data->dxcall, data);
                }
#endif
            }
        }
        updateSpots();
    }
}


void dxClusterClient::sendTcpData(QString data)
{
    qInfo(logCluster()) << "Sending:" << data;
    if (tcpSocket != Q_NULLPTR && tcpSocket->isValid() && tcpSocket->isOpen())
    {
        tcpSocket->write(data.toLatin1());
    }
    else
    {
        qInfo(logCluster()) << "socket not open!";
    }
}

void dxClusterClient::tcpCleanup()
{
#ifdef USESQL
    database db = database();
    db.query(QString("DELETE FROM spots where timestamp < datetime('now', '-%1 minutes')").arg(tcpTimeout));
#else
    QMap<QString, spotData*>::iterator spot = allSpots.begin();;
    while (spot != allSpots.end()) {
        if (spot.value()->timestamp.addSecs(tcpTimeout * 60) < QDateTime::currentDateTimeUtc())
        {
            delete spot.value(); // Stop memory leak?
            spot = allSpots.erase(spot);
        } 
        else
        {
            ++spot;
        }
    }
#endif
}

void dxClusterClient::tcpDisconnected() {
    qWarning(logCluster()) << "TCP Cluster server disconnected...";
    emit sendOutput(QString("\nDisconnected from %0 %1\n").arg(tcpServerName).arg(tcpPort));

    // Need to start a timer and attempt reconnect.
}

void dxClusterClient::freqRange(quint8 receiver, double low, double high)
{
    freqRanges[receiver] = {low,high};
    if (receiver) {
        lowSubFreq = low;
        highSubFreq = high;
    } else {
        lowMainFreq = low;
        highMainFreq = high;
    }
    updateSpots();
}

void dxClusterClient::updateSpots()
{
    QList<spotData> spots;
#ifdef USESQL
    // Set the required frequency range.
    QString queryText = QString("SELECT * FROM spots WHERE frequency > %1 AND frequency < %2").arg(lowFreq).arg(highFreq);
    //QString queryText = QString("SELECT * FROM spots");
    database db;
    auto query = db.query(queryText);

    while (query.next()) {
        // Step through all current spots within range
        spotData s = spotData();
        s.dxcall = query.value(query.record().indexOf("dxcall")).toString();
        s.frequency = query.value(query.record().indexOf("frequency")).toDouble();
        spots.append(s);
    }
#else
    // Iterate through all available VFO frequency ranges and send all relevant spots.
    QMap<uchar, rangeValues>::iterator range = freqRanges.begin();
    while (range != freqRanges.end())
    {
        spots.clear();
        QMap<QString, spotData*>::iterator mainSpot = allSpots.begin();
        while (mainSpot != allSpots.end())
        {
            if (mainSpot.value()->frequency >= range->low && mainSpot.value()->frequency <= range->high)
            {
                spots.append(**mainSpot);
            }
            ++mainSpot;
        }
        if (!spots.empty()) {
            emit sendSpots(range.key(),spots);
            //qInfo(logCluster()) << "Sending" << spots.size() << "DX spots to receiver" << range.key();
        }
        ++range;
    }

#endif

}

void dxClusterClient::enableSkimmerSpots(bool enable)
{
    skimmerSpots = enable;
    if (authenticated) {
        if (skimmerSpots) {
            sendTcpData(QString("set/skimmer\n"));
            sendTcpData(QString("set Dx Filter Skimmer\n"));
        }
        else
        {
            sendTcpData(QString("unset/skimmer\n"));
            //sendTcpData(QString("set Dx Filter Not Skimmer\n"));
        }
        
    }
}