NXDNLookup now use UserDB

This commit is contained in:
SASANO Takayoshi 2020-03-09 22:23:55 +09:00
parent 136a148480
commit f6b5036651
2 changed files with 35 additions and 72 deletions

View file

@ -30,7 +30,6 @@ CThread(),
m_filename(filename), m_filename(filename),
m_reloadTime(reloadTime), m_reloadTime(reloadTime),
m_table(), m_table(),
m_mutex(),
m_stop(false) m_stop(false)
{ {
} }
@ -41,7 +40,7 @@ CNXDNLookup::~CNXDNLookup()
bool CNXDNLookup::read() bool CNXDNLookup::read()
{ {
bool ret = load(); bool ret = m_table.load(m_filename);
if (m_reloadTime > 0U) if (m_reloadTime > 0U)
run(); run();
@ -61,7 +60,7 @@ void CNXDNLookup::entry()
timer.clock(); timer.clock();
if (timer.hasExpired()) { if (timer.hasExpired()) {
load(); m_table.load(m_filename);
timer.start(); timer.start();
} }
} }
@ -81,6 +80,27 @@ void CNXDNLookup::stop()
wait(); 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 CNXDNLookup::find(unsigned int id)
{ {
std::string callsign; std::string callsign;
@ -88,73 +108,19 @@ std::string CNXDNLookup::find(unsigned int id)
if (id == 0xFFFFU) if (id == 0xFFFFU)
return std::string("ALL"); return std::string("ALL");
m_mutex.lock(); class CUserDBentry entry;
if (m_table.lookup(id, &entry)) {
try { callsign = entry.get(keyCALLSIGN);
callsign = m_table.at(id); } else {
} catch (...) {
char text[10U]; char text[10U];
::sprintf(text, "%u", id); ::snprintf(text, sizeof(text), "%u", id);
callsign = std::string(text); callsign = std::string(text);
} }
m_mutex.unlock();
return callsign; return callsign;
} }
bool CNXDNLookup::exists(unsigned int id) bool CNXDNLookup::exists(unsigned int id)
{ {
m_mutex.lock(); return m_table.lookup(id, NULL);
bool found = m_table.count(id) == 1U;
m_mutex.unlock();
return found;
} }
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;
}

View file

@ -20,10 +20,9 @@
#define NXDNLookup_H #define NXDNLookup_H
#include "Thread.h" #include "Thread.h"
#include "Mutex.h" #include "UserDB.h"
#include <string> #include <string>
#include <unordered_map>
class CNXDNLookup : public CThread { class CNXDNLookup : public CThread {
public: public:
@ -35,19 +34,17 @@ public:
virtual void entry(); virtual void entry();
std::string find(unsigned int id); std::string find(unsigned int id);
void findWithName(unsigned int id, class CUserDBentry *entry);
bool exists(unsigned int id); bool exists(unsigned int id);
void stop(); void stop();
private: private:
std::string m_filename; std::string m_filename;
unsigned int m_reloadTime; unsigned int m_reloadTime;
std::unordered_map<unsigned int, std::string> m_table; class CUserDB m_table;
CMutex m_mutex; bool m_stop;
bool m_stop;
bool load();
}; };
#endif #endif