From d4d350a2850aaf78b9abc443e41eaa75e626045d Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 23 Jul 2019 09:29:08 -0700 Subject: [PATCH] Work in progress --- controller/EmbeddedNetworkController.cpp | 4 +- controller/LFDB.cpp | 85 +++++++++++++++++++++--- controller/LFDB.hpp | 12 ++++ objects.mk | 1 + 4 files changed, 91 insertions(+), 11 deletions(-) diff --git a/controller/EmbeddedNetworkController.cpp b/controller/EmbeddedNetworkController.cpp index 007a41127..3ebdccf9e 100644 --- a/controller/EmbeddedNetworkController.cpp +++ b/controller/EmbeddedNetworkController.cpp @@ -501,12 +501,12 @@ void EmbeddedNetworkController::init(const Identity &signingId,Sender *sender) #endif std::string lfJSON; - OSUtils::readFile((_path + ZT_PATH_SEPARATOR_S "local.conf").c_str(),lfJSON); + OSUtils::readFile((_path + ZT_PATH_SEPARATOR_S ".." ZT_PATH_SEPARATOR_S "local.conf").c_str(),lfJSON); if (lfJSON.length() > 0) { nlohmann::json lfConfig(OSUtils::jsonParse(lfJSON)); nlohmann::json &settings = lfConfig["settings"]; if (settings.is_object()) { - nlohmann::json &controllerDb = lfConfig["controllerDb"]; + nlohmann::json &controllerDb = settings["controllerDb"]; if (controllerDb.is_object()) { std::string type = controllerDb["type"]; if (type == "lf") { diff --git a/controller/LFDB.cpp b/controller/LFDB.cpp index 82858b500..9404f310e 100644 --- a/controller/LFDB.cpp +++ b/controller/LFDB.cpp @@ -27,6 +27,7 @@ #include "LFDB.hpp" #include +#include #include #include @@ -53,30 +54,95 @@ LFDB::LFDB(EmbeddedNetworkController *const nc,const Identity &myId,const char * _myId.address().toString(controllerAddress); httplib::Client htcli(_lfNodeHost.c_str(),_lfNodePort,600); + std::ostringstream query; + int64_t timeRangeStart = 0; while (_running) { - std::ostringstream query; + query.clear(); query << '{' << "\"Ranges\":[{" - << "\"Name\": \"com.zerotier.controller.lfdb:" << controllerAddress << "\"" + << "\"Name\": \"com.zerotier.controller.lfdb:" << controllerAddress << "/network\"," + << "\"Range\": [ 0,18446744073709551615 ]" << "}]," + << "\"TimeRange\": [ " << timeRangeStart << ",18446744073709551615 ]," << "\"MaskingKey\":\"" << controllerAddress << "\"," - << "\"Owners\":[\"" << _lfOwnerPublic << "\"]," - << "\"Open\":true" + << "\"Owners\":[\"" << _lfOwnerPublic << "\"]" << '}'; auto resp = htcli.Post("/query",query.str(),"application/json"); if (resp->status == 200) { - fprintf(stderr,"%d %s\n",resp->status,resp->body.c_str()); + nlohmann::json results(OSUtils::jsonParse(resp->body)); + if ((results.is_array())&&(results.size() > 0)) { + for(std::size_t ri=0;ri 0)) { + nlohmann::json &result = rset[0]; + if (result.is_object()) { + nlohmann::json &record = result["Record"]; + if (record.is_object()) { + int64_t ts = record["Timestamp"]; + std::string value = result["Value"]; + nlohmann::json network(OSUtils::jsonParse(value)); + if (network.is_object()) { + std::string idstr = network["id"]; + } + } + } + } + } + } } else { fprintf(stderr,"ERROR: LFDB: %d from node: %s" ZT_EOL_S,resp->status,resp->body.c_str()); } + query.clear(); + query + << '{' + << "\"Ranges\":[{" + << "\"Name\": \"com.zerotier.controller.lfdb:" << controllerAddress << "/network\"," + << "\"Range\": [ 0,18446744073709551615 ]" + << "},{" + << "\"Name\": \"com.zerotier.controller.lfdb:" << controllerAddress << "/network/member\"," + << "\"Range\": [ 0,18446744073709551615 ]" + << "}]," + << "\"TimeRange\": [ " << timeRangeStart << ",18446744073709551615 ]," + << "\"MaskingKey\":\"" << controllerAddress << "\"," + << "\"Owners\":[\"" << _lfOwnerPublic << "\"]" + << '}'; + auto resp = htcli.Post("/query",query.str(),"application/json"); + if (resp->status == 200) { + nlohmann::json results(OSUtils::jsonParse(resp->body)); + if ((results.is_array())&&(results.size() > 0)) { + for(std::size_t ri=0;ri 0)) { + nlohmann::json &result = rset[0]; + if (result.is_object()) { + nlohmann::json &record = result["Record"]; + if (record.is_object()) { + int64_t ts = record["Timestamp"]; + std::string value = result["Value"]; + nlohmann::json member(OSUtils::jsonParse(value)); + if (member.is_object()) { + std::string nwidstr = member["nwid"]; + std::string idstr = member["id"]; + } + } + } + } + } + } + } else { + fprintf(stderr,"ERROR: LFDB: %d from node: %s" ZT_EOL_S,resp->status,resp->body.c_str()); + } + + timeRangeStart = time(nullptr) - 120; // start next query 2m before now to avoid losing updates _ready = true; - for(int k=0;k<10;++k) { + // Delay 2s between queries, checking running flag every 100ms + for(int k=0;k<20;++k) { if (!_running) return; - usleep(100000); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); } } }); @@ -90,8 +156,9 @@ LFDB::~LFDB() bool LFDB::waitForReady() { - while (!_ready) - usleep(10000); + while (!_ready) { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } } bool LFDB::isReady() diff --git a/controller/LFDB.hpp b/controller/LFDB.hpp index a01e78e6e..2fcb47bad 100644 --- a/controller/LFDB.hpp +++ b/controller/LFDB.hpp @@ -73,14 +73,26 @@ protected: struct _MemberState { + _MemberState() : + lastOnlineAddress(), + lastOnlineTime(0), + recordTimestamp(0), + dirty(false), + lastOnlineDirty(false) {} InetAddress lastOnlineAddress; int64_t lastOnlineTime; + int64_t recordTimestamp; bool dirty; bool lastOnlineDirty; }; struct _NetworkState { + _NetworkState() : + members(), + recordTimestamp(0), + dirty(false) {} std::unordered_map members; + int64_t recordTimestamp; bool dirty; }; std::unordered_map _state; diff --git a/objects.mk b/objects.mk index eb348dca2..fcda13fdf 100644 --- a/objects.mk +++ b/objects.mk @@ -30,6 +30,7 @@ ONE_OBJS=\ controller/EmbeddedNetworkController.o \ controller/DB.o \ controller/FileDB.o \ + controller/LFDB.o \ controller/PostgreSQL.o \ controller/RabbitMQ.o \ osdep/ManagedRoute.o \