Basic support for streaming of changes via stdout from controller.

This commit is contained in:
Adam Ierymenko 2016-12-15 15:08:47 -08:00
parent 890f6f0d35
commit a54c2b438c
5 changed files with 28 additions and 9 deletions

View File

@ -458,9 +458,9 @@ static bool _parseRule(json &r,ZT_VirtualNetworkRule &rule)
return false;
}
EmbeddedNetworkController::EmbeddedNetworkController(Node *node,const char *dbPath) :
EmbeddedNetworkController::EmbeddedNetworkController(Node *node,const char *dbPath,FILE *feed) :
_threadsStarted(false),
_db(dbPath),
_db(dbPath,feed),
_node(node)
{
OSUtils::mkdir(dbPath);

View File

@ -53,7 +53,12 @@ class Node;
class EmbeddedNetworkController : public NetworkController
{
public:
EmbeddedNetworkController(Node *node,const char *dbPath);
/**
* @param node Parent node
* @param dbPath Path to store data
* @param feed FILE to send feed of all data and changes to (zero-delimited JSON objects) or NULL for none
*/
EmbeddedNetworkController(Node *node,const char *dbPath,FILE *feed);
virtual ~EmbeddedNetworkController();
virtual void init(const Identity &signingId,Sender *sender);

View File

@ -27,14 +27,17 @@ bool JSONDB::put(const std::string &n,const nlohmann::json &obj)
if (!_isValidObjectName(n))
return false;
std::string path(_genPath(n,true));
const std::string path(_genPath(n,true));
if (!path.length())
return false;
std::string buf(obj.dump(2));
const std::string buf(obj.dump(2));
if (!OSUtils::writeFile(path.c_str(),buf))
return false;
if (_feed)
fwrite(buf.c_str(),buf.length()+1,1,_feed);
_E &e = _db[n];
e.obj = obj;
e.lastModifiedOnDisk = OSUtils::getLastModified(path.c_str());
@ -55,7 +58,8 @@ const nlohmann::json &JSONDB::get(const std::string &n,unsigned long maxSinceChe
if (e != _db.end()) {
if ((now - e->second.lastCheck) <= (uint64_t)maxSinceCheck)
return e->second.obj;
std::string path(_genPath(n,false));
const std::string path(_genPath(n,false));
if (!path.length()) // sanity check
return _EMPTY_JSON;
@ -68,13 +72,16 @@ const nlohmann::json &JSONDB::get(const std::string &n,unsigned long maxSinceChe
e->second.obj = nlohmann::json::parse(buf);
e->second.lastModifiedOnDisk = lm; // don't update these if there is a parse error -- try again and again ASAP
e->second.lastCheck = now;
if (_feed)
fwrite(buf.c_str(),buf.length()+1,1,_feed); // it changed, so send to feed (also sends all objects on startup, which we want for Central)
} catch ( ... ) {} // parse errors result in "holding pattern" behavior
}
}
return e->second.obj;
} else {
std::string path(_genPath(n,false));
const std::string path(_genPath(n,false));
if (!path.length())
return _EMPTY_JSON;
@ -87,10 +94,14 @@ const nlohmann::json &JSONDB::get(const std::string &n,unsigned long maxSinceChe
e2.obj = nlohmann::json::parse(buf);
} catch ( ... ) {
e2.obj = _EMPTY_JSON;
buf = "{}";
}
e2.lastModifiedOnDisk = lm;
e2.lastCheck = now;
if (_feed)
fwrite(buf.c_str(),buf.length()+1,1,_feed);
return e2.obj;
}
}

View File

@ -19,6 +19,7 @@
#ifndef ZT_JSONDB_HPP
#define ZT_JSONDB_HPP
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -41,7 +42,8 @@ namespace ZeroTier {
class JSONDB
{
public:
JSONDB(const std::string &basePath) :
JSONDB(const std::string &basePath,FILE *feed) :
_feed(feed),
_basePath(basePath)
{
_reload(_basePath);
@ -106,6 +108,7 @@ private:
inline bool operator!=(const _E &e) const { return (obj != e.obj); }
};
FILE *_feed;
std::string _basePath;
std::map<std::string,_E> _db;
};

View File

@ -875,7 +875,7 @@ public:
for(int i=0;i<3;++i)
_portsBE[i] = Utils::hton((uint16_t)_ports[i]);
_controller = new EmbeddedNetworkController(_node,(_homePath + ZT_PATH_SEPARATOR_S + ZT_CONTROLLER_DB_PATH).c_str());
_controller = new EmbeddedNetworkController(_node,(_homePath + ZT_PATH_SEPARATOR_S + ZT_CONTROLLER_DB_PATH).c_str(),(FILE *)0);
_node->setNetconfMaster((void *)_controller);
#ifdef ZT_ENABLE_CLUSTER