better CQnetDB::Init

pull/14/head
Tom Early 6 years ago
parent 66fc6b1189
commit 2ea76bafa0

@ -17,6 +17,7 @@
*/
#include <string>
#include <thread>
#include "QnetDB.h"
@ -25,21 +26,26 @@ bool CQnetDB::Open(const char *name)
if (sqlite3_open(name, &db)) {
fprintf(stderr, "CQnetDB::Open: can't open %s\n", name);
return true;
} else
}
return false;
}
bool CQnetDB::Init()
void CQnetDB::Init()
{
std::string sql("DROP TABLE IF EXISTS LHEARD;");
char *eMsg;
if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) {
int rval = SQLITE_ERROR;
for (int i=0; i<10 && rval!=SQLITE_OK; i++) {
rval = sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg);
if (SQLITE_OK != rval) {
fprintf(stderr, "CQnetDB::Open drop table LHEARD error: %s\n", eMsg);
sqlite3_free(eMsg);
return true;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
if (SQLITE_OK == rval) {
sql.assign("CREATE TABLE LHEARD("
"callsign TEXT PRIMARY KEY, "
"sfx TEXT, "
@ -48,20 +54,30 @@ bool CQnetDB::Init()
"lasttime INT NOT NULL"
") WITHOUT ROWID;");
if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) {
rval = SQLITE_ERROR;
for (int i=0; i<10 && rval!=SQLITE_OK; i++) {
rval = sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg);
if (SQLITE_OK != rval) {
fprintf(stderr, "CQnetDB::Open create table LHEARD error: %s\n", eMsg);
sqlite3_free(eMsg);
return true;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
}
sql.assign("DROP TABLE IF EXISTS LINKSTATUS;");
if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) {
rval = SQLITE_ERROR;
for (int i=0; i<10 && rval!=SQLITE_OK; i++) {
rval = sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg);
if (SQLITE_OK != rval) {
fprintf(stderr, "CQnetDB::Open drop table LINKSTATUS error: %s\n", eMsg);
sqlite3_free(eMsg);
return true;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
if (SQLITE_OK == rval) {
sql.assign("CREATE TABLE LINKSTATUS("
"ip_address TEXT PRIMARY KEY, "
"from_mod TEXT NOT NULL, "
@ -70,33 +86,46 @@ bool CQnetDB::Init()
"linked_time INT NOT NULL"
") WITHOUT ROWID;");
if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) {
rval = SQLITE_ERROR;
for (int i=0; i<10 && rval!=SQLITE_OK; i++) {
rval = sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg);
if (SQLITE_OK != rval) {
fprintf(stderr, "CQnetDB::Open create table LINKSTATUS error: %s\n", eMsg);
sqlite3_free(eMsg);
return true;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
}
sql.assign("DROP TABLE IF EXISTS GATEWAYS;");
if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) {
fprintf(stderr, "CQnetDB::Open drop table LINKSTATUS error: %s\n", eMsg);
rval = SQLITE_ERROR;
for (int i=0; i<10 && rval!=SQLITE_OK; i++) {
rval = sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg);
if (SQLITE_OK != rval) {
fprintf(stderr, "CQnetDB::Open drop table GATEWAYS error: %s\n", eMsg);
sqlite3_free(eMsg);
return true;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
if (SQLITE_OK == rval) {
sql.assign("CREATE TABLE 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)) {
rval = SQLITE_ERROR;
for (int i=0; i<10 && rval!=SQLITE_OK; i++) {
rval = sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg);
if (SQLITE_OK != rval) {
fprintf(stderr, "CQnetDB::Open create table GATEWAYS error: %s\n", eMsg);
sqlite3_free(eMsg);
return true;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
}
return false;
}
bool CQnetDB::UpdateLH(const char *callsign, const char *sfx, const char module, const char *reflector)

@ -54,7 +54,7 @@ public:
CQnetDB() : db(NULL) {}
~CQnetDB() { if (db) sqlite3_close(db); }
bool Open(const char *name);
bool Init();
void Init();
bool UpdateLH(const char *callsign, const char *sfx, const char module, const char *reflector);
bool UpdateLS(const char *address, const char from_mod, const char *to_callsign, const char to_mod, time_t connect_time);
bool UpdateGW(const char *name, const char *address, unsigned short port);

@ -54,7 +54,7 @@
#define CFG_DIR "/usr/local/etc"
#endif
const std::string GW_VERSION("QnetGateway-415");
const std::string GW_VERSION("QnetGateway-503");
static std::atomic<bool> keep_running(true);
@ -2314,8 +2314,9 @@ bool CQnetGateway::Init(char *cfgfile)
std::string fname(CFG_DIR);
fname.append("/");
fname.append(DASH_SQL_NAME);
if (qnDB.Open(fname.c_str()) || qnDB.Init())
if (qnDB.Open(fname.c_str()))
return true;
qnDB.Init();
playNotInCache = false;

Loading…
Cancel
Save

Powered by TurnKey Linux.