2
0
Эх сурвалжийг харах

Basic support for streaming of changes via stdout from controller.

Adam Ierymenko 8 жил өмнө
parent
commit
a54c2b438c

+ 2 - 2
controller/EmbeddedNetworkController.cpp

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

+ 6 - 1
controller/EmbeddedNetworkController.hpp

@@ -53,7 +53,12 @@ class Node;
 class EmbeddedNetworkController : public NetworkController
 class EmbeddedNetworkController : public NetworkController
 {
 {
 public:
 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 ~EmbeddedNetworkController();
 
 
 	virtual void init(const Identity &signingId,Sender *sender);
 	virtual void init(const Identity &signingId,Sender *sender);

+ 15 - 4
controller/JSONDB.cpp

@@ -27,14 +27,17 @@ bool JSONDB::put(const std::string &n,const nlohmann::json &obj)
 	if (!_isValidObjectName(n))
 	if (!_isValidObjectName(n))
 		return false;
 		return false;
 
 
-	std::string path(_genPath(n,true));
+	const std::string path(_genPath(n,true));
 	if (!path.length())
 	if (!path.length())
 		return false;
 		return false;
 
 
-	std::string buf(obj.dump(2));
+	const std::string buf(obj.dump(2));
 	if (!OSUtils::writeFile(path.c_str(),buf))
 	if (!OSUtils::writeFile(path.c_str(),buf))
 		return false;
 		return false;
 
 
+	if (_feed)
+		fwrite(buf.c_str(),buf.length()+1,1,_feed);
+
 	_E &e = _db[n];
 	_E &e = _db[n];
 	e.obj = obj;
 	e.obj = obj;
 	e.lastModifiedOnDisk = OSUtils::getLastModified(path.c_str());
 	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 (e != _db.end()) {
 		if ((now - e->second.lastCheck) <= (uint64_t)maxSinceCheck)
 		if ((now - e->second.lastCheck) <= (uint64_t)maxSinceCheck)
 			return e->second.obj;
 			return e->second.obj;
-		std::string path(_genPath(n,false));
+
+		const std::string path(_genPath(n,false));
 		if (!path.length()) // sanity check
 		if (!path.length()) // sanity check
 			return _EMPTY_JSON;
 			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.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.lastModifiedOnDisk = lm; // don't update these if there is a parse error -- try again and again ASAP
 					e->second.lastCheck = now;
 					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
 				} catch ( ... ) {} // parse errors result in "holding pattern" behavior
 			}
 			}
 		}
 		}
 
 
 		return e->second.obj;
 		return e->second.obj;
 	} else {
 	} else {
-		std::string path(_genPath(n,false));
+		const std::string path(_genPath(n,false));
 		if (!path.length())
 		if (!path.length())
 			return _EMPTY_JSON;
 			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);
 			e2.obj = nlohmann::json::parse(buf);
 		} catch ( ... ) {
 		} catch ( ... ) {
 			e2.obj = _EMPTY_JSON;
 			e2.obj = _EMPTY_JSON;
+			buf = "{}";
 		}
 		}
 		e2.lastModifiedOnDisk = lm;
 		e2.lastModifiedOnDisk = lm;
 		e2.lastCheck = now;
 		e2.lastCheck = now;
 
 
+		if (_feed)
+			fwrite(buf.c_str(),buf.length()+1,1,_feed);
+
 		return e2.obj;
 		return e2.obj;
 	}
 	}
 }
 }

+ 4 - 1
controller/JSONDB.hpp

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

+ 1 - 1
service/OneService.cpp

@@ -875,7 +875,7 @@ public:
 			for(int i=0;i<3;++i)
 			for(int i=0;i<3;++i)
 				_portsBE[i] = Utils::hton((uint16_t)_ports[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);
 			_node->setNetconfMaster((void *)_controller);
 
 
 #ifdef ZT_ENABLE_CLUSTER
 #ifdef ZT_ENABLE_CLUSTER