diff options
author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2016-12-15 15:08:47 -0800 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2016-12-15 15:08:47 -0800 |
commit | a54c2b438c0abc574039055ecfc70dd829e2cade (patch) | |
tree | 0d606a4713382b7c11dddcab3bd3d09f17d2e915 | |
parent | 890f6f0d35cc715f759cb3014c536a8801bd0c99 (diff) | |
download | infinitytier-a54c2b438c0abc574039055ecfc70dd829e2cade.tar.gz infinitytier-a54c2b438c0abc574039055ecfc70dd829e2cade.zip |
Basic support for streaming of changes via stdout from controller.
-rw-r--r-- | controller/EmbeddedNetworkController.cpp | 4 | ||||
-rw-r--r-- | controller/EmbeddedNetworkController.hpp | 7 | ||||
-rw-r--r-- | controller/JSONDB.cpp | 19 | ||||
-rw-r--r-- | controller/JSONDB.hpp | 5 | ||||
-rw-r--r-- | service/OneService.cpp | 2 |
5 files changed, 28 insertions, 9 deletions
diff --git a/controller/EmbeddedNetworkController.cpp b/controller/EmbeddedNetworkController.cpp index 974936e4..df20d4ce 100644 --- a/controller/EmbeddedNetworkController.cpp +++ b/controller/EmbeddedNetworkController.cpp @@ -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); diff --git a/controller/EmbeddedNetworkController.hpp b/controller/EmbeddedNetworkController.hpp index cde6522d..37c067ff 100644 --- a/controller/EmbeddedNetworkController.hpp +++ b/controller/EmbeddedNetworkController.hpp @@ -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); diff --git a/controller/JSONDB.cpp b/controller/JSONDB.cpp index a9e9d05b..dfea9dd1 100644 --- a/controller/JSONDB.cpp +++ b/controller/JSONDB.cpp @@ -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; } } diff --git a/controller/JSONDB.hpp b/controller/JSONDB.hpp index bd1ae5a5..40113655 100644 --- a/controller/JSONDB.hpp +++ b/controller/JSONDB.hpp @@ -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; }; diff --git a/service/OneService.cpp b/service/OneService.cpp index 2e256307..3a7edacb 100644 --- a/service/OneService.cpp +++ b/service/OneService.cpp @@ -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 |