[Zrouter-src] ZRouter.org: push to zconf++ zconf++/platform/bsd_interfaces.cc ...

zrouter-src at zrouter.org zrouter-src at zrouter.org
Mon Jan 9 15:47:11 UTC 2012


details:   /rev/6af26c1b5997
changeset: 32:6af26c1b5997
user:      "Nicolai Petri <nicolai at petri.dk>"
date:      Mon Jan 09 16:19:50 2012 +0100
description:
Implement forced initialization order of extensions by adding a priority to registration call.
Parse any command line options into arguments.. Supports "key" and "key=value" options.

diffstat:

 zconf++/platform/bsd_interfaces.cc |   2 +-
 zconf++/platform/freebsd.cc        |   2 +-
 zconf++/platform/posix.cc          |   2 +-
 zconf++/zconfig.cc                 |   2 +-
 zconf++/zextension.h               |  13 ++++++++++---
 zconf++/zrouterhttp.cc             |  33 +++++++++++++++++++++++++--------
 6 files changed, 39 insertions(+), 15 deletions(-)

diffs (155 lines):

diff -r 0f6b0c730ae4 -r 6af26c1b5997 zconf++/platform/bsd_interfaces.cc
--- a/zconf++/platform/bsd_interfaces.cc	Mon Jan 09 01:34:15 2012 +0100
+++ b/zconf++/platform/bsd_interfaces.cc	Mon Jan 09 16:19:50 2012 +0100
@@ -133,7 +133,7 @@
 static class Platform_BSD_Interfaces {
 	public:
 	Platform_BSD_Interfaces() {
-		ZExtensionManager::register_extension("Platform::BSD_Interfaces", extension_init);
+		ZExtensionManager::register_extension("Platform::BSD_Interfaces", 100, extension_init);
 	}
 	static bool extension_init(boost::asio::io_service &io, Model &model) {
 		model["system"]["networking"]["interfaces"]=GetInterfaces();
diff -r 0f6b0c730ae4 -r 6af26c1b5997 zconf++/platform/freebsd.cc
--- a/zconf++/platform/freebsd.cc	Mon Jan 09 01:34:15 2012 +0100
+++ b/zconf++/platform/freebsd.cc	Mon Jan 09 16:19:50 2012 +0100
@@ -392,7 +392,7 @@
 		static class Platform_FreeBSD_KEnv_Module {
 			public:
 			Platform_FreeBSD_KEnv_Module() {
-				ZExtensionManager::register_extension("Platform::FreeBSD", extension_init);
+				ZExtensionManager::register_extension("Platform::FreeBSD", 90, extension_init);
 			}
 			static bool extension_init(boost::asio::io_service &io, Model &model) {
 				model["state"]["kernel_environment"]=kdumpenv();
diff -r 0f6b0c730ae4 -r 6af26c1b5997 zconf++/platform/posix.cc
--- a/zconf++/platform/posix.cc	Mon Jan 09 01:34:15 2012 +0100
+++ b/zconf++/platform/posix.cc	Mon Jan 09 16:19:50 2012 +0100
@@ -55,7 +55,7 @@
 static class Platform_Posix_Module {
 	public:
 	Platform_Posix_Module() {
-		ZExtensionManager::register_extension("Platform::Posix", extension_init);
+		ZExtensionManager::register_extension("Platform::Posix", 50, extension_init);
 	}
 	static bool extension_init(boost::asio::io_service &io, Model &model) {
 		struct timespec curtime;
diff -r 0f6b0c730ae4 -r 6af26c1b5997 zconf++/zconfig.cc
--- a/zconf++/zconfig.cc	Mon Jan 09 01:34:15 2012 +0100
+++ b/zconf++/zconfig.cc	Mon Jan 09 16:19:50 2012 +0100
@@ -140,7 +140,7 @@
 	public:
 		Z_Config_Module() : m_running(NULL), m_configured(NULL) {
 			// FIXME: Call module ZZ to initialize it last
-			ZExtensionManager::register_extension("ZZ_Config_Module", extension_init);
+			ZExtensionManager::register_extension("ZZ_Config_Module", 10, extension_init);
 		}
 		static bool extension_init(boost::asio::io_service &io, Model &model);
 } _module;
diff -r 0f6b0c730ae4 -r 6af26c1b5997 zconf++/zextension.h
--- a/zconf++/zextension.h	Mon Jan 09 01:34:15 2012 +0100
+++ b/zconf++/zextension.h	Mon Jan 09 16:19:50 2012 +0100
@@ -27,6 +27,7 @@
 #define __ZEXTENSION__H
 
 #include <map>
+//#include <multimap>
 #include <string>
 #include <boost/asio.hpp>
 #include <boost/function.hpp>
@@ -34,14 +35,20 @@
 #include "model.h"
 
 namespace ZRouter {
+	typedef bool (*FP_ExtensionInitializer)(boost::asio::io_service&, Model&);
+	struct TExtensionData {
+		std::string name;
+		int init_priority;
+		FP_ExtensionInitializer initializer;
+	};
 	//boost::function<bool (boost::asio::io_service &, ZRouter::Model &model)> FP_ExtensionInitializer;
-	typedef bool (*FP_ExtensionInitializer)(boost::asio::io_service&, Model&);
 	//typedef bool (int) FP_ExtensionInitializer;
-	typedef std::map<std::string, FP_ExtensionInitializer> T_ZExtensionMap; 
+	//typedef std::map<std::string, FP_ExtensionInitializer> T_ZExtensionMap; 
+	typedef std::multimap<int, TExtensionData> T_ZExtensionMap; 
 	class ZExtensionManager {
 		public:
 			ZExtensionManager();
-			static void register_extension(std::string, FP_ExtensionInitializer);
+			static void register_extension(std::string, int priority, FP_ExtensionInitializer);
 	};
 	//extern T_ZExtensionMap extension_init_map;
 }
diff -r 0f6b0c730ae4 -r 6af26c1b5997 zconf++/zrouterhttp.cc
--- a/zconf++/zrouterhttp.cc	Mon Jan 09 01:34:15 2012 +0100
+++ b/zconf++/zrouterhttp.cc	Mon Jan 09 16:19:50 2012 +0100
@@ -42,19 +42,26 @@
 
 using namespace ZRouter;
 
-static ZRouter::T_ZExtensionMap extension_init_map;
+//static ZRouter::T_ZExtensionMap extension_map;
+static ZRouter::T_ZExtensionMap extension_map;
 
 ZExtensionManager::ZExtensionManager() {
 	cout << "ZExtensionManager::" << endl;
 }
 
-void ZExtensionManager::register_extension(std::string mod_name, FP_ExtensionInitializer ext_initiator) {
-	extension_init_map[mod_name] = ext_initiator;
+void ZExtensionManager::register_extension(std::string mod_name, int mod_priority, FP_ExtensionInitializer ext_initiator) {
+	TExtensionData d;
+	d.name = mod_name;
+	d.init_priority = mod_priority;
+	d.initializer = ext_initiator;
+	cout << "Registering extension " << mod_name << " with init priority " << mod_priority << endl;
+	extension_map.insert(std::pair<const int, ZRouter::TExtensionData>(mod_priority, d));
+	//extension_map[mod_name] = d;
 }
 
 
 
-int main()
+int main(int argc, char **argv)
 {
 	// Make single IO handler
   boost::asio::io_service io;
@@ -70,6 +77,16 @@
 #endif
 	// Allocate object model
 	Model::Model model;
+	model["args"].map();
+	for (int ac=1; ac < argc; ac++) {
+		string arg = argv[ac];
+		string::size_type pos = arg.find("="); 
+		if (pos != string::npos) {
+			model["args"][argv[ac]] = string(arg, pos + 1);
+		} else {
+			model["args"][argv[ac]] = Leaf(true);
+		}
+	}	
 
 	// and fill with data
 	model.populate();
@@ -85,16 +102,16 @@
 
 	cout << "Loading extensions" << endl; 
 	// Add all available extensions
-	ZRouter::T_ZExtensionMap::iterator ext_ptr = extension_init_map.begin();
-	while (ext_ptr != extension_init_map.end()) {
-		cout << " initializing " << (*ext_ptr).first << endl;
+	ZRouter::T_ZExtensionMap::iterator ext_ptr = extension_map.begin();
+	while (ext_ptr != extension_map.end()) {
+		cout << " initializing " << (*ext_ptr).second.name << " using priority " << (*ext_ptr).second.init_priority << endl;
 		Leaf extObj;
 		extObj["loaded"] = true;
 		extObj["version"] = "unknown";
 		extObj["vendor"] = "unknown";
 			
 		model["system"]["extensions"][(*ext_ptr).first] = extObj;
-		(*ext_ptr).second(io, model);
+		(*ext_ptr).second.initializer(io, model);
 		ext_ptr++;
 	}
 	cout << "Loading extensions completed" << endl; 


More information about the Zrouter-src mailing list