Work in progress

This commit is contained in:
Adam Ierymenko 2019-07-23 09:29:08 -07:00
parent 5edd04638d
commit d4d350a285
4 changed files with 91 additions and 11 deletions

View File

@ -501,12 +501,12 @@ void EmbeddedNetworkController::init(const Identity &signingId,Sender *sender)
#endif #endif
std::string lfJSON; 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) { if (lfJSON.length() > 0) {
nlohmann::json lfConfig(OSUtils::jsonParse(lfJSON)); nlohmann::json lfConfig(OSUtils::jsonParse(lfJSON));
nlohmann::json &settings = lfConfig["settings"]; nlohmann::json &settings = lfConfig["settings"];
if (settings.is_object()) { if (settings.is_object()) {
nlohmann::json &controllerDb = lfConfig["controllerDb"]; nlohmann::json &controllerDb = settings["controllerDb"];
if (controllerDb.is_object()) { if (controllerDb.is_object()) {
std::string type = controllerDb["type"]; std::string type = controllerDb["type"];
if (type == "lf") { if (type == "lf") {

View File

@ -27,6 +27,7 @@
#include "LFDB.hpp" #include "LFDB.hpp"
#include <thread> #include <thread>
#include <chrono>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@ -53,30 +54,95 @@ LFDB::LFDB(EmbeddedNetworkController *const nc,const Identity &myId,const char *
_myId.address().toString(controllerAddress); _myId.address().toString(controllerAddress);
httplib::Client htcli(_lfNodeHost.c_str(),_lfNodePort,600); httplib::Client htcli(_lfNodeHost.c_str(),_lfNodePort,600);
while (_running) {
std::ostringstream query; std::ostringstream query;
int64_t timeRangeStart = 0;
while (_running) {
query.clear();
query query
<< '{' << '{'
<< "\"Ranges\":[{" << "\"Ranges\":[{"
<< "\"Name\": \"com.zerotier.controller.lfdb:" << controllerAddress << "\"" << "\"Name\": \"com.zerotier.controller.lfdb:" << controllerAddress << "/network\","
<< "\"Range\": [ 0,18446744073709551615 ]"
<< "}]," << "}],"
<< "\"TimeRange\": [ " << timeRangeStart << ",18446744073709551615 ],"
<< "\"MaskingKey\":\"" << controllerAddress << "\"," << "\"MaskingKey\":\"" << controllerAddress << "\","
<< "\"Owners\":[\"" << _lfOwnerPublic << "\"]," << "\"Owners\":[\"" << _lfOwnerPublic << "\"]"
<< "\"Open\":true"
<< '}'; << '}';
auto resp = htcli.Post("/query",query.str(),"application/json"); auto resp = htcli.Post("/query",query.str(),"application/json");
if (resp->status == 200) { 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<results.size();++ri) {
nlohmann::json &rset = results[ri];
if ((rset.is_array())&&(rset.size() > 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 { } else {
fprintf(stderr,"ERROR: LFDB: %d from node: %s" ZT_EOL_S,resp->status,resp->body.c_str()); 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<results.size();++ri) {
nlohmann::json &rset = results[ri];
if ((rset.is_array())&&(rset.size() > 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; _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) if (!_running)
return; return;
usleep(100000); std::this_thread::sleep_for(std::chrono::milliseconds(100));
} }
} }
}); });
@ -90,8 +156,9 @@ LFDB::~LFDB()
bool LFDB::waitForReady() bool LFDB::waitForReady()
{ {
while (!_ready) while (!_ready) {
usleep(10000); std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
} }
bool LFDB::isReady() bool LFDB::isReady()

View File

@ -73,14 +73,26 @@ protected:
struct _MemberState struct _MemberState
{ {
_MemberState() :
lastOnlineAddress(),
lastOnlineTime(0),
recordTimestamp(0),
dirty(false),
lastOnlineDirty(false) {}
InetAddress lastOnlineAddress; InetAddress lastOnlineAddress;
int64_t lastOnlineTime; int64_t lastOnlineTime;
int64_t recordTimestamp;
bool dirty; bool dirty;
bool lastOnlineDirty; bool lastOnlineDirty;
}; };
struct _NetworkState struct _NetworkState
{ {
_NetworkState() :
members(),
recordTimestamp(0),
dirty(false) {}
std::unordered_map<uint64_t,_MemberState> members; std::unordered_map<uint64_t,_MemberState> members;
int64_t recordTimestamp;
bool dirty; bool dirty;
}; };
std::unordered_map<uint64_t,_NetworkState> _state; std::unordered_map<uint64_t,_NetworkState> _state;

View File

@ -30,6 +30,7 @@ ONE_OBJS=\
controller/EmbeddedNetworkController.o \ controller/EmbeddedNetworkController.o \
controller/DB.o \ controller/DB.o \
controller/FileDB.o \ controller/FileDB.o \
controller/LFDB.o \
controller/PostgreSQL.o \ controller/PostgreSQL.o \
controller/RabbitMQ.o \ controller/RabbitMQ.o \
osdep/ManagedRoute.o \ osdep/ManagedRoute.o \