-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQnetDB.cpp
413 lines (351 loc) · 10.2 KB
/
QnetDB.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* Copyright (C) 2020 by Thomas Early N7TAE
*
* 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <string>
#include <sstream>
#include <thread>
#include "QnetDB.h"
bool CQnetDB::Open(const char *name)
{
if (sqlite3_open_v2(name, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL))
{
fprintf(stderr, "CQnetDB::Open: can't open %s\n", name);
return true;
}
auto rval = sqlite3_busy_timeout(db, 1000);
if (SQLITE_OK != rval)
{
fprintf(stderr, "sqlite3_busy_timeout returned %d\n", rval);
}
return Init();
}
bool CQnetDB::Init()
{
char *eMsg;
std::string sql("CREATE TABLE IF NOT EXISTS LHEARD("
"callsign TEXT PRIMARY KEY, "
"sfx TEXT DEFAULT ' ', "
"message TEXT DEFAULT ' ', "
"maidenhead TEXT DEFAULT ' ', "
"latitude REAL DEFAULT 0.0, "
"longitude REAL DEFAULT 0.0, "
"module TEXT, "
"reflector TEXT, "
"lasttime INT NOT NULL"
") WITHOUT ROWID;");
if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg))
{
fprintf(stderr, "CQnetDB::Init [%s] error: %s\n", sql.c_str(), eMsg);
sqlite3_free(eMsg);
return true;
}
sql.assign("CREATE TABLE IF NOT EXISTS LINKSTATUS("
"ip_address TEXT PRIMARY KEY, "
"from_mod TEXT NOT NULL, "
"to_callsign TEXT NOT NULL, "
"to_mod TEXT NOT NULL, "
"linked_time INT NOT NULL"
") WITHOUT ROWID;");
if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg))
{
fprintf(stderr, "CQnetDB::Init [%s] error: %s\n", sql.c_str(), eMsg);
sqlite3_free(eMsg);
return true;
}
sql.assign("CREATE TABLE IF NOT EXISTS GATEWAYS("
"name TEXT PRIMARY KEY, "
"address TEXT NOT NULL, "
"port INT NOT NULL"
") WITHOUT ROWID;");
if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg))
{
fprintf(stderr, "CQnetDB::Init [%s] error: %s\n", sql.c_str(), eMsg);
sqlite3_free(eMsg);
return true;
}
return false;
}
static int countcallback(void *count, int /*argc*/, char **argv, char ** /*azColName*/)
{
auto c = (int *)count;
*c = atoi(argv[0]);
return 0;
}
bool CQnetDB::UpdateLH(const char *callsign, const char *sfx, const char module, const char *reflector)
{
if (NULL == db)
return false;
std::stringstream sql;
sql << "SELECT COUNT(*) FROM LHEARD WHERE callsign='" << callsign << "';";
int count = 0;
char *eMsg;
if (SQLITE_OK != sqlite3_exec(db, sql.str().c_str(), countcallback, &count, &eMsg))
{
fprintf(stderr, "CQnetDB::UpdateLH [%s] error: %s\n", sql.str().c_str(), eMsg);
sqlite3_free(eMsg);
return true;
}
sql.clear();
if (count)
{
sql << "UPDATE LHEARD SET sfx = '" << sfx << "', module = '" << module << "', reflector = '" << reflector << "', lasttime = strftime('%s','now') WHERE callsign = '" << callsign << "';";
}
else
{
sql << "INSERT INTO LHEARD (callsign, sfx, module, reflector, lasttime) VALUES ('" << callsign << "', '" << sfx << "', '" << module << "', '" << reflector << "', strftime('%s','now'));";
}
if (SQLITE_OK != sqlite3_exec(db, sql.str().c_str(), NULL, 0, &eMsg))
{
fprintf(stderr, "CQnetDB::UpdateLH [%s] error: %s\n", sql.str().c_str(), eMsg);
sqlite3_free(eMsg);
return true;
}
return false;
}
bool CQnetDB::UpdatePosition(const char *callsign, const char *maidenhead, double latitude, double longitude)
{
if (NULL == db)
return false;
std::stringstream sql;
sql << "UPDATE LHEARD SET maidenhead = '" << maidenhead << "', latitude = " << latitude << ", longitude = " << longitude << ", lasttime = strftime('%s','now') WHERE callsign='" << callsign << "';";
char *eMsg;
if (SQLITE_OK != sqlite3_exec(db, sql.str().c_str(), NULL, 0, &eMsg))
{
fprintf(stderr, "CQnetDB::UpdatePosition [%s] error: %s\n", sql.str().c_str(), eMsg);
sqlite3_free(eMsg);
return true;
}
return false;
}
bool CQnetDB::UpdateMessage(const char *callsign, const char *message)
{
if (NULL == db)
return false;
std::stringstream sql;
sql << "UPDATE LHEARD SET message = '" << message << "', lasttime = strftime('%s','now') WHERE callsign='" << callsign << "';";
char *eMsg;
if (SQLITE_OK != sqlite3_exec(db, sql.str().c_str(), NULL, 0, &eMsg))
{
fprintf(stderr, "CQnetDB::UpdateMessage [%s] error: %s\n", sql.str().c_str(), eMsg);
sqlite3_free(eMsg);
return true;
}
return false;
}
bool CQnetDB::UpdateLS(const char *address, const char from_mod, const char *to_callsign, const char to_mod, time_t linked_time)
{
if (NULL == db)
return false;
std::stringstream sql;
sql << "INSERT OR REPLACE INTO LINKSTATUS (ip_address, from_mod, to_callsign, to_mod, linked_time) VALUES ('" << address << "', '" << from_mod << "', '" << to_callsign << "', '" << to_mod << "', " << linked_time << ");";
char *eMsg;
if (SQLITE_OK != sqlite3_exec(db, sql.str().c_str(), NULL, 0, &eMsg))
{
fprintf(stderr, "CQnetDB::UpdateLS [%s] error: %s\n", sql.str().c_str(), eMsg);
sqlite3_free(eMsg);
return true;
}
return false;
}
bool CQnetDB::UpdateGW(const char *name, const char *address, unsigned short port)
{
if (NULL == db)
return true;
std::string n(name);
n.resize(6, ' ');
std::stringstream sql;
sql << "INSERT OR REPLACE INTO GATEWAYS (name, address, port) VALUES ('" << n << "', '" << address << "', " << port << ");";
char *eMsg;
if (SQLITE_OK != sqlite3_exec(db, sql.str().c_str(), NULL, 0, &eMsg))
{
fprintf(stderr, "CQnetDB::UpdateGW [%s] error: %s\n", sql.str().c_str(), eMsg);
sqlite3_free(eMsg);
return true;
}
return false;
}
bool CQnetDB::UpdateGW(CHostQueue &hqueue)
{
if (NULL == db)
return false;
char *eMsg;
if (SQLITE_OK != sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, 0, &eMsg))
{
fprintf(stderr, "CQnetDB::UpdateGW BEGIN TRANSATION error: %s\n", eMsg);
sqlite3_free(eMsg);
return true;
}
while (!hqueue.Empty())
{
auto h = hqueue.Pop();
UpdateGW(h.name.c_str(), h.addr.c_str(), h.port);
}
if (SQLITE_OK != sqlite3_exec(db, "COMMIT TRANSACTION;", NULL, 0, &eMsg))
{
fprintf(stderr, "CQnetDB::UpdateGW COMMIT TRANSACTION error: %s\n", eMsg);
sqlite3_free(eMsg);
return true;
}
return false;
}
bool CQnetDB::DeleteLS(const char *address)
{
if (NULL == db)
return false;
std::stringstream sql;
sql << "DELETE FROM LINKSTATUS WHERE ip_address=='" << address << "';";
char *eMsg;
if (SQLITE_OK != sqlite3_exec(db, sql.str().c_str(), NULL, 0, &eMsg))
{
fprintf(stderr, "CQnetDB::DeleteLS [%s] error: %s\n", sql.str().c_str(), eMsg);
sqlite3_free(eMsg);
return true;
}
return false;
}
bool CQnetDB::FindLS(const char mod, std::list<CLink> &linklist)
{
if (NULL == db)
return false;
std::stringstream sql;
sql << "SELECT ip_address,to_callsign,to_mod,linked_time FROM LINKSTATUS WHERE from_mod=='" << mod << "';";
sqlite3_stmt *stmt;
int rval = sqlite3_prepare_v2(db, sql.str().c_str(), -1, &stmt, 0);
if (SQLITE_OK != rval)
{
fprintf(stderr, "CQnetDB::FindLS [%s] error\n", sql.str().c_str());
return true;
}
while (SQLITE_ROW == sqlite3_step(stmt))
{
std::string cs((const char *)sqlite3_column_text(stmt, 1));
std::string mod((const char *)sqlite3_column_text(stmt, 2));
if (mod.at(0) != 'p')
{
cs.resize(7, ' ');
cs.append(mod);
}
linklist.push_back(CLink(cs, sqlite3_column_text(stmt, 0), sqlite3_column_int(stmt, 3)));
}
sqlite3_finalize(stmt);
return false;
}
bool CQnetDB::FindGW(const char *name, std::string &address, unsigned short &port)
// returns true if NOT found
{
if (NULL == db)
return false;
std::string n(name);
n.resize(6, ' ');
std::stringstream sql;
sql << "SELECT address, port FROM GATEWAYS WHERE name=='" << n << "';";
sqlite3_stmt *stmt;
int rval = sqlite3_prepare_v2(db, sql.str().c_str(), -1, &stmt, 0);
if (SQLITE_OK != rval)
{
fprintf(stderr, "CQnetDB::FindGW error: %d\n", rval);
return true;
}
if (SQLITE_ROW == sqlite3_step(stmt))
{
address.assign((const char *)sqlite3_column_text(stmt, 0));
port = (unsigned short)(sqlite3_column_int(stmt, 1));
sqlite3_finalize(stmt);
return false;
}
else
{
sqlite3_finalize(stmt);
return true;
}
}
bool CQnetDB::FindGW(const char *name)
// returns true if found
{
if (NULL == db)
return false;
std::string n(name);
n.resize(6, ' ');
std::stringstream sql;
sql << "SELECT address,port FROM GATEWAYS WHERE name=='" << n << "';";
sqlite3_stmt *stmt;
int rval = sqlite3_prepare_v2(db, sql.str().c_str(), -1, &stmt, 0);
if (SQLITE_OK != rval)
{
fprintf(stderr, "CQnetDB::FindGW error: %d\n", rval);
return true;
}
if (SQLITE_ROW == sqlite3_step(stmt))
{
sqlite3_finalize(stmt);
return true;
}
else
{
sqlite3_finalize(stmt);
return false;
}
}
void CQnetDB::ClearLH()
{
if (NULL == db)
return;
char *eMsg;
if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM LHEARD;", NULL, 0, &eMsg))
{
fprintf(stderr, "CQnetDB::ClearLH error: %s\n", eMsg);
sqlite3_free(eMsg);
}
}
void CQnetDB::ClearLS()
{
if (NULL == db)
return;
char *eMsg;
if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM LINKSTATUS;", NULL, 0, &eMsg))
{
fprintf(stderr, "CQnetDB::ClearLS error: %s\n", eMsg);
sqlite3_free(eMsg);
}
}
void CQnetDB::ClearGW()
{
if (NULL == db)
return;
char *eMsg;
if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM GATEWAYS;", NULL, 0, &eMsg))
{
fprintf(stderr, "CQnetDB::ClearGW error: %s\n", eMsg);
sqlite3_free(eMsg);
}
}
int CQnetDB::Count(const char *table)
{
if (NULL == db)
return 0;
std::string sql("SELECT COUNT(*) FROM ");
sql.append(table);
sql.append(";");
int count = 0;
char *eMsg;
if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), countcallback, &count, &eMsg))
{
fprintf(stderr, "CQnetDB::Count error: %s\n", eMsg);
sqlite3_free(eMsg);
}
return count;
}