[Zrouter-src] ZRouter.org: push to zconf++ libzhttp/i_request_handler.hpp libz...
zrouter-src at zrouter.org
zrouter-src at zrouter.org
Fri Feb 3 10:04:48 UTC 2012
details: /rev/728193b62c30
changeset: 59:728193b62c30
user: "Nicolai Petri <nicolai at petri.dk>"
date: Fri Feb 03 11:04:19 2012 +0100
description:
Put mainloop into own class and add iApp* interface that can be used for obtaining http server and model pointers.
Pass iApp* at extension initialization.
diffstat:
libzhttp/i_request_handler.hpp | 10 ++
libzhttp/server.cpp | 3 +
libzhttp/server.hpp | 4 +-
zconf++/platform/bsd_interfaces.cc | 2 +-
zconf++/platform/freebsd.cc | 2 +-
zconf++/platform/posix.cc | 2 +-
zconf++/zconfig.cc | 4 +-
zconf++/zextension.h | 15 +++-
zconf++/zrouterhttp.cc | 140 +++++++++++++++++++++---------------
9 files changed, 116 insertions(+), 66 deletions(-)
diffs (290 lines):
diff -r 2a89ece7f945 -r 728193b62c30 libzhttp/i_request_handler.hpp
--- a/libzhttp/i_request_handler.hpp Tue Jan 31 21:43:40 2012 +0100
+++ b/libzhttp/i_request_handler.hpp Fri Feb 03 11:04:19 2012 +0100
@@ -9,6 +9,7 @@
{
struct reply;
struct request;
+
}
namespace handlers
{
@@ -26,5 +27,14 @@
};
}
+ namespace server
+ {
+ class iHTTPServer {
+ public:
+ virtual void register_handler(handlers::i_request_handler *handler)=0;
+ virtual ~iHTTPServer() {};
+
+ };
+ }
}
#endif // __I_REQUEST_HANDLER__H
diff -r 2a89ece7f945 -r 728193b62c30 libzhttp/server.cpp
--- a/libzhttp/server.cpp Tue Jan 31 21:43:40 2012 +0100
+++ b/libzhttp/server.cpp Fri Feb 03 11:04:19 2012 +0100
@@ -37,6 +37,9 @@
boost::asio::placeholders::error));
}
+ server::~server() {
+ }
+
void server::register_handler(handlers::i_request_handler *handler) {
request_handler_.register_handler(handler);
}
diff -r 2a89ece7f945 -r 728193b62c30 libzhttp/server.hpp
--- a/libzhttp/server.hpp Tue Jan 31 21:43:40 2012 +0100
+++ b/libzhttp/server.hpp Fri Feb 03 11:04:19 2012 +0100
@@ -25,7 +25,8 @@
/// The top-level class of the HTTP server.
class server
- : private boost::noncopyable
+ : public iHTTPServer
+ , private boost::noncopyable
{
public:
/// Construct the server to listen on the specified TCP address and port, and
@@ -33,6 +34,7 @@
explicit server(boost::asio::io_service &io_svc, const std::string& address, const std::string& port,
const std::string& doc_root);
+ virtual ~server();
/// Run the server's io_service loop.
void run();
diff -r 2a89ece7f945 -r 728193b62c30 zconf++/platform/bsd_interfaces.cc
--- a/zconf++/platform/bsd_interfaces.cc Tue Jan 31 21:43:40 2012 +0100
+++ b/zconf++/platform/bsd_interfaces.cc Fri Feb 03 11:04:19 2012 +0100
@@ -135,7 +135,7 @@
Platform_BSD_Interfaces() {
ZExtensionManager::register_extension("Platform::BSD_Interfaces", 100, extension_init);
}
- static bool extension_init(boost::asio::io_service &io, Model &model) {
+ static bool extension_init(iApp *app, boost::asio::io_service &io, Model &model) {
model["system"]["networking"]["interfaces"]=GetInterfaces();
//model["system"].get_obj()["hostname"]=getHostname();
//model["system"]["extensions"]["Platform_Posix_Module"]["vendor"]="ZRouter.org";
diff -r 2a89ece7f945 -r 728193b62c30 zconf++/platform/freebsd.cc
--- a/zconf++/platform/freebsd.cc Tue Jan 31 21:43:40 2012 +0100
+++ b/zconf++/platform/freebsd.cc Fri Feb 03 11:04:19 2012 +0100
@@ -394,7 +394,7 @@
Platform_FreeBSD_KEnv_Module() {
ZExtensionManager::register_extension("Platform::FreeBSD", 90, extension_init);
}
- static bool extension_init(boost::asio::io_service &io, Model &model) {
+ static bool extension_init(iApp *app, boost::asio::io_service &io, Model &model) {
model["state"]["kernel_environment"]=kdumpenv();
model["system"]["networking"]["arp"]=ARP::Query();
return true;
diff -r 2a89ece7f945 -r 728193b62c30 zconf++/platform/posix.cc
--- a/zconf++/platform/posix.cc Tue Jan 31 21:43:40 2012 +0100
+++ b/zconf++/platform/posix.cc Fri Feb 03 11:04:19 2012 +0100
@@ -57,7 +57,7 @@
Platform_Posix_Module() {
ZExtensionManager::register_extension("Platform::Posix", 50, extension_init);
}
- static bool extension_init(boost::asio::io_service &io, Model &model) {
+ static bool extension_init(iApp *app, boost::asio::io_service &io, Model &model) {
struct timespec curtime;
struct timespec uptime;
clock_gettime(CLOCK_REALTIME_FAST, &curtime);
diff -r 2a89ece7f945 -r 728193b62c30 zconf++/zconfig.cc
--- a/zconf++/zconfig.cc Tue Jan 31 21:43:40 2012 +0100
+++ b/zconf++/zconfig.cc Fri Feb 03 11:04:19 2012 +0100
@@ -140,10 +140,10 @@
// FIXME: Call module ZZ to initialize it last
ZExtensionManager::register_extension("ZZ_Config_Module", 10, extension_init);
}
- static bool extension_init(boost::asio::io_service &io, Model &model);
+ static bool extension_init(iApp *app, boost::asio::io_service &io, Model &model);
} _module;
-bool Z_Config_Module::extension_init(boost::asio::io_service &io, Model &model) {
+bool Z_Config_Module::extension_init(iApp *app, boost::asio::io_service &io, Model &model) {
//model["system"]["extensions"]["Platform_Posix_Module"]["vendor"]="ZRouter.org";
return _module.self_init(io, model);
}
diff -r 2a89ece7f945 -r 728193b62c30 zconf++/zextension.h
--- a/zconf++/zextension.h Tue Jan 31 21:43:40 2012 +0100
+++ b/zconf++/zextension.h Fri Feb 03 11:04:19 2012 +0100
@@ -34,9 +34,22 @@
#include "model.h"
+// Forward declaration of http server class
+namespace http {
+ namespace server {
+ class iHTTPServer;
+ }
+}
+
namespace ZRouter
{
- typedef bool (*FP_ExtensionInitializer)(boost::asio::io_service&, Model&);
+ class iApp {
+ public:
+ virtual http::server::iHTTPServer* getIHTTP() = 0;
+ virtual Model& getModel() = 0;
+ };
+
+ typedef bool (*FP_ExtensionInitializer)(iApp*, boost::asio::io_service&, Model&);
struct TExtensionData
{
std::string name;
diff -r 2a89ece7f945 -r 728193b62c30 zconf++/zrouterhttp.cc
--- a/zconf++/zrouterhttp.cc Tue Jan 31 21:43:40 2012 +0100
+++ b/zconf++/zrouterhttp.cc Fri Feb 03 11:04:19 2012 +0100
@@ -58,66 +58,88 @@
//extension_map[mod_name] = d;
}
+ class App : public iApp {
+ protected:
+ http::server::server* mHttpServer;
+ ZRouter::Model::Model model;
+ public:
+ App()
+ :mHttpServer(NULL)
+ {
+ // Init random seed stuff
+ srandomdev();
+ };
+ virtual Model& getModel() {
+ return model;
+ }
+ virtual http::server::iHTTPServer* getIHTTP() {
+ return mHttpServer;
+ }
+ int Run(int argc, char **argv) {
+ // Make single IO handler
+ boost::asio::io_service io;
+
+ // Register signal handlers so that the daemon may be shut down. You may
+ // also want to register for other signals, such as SIGHUP to trigger a
+ // re-read of a configuration file.
+ #ifdef WITH_BOOST_SIGNAL_SET
+ // Not yet, waiting for new boost to be default in freebsd ports.
+ boost::asio::signal_set signals(io_service, SIGINT, SIGTERM);
+ signals.async_wait(
+ boost::bind(&boost::asio::io_service::stop, &io_service));
+ #endif
+ // Allocate object model
+ model["args"].map();
+ for (int ac=1; ac < argc; ac++) {
+ std::string arg = argv[ac];
+ std::string::size_type pos = arg.find("=");
+ if (pos != std::string::npos) {
+ model["args"][argv[ac]] = std::string(arg, pos + 1);
+ }
+ else {
+ model["args"][argv[ac]] = ZRouter::Leaf(true);
+ }
+ }
+
+ // and fill with data
+ model.populate();
+
+ // Create our custom http model interface handler
+ ZRouter::http_json_handler json_handler(model);
+
+ // Start server
+ mHttpServer = new http::server::server(io, "0.0.0.0", "9080", "html");
+
+ // Register model interface
+ mHttpServer->register_handler(&json_handler);
+
+ std::cout << "Loading extensions" << std::endl;
+ // Add all available extensions
+ ZRouter::T_ZExtensionMap::iterator ext_ptr = ZRouter::extension_map.begin();
+ while (ext_ptr != ZRouter::extension_map.end()) {
+ std::cout << " initializing " << (*ext_ptr).second.name << " using priority " << (*ext_ptr).second.init_priority << std::endl;
+ ZRouter::Leaf extObj;
+ extObj["loaded"] = true;
+ extObj["version"] = "unknown";
+ extObj["vendor"] = "unknown";
+
+ model["system"]["extensions"][(*ext_ptr).second.name] = extObj;
+ (*ext_ptr).second.initializer(this, io, model);
+ ext_ptr++;
+ }
+ std::cout << "Loading extensions completed" << std::endl;
+
+ io.run();
+
+ delete mHttpServer;
+ mHttpServer=NULL;
+ return 0;
+
+ }
+ };
}
int main(int argc, char **argv) {
- // Init random seed stuff
- srandomdev();
- // Make single IO handler
- boost::asio::io_service io;
-
- // Register signal handlers so that the daemon may be shut down. You may
- // also want to register for other signals, such as SIGHUP to trigger a
- // re-read of a configuration file.
- #ifdef WITH_BOOST_SIGNAL_SET
- // Not yet, waiting for new boost to be default in freebsd ports.
- boost::asio::signal_set signals(io_service, SIGINT, SIGTERM);
- signals.async_wait(
- boost::bind(&boost::asio::io_service::stop, &io_service));
- #endif
- // Allocate object model
- ZRouter::Model::Model model;
- model["args"].map();
- for (int ac=1; ac < argc; ac++) {
- std::string arg = argv[ac];
- std::string::size_type pos = arg.find("=");
- if (pos != std::string::npos) {
- model["args"][argv[ac]] = std::string(arg, pos + 1);
- }
- else {
- model["args"][argv[ac]] = ZRouter::Leaf(true);
- }
- }
-
- // and fill with data
- model.populate();
-
- // Create our custom http model interface handler
- ZRouter::http_json_handler json_handler(model);
-
- // Start server
- http::server::server s(io, "0.0.0.0", "9080", "html");
-
- // Register model interface
- s.register_handler(&json_handler);
-
- std::cout << "Loading extensions" << std::endl;
- // Add all available extensions
- ZRouter::T_ZExtensionMap::iterator ext_ptr = ZRouter::extension_map.begin();
- while (ext_ptr != ZRouter::extension_map.end()) {
- std::cout << " initializing " << (*ext_ptr).second.name << " using priority " << (*ext_ptr).second.init_priority << std::endl;
- ZRouter::Leaf extObj;
- extObj["loaded"] = true;
- extObj["version"] = "unknown";
- extObj["vendor"] = "unknown";
-
- model["system"]["extensions"][(*ext_ptr).second.name] = extObj;
- (*ext_ptr).second.initializer(io, model);
- ext_ptr++;
- }
- std::cout << "Loading extensions completed" << std::endl;
-
- io.run();
-
- return 0;
+ ZRouter::App A;
+ return A.Run(argc, argv);
}
More information about the Zrouter-src
mailing list