diff --git a/NXDNLookup.cpp b/NXDNLookup.cpp index 2c621b6..c12ed27 100644 --- a/NXDNLookup.cpp +++ b/NXDNLookup.cpp @@ -30,7 +30,6 @@ CThread(), m_filename(filename), m_reloadTime(reloadTime), m_table(), -m_mutex(), m_stop(false) { } @@ -41,7 +40,7 @@ CNXDNLookup::~CNXDNLookup() bool CNXDNLookup::read() { - bool ret = load(); + bool ret = m_table.load(m_filename); if (m_reloadTime > 0U) run(); @@ -61,7 +60,7 @@ void CNXDNLookup::entry() timer.clock(); if (timer.hasExpired()) { - load(); + m_table.load(m_filename); timer.start(); } } @@ -81,6 +80,27 @@ void CNXDNLookup::stop() wait(); } +void CNXDNLookup::findWithName(unsigned int id, class CUserDBentry *entry) +{ + if (id == 0xFFFFU) { + entry->clear(); + entry->set(keyCALLSIGN, "ALL"); + return; + } + + if (m_table.lookup(id, entry)) { + LogDebug("FindWithName =%s %s", entry->get(keyCALLSIGN).c_str(), entry->get(keyFIRST_NAME).c_str()); + } else { + entry->clear(); + + char text[10U]; + ::snprintf(text, sizeof(text), "%u", id); + entry->set(keyCALLSIGN, text); + } + + return; +} + std::string CNXDNLookup::find(unsigned int id) { std::string callsign; @@ -88,73 +108,19 @@ std::string CNXDNLookup::find(unsigned int id) if (id == 0xFFFFU) return std::string("ALL"); - m_mutex.lock(); - - try { - callsign = m_table.at(id); - } catch (...) { + class CUserDBentry entry; + if (m_table.lookup(id, &entry)) { + callsign = entry.get(keyCALLSIGN); + } else { char text[10U]; - ::sprintf(text, "%u", id); + ::snprintf(text, sizeof(text), "%u", id); callsign = std::string(text); } - m_mutex.unlock(); - return callsign; } bool CNXDNLookup::exists(unsigned int id) { - m_mutex.lock(); - - bool found = m_table.count(id) == 1U; - - m_mutex.unlock(); - - return found; + return m_table.lookup(id, NULL); } - -bool CNXDNLookup::load() -{ - FILE* fp = ::fopen(m_filename.c_str(), "rt"); - if (fp == NULL) { - LogWarning("Cannot open the NXDN Id lookup file - %s", m_filename.c_str()); - return false; - } - - m_mutex.lock(); - - // Remove the old entries - m_table.clear(); - - char buffer[100U]; - while (::fgets(buffer, 100U, fp) != NULL) { - if (buffer[0U] == '#') - continue; - - char* p1 = ::strtok(buffer, ",\t\r\n"); - char* p2 = ::strtok(NULL, ",\t\r\n"); - - if (p1 != NULL && p2 != NULL) { - unsigned int id = (unsigned int)::atoi(p1); - if (id > 0U) { - for (char* p = p2; *p != 0x00U; p++) - *p = ::toupper(*p); - - m_table[id] = std::string(p2); - } - } - } - - m_mutex.unlock(); - - ::fclose(fp); - - size_t size = m_table.size(); - if (size == 0U) - return false; - - LogInfo("Loaded %u Ids to the NXDN callsign lookup table", size); - - return true; -} \ No newline at end of file diff --git a/NXDNLookup.h b/NXDNLookup.h index d7b0486..c53a927 100644 --- a/NXDNLookup.h +++ b/NXDNLookup.h @@ -20,10 +20,9 @@ #define NXDNLookup_H #include "Thread.h" -#include "Mutex.h" +#include "UserDB.h" #include -#include class CNXDNLookup : public CThread { public: @@ -35,19 +34,17 @@ public: virtual void entry(); std::string find(unsigned int id); + void findWithName(unsigned int id, class CUserDBentry *entry); bool exists(unsigned int id); void stop(); private: - std::string m_filename; - unsigned int m_reloadTime; - std::unordered_map m_table; - CMutex m_mutex; - bool m_stop; - - bool load(); + std::string m_filename; + unsigned int m_reloadTime; + class CUserDB m_table; + bool m_stop; }; #endif