diff --git a/reflector/Configure.cpp b/reflector/Configure.cpp
index b59db6d..c6546c5 100644
--- a/reflector/Configure.cpp
+++ b/reflector/Configure.cpp
@@ -49,7 +49,6 @@
#define JFILES "Files"
#define JFILEPATH "FilePath"
#define JG3TERMINALPATH "G3TerminalPath"
-#define JHOSTNAME "Hostname"
#define JINTERLINKPATH "InterlinkPath"
#define JIPADDRESSES "IpAddresses"
#define JIPV4BINDING "IPv4Binding"
@@ -73,11 +72,11 @@
#define JREGISTRATIONID "RegistrationID"
#define JREGISTRATIONNAME "RegistrationName"
#define JSPONSOR "Sponsor"
-#define JSUFFIX "Suffix"
#define JSYSOPEMAIL "SysopEmail"
#define JTRANSCODED "Transcoded"
#define JTRANSCODER "Transcoder"
#define JURF "URF"
+#define JURL "URL"
#define JUSRP "USRP"
#define JWHITELISTPATH "WhitelistPath"
#define JYSF "YSF"
@@ -142,11 +141,10 @@ bool CConfigure::ReadData(const std::string &path)
{
ipv4.assign(ss.str());
trim(ipv4);
- ss.clear();
}
+ ss.str(std::string());
if (CURLE_OK == curl.GetURL("https://ipv6.icanhazip.com", ss))
{
- std::cout << ss.str();
ipv6.assign(ss.str());
trim(ipv6);
}
@@ -436,10 +434,8 @@ bool CConfigure::ReadData(const std::string &path)
case ESection::nxdnid: pdb = &g_Keys.nxdniddb; break;
case ESection::ysffreq: pdb = &g_Keys.ysftxrxdb; break;
}
- if (0 == key.compare(JHOSTNAME))
- data[pdb->hostname] = value;
- else if (0 == key.compare(JSUFFIX))
- data[pdb->suffix] = value;
+ if (0 == key.compare(JURL))
+ data[pdb->url] = value;
else if (0 == key.compare(JMODE))
{
if ((0==value.compare("file")) || (0==value.compare("http")) || (0==value.compare("both")))
@@ -562,7 +558,6 @@ bool CConfigure::ReadData(const std::string &path)
data[g_Keys.ip.ipv6address] = ipv6;
else
{
- std::cout << ipv6;
std::cerr << "ERROR: could not detect IPv6 address at this time" << std::endl;
rval = true;
}
@@ -670,8 +665,7 @@ bool CConfigure::ReadData(const std::string &path)
};
for ( auto &item : dbs )
{
- isDefined(ErrorLevel::fatal, item.first, JHOSTNAME, item.second->hostname, rval);
- isDefined(ErrorLevel::fatal, item.first, JSUFFIX, item.second->suffix, rval);
+ isDefined(ErrorLevel::fatal, item.first, JURL, item.second->url, rval);
isDefined(ErrorLevel::fatal, item.first, JMODE, item.second->mode, rval);
isDefined(ErrorLevel::fatal, item.first, JREFRESHMIN, item.second->refreshmin, rval);
isDefined(ErrorLevel::fatal, item.first, JFILEPATH, item.second->filepath, rval);
diff --git a/reflector/JsonKeys.h b/reflector/JsonKeys.h
index 02921bc..83a2b03 100644
--- a/reflector/JsonKeys.h
+++ b/reflector/JsonKeys.h
@@ -48,10 +48,10 @@ struct SJsonKeys {
struct YSLREG { const std::string id, name, description; } ysfreflectordb; }
ysf { "YSFPort", "YSFAutoLinkMod", "YSFDefaultTxFreq", "YSFDefaultRxFreq",
{ "ysfrefdbid", "ysfrefdbname", "ysfrefdbdesc" } };
- struct DB { const std::string hostname, suffix, mode, refreshmin, filepath; }
- dmriddb { "dmrIdDbHost", "dmrIdDbSuffix", "dmrIdDbMode", "dmrIdDbRefresh", "dmrIdDbFilePath" },
- nxdniddb { "nxdnIdDbHost", "nxdnIdDbSuffix", "nxdnIdDbMode", "nxdnIdDbRefresh", "nxdnIdDbFilePath" },
- ysftxrxdb { "ysfIdDbHost", "ysfIdDbSuffix", "ysfIdDbMode", "ysfIdDbRefresh", "ysfIdDbFilePath" };
+ struct DB { const std::string url, mode, refreshmin, filepath; }
+ dmriddb { "dmrIdDbUrl", "dmrIdDbMode", "dmrIdDbRefresh", "dmrIdDbFilePath" },
+ nxdniddb { "nxdnIdDbUrl", "nxdnIdDbMode", "nxdnIdDbRefresh", "nxdnIdDbFilePath" },
+ ysftxrxdb { "ysfIdDbUrl", "ysfIdDbMode", "ysfIdDbRefresh", "ysfIdDbFilePath" };
struct FILES { const std::string pid, json, white, black, interlink, terminal; }
files { "pidFilePath", "jsonFilePath", "whitelistFilePath", "blacklistFilePath", "interlinkFilePath", "g3TerminalFilePath" };
};
diff --git a/reflector/Lookup.cpp b/reflector/Lookup.cpp
index dcb8599..5d4d07e 100644
--- a/reflector/Lookup.cpp
+++ b/reflector/Lookup.cpp
@@ -16,16 +16,13 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
-#include
+#include
+#include
#include
#include
+#include "CurlGet.h"
#include "Lookup.h"
-CLookup::~CLookup()
-{
- LookupClose();
-}
-
void CLookup::LookupClose()
{
keep_running = false;
@@ -52,54 +49,78 @@ void CLookup::LookupInit()
void CLookup::Thread()
{
+ const unsigned long wait_cycles = m_Refresh * 6u; // the number of while loops in m_Refresh
+ unsigned long count = 0;
while (keep_running)
{
- bool loaded = false;
+ std::stringstream ss;
+ bool http_loaded = false;
+ bool file_loaded = false;
- if (m_Type != ERefreshType::file) // get the HTTP contents
+ // load http section first, if configured and m_Refresh minutes have lapsed
+ // on the first pass through this while loop (count == 0)
+ if (ERefreshType::file != m_Type && 0ul == count++ % wait_cycles)
{
- CBuffer buf;
- loaded = LoadContentHttp(buf);
- if (loaded)
- {
- Lock();
- ClearContents();
- RefreshContentHttp(buf);
- Unlock();
- }
+ // if SIG_INT was received at this point in time,
+ // in might take a bit more than 10 seconds to soft close
+ http_loaded = LoadContentHttp(ss);
}
-
- if (m_Type != ERefreshType::http) // get the file contents
+ // load the file if http was loaded or if we haven't loaded since the last mod time
+ if (ERefreshType::http != m_Type)
{
- auto lastTime = GetLastModTime();
- if (lastTime > m_LastModTime)
+ GetLastModTime();
+ if (http_loaded || m_LastLoadTime < m_LastModTime)
{
- CBuffer buf;
- if (LoadContentFile(buf))
- {
- Lock();
- if (! loaded)
- ClearContents();
- RefreshContentFile(buf);
- Unlock();
- m_LastModTime = lastTime;
- }
+ file_loaded = LoadContentFile(ss);
+ time(&m_LastLoadTime);
}
}
- // now wait for a while...
- for (unsigned i=0; i<20u*m_Refresh && keep_running; i++)
- std::this_thread::sleep_for(std::chrono::seconds(3));
+ // now update the map(s) if anything was loaded
+ if (http_loaded || file_loaded)
+ {
+ Lock();
+ // if m_Type == ERefreshType::both, and if something was deleted from the file,
+ // it won't be purged from the map(s) until http is loaded
+ // It would be a lot of work (iterating on an unordered_map) to do otherwise!
+ if (http_loaded || ERefreshType::file == m_Type)
+ ClearContents();
+ UpdateContent(ss);
+ Unlock();
+ }
+
+ // now wait for 10 seconds
+ std::this_thread::sleep_for(std::chrono::seconds(10));
}
}
+bool CLookup::LoadContentHttp(std::stringstream &ss)
+{
+ CCurlGet get;
+ auto code = get.GetURL(m_Url, ss);
+ return CURLE_OK == code;
+}
+
+bool CLookup::LoadContentFile(std::stringstream &ss)
+{
+ bool rval = false;
+ std::ifstream file(m_Path);
+ if ( file )
+ {
+ ss << file.rdbuf();
+ file.close();
+ rval = true;
+ }
+ return rval;
+}
+
bool CLookup::Dump()
{
- CBuffer buf;
+ std::stringstream ss;
LoadParameters();
- auto rval = LoadContentHttp(buf);
+ auto rval = LoadContentHttp(ss);
if (rval)
- std::cout << (const char *)buf.data();
+ std::cout << ss.str() << std::endl;
return rval;
}
diff --git a/reflector/Lookup.h b/reflector/Lookup.h
index db52d2e..f921c81 100644
--- a/reflector/Lookup.h
+++ b/reflector/Lookup.h
@@ -18,20 +18,20 @@
#pragma once
-#include