forked from F4FXL/smart-group-server-xl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRCDDBMultiClient.cpp
370 lines (300 loc) · 11 KB
/
IRCDDBMultiClient.cpp
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
CIRCDDBClient - ircDDB client library in C++
Copyright (C) 2010-2011 Michael Dirska, DL1BFF ([email protected])
Copyright (C) 2011,2012 Jonathan Naylor, G4KLX
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "IRCDDBMultiClient.h"
#include <stdio.h>
CIRCDDBMultiClient::CIRCDDBMultiClient(const CIRCDDB_Array& clients) :
m_clients(),
m_queriesLock(),
m_responseQueueLock()
{
for (unsigned int i = 0; i < clients.size(); i++) {
if (clients[i] != NULL)
m_clients.push_back(clients[i]);
}
m_clients.shrink_to_fit();
}
CIRCDDBMultiClient::~CIRCDDBMultiClient()
{
for (unsigned int i = 0; i < m_clients.size(); i++) {
delete m_clients[i];
}
while (m_responseQueue.size() > 0) {
delete m_responseQueue[0];
m_responseQueue.erase(m_responseQueue.begin());
}
for (CIRCDDBMultiClientQuery_HashMap::iterator it = m_userQueries.begin(); it != m_userQueries.end(); it++)
delete it->second;
m_userQueries.clear();
for (CIRCDDBMultiClientQuery_HashMap::iterator it = m_repeaterQueries.begin(); it != m_repeaterQueries.end(); it++)
delete it->second;
m_repeaterQueries.clear();
for (CIRCDDBMultiClientQuery_HashMap::iterator it = m_gatewayQueries.begin(); it != m_gatewayQueries.end(); it++)
delete it->second;
m_gatewayQueries.clear();
}
bool CIRCDDBMultiClient::open()
{
bool result = true;
for (unsigned int i = 0; i < m_clients.size(); i++) {
result = m_clients[i]->open() && result;
}
if (!result) close();
return result;
}
void CIRCDDBMultiClient::rptrQTH(const std::string & callsign, double latitude, double longitude, const std::string & desc1, const std::string & desc2, const std::string & infoURL)
{
for (unsigned int i = 0; i < m_clients.size(); i++) {
m_clients[i]->rptrQTH(callsign, latitude, longitude, desc1, desc2, infoURL);
}
}
void CIRCDDBMultiClient::rptrQRG(const std::string & callsign, double txFrequency, double duplexShift, double range, double agl)
{
for (unsigned int i = 0; i < m_clients.size(); i++) {
m_clients[i]->rptrQRG(callsign, txFrequency, duplexShift, range, agl);
}
}
void CIRCDDBMultiClient::kickWatchdog(const std::string & callsign, const std::string & wdInfo)
{
for (unsigned int i = 0; i < m_clients.size(); i++) {
m_clients[i]->kickWatchdog(callsign, wdInfo);
}
}
int CIRCDDBMultiClient::getConnectionState()
{
for (unsigned int i = 0; i < m_clients.size(); i++) {
int state = m_clients[i]->getConnectionState();
if (state != 7)
return state;
}
return 7;
}
bool CIRCDDBMultiClient::sendHeard(const std::string & myCall, const std::string & myCallExt, const std::string & yourCall, const std::string & rpt1, const std::string & rpt2, unsigned char flag1, unsigned char flag2, unsigned char flag3)
{
bool result = true;
for (unsigned int i = 0; i < m_clients.size(); i++) {
result = m_clients[i]->sendHeard(myCall, myCallExt, yourCall, rpt1, rpt2, flag1, flag2, flag3) && result;
}
return result;
}
bool CIRCDDBMultiClient::sendHeardWithTXMsg(const std::string & myCall, const std::string & myCallExt, const std::string & yourCall, const std::string & rpt1, const std::string & rpt2, unsigned char flag1, unsigned char flag2, unsigned char flag3, const std::string & network_destination, const std::string & tx_message)
{
bool result = true;
for (unsigned int i = 0; i < m_clients.size(); i++) {
result = m_clients[i]->sendHeardWithTXMsg(myCall, myCallExt, yourCall, rpt1, rpt2, flag1, flag2, flag3, network_destination, tx_message) && result;
}
return result;
}
bool CIRCDDBMultiClient::sendHeardWithTXStats(const std::string & myCall, const std::string & myCallExt, const std::string & yourCall, const std::string & rpt1, const std::string & rpt2, unsigned char flag1, unsigned char flag2, unsigned char flag3, int num_dv_frames, int num_dv_silent_frames, int num_bit_errors)
{
bool result = true;
for (unsigned int i = 0; i < m_clients.size(); i++) {
result = m_clients[i]->sendHeardWithTXStats(myCall, myCallExt, yourCall, rpt1, rpt2, flag1, flag2, flag3, num_dv_frames, num_dv_silent_frames, num_bit_errors) && result;
}
return result;
}
void CIRCDDBMultiClient::sendSGSInfo(const std::string subcommand, const std::vector<std::string> parms)
{
for (unsigned int i = 0; i < m_clients.size(); i++) {
m_clients[i]->sendSGSInfo(subcommand, parms);
}
}
bool CIRCDDBMultiClient::findGateway(const std::string & gatewayCallsign)
{
pushQuery(IDRT_GATEWAY, gatewayCallsign, new CIRCDDBMultiClientQuery("", "", gatewayCallsign, "", "", IDRT_GATEWAY));
bool result = true;
for (unsigned int i = 0; i < m_clients.size(); i++) {
result = m_clients[i]->findGateway(gatewayCallsign) && result;
}
return result;
}
bool CIRCDDBMultiClient::findRepeater(const std::string & repeaterCallsign)
{
pushQuery(IDRT_REPEATER, repeaterCallsign, new CIRCDDBMultiClientQuery("", repeaterCallsign, "", "", "", IDRT_REPEATER));
bool result = true;
for (unsigned int i = 0; i < m_clients.size(); i++) {
result = m_clients[i]->findRepeater(repeaterCallsign) && result;
}
return result;
}
bool CIRCDDBMultiClient::findUser(const std::string & userCallsign)
{
pushQuery(IDRT_USER, userCallsign, new CIRCDDBMultiClientQuery(userCallsign, "", "", "", "", IDRT_USER));
bool result = true;
for (unsigned int i = 0; i < m_clients.size(); i++) {
result = m_clients[i]->findUser(userCallsign) && result;
}
return result;
}
IRCDDB_RESPONSE_TYPE CIRCDDBMultiClient::getMessageType()
{
//procees the inner clients at each call
for (unsigned int i = 0; i < m_clients.size(); i++) {
std::string user = "", repeater = "", gateway = "", address = "", timestamp = "", key = "";
IRCDDB_RESPONSE_TYPE type = m_clients[i]->getMessageType();
switch (type) {
case IDRT_USER: {
if (!m_clients[i]->receiveUser(user, repeater, gateway, address, timestamp))
type = IDRT_NONE;
key = user;
break;
}
case IDRT_GATEWAY: {
if (!m_clients[i]->receiveGateway(gateway, address))
type = IDRT_NONE;
key = gateway;
break;
}
case IDRT_REPEATER: {
if (!m_clients[i]->receiveRepeater(repeater, gateway, address))
type = IDRT_NONE;
key = repeater;
break;
}
case IDRT_NONE: {
default:
break;
}
}
if (type != IDRT_NONE)
{
m_queriesLock.lock();
bool canAddToQueue = false;
bool wasQuery = false;
CIRCDDBMultiClientQuery * item = popQuery(type, key);
if (item != NULL) {//is this a response to a query we've sent ?
item->Update(user, repeater, gateway, address, timestamp);//update item (if needed)
canAddToQueue = (item->incrementResponseCount() >= m_clients.size()); //did all the clients respond or did we have an answer ?
wasQuery = true;
}
else {
item = new CIRCDDBMultiClientQuery(user, repeater, gateway, address, timestamp, type);
canAddToQueue = true;
}
if (canAddToQueue) {
m_responseQueueLock.lock();
m_responseQueue.push_back(item);
m_responseQueueLock.unlock();
}
else if (wasQuery)
pushQuery(type, key, item);
m_queriesLock.unlock();
}
}
IRCDDB_RESPONSE_TYPE result = IDRT_NONE;
m_responseQueueLock.lock();
if (m_responseQueue.size() != 0) result = m_responseQueue[0]->getType();
m_responseQueueLock.unlock();
return result;
}
bool CIRCDDBMultiClient::receiveRepeater(std::string & repeaterCallsign, std::string & gatewayCallsign, std::string & address)
{
CIRCDDBMultiClientQuery * item = checkAndGetNextResponse(IDRT_REPEATER, "CIRCDDBMultiClient::receiveRepeater: unexpected response type");
if (item == NULL)
return false;
repeaterCallsign = item->getRepeater();
gatewayCallsign = item->getGateway();
address = item->getAddress();
delete item;
return true;
}
bool CIRCDDBMultiClient::receiveGateway(std::string & gatewayCallsign, std::string & address)
{
CIRCDDBMultiClientQuery * item = checkAndGetNextResponse(IDRT_GATEWAY, "CIRCDDBMultiClient::receiveGateway: unexpected response type");
if (item == NULL)
return false;
gatewayCallsign = item->getGateway();
address = item->getAddress();
delete item;
return true;
}
bool CIRCDDBMultiClient::receiveUser(std::string & userCallsign, std::string & repeaterCallsign, std::string & gatewayCallsign, std::string & address)
{
std::string dummy;
return receiveUser(userCallsign, repeaterCallsign, gatewayCallsign, address, dummy);
}
bool CIRCDDBMultiClient::receiveUser(std::string & userCallsign, std::string & repeaterCallsign, std::string & gatewayCallsign, std::string & address, std::string & timeStamp)
{
CIRCDDBMultiClientQuery * item = checkAndGetNextResponse(IDRT_USER, "CIRCDDBMultiClient::receiveUser: unexpected response type");
if (item == NULL) {
//wxLogMessage(wxT("CIRCDDBMultiClient::receiveUser NO USER IN QUEUE"));
return false;
}
//wxLogMessage(wxT("CIRCDDBMultiClient::receiveUser : %s"), item->toString());
userCallsign = item->getUser();
repeaterCallsign = item->getRepeater();
gatewayCallsign = item->getGateway();
address = item->getAddress();
timeStamp = item->getTimestamp();
delete item;
return true;
}
void CIRCDDBMultiClient::close()
{
for (unsigned int i = 0; i < m_clients.size(); i++) {
m_clients[i]->close();
}
}
CIRCDDBMultiClientQuery * CIRCDDBMultiClient::checkAndGetNextResponse(IRCDDB_RESPONSE_TYPE expectedType, std::string errorMessage)
{
CIRCDDBMultiClientQuery * item = NULL;
m_responseQueueLock.lock();
if (m_responseQueue.size() == 0 || m_responseQueue[0]->getType() != expectedType) {
printf(errorMessage.c_str());
}
else {
item = m_responseQueue[0];
m_responseQueue.erase(m_responseQueue.begin());
}
m_responseQueueLock.unlock();
return item;
}
void CIRCDDBMultiClient::pushQuery(IRCDDB_RESPONSE_TYPE type, const std::string& key, CIRCDDBMultiClientQuery * query)
{
CIRCDDBMultiClientQuery_HashMap * queries = getQueriesHashMap(type);
m_queriesLock.lock();
if (queries != NULL && (*queries)[key] == NULL)
(*queries)[key] = query;
else
delete query;
m_queriesLock.unlock();
}
CIRCDDBMultiClientQuery * CIRCDDBMultiClient::popQuery(IRCDDB_RESPONSE_TYPE type, const std::string & key)
{
CIRCDDBMultiClientQuery_HashMap * queries = getQueriesHashMap(type);
m_queriesLock.lock();
CIRCDDBMultiClientQuery * item = NULL;
if (queries != NULL && queries->count(key) != 0) {
item = queries->at(key);
queries->erase(key);
}
m_queriesLock.unlock();
return item;
}
CIRCDDBMultiClientQuery_HashMap * CIRCDDBMultiClient::getQueriesHashMap(IRCDDB_RESPONSE_TYPE type)
{
switch (type)
{
case IDRT_USER:
return &m_userQueries;
case IDRT_GATEWAY:
return &m_gatewayQueries;
case IDRT_REPEATER:
return &m_repeaterQueries;
case IDRT_NONE:
default:
return NULL;
}
}