diff --git a/QnetDB.cpp b/QnetDB.cpp index 8fd8563..1e72b7b 100644 --- a/QnetDB.cpp +++ b/QnetDB.cpp @@ -23,62 +23,33 @@ bool CQnetDB::Open(const char *name) { - if (sqlite3_open(name, &db)) { + 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; } - return false; + + return Init(); } -void CQnetDB::Init() +bool CQnetDB::Init() { - std::string sql("DROP TABLE IF EXISTS LHEARD;"); - char *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); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - } - } - if (SQLITE_OK == rval) { - sql.assign("CREATE TABLE LHEARD(" - "callsign TEXT PRIMARY KEY, " - "sfx TEXT, " - "module TEXT, " - "reflector TEXT, " - "lasttime INT NOT NULL" - ") WITHOUT ROWID;"); + std::string sql("CREATE TABLE IF NOT EXIST LHEARD(" + "callsign TEXT PRIMARY KEY, " + "sfx TEXT, " + "module TEXT, " + "reflector TEXT, " + "lasttime INT NOT NULL" + ") WITHOUT ROWID;"); - 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); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - } - } - } - - sql.assign("DROP TABLE IF EXISTS LINKSTATUS;"); - - 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); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - } + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) { + fprintf(stderr, "CQnetDB::Open create table LHEARD error: %s\n", eMsg); + sqlite3_free(eMsg); + return true; } - if (SQLITE_OK == rval) { - sql.assign("CREATE TABLE LINKSTATUS(" + sql.assign("CREATE TABLE IF NOT EXIST LINKSTATUS(" "ip_address TEXT PRIMARY KEY, " "from_mod TEXT NOT NULL, " "to_callsign TEXT NOT NULL, " @@ -86,46 +57,24 @@ void CQnetDB::Init() "linked_time INT NOT NULL" ") WITHOUT ROWID;"); - 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); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - } - } - } - - sql.assign("DROP TABLE IF EXISTS GATEWAYS;"); - - 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); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - } + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) { + fprintf(stderr, "CQnetDB::Open create table LINKSTATUS error: %s\n", eMsg); + sqlite3_free(eMsg); + return true; } - if (SQLITE_OK == rval) { - sql.assign("CREATE TABLE GATEWAYS(" + sql.assign("CREATE TABLE IF NOT EXIST GATEWAYS(" "name TEXT PRIMARY KEY, " "address TEXT NOT NULL, " "port INT NOT NULL" ") WITHOUT ROWID;"); - 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); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - } - } + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) { + fprintf(stderr, "CQnetDB::Open create table GATEWAYS error: %s\n", eMsg); + sqlite3_free(eMsg); + return true; } + return false; } bool CQnetDB::UpdateLH(const char *callsign, const char *sfx, const char module, const char *reflector) @@ -206,7 +155,7 @@ bool CQnetDB::UpdateGW(const char *name, const char *address, unsigned short por bool CQnetDB::UpdateGW(CHostQueue &hqueue) { if (NULL == db) - return true; + return false; char *eMsg; if (SQLITE_OK != sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, 0, &eMsg)) { @@ -221,7 +170,7 @@ bool CQnetDB::UpdateGW(CHostQueue &hqueue) } if (SQLITE_OK != sqlite3_exec(db, "COMMIT TRANSACTION;", NULL, 0, &eMsg)) { - fprintf(stderr, "CQnetDB::UpdateGW END TRANSACTION error: %s\n", eMsg); + fprintf(stderr, "CQnetDB::UpdateGW COMMIT TRANSACTION error: %s\n", eMsg); sqlite3_free(eMsg); return true; } @@ -331,15 +280,40 @@ bool CQnetDB::FindGW(const char *name) } } +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; - std::string sql("DELETE FROM GATEWAYS;"); char *eMsg; - if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) { + if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM GATEWAYS;", NULL, 0, &eMsg)) { fprintf(stderr, "CQnetDB::ClearGW error: %s\n", eMsg); sqlite3_free(eMsg); } diff --git a/QnetDB.h b/QnetDB.h index cdb2774..4c66288 100644 --- a/QnetDB.h +++ b/QnetDB.h @@ -54,18 +54,20 @@ public: CQnetDB() : db(NULL) {} ~CQnetDB() { if (db) sqlite3_close(db); } bool Open(const char *name); - 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); bool UpdateGW(CHostQueue &); bool DeleteLS(const char *address); bool FindLS(const char mod, std::list &linklist); bool FindGW(const char *name, std::string &address, unsigned short &port); bool FindGW(const char *name); + void ClearLH(); + void ClearLS(); void ClearGW(); int Count(const char *table); private: + bool Init(); + bool UpdateGW(const char *name, const char *address, unsigned short port); sqlite3 *db; }; diff --git a/QnetGateway.cpp b/QnetGateway.cpp index 90a8e86..e973a2d 100644 --- a/QnetGateway.cpp +++ b/QnetGateway.cpp @@ -54,7 +54,7 @@ #define CFG_DIR "/usr/local/etc" #endif -const std::string GW_VERSION("QnetGateway-510"); +const std::string GW_VERSION("QnetGateway-511"); static std::atomic keep_running(true); @@ -2317,7 +2317,7 @@ bool CQnetGateway::Init(char *cfgfile) fname.append(DASH_SQL_NAME); if (qnDB.Open(fname.c_str())) return true; - qnDB.Init(); + qnDB.ClearLH(); playNotInCache = false; diff --git a/QnetLink.cpp b/QnetLink.cpp index 3e3af14..b1b41ee 100644 --- a/QnetLink.cpp +++ b/QnetLink.cpp @@ -54,7 +54,7 @@ #include "QnetLink.h" #include "Utilities.h" -#define LINK_VERSION "QnetLink-510" +#define LINK_VERSION "QnetLink-511" #ifndef BIN_DIR #define BIN_DIR "/usr/local/bin" #endif @@ -292,6 +292,7 @@ void CQnetLink::RptrAckThread(char *arg) /* Open text file of repeaters, reflectors */ void CQnetLink::LoadGateways(const std::string &filename) { + qnDB.ClearGW(); const std::string website("auth.dstargateway.org"); int dplus = 0; // DPlus Authenticate @@ -3025,7 +3026,6 @@ void CQnetLink::Process() notify_msg[i][0] = '\0'; } if (loadG[i] && 0x0U == tracing[i].streamid) { - qnDB.ClearGW(); LoadGateways(gwys); loadG[i] = false; if (bool_rptr_ack) @@ -3292,7 +3292,7 @@ bool CQnetLink::Init(const char *cfgfile) fname.append(dash_sql_name); if (qnDB.Open(fname.c_str())) return true; - + qnDB.ClearLS(); LoadGateways(gwys); /* create our server */