Load the DMR Id lookup table periodically.

This commit is contained in:
Jonathan Naylor
2016-10-10 19:15:30 +01:00
parent 878b4d6d73
commit 40c086bad8
7 changed files with 157 additions and 47 deletions

View File

@@ -17,6 +17,7 @@
*/
#include "DMRLookup.h"
#include "Timer.h"
#include "Log.h"
#include <cstdio>
@@ -24,8 +25,13 @@
#include <cstring>
#include <cctype>
CDMRLookup::CDMRLookup(const std::string& filename) :
m_filename(filename)
CDMRLookup::CDMRLookup(const std::string& filename, unsigned int reloadTime) :
CThread(),
m_filename(filename),
m_reloadTime(reloadTime),
m_table(),
m_mutex(),
m_stop(false)
{
}
@@ -34,6 +40,70 @@ CDMRLookup::~CDMRLookup()
}
bool CDMRLookup::read()
{
bool ret = load();
if (m_reloadTime > 0U)
run();
return ret;
}
void CDMRLookup::entry()
{
LogInfo("Started the DMR Id lookup reload thread");
CTimer timer(1U, 3600U * m_reloadTime);
timer.start();
while (!m_stop) {
sleep(1000U);
timer.clock();
if (timer.hasExpired()) {
load();
timer.start();
}
}
LogInfo("Stopped the DMR Id lookup reload thread");
}
void CDMRLookup::stop()
{
if (m_reloadTime == 0U) {
delete this;
return;
}
m_stop = true;
wait();
}
std::string CDMRLookup::find(unsigned int id)
{
std::string callsign;
if (id == 0xFFFFFFU)
return std::string("ALL");
m_mutex.lock();
try {
callsign = m_table.at(id);
} catch (...) {
char text[10U];
::sprintf(text, "%u", id);
callsign = std::string(text);
}
m_mutex.unlock();
return callsign;
}
bool CDMRLookup::load()
{
FILE* fp = ::fopen(m_filename.c_str(), "rt");
if (fp == NULL) {
@@ -41,6 +111,11 @@ bool CDMRLookup::read()
return false;
}
m_mutex.lock();
// Remove the old entries
m_table.clear();
char buffer[100U];
while (::fgets(buffer, 100U, fp) != NULL) {
if (buffer[0U] == '#')
@@ -58,6 +133,8 @@ bool CDMRLookup::read()
}
}
m_mutex.unlock();
::fclose(fp);
size_t size = m_table.size();
@@ -67,22 +144,4 @@ bool CDMRLookup::read()
LogInfo("Loaded %u Ids to the callsign lookup table", size);
return true;
}
std::string CDMRLookup::find(unsigned int id) const
{
std::string callsign;
if (id == 0xFFFFFFU)
return std::string("ALL");
try {
callsign = m_table.at(id);
} catch (...) {
char text[10U];
::sprintf(text, "%u", id);
callsign = std::string(text);
}
return callsign;
}
}