From 766c56a693e8b1bd4293459bb256abdc0515a0b5 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 9 Jun 2009 19:01:02 +0200 Subject: Teile der Namespaces Common und Net in den neuen Namespace Core verschoben --- src/CMakeLists.txt | 1 + src/Client/CommandParser.cpp | 30 ++--- src/Client/InformationManager.cpp | 8 +- src/Client/InformationManager.h | 7 +- src/Client/SystemCommands.cpp | 26 ++-- src/Client/UserCommands.cpp | 40 +++--- src/Common/CMakeLists.txt | 9 -- src/Common/ClientConnection.cpp | 5 +- src/Common/ClientConnection.h | 4 +- src/Common/ConfigEntry.cpp | 64 --------- src/Common/ConfigEntry.h | 96 ------------- src/Common/ConfigManager.cpp | 141 ------------------- src/Common/ConfigManager.h | 69 ---------- src/Common/Configurable.h | 47 ------- src/Common/Connection.h | 8 +- src/Common/Initializable.cpp | 71 ---------- src/Common/Initializable.h | 54 -------- src/Common/LogManager.cpp | 146 -------------------- src/Common/LogManager.h | 149 --------------------- src/Common/Logger.cpp | 89 ------------ src/Common/Logger.h | 68 ---------- src/Common/LoggerBase.h | 79 ----------- src/Common/ModuleManager.cpp | 8 +- src/Common/ModuleManager.h | 8 +- src/Common/RemoteLogger.h | 42 ------ src/Common/Request.cpp | 4 +- src/Common/Request.h | 22 +-- src/Common/RequestHandler.h | 8 +- .../RequestHandlers/DisconnectRequestHandler.cpp | 8 +- .../RequestHandlers/SimpleRequestHandler.cpp | 10 +- .../RequestHandlers/StatusRequestHandler.cpp | 2 +- src/Common/RequestManager.cpp | 18 +-- src/Common/RequestManager.h | 5 - src/Common/Requests/DisconnectRequest.cpp | 4 +- src/Common/SystemBackend.h | 24 ++-- src/Common/SystemManager.cpp | 48 +++---- src/Common/SystemManager.h | 14 +- src/Common/Tokenizer.cpp | 131 ------------------ src/Common/Tokenizer.h | 44 ------ src/Core/CMakeLists.txt | 25 ++++ src/Core/ConfigEntry.cpp | 64 +++++++++ src/Core/ConfigEntry.h | 96 +++++++++++++ src/Core/ConfigManager.cpp | 141 +++++++++++++++++++ src/Core/ConfigManager.h | 69 ++++++++++ src/Core/Configurable.h | 47 +++++++ src/Core/Exception.cpp | 60 +++++++++ src/Core/Exception.h | 65 +++++++++ src/Core/Initializable.cpp | 69 ++++++++++ src/Core/Initializable.h | 54 ++++++++ src/Core/LogManager.cpp | 146 ++++++++++++++++++++ src/Core/LogManager.h | 146 ++++++++++++++++++++ src/Core/Logger.cpp | 89 ++++++++++++ src/Core/Logger.h | 68 ++++++++++ src/Core/LoggerBase.h | 79 +++++++++++ src/Core/RemoteLogger.h | 42 ++++++ src/Core/Signals.h | 27 ++++ src/Core/Signals/Connection.h | 53 ++++++++ src/Core/Signals/GenericSignal.h | 61 +++++++++ src/Core/Signals/Signal0.h | 46 +++++++ src/Core/Signals/Signal1.h | 47 +++++++ src/Core/Signals/Signal2.h | 47 +++++++ src/Core/Signals/SignalBase.h | 53 ++++++++ src/Core/ThreadManager.cpp | 143 ++++++++++++++++++++ src/Core/ThreadManager.h | 113 ++++++++++++++++ src/Core/Tokenizer.cpp | 131 ++++++++++++++++++ src/Core/Tokenizer.h | 44 ++++++ src/Daemon/Backends/NetworkLogger.h | 6 +- src/Daemon/Requests/LogRequest.h | 8 +- src/Net/CMakeLists.txt | 13 +- src/Net/ClientConnection.cpp | 4 +- src/Net/ClientConnection.h | 6 +- src/Net/Connection.cpp | 12 +- src/Net/Connection.h | 25 ++-- src/Net/Exception.cpp | 60 --------- src/Net/Exception.h | 65 --------- src/Net/Listener.cpp | 20 +-- src/Net/Listener.h | 12 +- src/Net/Signals/Connection.h | 53 -------- src/Net/Signals/GenericSignal.h | 61 --------- src/Net/Signals/Signal0.h | 46 ------- src/Net/Signals/Signal1.h | 47 ------- src/Net/Signals/Signal2.h | 47 ------- src/Net/Signals/SignalBase.h | 53 -------- src/Net/Signals/Signals.h | 27 ---- src/Net/ThreadManager.cpp | 146 -------------------- src/Net/ThreadManager.h | 107 --------------- src/Server/ConnectionManager.cpp | 47 +++---- src/Server/ConnectionManager.h | 16 +-- .../RequestHandlers/DaemonListRequestHandler.cpp | 1 - .../RequestHandlers/DaemonRequestHandlerGroup.cpp | 10 +- .../RequestHandlers/DaemonRequestHandlerGroup.h | 4 +- .../RequestHandlers/IdentifyRequestHandler.cpp | 1 - src/Server/RequestHandlers/LogRequestHandler.cpp | 9 +- .../RequestHandlers/UserRequestHandlerGroup.cpp | 2 - src/Server/UserBackend.h | 44 +++--- src/Server/UserManager.cpp | 80 +++++------ src/Server/UserManager.h | 22 +-- src/mad-server.cpp | 12 +- src/mad.cpp | 25 ++-- src/madc.cpp | 19 +-- src/modules/FileLogger/FileLogger.cpp | 6 +- src/modules/FileLogger/FileLogger.h | 16 +-- .../SystemBackendPosix/SystemBackendPosix.cpp | 20 +-- .../SystemBackendPosix/SystemBackendPosix.h | 6 +- .../SystemBackendProc/SystemBackendProc.cpp | 24 ++-- src/modules/SystemBackendProc/SystemBackendProc.h | 6 +- src/modules/UserBackendMysql/UserBackendMysql.cpp | 90 ++++++------- src/modules/UserBackendMysql/UserBackendMysql.h | 22 +-- 108 files changed, 2462 insertions(+), 2474 deletions(-) delete mode 100644 src/Common/ConfigEntry.cpp delete mode 100644 src/Common/ConfigEntry.h delete mode 100644 src/Common/ConfigManager.cpp delete mode 100644 src/Common/ConfigManager.h delete mode 100644 src/Common/Configurable.h delete mode 100644 src/Common/Initializable.cpp delete mode 100644 src/Common/Initializable.h delete mode 100644 src/Common/LogManager.cpp delete mode 100644 src/Common/LogManager.h delete mode 100644 src/Common/Logger.cpp delete mode 100644 src/Common/Logger.h delete mode 100644 src/Common/LoggerBase.h delete mode 100644 src/Common/RemoteLogger.h delete mode 100644 src/Common/Tokenizer.cpp delete mode 100644 src/Common/Tokenizer.h create mode 100644 src/Core/CMakeLists.txt create mode 100644 src/Core/ConfigEntry.cpp create mode 100644 src/Core/ConfigEntry.h create mode 100644 src/Core/ConfigManager.cpp create mode 100644 src/Core/ConfigManager.h create mode 100644 src/Core/Configurable.h create mode 100644 src/Core/Exception.cpp create mode 100644 src/Core/Exception.h create mode 100644 src/Core/Initializable.cpp create mode 100644 src/Core/Initializable.h create mode 100644 src/Core/LogManager.cpp create mode 100644 src/Core/LogManager.h create mode 100644 src/Core/Logger.cpp create mode 100644 src/Core/Logger.h create mode 100644 src/Core/LoggerBase.h create mode 100644 src/Core/RemoteLogger.h create mode 100644 src/Core/Signals.h create mode 100644 src/Core/Signals/Connection.h create mode 100644 src/Core/Signals/GenericSignal.h create mode 100644 src/Core/Signals/Signal0.h create mode 100644 src/Core/Signals/Signal1.h create mode 100644 src/Core/Signals/Signal2.h create mode 100644 src/Core/Signals/SignalBase.h create mode 100644 src/Core/ThreadManager.cpp create mode 100644 src/Core/ThreadManager.h create mode 100644 src/Core/Tokenizer.cpp create mode 100644 src/Core/Tokenizer.h delete mode 100644 src/Net/Exception.cpp delete mode 100644 src/Net/Exception.h delete mode 100644 src/Net/Signals/Connection.h delete mode 100644 src/Net/Signals/GenericSignal.h delete mode 100644 src/Net/Signals/Signal0.h delete mode 100644 src/Net/Signals/Signal1.h delete mode 100644 src/Net/Signals/Signal2.h delete mode 100644 src/Net/Signals/SignalBase.h delete mode 100644 src/Net/Signals/Signals.h delete mode 100644 src/Net/ThreadManager.cpp delete mode 100644 src/Net/ThreadManager.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index eae793e..ddc9867 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,4 @@ +add_subdirectory(Core) add_subdirectory(Net) add_subdirectory(Common) add_subdirectory(Server) diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 19aa8a9..be2383a 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -22,10 +22,10 @@ #include "SystemCommands.h" #include "UserCommands.h" -#include -#include -#include +#include +#include +#include #include #include @@ -71,7 +71,7 @@ void CommandParser::printUsage(const std::string& command) { const CommandParser::Command *cmd = findCommand(command); if(cmd) - Common::Logger::logf("Usage: %s\n", cmd->cmdline); + Core::Logger::logf("Usage: %s\n", cmd->cmdline); } @@ -88,7 +88,7 @@ std::map CommandParser::parseHostList(const std:: } if(ret.empty()) - Common::Logger::log("No hosts active."); + Core::Logger::log("No hosts active."); } else { ret = hosts; @@ -98,9 +98,9 @@ std::map CommandParser::parseHostList(const std:: std::map::const_iterator host = hosts.find(*arg); if(host == hosts.end()) - Common::Logger::logf(Common::Logger::ERROR, "Host '%s' doesn't exist.", arg->c_str()); + Core::Logger::logf(Core::Logger::ERROR, "Host '%s' doesn't exist.", arg->c_str()); else if(mustBeActive && host->second.getState() == Common::HostInfo::INACTIVE) - Common::Logger::logf(Common::Logger::WARNING, "Host '%s' is inactive.", arg->c_str()); + Core::Logger::logf(Core::Logger::WARNING, "Host '%s' is inactive.", arg->c_str()); else ret.insert(*host); } @@ -130,10 +130,10 @@ void CommandParser::helpCommand(const std::vector &args, Common::Co std::cout << command->longdesc << std::endl << std::endl; } else - Common::Logger::logf(Common::Logger::WARNING, "%s: Command '%s' doesn't exist.", args[0].c_str(), args[1].c_str()); + Core::Logger::logf(Core::Logger::WARNING, "%s: Command '%s' doesn't exist.", args[0].c_str(), args[1].c_str()); } else { - Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); printUsage("help"); } } @@ -167,7 +167,7 @@ void CommandParser::listHostsCommand(const std::vector &args, Commo std::cout << std::endl; } else if(args.size() > 2) { - Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); printUsage("list_hosts"); } else if(args[1] == "-a") { @@ -185,7 +185,7 @@ void CommandParser::listHostsCommand(const std::vector &args, Commo std::cout << std::endl; } else { - Common::Logger::logf(Common::Logger::ERROR, "%s: Don't understand argument '%s'.", args[0].c_str(), args[1].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: Don't understand argument '%s'.", args[0].c_str(), args[1].c_str()); printUsage("list_hosts"); } } @@ -196,10 +196,10 @@ void CommandParser::exitCommand(const std::vector &args _UNUSED_PAR Common::RequestManager::get()->sendRequest(connection, request); request->wait(); - std::pair, Net::Exception> result = request->getResult(); + std::pair, Core::Exception> result = request->getResult(); if(result.second) - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); else commandParser.disconnect = true; } @@ -207,7 +207,7 @@ void CommandParser::exitCommand(const std::vector &args _UNUSED_PAR bool CommandParser::parse(const std::string &cmd) { std::vector splitCmd; - Common::Tokenizer::tokenize(cmd, splitCmd); + Core::Tokenizer::tokenize(cmd, splitCmd); if(splitCmd.empty()) return true; @@ -217,7 +217,7 @@ bool CommandParser::parse(const std::string &cmd) { if(command) command->function(splitCmd, connection); else - Common::Logger::logf(Common::Logger::ERROR, "Unknown command '%s'.", splitCmd[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "Unknown command '%s'.", splitCmd[0].c_str()); return true; } diff --git a/src/Client/InformationManager.cpp b/src/Client/InformationManager.cpp index 6bcdc8c..c5ff9f8 100644 --- a/src/Client/InformationManager.cpp +++ b/src/Client/InformationManager.cpp @@ -19,7 +19,7 @@ #include "InformationManager.h" #include "Requests/DaemonListRequest.h" -#include +#include #include @@ -39,7 +39,7 @@ void InformationManager::DaemonStateUpdateRequestHandler::handleRequest(boost::s if(host != informationManager.daemons.end()) host->second.setState((*packet)["state"]); else - Common::Logger::log(Common::Logger::WARNING, "Received a state update for an unknown host."); + Core::Logger::log(Core::Logger::WARNING, "Received a state update for an unknown host."); } ret->setType("OK"); @@ -68,11 +68,11 @@ void InformationManager::updateDaemonList(Common::Connection *con) { updating = true; } -void InformationManager::daemonListRequestFinished(boost::shared_ptr packet, Net::Exception error) { +void InformationManager::daemonListRequestFinished(boost::shared_ptr packet, Core::Exception error) { boost::lock_guard lock(mutex); if(!packet || error) { - Common::Logger::logf(Common::Logger::CRITICAL, "Host list request failed: %s", error.strerror().c_str()); + Core::Logger::logf(Core::Logger::CRITICAL, "Host list request failed: %s", error.strerror().c_str()); } else { const Common::XmlPacket::Element &hostInfo = (*packet)["hosts"]; diff --git a/src/Client/InformationManager.h b/src/Client/InformationManager.h index 78b1f88..ffc24cf 100644 --- a/src/Client/InformationManager.h +++ b/src/Client/InformationManager.h @@ -23,8 +23,9 @@ #include #include +#include + #include -#include #include #include @@ -35,7 +36,7 @@ namespace Mad { namespace Client { -class InformationManager : public Common::Initializable, private boost::noncopyable { +class InformationManager : public Core::Initializable, private boost::noncopyable { private: class DaemonStateUpdateRequestHandler : public Common::RequestHandlers::SimpleRequestHandler { private: @@ -57,7 +58,7 @@ class InformationManager : public Common::Initializable, private boost::noncopya InformationManager() : updating(false) {} - void daemonListRequestFinished(boost::shared_ptr packet, Net::Exception error); + void daemonListRequestFinished(boost::shared_ptr packet, Core::Exception error); protected: virtual void doInit(); diff --git a/src/Client/SystemCommands.cpp b/src/Client/SystemCommands.cpp index 5409828..490f278 100644 --- a/src/Client/SystemCommands.cpp +++ b/src/Client/SystemCommands.cpp @@ -20,7 +20,7 @@ #include "SystemCommands.h" #include "CommandParser.h" -#include +#include #include #include "Requests/DaemonFSInfoRequest.h" @@ -146,7 +146,7 @@ void SystemCommands::fsinfoCommand(const std::vector &args, Common: else if(args.size() == 2) request = boost::shared_ptr(new Requests::DaemonFSInfoRequest(args[1])); else { - Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); CommandParser::printUsage("fsinfo"); return; } @@ -154,10 +154,10 @@ void SystemCommands::fsinfoCommand(const std::vector &args, Common: Common::RequestManager::get()->sendRequest(connection, request); request->wait(); - std::pair, Net::Exception> result = request->getResult(); + std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); } else { if(args.size() == 1) @@ -171,7 +171,7 @@ void SystemCommands::fsinfoCommand(const std::vector &args, Common: void SystemCommands::rebootCommand(const std::vector &args, Common::Connection *connection) { if(args.size() < 2) { - Common::Logger::logf(Common::Logger::ERROR, "%s: No host given.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: No host given.", args[0].c_str()); CommandParser::printUsage("reboot"); return; } @@ -191,16 +191,16 @@ void SystemCommands::rebootCommand(const std::vector &args, Common: (*request)->wait(); for(std::vector >::iterator request = requests.begin(); request != requests.end(); ++request) { - std::pair, Net::Exception> result = (*request)->getResult(); + std::pair, Core::Exception> result = (*request)->getResult(); if(result.second) - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); } } void SystemCommands::shutdownCommand(const std::vector &args, Common::Connection *connection) { if(args.size() < 2) { - Common::Logger::logf(Common::Logger::ERROR, "%s: No host given.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: No host given.", args[0].c_str()); CommandParser::printUsage("shutdown"); return; } @@ -220,10 +220,10 @@ void SystemCommands::shutdownCommand(const std::vector &args, Commo (*request)->wait(); for(std::vector >::iterator request = requests.begin(); request != requests.end(); ++request) { - std::pair, Net::Exception> result = (*request)->getResult(); + std::pair, Core::Exception> result = (*request)->getResult(); if(result.second) - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); } } @@ -235,7 +235,7 @@ void SystemCommands::statusCommand(const std::vector &args, Common: else if(args.size() == 2) request = boost::shared_ptr(new Requests::DaemonStatusRequest(args[1])); else { - Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); CommandParser::printUsage("status"); return; } @@ -243,10 +243,10 @@ void SystemCommands::statusCommand(const std::vector &args, Common: Common::RequestManager::get()->sendRequest(connection, request); request->wait(); - std::pair, Net::Exception> result = request->getResult(); + std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); } else { if(args.size() == 1) diff --git a/src/Client/UserCommands.cpp b/src/Client/UserCommands.cpp index 6defbcf..97ea27b 100644 --- a/src/Client/UserCommands.cpp +++ b/src/Client/UserCommands.cpp @@ -20,7 +20,7 @@ #include "UserCommands.h" #include "CommandParser.h" -#include +#include #include #include @@ -37,12 +37,12 @@ namespace Client { void UserCommands::userInfoCommand(const std::vector &args, Common::Connection *connection) { if(args.size() == 1) { - Common::Logger::logf(Common::Logger::ERROR, "%s: No user id given.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: No user id given.", args[0].c_str()); CommandParser::printUsage("user_info"); return; } if(args.size() > 2) { - Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); CommandParser::printUsage("user_info"); return; } @@ -50,7 +50,7 @@ void UserCommands::userInfoCommand(const std::vector &args, Common: char *endptr; unsigned long uid = std::strtoul(args[1].c_str(), &endptr, 10); if(args[1].empty() || *endptr != '\0') { - Common::Logger::logf(Common::Logger::ERROR, "%s: Unable to parse user id.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: Unable to parse user id.", args[0].c_str()); CommandParser::printUsage("user_info"); return; } @@ -59,10 +59,10 @@ void UserCommands::userInfoCommand(const std::vector &args, Common: Common::RequestManager::get()->sendRequest(connection, request); request->wait(); - std::pair, Net::Exception> result = request->getResult(); + std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); } else { const Common::XmlPacket &packet = *result.first; @@ -78,10 +78,10 @@ void UserCommands::listUsersCommand(const std::vector &args _UNUSED Common::RequestManager::get()->sendRequest(connection, request); request->wait(); - std::pair, Net::Exception> result = request->getResult(); + std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); } else { const Common::XmlPacket::Element &users = (*result.first)["users"]; @@ -103,12 +103,12 @@ void UserCommands::listUsersCommand(const std::vector &args _UNUSED void UserCommands::listUserGroupsCommand(const std::vector &args, Common::Connection *connection) { if(args.size() == 1) { - Common::Logger::logf(Common::Logger::ERROR, "%s: No user id given.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: No user id given.", args[0].c_str()); CommandParser::printUsage("list_user_groups"); return; } if(args.size() > 2) { - Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); CommandParser::printUsage("list_user_groups"); return; } @@ -116,7 +116,7 @@ void UserCommands::listUserGroupsCommand(const std::vector &args, C char *endptr; unsigned long uid = std::strtoul(args[1].c_str(), &endptr, 10); if(args[1].empty() || *endptr != '\0') { - Common::Logger::logf(Common::Logger::ERROR, "%s: Unable to parse user id.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: Unable to parse user id.", args[0].c_str()); CommandParser::printUsage("list_user_groups"); return; } @@ -125,10 +125,10 @@ void UserCommands::listUserGroupsCommand(const std::vector &args, C Common::RequestManager::get()->sendRequest(connection, request); request->wait(); - std::pair, Net::Exception> result = request->getResult(); + std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); } else { const Common::XmlPacket::Element &groups = (*result.first)["groups"]; @@ -155,10 +155,10 @@ void UserCommands::listGroupsCommand(const std::vector &args _UNUSE Common::RequestManager::get()->sendRequest(connection, request); request->wait(); - std::pair, Net::Exception> result = request->getResult(); + std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); } else { const Common::XmlPacket::Element &groups = (*result.first)["groups"]; @@ -180,12 +180,12 @@ void UserCommands::listGroupsCommand(const std::vector &args _UNUSE void UserCommands::listGroupUsersCommand(const std::vector &args, Common::Connection *connection) { if(args.size() == 1) { - Common::Logger::logf(Common::Logger::ERROR, "%s: No group id given.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: No group id given.", args[0].c_str()); CommandParser::printUsage("list_group_users"); return; } if(args.size() > 2) { - Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); CommandParser::printUsage("list_group_users"); return; } @@ -193,7 +193,7 @@ void UserCommands::listGroupUsersCommand(const std::vector &args, C char *endptr; unsigned long gid = std::strtoul(args[1].c_str(), &endptr, 10); if(args[1].empty() || *endptr != '\0') { - Common::Logger::logf(Common::Logger::ERROR, "%s: Unable to parse group id.", args[0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "%s: Unable to parse group id.", args[0].c_str()); CommandParser::printUsage("list_group_users"); return; } @@ -202,10 +202,10 @@ void UserCommands::listGroupUsersCommand(const std::vector &args, C Common::RequestManager::get()->sendRequest(connection, request); request->wait(); - std::pair, Net::Exception> result = request->getResult(); + std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); } else { const Common::XmlPacket::Element &users = (*result.first)["users"]; diff --git a/src/Common/CMakeLists.txt b/src/Common/CMakeLists.txt index 25c3dee..078df71 100644 --- a/src/Common/CMakeLists.txt +++ b/src/Common/CMakeLists.txt @@ -8,25 +8,16 @@ link_directories(${LTDL_LIBRARY_DIR}) add_library(Common Base64Encoder.cpp Base64Encoder.h ClientConnection.cpp ClientConnection.h - ConfigEntry.cpp ConfigEntry.h - ConfigManager.cpp ConfigManager.h - Configurable.h Connection.cpp Connection.h - Initializable.cpp Initializable.h GroupInfo.h HostInfo.h - Logger.cpp Logger.h - LoggerBase.h - LogManager.cpp LogManager.h ModuleManager.cpp ModuleManager.h - RemoteLogger.h Request.cpp Request.h RequestHandler.cpp RequestHandler.h RequestHandlerGroup.h RequestManager.cpp RequestManager.h SystemBackend.h SystemManager.cpp SystemManager.h - Tokenizer.cpp Tokenizer.h UserInfo.h XmlPacket.cpp XmlPacket.h ) diff --git a/src/Common/ClientConnection.cpp b/src/Common/ClientConnection.cpp index b9a6721..ba558f4 100644 --- a/src/Common/ClientConnection.cpp +++ b/src/Common/ClientConnection.cpp @@ -18,9 +18,8 @@ */ #include "ClientConnection.h" -#include -#include "Logger.h" +#include namespace Mad { namespace Common { @@ -33,7 +32,7 @@ bool ClientConnection::send(const Net::Packet &packet) { return connection->send(packet); } -void ClientConnection::connect(const boost::asio::ip::tcp::endpoint &address) throw(Net::Exception) { +void ClientConnection::connect(const boost::asio::ip::tcp::endpoint &address) throw(Core::Exception) { connection->connect(address); } diff --git a/src/Common/ClientConnection.h b/src/Common/ClientConnection.h index a455c29..305df44 100644 --- a/src/Common/ClientConnection.h +++ b/src/Common/ClientConnection.h @@ -21,7 +21,7 @@ #define MAD_COMMON_CLIENTCONNECTION_H_ #include "Connection.h" -#include +#include #include @@ -44,7 +44,7 @@ class ClientConnection : public Connection { ClientConnection(); virtual ~ClientConnection() {} - void connect(const boost::asio::ip::tcp::endpoint &address) throw(Net::Exception); + void connect(const boost::asio::ip::tcp::endpoint &address) throw(Core::Exception); bool isConnecting() const; bool isConnected() const; diff --git a/src/Common/ConfigEntry.cpp b/src/Common/ConfigEntry.cpp deleted file mode 100644 index f47cf7a..0000000 --- a/src/Common/ConfigEntry.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* - * ConfigEntry.cpp - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "ConfigEntry.h" - -namespace Mad { -namespace Common { - -ConfigEntry::String& ConfigEntry::Entry::operator[] (size_t i) { - try { - return value.at(i); - } - catch(std::out_of_range &e) { - zero = String(); - return zero; - } -} - -const ConfigEntry::String& ConfigEntry::Entry::operator[] (size_t i) const { - try { - return value.at(i); - } - catch(std::out_of_range &e) { - return constZero; - } -} - -ConfigEntry::Entry& ConfigEntry::operator[] (size_t i) { - try { - return entries.at(i); - } - catch(std::out_of_range &e) { - zero = Entry(); - return zero; - } -} - -const ConfigEntry::Entry& ConfigEntry::operator[] (size_t i) const { - try { - return entries.at(i); - } - catch(std::out_of_range &e) { - return constZero; - } -} - -} -} diff --git a/src/Common/ConfigEntry.h b/src/Common/ConfigEntry.h deleted file mode 100644 index 8358726..0000000 --- a/src/Common/ConfigEntry.h +++ /dev/null @@ -1,96 +0,0 @@ -/* - * ConfigEntry.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_COMMON_CONFIGENTRY_H_ -#define MAD_COMMON_CONFIGENTRY_H_ - -#include -#include -#include -#include - -namespace Mad { -namespace Common { - -class ConfigEntry { - public: - class String : public std::string { - public: - String() {} - String(const std::string &str) : std::string(str) {} - - bool matches(const std::string &str) const { - return (strcasecmp(c_str(), str.c_str()) == 0); - } - }; - - class Entry { - private: - String key; - std::vector value; - - String zero, constZero; - - public: - Entry() {} - Entry(const std::vector &args) { - if(args.empty()) - return; - - key = args.front(); - - value.assign(args.begin()+1, args.end()); - } - - bool empty() const { - return key.empty(); - } - - String &getKey() {return key;} - const String &getKey() const {return key;} - - size_t getSize() const {return value.size();} - - String& operator[] (size_t i); - const String& operator[] (size_t i) const; - }; - - private: - std::vector entries; - Entry zero, constZero; - - public: - size_t getSize() const {return entries.size();} - - Entry& operator[] (size_t i); - const Entry& operator[] (size_t i) const; - - void push(const Entry &entry) { - entries.push_back(entry); - } - - void pop() { - entries.pop_back(); - } -}; - -} -} - -#endif /* MAD_COMMON_CONFIGENTRY_H_ */ diff --git a/src/Common/ConfigManager.cpp b/src/Common/ConfigManager.cpp deleted file mode 100644 index 3bf0c85..0000000 --- a/src/Common/ConfigManager.cpp +++ /dev/null @@ -1,141 +0,0 @@ -/* - * ConfigManager.cpp - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "ConfigManager.h" -#include "ConfigEntry.h" -#include "Configurable.h" -#include "Logger.h" -#include "LogManager.h" -#include "Tokenizer.h" - -#include -#include - -namespace Mad { -namespace Common { - -ConfigManager ConfigManager::configManager; - - -bool ConfigManager::Compare::operator() (const Configurable *c1, const Configurable *c2) { - if(c1->getPriority() != c2->getPriority()) - return c1->getPriority() > c2->getPriority(); - else - return c1 < c2; -} - - -void ConfigManager::handleConfigEntry(const ConfigEntry &entry) { - bool handled = false; - - for(std::set::iterator c = configurables.begin(); c != configurables.end(); ++c) { - if((*c)->handleConfigEntry(entry, handled)) - handled = true; - } - - if(!handled) - Logger::logf(Logger::WARNING, "Invalid config option '%s'.", entry[entry.getSize()-1].getKey().c_str()); -} - -bool ConfigManager::loadFile(const std::string &filename) { - if(finished) - return false; - - std::ifstream file(filename.c_str()); - ConfigEntry entry; - std::string line, input; - char delim; - std::vector splitLine, lastConfigLine; - - if(!file.good()) - return false; - - while(!(file.eof() && line.empty() && input.empty())) { - while(input.empty() && !file.eof()) - std::getline(file, input); - - if(input.empty()) - break; - - size_t pos = input.find_first_of("#{}"); - if(pos == std::string::npos) { - line += input; - delim = '\n'; - input.clear(); - } - else { - line += input.substr(0, pos); - delim = input[pos]; - input = input.substr(pos+1); - } - - if(!Tokenizer::tokenize(line, splitLine)) { - line += delim; - continue; - } - - if(!splitLine.empty()) { - pos = line.find_first_of(" \t"); - - entry.push(splitLine); - handleConfigEntry(entry); - entry.pop(); - - lastConfigLine = splitLine; - } - - switch(delim) { - case '#': - input.clear(); - break; - case '{': - entry.push(lastConfigLine); - break; - case '}': - entry.pop(); - } - - line.clear(); - } - - // TODO Depth check - - return true; -} - -void ConfigManager::finish() { - if(finished) - return; - - for(std::set::iterator c = configurables.begin(); c != configurables.end(); ++c) - (*c)->configFinished(); - - finished = true; -} - -ConfigManager::ConfigManager() : finished(false) { - registerConfigurable(LogManager::get()); -} - -ConfigManager::~ConfigManager() { - unregisterConfigurable(LogManager::get()); -} - -} -} diff --git a/src/Common/ConfigManager.h b/src/Common/ConfigManager.h deleted file mode 100644 index e7885b8..0000000 --- a/src/Common/ConfigManager.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * ConfigManager.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_COMMON_CONFIGMANAGER_H_ -#define MAD_COMMON_CONFIGMANAGER_H_ - -#include -#include -#include - -namespace Mad { -namespace Common { - -class ConfigEntry; -class Configurable; - -class ConfigManager { - private: - struct Compare { - bool operator() (const Configurable *c1, const Configurable *c2); - }; - - static ConfigManager configManager; - - std::set configurables; - bool finished; - - ConfigManager(); - ~ConfigManager(); - - void handleConfigEntry(const ConfigEntry &entry); - - public: - bool loadFile(const std::string &filename); - void finish(); - - void registerConfigurable(Configurable *c) { - configurables.insert(c); - } - - void unregisterConfigurable(Configurable *c) { - configurables.erase(c); - } - - static ConfigManager *get() { - return &configManager; - } -}; - -} -} - -#endif /* MAD_COMMON_CONFIGMANAGER_H_ */ diff --git a/src/Common/Configurable.h b/src/Common/Configurable.h deleted file mode 100644 index 47f919d..0000000 --- a/src/Common/Configurable.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Configurable.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_COMMON_CONFIGURABLE_H_ -#define MAD_COMMON_CONFIGURABLE_H_ - -namespace Mad { -namespace Common { - -class ConfigEntry; -class ConfigManager; - -class Configurable { - public: - virtual ~Configurable() {} - - virtual int getPriority() const {return 0;} - - protected: - friend class ConfigManager; - - Configurable() {} - - virtual bool handleConfigEntry(const ConfigEntry&, bool) {return false;} - virtual void configFinished() {} -}; - -} -} - -#endif /* MAD_COMMON_CONFIGURABLE_H_ */ diff --git a/src/Common/Connection.h b/src/Common/Connection.h index c8ff3e8..6700574 100644 --- a/src/Common/Connection.h +++ b/src/Common/Connection.h @@ -20,7 +20,7 @@ #ifndef MAD_COMMON_CONNECTION_H_ #define MAD_COMMON_CONNECTION_H_ -#include +#include #include #include @@ -39,7 +39,7 @@ class Connection : private boost::noncopyable { private: bool authenticated; - Net::Signals::Signal2, uint16_t> signalReceive; + Core::Signals::Signal2, uint16_t> signalReceive; protected: Connection() : authenticated(0) {} @@ -53,10 +53,10 @@ class Connection : private boost::noncopyable { bool sendPacket(const XmlPacket &packet, uint16_t requestId); - Net::Signals::Connection connectSignalReceive(const Net::Signals::Signal2, uint16_t>::slot_type &slot) { + Core::Signals::Connection connectSignalReceive(const Core::Signals::Signal2, uint16_t>::slot_type &slot) { return signalReceive.connect(slot); } - void disconnectSignalReceive(const Net::Signals::Connection &con) { + void disconnectSignalReceive(const Core::Signals::Connection &con) { signalReceive.disconnect(con); } diff --git a/src/Common/Initializable.cpp b/src/Common/Initializable.cpp deleted file mode 100644 index d2dc9fd..0000000 --- a/src/Common/Initializable.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Initializable.cpp - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "Initializable.h" - -#include "ConfigManager.h" -#include "Configurable.h" -#include "Logger.h" - -#include - -namespace Mad { -namespace Common { - -std::stack Initializable::initializedObjects; - -void Initializable::init() { - if(initialized) - return; - - if(initializing) { - Logger::log(Logger::CRITICAL, "Fatal initialization error: cyclic dependencies."); - std::terminate(); - } - - initializing = true; - - doInit(); - - Configurable *c = dynamic_cast(this); - if(c) - ConfigManager::get()->registerConfigurable(c); - - initializing = false; - initialized = true; - initializedObjects.push(this); -} - -void Initializable::deinit() { - while(!initializedObjects.empty()) { - Initializable *in = initializedObjects.top(); - - Configurable *c = dynamic_cast(in); - if(c) - ConfigManager::get()->unregisterConfigurable(c); - - in->doDeinit(); - in->initialized = false; - - initializedObjects.pop(); - } -} - -} -} diff --git a/src/Common/Initializable.h b/src/Common/Initializable.h deleted file mode 100644 index 6f67678..0000000 --- a/src/Common/Initializable.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Initializable.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_COMMON_INITIALIZABLE_H_ -#define MAD_COMMON_INITIALIZABLE_H_ - -#include - -namespace Mad { -namespace Common { - -class Initializable { - private: - static std::stack initializedObjects; - - bool initializing; - bool initialized; - - protected: - Initializable() : initializing(false), initialized(false) {} - virtual void doInit() {} - virtual void doDeinit() {} - - public: - virtual ~Initializable() {} - - void init(); - - bool isInitialized() const {return initialized;} - bool isInitializing() const {return initializing;} - - static void deinit(); -}; - -} -} - -#endif /* MAD_COMMON_INITIALIZABLE_H_ */ diff --git a/src/Common/LogManager.cpp b/src/Common/LogManager.cpp deleted file mode 100644 index bd83a5e..0000000 --- a/src/Common/LogManager.cpp +++ /dev/null @@ -1,146 +0,0 @@ -/* - * LogManager.cpp - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "LogManager.h" -#include "ConfigEntry.h" - -#include - -namespace Mad { -namespace Common { - -LogManager LogManager::logManager; - - -void LogManager::ConsoleLogger::logMessage(MessageCategory category _UNUSED_PARAMETER_, MessageLevel level, time_t timestamp _UNUSED_PARAMETER_, const std::string &message) { - if(level != CRITICAL) {// Critical messages are printed to cerr directly, so don't print them a second time - cerrLock.lock(); - std::cerr << message << std::endl; - cerrLock.unlock(); - } -} - -void LogManager::ConsoleLogger::logMessage(MessageCategory category _UNUSED_PARAMETER_, MessageLevel, time_t timestamp _UNUSED_PARAMETER_, const std::string &message, const std::string &messageSource) { - cerrLock.lock(); - std::cerr << message << " from " << messageSource << std::endl; - cerrLock.unlock(); -} - -void LogManager::ConsoleLogger::logMessageDirect(MessageCategory category _UNUSED_PARAMETER_, MessageLevel level _UNUSED_PARAMETER_, time_t timestamp _UNUSED_PARAMETER_, const std::string &message) { - cerrLock.lock(); - std::cerr << message << std::endl; - cerrLock.unlock(); -} - - -bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) { - if(handled) - return false; - - if(entry[0].getKey().matches("Logger")) { - if(entry[0][0].matches("Console")) { - if(entry[1].empty()) { - registerLogger(static_cast(&consoleLogger)); - return true; - } - } - else if(entry[1].empty()) { - Logger::logf(Logger::WARNING, "Unknown logger '%s'.", entry[0][0].c_str()); - return true; - } - } - - return false; -} - -void LogManager::configFinished() { - if(loggers.empty()) - registerLogger(static_cast(&consoleLogger)); - - // TODO Debug - consoleLogger.Logger::setLevel(LoggerBase::DEBUG); - - queueLock.lock(); - configured = true; - queueLock.unlock(); - queueCond.notify_one(); -} - -void LogManager::log(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message) { - if(level == LoggerBase::CRITICAL) - consoleLogger.logMessageDirect(category, level, timestamp, message); - - queueLock.lock(); - Message m = {category, level, timestamp, message}; - messageQueue.push(m); - queueLock.unlock(); - queueCond.notify_one(); -} - -void LogManager::log(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message, const std::string &source) { - queueLock.lock(); - RemoteMessage m = {category, level, timestamp, message, source}; - remoteMessageQueue.push(m); - queueLock.unlock(); - queueCond.notify_one(); -} - -void LogManager::loggerThread() { - boost::unique_lock lock(queueLock); - - running = true; - - while(running) { - while(running && ((messageQueue.empty() && messageQueue.empty()) || !configured)) - queueCond.wait(lock); - - while(!messageQueue.empty()) { - Message message = messageQueue.front(); - messageQueue.pop(); - lock.unlock(); - - loggerLock.lock(); - for(std::set::iterator logger = loggers.begin(); logger != loggers.end(); ++logger) { - if((*logger)->getLevel() >= message.level && (*logger)->isCategorySet(message.category)) - (*logger)->logMessage(message.category, message.level, message.timestamp, message.message); - } - loggerLock.unlock(); - - lock.lock(); - } - - while(!remoteMessageQueue.empty()) { - RemoteMessage message = remoteMessageQueue.front(); - remoteMessageQueue.pop(); - lock.unlock(); - - remoteLoggerLock.lock(); - for(std::set::iterator logger = remoteLoggers.begin(); logger != remoteLoggers.end(); ++logger) { - if((*logger)->getLevel() >= message.level && (*logger)->isCategorySet(message.category)) - (*logger)->logMessage(message.category, message.level, message.timestamp, message.message, message.source); - } - remoteLoggerLock.unlock(); - - lock.lock(); - } - } -} - -} -} diff --git a/src/Common/LogManager.h b/src/Common/LogManager.h deleted file mode 100644 index 98bb6f7..0000000 --- a/src/Common/LogManager.h +++ /dev/null @@ -1,149 +0,0 @@ -/* - * LogManager.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_COMMON_LOGMANAGER_H_ -#define MAD_COMMON_LOGMANAGER_H_ - -#include - -#include "Configurable.h" -#include "Logger.h" -#include "RemoteLogger.h" - -#include -#include - -#include -#include - -namespace Mad { - -namespace Net { -class ThreadManager; -} - -namespace Common { - -class LogManager : public Configurable { - private: - friend class Net::ThreadManager; - - typedef LoggerBase::MessageCategory MessageCategory; - typedef LoggerBase::MessageLevel MessageLevel; - - struct Message { - MessageCategory category; - MessageLevel level; - time_t timestamp; - std::string message; - }; - - struct RemoteMessage { - MessageCategory category; - MessageLevel level; - time_t timestamp; - std::string message; - std::string source; - }; - - class ConsoleLogger : public Logger, public RemoteLogger { - private: - // For long messages, writing to cerr is not atomic - // -> lock cerr to prevent mixing messages up - boost::mutex cerrLock; - - protected: - virtual void logMessage(MessageCategory category _UNUSED_PARAMETER_, MessageLevel level, time_t timestamp _UNUSED_PARAMETER_, const std::string &message); - virtual void logMessage(MessageCategory category _UNUSED_PARAMETER_, MessageLevel, time_t timestamp _UNUSED_PARAMETER_, const std::string &message, const std::string &messageSource); - - public: - ConsoleLogger() {} - - void logMessageDirect(MessageCategory category _UNUSED_PARAMETER_, MessageLevel level _UNUSED_PARAMETER_, time_t timestamp _UNUSED_PARAMETER_, const std::string &message); - }; - - - static LogManager logManager; - - ConsoleLogger consoleLogger; - - std::set loggers; - std::set remoteLoggers; - bool configured, running; - - boost::mutex queueLock; - boost::condition_variable queueCond; - - boost::mutex loggerLock; - boost::mutex remoteLoggerLock; - - std::queue messageQueue; - std::queue remoteMessageQueue; - - void loggerThread(); - void stopLoggerThread() { - queueLock.lock(); - running = false; - queueLock.unlock(); - queueCond.notify_one(); - } - - LogManager() : configured(false), running(false) {} - - protected: - virtual bool handleConfigEntry(const ConfigEntry &entry, bool handled); - virtual void configFinished(); - - public: - void log(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message); - void log(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message, const std::string &source); - - void registerLogger(Logger *logger) { - loggerLock.lock(); - loggers.insert(logger); - loggerLock.unlock(); - } - - void unregisterLogger(Logger *logger) { - loggerLock.lock(); - loggers.erase(logger); - loggerLock.unlock(); - } - - void registerLogger(RemoteLogger *logger) { - remoteLoggerLock.lock(); - remoteLoggers.insert(logger); - remoteLoggerLock.unlock(); - } - - void unregisterLogger(RemoteLogger *logger) { - remoteLoggerLock.lock(); - remoteLoggers.erase(logger); - remoteLoggerLock.unlock(); - } - - static LogManager *get() { - return &logManager; - } -}; - -} -} - -#endif /* MAD_COMMON_LOGMANAGER_H_ */ diff --git a/src/Common/Logger.cpp b/src/Common/Logger.cpp deleted file mode 100644 index e746faf..0000000 --- a/src/Common/Logger.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Logger.cpp - * - * Copyright (C) 2008 Johannes Thorn - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "Logger.h" -#include "LogManager.h" - -#include - -namespace Mad { -namespace Common { - -void Logger::logfv(MessageCategory category, MessageLevel level, const char *format, va_list ap) { - int size = 100; - char *buf = (char*)std::malloc(size); - - // If buffer is too small, try again with bigger buffer - while(true) { - va_list ap2; - - va_copy(ap2, ap); - int n = std::vsnprintf(buf, size, format, ap2); - va_end(ap2); - - if(n > -1 && n < size) { - log(category, level, buf); - std::free(buf); - return; - } - - if(n > -1) - size = n+1; - else - size *= 2; - - buf = (char*)std::realloc(buf, size); - } - -} - -void Logger::log(MessageCategory category, MessageLevel level, const std::string &message) { - LogManager::get()->log(category, level, std::time(0), message); -} - -void Logger::logf(MessageCategory category, MessageLevel level, const char *format, ...) { - va_list ap; - va_start(ap, format); - logfv(category, level, format, ap); - va_end(ap); -} - -void Logger::logf(MessageCategory category, const char *format, ...) { - va_list ap; - va_start(ap, format); - logfv(category, DEFAULT, format, ap); - va_end(ap); -} - -void Logger::logf(MessageLevel level, const char *format, ...) { - va_list ap; - va_start(ap, format); - logfv(GENERAL, level, format, ap); - va_end(ap); -} - -void Logger::logf(const char *format, ...) { - va_list ap; - va_start(ap, format); - logfv(GENERAL, DEFAULT, format, ap); - va_end(ap); -} - -} -} diff --git a/src/Common/Logger.h b/src/Common/Logger.h deleted file mode 100644 index 436c232..0000000 --- a/src/Common/Logger.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Logger.h - * - * Copyright (C) 2008 Johannes Thorn - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_COMMON_LOGGER_H_ -#define MAD_COMMON_LOGGER_H_ - -#include "LoggerBase.h" - -#include -#include -#include - -namespace Mad { -namespace Common { - -class LogManager; - -class Logger : public LoggerBase { - private: - friend class LogManager; - - static void logfv(MessageCategory category, MessageLevel level, const char *format, va_list ap); - - protected: - virtual void logMessage(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message) = 0; - - public: - static void log(MessageCategory category, MessageLevel level, const std::string &message); - - static void log(MessageCategory category, const std::string &message) { - log(category, DEFAULT, message); - } - - static void log(MessageLevel level, const std::string &message) { - log(GENERAL, level, message); - } - - static void log(const std::string &message) { - log(GENERAL, DEFAULT, message); - } - - - static void logf(MessageCategory category, MessageLevel level, const char *format, ...); - static void logf(MessageCategory category, const char *format, ...); - static void logf(MessageLevel level, const char *format, ...); - static void logf(const char *format, ...); -}; - -} -} - -#endif /* MAD_COMMON_LOGGER_H_ */ diff --git a/src/Common/LoggerBase.h b/src/Common/LoggerBase.h deleted file mode 100644 index 9843d4d..0000000 --- a/src/Common/LoggerBase.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * LoggerBase.h - * - * Copyright (C) 2008 Johannes Thorn - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_COMMON_LOGGERBASE_H_ -#define MAD_COMMON_LOGGERBASE_H_ - -#include -#include - -namespace Mad { -namespace Common { - -class LoggerBase { - public: - enum MessageLevel { - CRITICAL, ERROR, WARNING, DEFAULT, VERBOSE, DEBUG - }; - - enum MessageCategory { - SYSTEM, NETWORK, DAEMON, USER, DISK, PROGRAM, GENERAL - }; - - protected: - std::bitset<16> categories; - MessageLevel level; - - LoggerBase() : level(DEFAULT) {setAllCategories();} - virtual ~LoggerBase() {} - - public: - void setCategory(MessageCategory newCategory) { - categories.set(newCategory); - } - - void unsetCategory(MessageCategory oldCategory) { - categories.reset(oldCategory); - } - - void setAllCategories() { - categories.set(); - } - - void unsetAllCategories() { - categories.reset(); - } - - bool isCategorySet(MessageCategory category) const { - return categories.test(category); - } - - MessageLevel getLevel() const { - return level; - } - - void setLevel(MessageLevel newLevel) { - level = newLevel; - } -}; - -} -} - -#endif /* MAD_COMMON_LOGGERBASE_H_ */ diff --git a/src/Common/ModuleManager.cpp b/src/Common/ModuleManager.cpp index 05a2b73..bec3618 100644 --- a/src/Common/ModuleManager.cpp +++ b/src/Common/ModuleManager.cpp @@ -22,8 +22,8 @@ #include "ModuleManager.h" -#include "ConfigEntry.h" -#include "Logger.h" +#include +#include //extern const lt_dlsymlist lt_preloaded_symbols[]; @@ -58,13 +58,13 @@ void ModuleManager::doDeinit() { lt_dlexit(); } -bool ModuleManager::handleConfigEntry(const ConfigEntry &entry, bool handled) { +bool ModuleManager::handleConfigEntry(const Core::ConfigEntry &entry, bool handled) { if(handled) return false; if(entry[0].getKey().matches("LoadModule")) { if(!loadModule(entry[0][0].c_str())) - Logger::logf(Logger::ERROR, "Can't load module '%s'.", entry[0][0].c_str()); + Core::Logger::logf(Core::Logger::ERROR, "Can't load module '%s'.", entry[0][0].c_str()); return true; } diff --git a/src/Common/ModuleManager.h b/src/Common/ModuleManager.h index fa7316f..de5535f 100644 --- a/src/Common/ModuleManager.h +++ b/src/Common/ModuleManager.h @@ -20,8 +20,8 @@ #ifndef MAD_COMMON_MODULEMANAGER_H_ #define MAD_COMMON_MODULEMANAGER_H_ -#include "Configurable.h" -#include "Initializable.h" +#include +#include #include #include @@ -32,7 +32,7 @@ namespace Mad { namespace Common { -class ModuleManager : public Configurable, public Initializable { +class ModuleManager : public Core::Configurable, public Core::Initializable { private: static ModuleManager moduleManager; @@ -47,7 +47,7 @@ class ModuleManager : public Configurable, public Initializable { virtual void doInit(); virtual void doDeinit(); - virtual bool handleConfigEntry(const ConfigEntry &entry, bool handled); + virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool handled); public: lt_dlhandle loadModule(const std::string &name); diff --git a/src/Common/RemoteLogger.h b/src/Common/RemoteLogger.h deleted file mode 100644 index 579a54b..0000000 --- a/src/Common/RemoteLogger.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * LogRequestLogger.h - * - * Copyright (C) 2008 Johannes Thorn - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_COMMON_REMOTELOGGER_H_ -#define MAD_COMMON_REMOTELOGGER_H_ - -#include "LoggerBase.h" - -#include - -namespace Mad { -namespace Common { - -class LogManager; - -class RemoteLogger : public LoggerBase { - protected: - friend class LogManager; - - virtual void logMessage(MessageCategory category, MessageLevel level, time_t messageTimestamp, const std::string &message, const std::string &messageSource) = 0; -}; - -} -} - -#endif /* MAD_COMMON_REMOTELOGGER_H_ */ diff --git a/src/Common/Request.cpp b/src/Common/Request.cpp index 723039f..9aafd7f 100644 --- a/src/Common/Request.cpp +++ b/src/Common/Request.cpp @@ -24,11 +24,11 @@ namespace Common { void Request::handlePacket(boost::shared_ptr packet) { if(packet->getType() == "Error") { - signalFinished(Net::Exception((*packet)["Where"], (*packet)["ErrorCode"], (*packet)["SubCode"], (*packet)["SubSubCode"])); + signalFinished(Core::Exception((*packet)["Where"], (*packet)["ErrorCode"], (*packet)["SubCode"], (*packet)["SubSubCode"])); return; } else if(packet->getType() != "OK") { - signalFinished(Net::Exception(Net::Exception::UNEXPECTED_PACKET)); + signalFinished(Core::Exception(Core::Exception::UNEXPECTED_PACKET)); return; // TODO Logging } diff --git a/src/Common/Request.h b/src/Common/Request.h index 4698ae5..ac59e45 100644 --- a/src/Common/Request.h +++ b/src/Common/Request.h @@ -21,7 +21,7 @@ #define MAD_COMMON_REQUEST_H_ #include "RequestHandler.h" -#include +#include #include @@ -42,14 +42,14 @@ class Request : public RequestHandler { bool isFinished; boost::shared_ptr packet; - Net::Exception exception; + Core::Exception exception; - Net::Signals::Signal2, Net::Exception> finished; + Core::Signals::Signal2, Core::Exception> finished; protected: Request() : isFinished(false) {} - void signalFinished(boost::shared_ptr pkt, Net::Exception exp) { + void signalFinished(boost::shared_ptr pkt, Core::Exception exp) { { boost::lock_guard lock(mutex); @@ -65,25 +65,25 @@ class Request : public RequestHandler { } void signalFinished(boost::shared_ptr packet) { - signalFinished(packet, Net::Exception()); + signalFinished(packet, Core::Exception()); } - void signalFinished(Net::Exception exp) { + void signalFinished(Core::Exception exp) { signalFinished(boost::shared_ptr(), exp); } void signalFinished() { - signalFinished(boost::shared_ptr(), Net::Exception()); + signalFinished(boost::shared_ptr(), Core::Exception()); } virtual void sendRequest() = 0; virtual void handlePacket(boost::shared_ptr packet); public: - Net::Signals::Connection connectSignalFinished(const Net::Signals::Signal2, Net::Exception>::slot_type &slot) { + Core::Signals::Connection connectSignalFinished(const Core::Signals::Signal2, Core::Exception>::slot_type &slot) { return finished.connect(slot); } - void disconnectSignalFinished(const Net::Signals::Connection &con) { + void disconnectSignalFinished(const Core::Signals::Connection &con) { finished.disconnect(con); } @@ -94,11 +94,11 @@ class Request : public RequestHandler { finishCond.wait(lock); } - std::pair, Net::Exception> getResult() { + std::pair, Core::Exception> getResult() { boost::lock_guard lock(mutex); if(!isFinished) - return std::make_pair(boost::shared_ptr(), Net::Exception(Net::Exception::NOT_FINISHED)); + return std::make_pair(boost::shared_ptr(), Core::Exception(Core::Exception::NOT_FINISHED)); else return std::make_pair(packet, exception); } diff --git a/src/Common/RequestHandler.h b/src/Common/RequestHandler.h index f01db41..3deb8c0 100644 --- a/src/Common/RequestHandler.h +++ b/src/Common/RequestHandler.h @@ -23,7 +23,7 @@ #include "Connection.h" #include "XmlPacket.h" -#include +#include #include #include @@ -41,7 +41,7 @@ class RequestHandler : private boost::noncopyable { boost::mutex mutex; - Net::Signals::Signal0 finished; + Core::Signals::Signal0 finished; protected: RequestHandler() {} @@ -59,10 +59,10 @@ class RequestHandler : private boost::noncopyable { public: virtual ~RequestHandler() {} - Net::Signals::Connection connectSignalFinished(const Net::Signals::Signal0::slot_type &slot) { + Core::Signals::Connection connectSignalFinished(const Core::Signals::Signal0::slot_type &slot) { return finished.connect(slot); } - void disconnectSignalFinished(const Net::Signals::Connection &con) { + void disconnectSignalFinished(const Core::Signals::Connection &con) { finished.disconnect(con); } }; diff --git a/src/Common/RequestHandlers/DisconnectRequestHandler.cpp b/src/Common/RequestHandlers/DisconnectRequestHandler.cpp index 942da0a..6c89ab2 100644 --- a/src/Common/RequestHandlers/DisconnectRequestHandler.cpp +++ b/src/Common/RequestHandlers/DisconnectRequestHandler.cpp @@ -18,8 +18,8 @@ */ #include "DisconnectRequestHandler.h" -#include -#include "../Logger.h" +#include +#include namespace Mad { namespace Common { @@ -27,11 +27,11 @@ namespace RequestHandlers { void DisconnectRequestHandler::handlePacket(boost::shared_ptr packet) { if(packet->getType() != "Disconnect") { - Logger::log(Logger::ERROR, "Received an unexpected packet."); + Core::Logger::log(Core::Logger::ERROR, "Received an unexpected packet."); XmlPacket ret; ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); + ret.add("ErrorCode", Core::Exception::UNEXPECTED_PACKET); sendPacket(ret); diff --git a/src/Common/RequestHandlers/SimpleRequestHandler.cpp b/src/Common/RequestHandlers/SimpleRequestHandler.cpp index 0abc484..b6de0f4 100644 --- a/src/Common/RequestHandlers/SimpleRequestHandler.cpp +++ b/src/Common/RequestHandlers/SimpleRequestHandler.cpp @@ -18,9 +18,9 @@ */ #include "SimpleRequestHandler.h" -#include "../Logger.h" -#include +#include +#include namespace Mad { namespace Common { @@ -28,11 +28,11 @@ namespace RequestHandlers { void SimpleRequestHandler::handlePacket(boost::shared_ptr packet) { if(packet->getType() != type) { - Logger::log(Logger::ERROR, "Received an unexpected packet."); + Core::Logger::log(Core::Logger::ERROR, "Received an unexpected packet."); XmlPacket ret; ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); + ret.add("ErrorCode", Core::Exception::UNEXPECTED_PACKET); sendPacket(ret); @@ -47,7 +47,7 @@ void SimpleRequestHandler::handlePacket(boost::shared_ptr packe try { handler(packet, &ret); } - catch(Net::Exception e) { + catch(Core::Exception e) { ret.setType("Error"); ret.add("ErrorCode", e.getErrorCode()); ret.add("SubCode", e.getSubCode()); diff --git a/src/Common/RequestHandlers/StatusRequestHandler.cpp b/src/Common/RequestHandlers/StatusRequestHandler.cpp index 25d2f23..95bdd30 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.cpp +++ b/src/Common/RequestHandlers/StatusRequestHandler.cpp @@ -38,7 +38,7 @@ void StatusRequestHandler::handleRequest(boost::shared_ptrgetMemoryInfo(&totalMem, &freeMem, &totalSwap, &freeSwap); SystemManager::get()->getLoadInfo(¤tLoad, &nProcesses, &loadAvg1, &loadAvg5, &loadAvg15); } - catch(Net::Exception e) {} + catch(Core::Exception e) {} ret->setType("OK"); diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp index b443292..a8b817f 100644 --- a/src/Common/RequestManager.cpp +++ b/src/Common/RequestManager.cpp @@ -19,7 +19,7 @@ #include "RequestManager.h" #include "RequestHandlers/DisconnectRequestHandler.h" -#include "Logger.h" +#include #include @@ -96,14 +96,14 @@ void RequestManager::receiveHandler(Connection *connection, boost::shared_ptr request = requestMap.findRequest(connection, requestId); if(request) { lock.unlock(); - Net::ThreadManager::get()->pushWork(boost::bind(&RequestHandler::handlePacket, request, packet)); + Core::ThreadManager::get()->pushWork(boost::bind(&RequestHandler::handlePacket, request, packet)); return; } if(!requestMap.isConnectionRegistered(connection)) { // TODO: Error - Logger::log(Logger::ERROR, "Received a packet from an unregistered connection."); + Core::Logger::log(Core::Logger::ERROR, "Received a packet from an unregistered connection."); return; } @@ -122,7 +122,7 @@ void RequestManager::receiveHandler(Connection *connection, boost::shared_ptrpushWork(boost::bind(&RequestHandler::handlePacket, request, packet)); + Core::ThreadManager::get()->pushWork(boost::bind(&RequestHandler::handlePacket, request, packet)); return; } @@ -130,11 +130,11 @@ void RequestManager::receiveHandler(Connection *connection, boost::shared_ptrgetType().c_str()); + Core::Logger::logf(Core::Logger::ERROR, "Received an unexpected packet with type '%s'.", packet->getType().c_str()); XmlPacket ret; ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); + ret.add("ErrorCode", Core::Exception::UNEXPECTED_PACKET); connection->sendPacket(ret, requestId); } @@ -143,20 +143,20 @@ bool RequestManager::sendRequest(Connection *connection, boost::shared_ptr lock(mutex); if(!requestMap.isConnectionRegistered(connection)) { - Logger::log(Logger::CRITICAL, "Trying to send a request over an unregistered connecion."); + Core::Logger::log(Core::Logger::CRITICAL, "Trying to send a request over an unregistered connecion."); return false; } uint16_t requestId = _getUnusedRequestId(connection); if(request->isFinished || !requestMap.addRequest(connection, requestId, request)) { - Logger::log(Logger::CRITICAL, "Trying resend a request."); + Core::Logger::log(Core::Logger::CRITICAL, "Trying resend a request."); return false; } request->connectSignalFinished(boost::bind(&RequestManager::handleRequestFinished, this, connection, requestId)); lock.unlock(); - Net::ThreadManager::get()->pushWork(boost::bind(&Request::sendRequest, request)); + Core::ThreadManager::get()->pushWork(boost::bind(&Request::sendRequest, request)); return true; } diff --git a/src/Common/RequestManager.h b/src/Common/RequestManager.h index e66b0c1..2dd892e 100644 --- a/src/Common/RequestManager.h +++ b/src/Common/RequestManager.h @@ -31,11 +31,6 @@ #include namespace Mad { - -namespace Net { -class Packet; -} - namespace Common { class RequestManager : boost::noncopyable { diff --git a/src/Common/Requests/DisconnectRequest.cpp b/src/Common/Requests/DisconnectRequest.cpp index 249bbc1..6c4fa0d 100644 --- a/src/Common/Requests/DisconnectRequest.cpp +++ b/src/Common/Requests/DisconnectRequest.cpp @@ -32,11 +32,11 @@ void DisconnectRequest::sendRequest() { void DisconnectRequest::handlePacket(boost::shared_ptr packet) { if(packet->getType() == "Error") { - signalFinished(Net::Exception((*packet)["Where"], (*packet)["ErrorCode"], (*packet)["SubCode"], (*packet)["SubSubCode"])); + signalFinished(Core::Exception((*packet)["Where"], (*packet)["ErrorCode"], (*packet)["SubCode"], (*packet)["SubSubCode"])); return; } else if(packet->getType() != "OK") { - signalFinished(Net::Exception(Net::Exception::UNEXPECTED_PACKET)); + signalFinished(Core::Exception(Core::Exception::UNEXPECTED_PACKET)); return; // TODO Logging } diff --git a/src/Common/SystemBackend.h b/src/Common/SystemBackend.h index 1543207..418fa2b 100644 --- a/src/Common/SystemBackend.h +++ b/src/Common/SystemBackend.h @@ -31,28 +31,28 @@ class SystemBackend { protected: friend class SystemManager; - virtual void getUptimeInfo(unsigned long *uptime _UNUSED_PARAMETER_, unsigned long *idleTime _UNUSED_PARAMETER_) throw(Net::Exception) { - throw Net::Exception(Net::Exception::NOT_IMPLEMENTED); + virtual void getUptimeInfo(unsigned long *uptime _UNUSED_PARAMETER_, unsigned long *idleTime _UNUSED_PARAMETER_) throw(Core::Exception) { + throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); } - virtual void getMemoryInfo(unsigned long *totalMem _UNUSED_PARAMETER_, unsigned long *freeMem _UNUSED_PARAMETER_, unsigned long *totalSwap _UNUSED_PARAMETER_, unsigned long *freeSwap _UNUSED_PARAMETER_) throw(Net::Exception) { - throw Net::Exception(Net::Exception::NOT_IMPLEMENTED); + virtual void getMemoryInfo(unsigned long *totalMem _UNUSED_PARAMETER_, unsigned long *freeMem _UNUSED_PARAMETER_, unsigned long *totalSwap _UNUSED_PARAMETER_, unsigned long *freeSwap _UNUSED_PARAMETER_) throw(Core::Exception) { + throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); } - virtual void getLoadInfo(unsigned long *currentLoad _UNUSED_PARAMETER_, unsigned long *nProcesses _UNUSED_PARAMETER_, float *loadAvg1 _UNUSED_PARAMETER_, float *loadAvg5 _UNUSED_PARAMETER_, float *loadAvg15 _UNUSED_PARAMETER_) throw(Net::Exception) { - throw Net::Exception(Net::Exception::NOT_IMPLEMENTED); + virtual void getLoadInfo(unsigned long *currentLoad _UNUSED_PARAMETER_, unsigned long *nProcesses _UNUSED_PARAMETER_, float *loadAvg1 _UNUSED_PARAMETER_, float *loadAvg5 _UNUSED_PARAMETER_, float *loadAvg15 _UNUSED_PARAMETER_) throw(Core::Exception) { + throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); } - virtual void getFSInfo(std::vector *fsInfo _UNUSED_PARAMETER_) throw(Net::Exception) { - throw Net::Exception(Net::Exception::NOT_IMPLEMENTED); + virtual void getFSInfo(std::vector *fsInfo _UNUSED_PARAMETER_) throw(Core::Exception) { + throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); } - virtual void shutdown() throw(Net::Exception) { - throw Net::Exception(Net::Exception::NOT_IMPLEMENTED); + virtual void shutdown() throw(Core::Exception) { + throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); } - virtual void reboot() throw(Net::Exception) { - throw Net::Exception(Net::Exception::NOT_IMPLEMENTED); + virtual void reboot() throw(Core::Exception) { + throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); } virtual int getPriority() const { diff --git a/src/Common/SystemManager.cpp b/src/Common/SystemManager.cpp index e58c2ba..fac50d5 100644 --- a/src/Common/SystemManager.cpp +++ b/src/Common/SystemManager.cpp @@ -34,16 +34,16 @@ bool SystemManager::Compare::operator() (boost::shared_ptr b1, bo } -void SystemManager::getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +void SystemManager::getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { (*backend)->getUptimeInfo(uptime, idleTime); return; } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } @@ -51,16 +51,16 @@ void SystemManager::getUptimeInfo(unsigned long *uptime, unsigned long *idleTime throw e; } -void SystemManager::getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +void SystemManager::getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { (*backend)->getMemoryInfo(totalMem, freeMem, totalSwap, freeSwap); return; } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } @@ -68,16 +68,16 @@ void SystemManager::getMemoryInfo(unsigned long *totalMem, unsigned long *freeMe throw e; } -void SystemManager::getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +void SystemManager::getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { (*backend)->getLoadInfo(currentLoad, nProcesses, loadAvg1, loadAvg5, loadAvg15); return; } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } @@ -85,16 +85,16 @@ void SystemManager::getLoadInfo(unsigned long *currentLoad, unsigned long *nProc throw e; } -void SystemManager::getFSInfo(std::vector *fsInfo) throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +void SystemManager::getFSInfo(std::vector *fsInfo) throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { (*backend)->getFSInfo(fsInfo); return; } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } @@ -102,16 +102,16 @@ void SystemManager::getFSInfo(std::vector *fsInfo) throw(Net::Exception) throw e; } -void SystemManager::shutdown() throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +void SystemManager::shutdown() throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { (*backend)->shutdown(); return; } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } @@ -119,16 +119,16 @@ void SystemManager::shutdown() throw(Net::Exception) { throw e; } -void SystemManager::reboot() throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +void SystemManager::reboot() throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { (*backend)->reboot(); return; } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } diff --git a/src/Common/SystemManager.h b/src/Common/SystemManager.h index 9252b2b..6870489 100644 --- a/src/Common/SystemManager.h +++ b/src/Common/SystemManager.h @@ -26,7 +26,7 @@ #include -#include +#include namespace Mad { namespace Common { @@ -62,14 +62,14 @@ class SystemManager { backends.erase(backend); } - void getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) throw(Net::Exception); - void getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) throw(Net::Exception); - void getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) throw(Net::Exception); + void getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) throw(Core::Exception); + void getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) throw(Core::Exception); + void getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) throw(Core::Exception); - void getFSInfo(std::vector *fsInfo) throw(Net::Exception); + void getFSInfo(std::vector *fsInfo) throw(Core::Exception); - void shutdown() throw(Net::Exception); - void reboot() throw(Net::Exception); + void shutdown() throw(Core::Exception); + void reboot() throw(Core::Exception); static SystemManager *get() { return &systemManager; diff --git a/src/Common/Tokenizer.cpp b/src/Common/Tokenizer.cpp deleted file mode 100644 index 8b01482..0000000 --- a/src/Common/Tokenizer.cpp +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Tokenizer.cpp - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "Tokenizer.h" - -namespace Mad { -namespace Common { - -const std::string Tokenizer::delimiters = " \t\n\"'\\"; - - -std::vector Tokenizer::split(const std::string &str) { - std::vector ret; - - for(size_t s = 0; s < str.length();) { - size_t index = str.find_first_of(delimiters, s); - size_t length = (index == std::string::npos) ? std::string::npos : index-s; - - ret.push_back(str.substr(s, length)); - - if(index != std::string::npos) { - size_t index2 = str.find_first_not_of(delimiters, index); - - length = (index2 == std::string::npos) ? std::string::npos : index2-index; - - ret.push_back(str.substr(index, length)); - - if(index2 != std::string::npos) - s = index2; - else - break; - } - else - break; - } - - return ret; -} - -bool Tokenizer::tokenize(const std::string &str, std::vector &out) { - std::vector splitString = split(str); - - bool singleQuotes = false, doubleQuotes = false, escape = false; - std::string token; - bool forceToken = false; - - out.clear(); - - for(std::vector::iterator s = splitString.begin(); s != splitString.end(); ++s) { - token += *s; - escape = false; - - if(++s == splitString.end()) - break; - - for(std::string::iterator c = s->begin(); c != s->end(); ++c) { - if(*c == '\n' && escape) { - escape = false; - - if(doubleQuotes) - continue; - } - - if(escape || (singleQuotes && *c != '\'')) { - token += *c; - - escape = false; - continue; - } - - switch(*c) { - case ' ': - case '\t': - case '\n': - if(doubleQuotes) { - token += *c; - } - else { - if(!token.empty() || forceToken) { - out.push_back(token); - token.clear(); - forceToken = false; - } - } - break; - - case '"': - doubleQuotes = !doubleQuotes; - forceToken = true; - break; - - case '\'': - if(doubleQuotes) { - token += *c; - } - else { - singleQuotes = !singleQuotes; - forceToken = true; - } - break; - - case '\\': - escape = true; - } - } - } - - if(!token.empty() || forceToken) - out.push_back(token); - - return !(singleQuotes || doubleQuotes || escape); -} - -} -} diff --git a/src/Common/Tokenizer.h b/src/Common/Tokenizer.h deleted file mode 100644 index 6b8436b..0000000 --- a/src/Common/Tokenizer.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Tokenizer.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_COMMON_TOKENIZER_H_ -#define MAD_COMMON_TOKENIZER_H_ - -#include -#include - -namespace Mad { -namespace Common { - -class Tokenizer { - private: - static const std::string delimiters; - - Tokenizer(); - - static std::vector split(const std::string &str); - - public: - static bool tokenize(const std::string &str, std::vector &out); -}; - -} -} - -#endif /* MAD_COMMON_TOKENIZER_H_ */ diff --git a/src/Core/CMakeLists.txt b/src/Core/CMakeLists.txt new file mode 100644 index 0000000..30947e3 --- /dev/null +++ b/src/Core/CMakeLists.txt @@ -0,0 +1,25 @@ +include_directories(${INCLUDES}) +link_directories(${Boost_LIBRARY_DIRS}) + +add_library(Core + Signals/Connection.h + Signals/GenericSignal.h + Signals/Signal0.h + Signals/Signal1.h + Signals/Signal2.h + Signals/SignalBase.h + + ConfigEntry.cpp ConfigEntry.h + ConfigManager.cpp ConfigManager.h + Configurable.h + Exception.cpp Exception.h + Initializable.cpp Initializable.h + Logger.cpp Logger.h + LoggerBase.h + LogManager.cpp LogManager.h + RemoteLogger.h + Signals.h + ThreadManager.cpp ThreadManager.h + Tokenizer.cpp Tokenizer.h +) +target_link_libraries(Core ${Boost_LIBRARIES}) diff --git a/src/Core/ConfigEntry.cpp b/src/Core/ConfigEntry.cpp new file mode 100644 index 0000000..d85235a --- /dev/null +++ b/src/Core/ConfigEntry.cpp @@ -0,0 +1,64 @@ +/* + * ConfigEntry.cpp + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "ConfigEntry.h" + +namespace Mad { +namespace Core { + +ConfigEntry::String& ConfigEntry::Entry::operator[] (size_t i) { + try { + return value.at(i); + } + catch(std::out_of_range &e) { + zero = String(); + return zero; + } +} + +const ConfigEntry::String& ConfigEntry::Entry::operator[] (size_t i) const { + try { + return value.at(i); + } + catch(std::out_of_range &e) { + return constZero; + } +} + +ConfigEntry::Entry& ConfigEntry::operator[] (size_t i) { + try { + return entries.at(i); + } + catch(std::out_of_range &e) { + zero = Entry(); + return zero; + } +} + +const ConfigEntry::Entry& ConfigEntry::operator[] (size_t i) const { + try { + return entries.at(i); + } + catch(std::out_of_range &e) { + return constZero; + } +} + +} +} diff --git a/src/Core/ConfigEntry.h b/src/Core/ConfigEntry.h new file mode 100644 index 0000000..42f8f91 --- /dev/null +++ b/src/Core/ConfigEntry.h @@ -0,0 +1,96 @@ +/* + * ConfigEntry.h + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_CONFIGENTRY_H_ +#define MAD_CORE_CONFIGENTRY_H_ + +#include +#include +#include +#include + +namespace Mad { +namespace Core { + +class ConfigEntry { + public: + class String : public std::string { + public: + String() {} + String(const std::string &str) : std::string(str) {} + + bool matches(const std::string &str) const { + return (strcasecmp(c_str(), str.c_str()) == 0); + } + }; + + class Entry { + private: + String key; + std::vector value; + + String zero, constZero; + + public: + Entry() {} + Entry(const std::vector &args) { + if(args.empty()) + return; + + key = args.front(); + + value.assign(args.begin()+1, args.end()); + } + + bool empty() const { + return key.empty(); + } + + String &getKey() {return key;} + const String &getKey() const {return key;} + + size_t getSize() const {return value.size();} + + String& operator[] (size_t i); + const String& operator[] (size_t i) const; + }; + + private: + std::vector entries; + Entry zero, constZero; + + public: + size_t getSize() const {return entries.size();} + + Entry& operator[] (size_t i); + const Entry& operator[] (size_t i) const; + + void push(const Entry &entry) { + entries.push_back(entry); + } + + void pop() { + entries.pop_back(); + } +}; + +} +} + +#endif /* MAD_CORE_CONFIGENTRY_H_ */ diff --git a/src/Core/ConfigManager.cpp b/src/Core/ConfigManager.cpp new file mode 100644 index 0000000..8b1ebd4 --- /dev/null +++ b/src/Core/ConfigManager.cpp @@ -0,0 +1,141 @@ +/* + * ConfigManager.cpp + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "ConfigManager.h" +#include "ConfigEntry.h" +#include "Configurable.h" +#include "Logger.h" +#include "LogManager.h" +#include "Tokenizer.h" + +#include +#include + +namespace Mad { +namespace Core { + +ConfigManager ConfigManager::configManager; + + +bool ConfigManager::Compare::operator() (const Configurable *c1, const Configurable *c2) { + if(c1->getPriority() != c2->getPriority()) + return c1->getPriority() > c2->getPriority(); + else + return c1 < c2; +} + + +void ConfigManager::handleConfigEntry(const ConfigEntry &entry) { + bool handled = false; + + for(std::set::iterator c = configurables.begin(); c != configurables.end(); ++c) { + if((*c)->handleConfigEntry(entry, handled)) + handled = true; + } + + if(!handled) + Logger::logf(Logger::WARNING, "Invalid config option '%s'.", entry[entry.getSize()-1].getKey().c_str()); +} + +bool ConfigManager::loadFile(const std::string &filename) { + if(finished) + return false; + + std::ifstream file(filename.c_str()); + ConfigEntry entry; + std::string line, input; + char delim; + std::vector splitLine, lastConfigLine; + + if(!file.good()) + return false; + + while(!(file.eof() && line.empty() && input.empty())) { + while(input.empty() && !file.eof()) + std::getline(file, input); + + if(input.empty()) + break; + + size_t pos = input.find_first_of("#{}"); + if(pos == std::string::npos) { + line += input; + delim = '\n'; + input.clear(); + } + else { + line += input.substr(0, pos); + delim = input[pos]; + input = input.substr(pos+1); + } + + if(!Tokenizer::tokenize(line, splitLine)) { + line += delim; + continue; + } + + if(!splitLine.empty()) { + pos = line.find_first_of(" \t"); + + entry.push(splitLine); + handleConfigEntry(entry); + entry.pop(); + + lastConfigLine = splitLine; + } + + switch(delim) { + case '#': + input.clear(); + break; + case '{': + entry.push(lastConfigLine); + break; + case '}': + entry.pop(); + } + + line.clear(); + } + + // TODO Depth check + + return true; +} + +void ConfigManager::finish() { + if(finished) + return; + + for(std::set::iterator c = configurables.begin(); c != configurables.end(); ++c) + (*c)->configFinished(); + + finished = true; +} + +ConfigManager::ConfigManager() : finished(false) { + registerConfigurable(LogManager::get()); +} + +ConfigManager::~ConfigManager() { + unregisterConfigurable(LogManager::get()); +} + +} +} diff --git a/src/Core/ConfigManager.h b/src/Core/ConfigManager.h new file mode 100644 index 0000000..2d49a97 --- /dev/null +++ b/src/Core/ConfigManager.h @@ -0,0 +1,69 @@ +/* + * ConfigManager.h + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_CONFIGMANAGER_H_ +#define MAD_CORE_CONFIGMANAGER_H_ + +#include +#include +#include + +namespace Mad { +namespace Core { + +class ConfigEntry; +class Configurable; + +class ConfigManager { + private: + struct Compare { + bool operator() (const Configurable *c1, const Configurable *c2); + }; + + static ConfigManager configManager; + + std::set configurables; + bool finished; + + ConfigManager(); + ~ConfigManager(); + + void handleConfigEntry(const ConfigEntry &entry); + + public: + bool loadFile(const std::string &filename); + void finish(); + + void registerConfigurable(Configurable *c) { + configurables.insert(c); + } + + void unregisterConfigurable(Configurable *c) { + configurables.erase(c); + } + + static ConfigManager *get() { + return &configManager; + } +}; + +} +} + +#endif /* MAD_CORE_CONFIGMANAGER_H_ */ diff --git a/src/Core/Configurable.h b/src/Core/Configurable.h new file mode 100644 index 0000000..db2e4a0 --- /dev/null +++ b/src/Core/Configurable.h @@ -0,0 +1,47 @@ +/* + * Configurable.h + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_CONFIGURABLE_H_ +#define MAD_CORE_CONFIGURABLE_H_ + +namespace Mad { +namespace Core { + +class ConfigEntry; +class ConfigManager; + +class Configurable { + public: + virtual ~Configurable() {} + + virtual int getPriority() const {return 0;} + + protected: + friend class ConfigManager; + + Configurable() {} + + virtual bool handleConfigEntry(const ConfigEntry&, bool) {return false;} + virtual void configFinished() {} +}; + +} +} + +#endif /* MAD_CORE_CONFIGURABLE_H_ */ diff --git a/src/Core/Exception.cpp b/src/Core/Exception.cpp new file mode 100644 index 0000000..fbae6aa --- /dev/null +++ b/src/Core/Exception.cpp @@ -0,0 +1,60 @@ +/* + * Exception.cpp + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "Exception.h" + +#include + +namespace Mad { +namespace Core { + +std::string Exception::strerror() const { + std::string ret; + + if(!where.empty()) + ret = where + ": "; + + switch(errorCode) { + case SUCCESS: + return ret + "Success"; + case UNEXPECTED_PACKET: + return ret + "An unexpected packet was received"; + case INVALID_ACTION: + return ret + "The action is invalid"; + case NOT_AVAILABLE: + return ret + "Not available"; + case NOT_FINISHED: + return ret + "Not finished"; + case NOT_IMPLEMENTED: + return ret + "Not implemented"; + case INTERNAL_ERRNO: + return ret + std::strerror(subCode); + case INVALID_ADDRESS: + return ret + "Invalid address"; + case ALREADY_IDENTIFIED: + return ret + "The host is already identified"; + case UNKNOWN_DAEMON: + return ret + "The daemon is unknown"; + default: + return ret + "Unknown error"; + } +} + +} +} diff --git a/src/Core/Exception.h b/src/Core/Exception.h new file mode 100644 index 0000000..6c89549 --- /dev/null +++ b/src/Core/Exception.h @@ -0,0 +1,65 @@ +/* + * Exception.h + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_EXCEPTION_H_ +#define MAD_CORE_EXCEPTION_H_ + +#include + +namespace Mad { +namespace Core { + +class Exception { + public: + enum ErrorCode { + SUCCESS = 0x0000, UNEXPECTED_PACKET = 0x0001, INVALID_ACTION = 0x0002, NOT_AVAILABLE = 0x0003, NOT_FINISHED = 0x0004, NOT_IMPLEMENTED = 0x0005, + INTERNAL_ERRNO = 0x0010, + INVALID_ADDRESS = 0x0020, + ALREADY_IDENTIFIED = 0x0030, UNKNOWN_DAEMON = 0x0031 + }; + + private: + std::string where; + + ErrorCode errorCode; + long subCode; + long subSubCode; + + public: + Exception(const std::string &where0, ErrorCode errorCode0 = SUCCESS, long subCode0 = 0, long subSubCode0 = 0) + : where(where0), errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) {} + Exception(ErrorCode errorCode0 = SUCCESS, long subCode0 = 0, long subSubCode0 = 0) : errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) {} + virtual ~Exception() {} + + const std::string& getWhere() const {return where;} + ErrorCode getErrorCode() const {return errorCode;} + long getSubCode() const {return subCode;} + long getSubSubCode() const {return subSubCode;} + + std::string strerror() const; + + operator bool() const { + return (errorCode != SUCCESS); + } +}; + +} +} + +#endif /* MAD_CORE_EXCEPTION_H_ */ diff --git a/src/Core/Initializable.cpp b/src/Core/Initializable.cpp new file mode 100644 index 0000000..95d527f --- /dev/null +++ b/src/Core/Initializable.cpp @@ -0,0 +1,69 @@ +/* + * Initializable.cpp + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "Initializable.h" + +#include "ConfigManager.h" +#include "Configurable.h" +#include "Logger.h" + +namespace Mad { +namespace Core { + +std::stack Initializable::initializedObjects; + +void Initializable::init() { + if(initialized) + return; + + if(initializing) { + Logger::log(Logger::CRITICAL, "Fatal initialization error: cyclic dependencies."); + std::terminate(); + } + + initializing = true; + + doInit(); + + Configurable *c = dynamic_cast(this); + if(c) + ConfigManager::get()->registerConfigurable(c); + + initializing = false; + initialized = true; + initializedObjects.push(this); +} + +void Initializable::deinit() { + while(!initializedObjects.empty()) { + Initializable *in = initializedObjects.top(); + + Configurable *c = dynamic_cast(in); + if(c) + ConfigManager::get()->unregisterConfigurable(c); + + in->doDeinit(); + in->initialized = false; + + initializedObjects.pop(); + } +} + +} +} diff --git a/src/Core/Initializable.h b/src/Core/Initializable.h new file mode 100644 index 0000000..e7d329d --- /dev/null +++ b/src/Core/Initializable.h @@ -0,0 +1,54 @@ +/* + * Initializable.h + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_INITIALIZABLE_H_ +#define MAD_CORE_INITIALIZABLE_H_ + +#include + +namespace Mad { +namespace Core { + +class Initializable { + private: + static std::stack initializedObjects; + + bool initializing; + bool initialized; + + protected: + Initializable() : initializing(false), initialized(false) {} + virtual void doInit() {} + virtual void doDeinit() {} + + public: + virtual ~Initializable() {} + + void init(); + + bool isInitialized() const {return initialized;} + bool isInitializing() const {return initializing;} + + static void deinit(); +}; + +} +} + +#endif /* MAD_CORE_INITIALIZABLE_H_ */ diff --git a/src/Core/LogManager.cpp b/src/Core/LogManager.cpp new file mode 100644 index 0000000..bf9767b --- /dev/null +++ b/src/Core/LogManager.cpp @@ -0,0 +1,146 @@ +/* + * LogManager.cpp + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "LogManager.h" +#include "ConfigEntry.h" + +#include + +namespace Mad { +namespace Core { + +LogManager LogManager::logManager; + + +void LogManager::ConsoleLogger::logMessage(MessageCategory category _UNUSED_PARAMETER_, MessageLevel level, time_t timestamp _UNUSED_PARAMETER_, const std::string &message) { + if(level != CRITICAL) {// Critical messages are printed to cerr directly, so don't print them a second time + cerrLock.lock(); + std::cerr << message << std::endl; + cerrLock.unlock(); + } +} + +void LogManager::ConsoleLogger::logMessage(MessageCategory category _UNUSED_PARAMETER_, MessageLevel, time_t timestamp _UNUSED_PARAMETER_, const std::string &message, const std::string &messageSource) { + cerrLock.lock(); + std::cerr << message << " from " << messageSource << std::endl; + cerrLock.unlock(); +} + +void LogManager::ConsoleLogger::logMessageDirect(MessageCategory category _UNUSED_PARAMETER_, MessageLevel level _UNUSED_PARAMETER_, time_t timestamp _UNUSED_PARAMETER_, const std::string &message) { + cerrLock.lock(); + std::cerr << message << std::endl; + cerrLock.unlock(); +} + + +bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) { + if(handled) + return false; + + if(entry[0].getKey().matches("Logger")) { + if(entry[0][0].matches("Console")) { + if(entry[1].empty()) { + registerLogger(static_cast(&consoleLogger)); + return true; + } + } + else if(entry[1].empty()) { + Logger::logf(Logger::WARNING, "Unknown logger '%s'.", entry[0][0].c_str()); + return true; + } + } + + return false; +} + +void LogManager::configFinished() { + if(loggers.empty()) + registerLogger(static_cast(&consoleLogger)); + + // TODO Debug + consoleLogger.Logger::setLevel(LoggerBase::DEBUG); + + queueLock.lock(); + configured = true; + queueLock.unlock(); + queueCond.notify_one(); +} + +void LogManager::log(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message) { + if(level == LoggerBase::CRITICAL) + consoleLogger.logMessageDirect(category, level, timestamp, message); + + queueLock.lock(); + Message m = {category, level, timestamp, message}; + messageQueue.push(m); + queueLock.unlock(); + queueCond.notify_one(); +} + +void LogManager::log(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message, const std::string &source) { + queueLock.lock(); + RemoteMessage m = {category, level, timestamp, message, source}; + remoteMessageQueue.push(m); + queueLock.unlock(); + queueCond.notify_one(); +} + +void LogManager::loggerThread() { + boost::unique_lock lock(queueLock); + + running = true; + + while(running) { + while(running && ((messageQueue.empty() && messageQueue.empty()) || !configured)) + queueCond.wait(lock); + + while(!messageQueue.empty()) { + Message message = messageQueue.front(); + messageQueue.pop(); + lock.unlock(); + + loggerLock.lock(); + for(std::set::iterator logger = loggers.begin(); logger != loggers.end(); ++logger) { + if((*logger)->getLevel() >= message.level && (*logger)->isCategorySet(message.category)) + (*logger)->logMessage(message.category, message.level, message.timestamp, message.message); + } + loggerLock.unlock(); + + lock.lock(); + } + + while(!remoteMessageQueue.empty()) { + RemoteMessage message = remoteMessageQueue.front(); + remoteMessageQueue.pop(); + lock.unlock(); + + remoteLoggerLock.lock(); + for(std::set::iterator logger = remoteLoggers.begin(); logger != remoteLoggers.end(); ++logger) { + if((*logger)->getLevel() >= message.level && (*logger)->isCategorySet(message.category)) + (*logger)->logMessage(message.category, message.level, message.timestamp, message.message, message.source); + } + remoteLoggerLock.unlock(); + + lock.lock(); + } + } +} + +} +} diff --git a/src/Core/LogManager.h b/src/Core/LogManager.h new file mode 100644 index 0000000..6d75b82 --- /dev/null +++ b/src/Core/LogManager.h @@ -0,0 +1,146 @@ +/* + * LogManager.h + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_LOGMANAGER_H_ +#define MAD_CORE_LOGMANAGER_H_ + +#include + +#include "Configurable.h" +#include "Logger.h" +#include "RemoteLogger.h" + +#include +#include + +#include +#include + +namespace Mad { +namespace Core { + +class ThreadManager; + +class LogManager : public Configurable { + private: + friend class ThreadManager; + + typedef LoggerBase::MessageCategory MessageCategory; + typedef LoggerBase::MessageLevel MessageLevel; + + struct Message { + MessageCategory category; + MessageLevel level; + time_t timestamp; + std::string message; + }; + + struct RemoteMessage { + MessageCategory category; + MessageLevel level; + time_t timestamp; + std::string message; + std::string source; + }; + + class ConsoleLogger : public Logger, public RemoteLogger { + private: + // For long messages, writing to cerr is not atomic + // -> lock cerr to prevent mixing messages up + boost::mutex cerrLock; + + protected: + virtual void logMessage(MessageCategory category _UNUSED_PARAMETER_, MessageLevel level, time_t timestamp _UNUSED_PARAMETER_, const std::string &message); + virtual void logMessage(MessageCategory category _UNUSED_PARAMETER_, MessageLevel, time_t timestamp _UNUSED_PARAMETER_, const std::string &message, const std::string &messageSource); + + public: + ConsoleLogger() {} + + void logMessageDirect(MessageCategory category _UNUSED_PARAMETER_, MessageLevel level _UNUSED_PARAMETER_, time_t timestamp _UNUSED_PARAMETER_, const std::string &message); + }; + + + static LogManager logManager; + + ConsoleLogger consoleLogger; + + std::set loggers; + std::set remoteLoggers; + bool configured, running; + + boost::mutex queueLock; + boost::condition_variable queueCond; + + boost::mutex loggerLock; + boost::mutex remoteLoggerLock; + + std::queue messageQueue; + std::queue remoteMessageQueue; + + void loggerThread(); + void stopLoggerThread() { + queueLock.lock(); + running = false; + queueLock.unlock(); + queueCond.notify_one(); + } + + LogManager() : configured(false), running(false) {} + + protected: + virtual bool handleConfigEntry(const ConfigEntry &entry, bool handled); + virtual void configFinished(); + + public: + void log(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message); + void log(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message, const std::string &source); + + void registerLogger(Logger *logger) { + loggerLock.lock(); + loggers.insert(logger); + loggerLock.unlock(); + } + + void unregisterLogger(Logger *logger) { + loggerLock.lock(); + loggers.erase(logger); + loggerLock.unlock(); + } + + void registerLogger(RemoteLogger *logger) { + remoteLoggerLock.lock(); + remoteLoggers.insert(logger); + remoteLoggerLock.unlock(); + } + + void unregisterLogger(RemoteLogger *logger) { + remoteLoggerLock.lock(); + remoteLoggers.erase(logger); + remoteLoggerLock.unlock(); + } + + static LogManager *get() { + return &logManager; + } +}; + +} +} + +#endif /* MAD_CORE_LOGMANAGER_H_ */ diff --git a/src/Core/Logger.cpp b/src/Core/Logger.cpp new file mode 100644 index 0000000..e4e0341 --- /dev/null +++ b/src/Core/Logger.cpp @@ -0,0 +1,89 @@ +/* + * Logger.cpp + * + * Copyright (C) 2008 Johannes Thorn + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "Logger.h" +#include "LogManager.h" + +#include + +namespace Mad { +namespace Core { + +void Logger::logfv(MessageCategory category, MessageLevel level, const char *format, va_list ap) { + int size = 100; + char *buf = (char*)std::malloc(size); + + // If buffer is too small, try again with bigger buffer + while(true) { + va_list ap2; + + va_copy(ap2, ap); + int n = std::vsnprintf(buf, size, format, ap2); + va_end(ap2); + + if(n > -1 && n < size) { + log(category, level, buf); + std::free(buf); + return; + } + + if(n > -1) + size = n+1; + else + size *= 2; + + buf = (char*)std::realloc(buf, size); + } + +} + +void Logger::log(MessageCategory category, MessageLevel level, const std::string &message) { + LogManager::get()->log(category, level, std::time(0), message); +} + +void Logger::logf(MessageCategory category, MessageLevel level, const char *format, ...) { + va_list ap; + va_start(ap, format); + logfv(category, level, format, ap); + va_end(ap); +} + +void Logger::logf(MessageCategory category, const char *format, ...) { + va_list ap; + va_start(ap, format); + logfv(category, DEFAULT, format, ap); + va_end(ap); +} + +void Logger::logf(MessageLevel level, const char *format, ...) { + va_list ap; + va_start(ap, format); + logfv(GENERAL, level, format, ap); + va_end(ap); +} + +void Logger::logf(const char *format, ...) { + va_list ap; + va_start(ap, format); + logfv(GENERAL, DEFAULT, format, ap); + va_end(ap); +} + +} +} diff --git a/src/Core/Logger.h b/src/Core/Logger.h new file mode 100644 index 0000000..3de22d2 --- /dev/null +++ b/src/Core/Logger.h @@ -0,0 +1,68 @@ +/* + * Logger.h + * + * Copyright (C) 2008 Johannes Thorn + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_LOGGER_H_ +#define MAD_CORE_LOGGER_H_ + +#include "LoggerBase.h" + +#include +#include +#include + +namespace Mad { +namespace Core { + +class LogManager; + +class Logger : public LoggerBase { + private: + friend class LogManager; + + static void logfv(MessageCategory category, MessageLevel level, const char *format, va_list ap); + + protected: + virtual void logMessage(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message) = 0; + + public: + static void log(MessageCategory category, MessageLevel level, const std::string &message); + + static void log(MessageCategory category, const std::string &message) { + log(category, DEFAULT, message); + } + + static void log(MessageLevel level, const std::string &message) { + log(GENERAL, level, message); + } + + static void log(const std::string &message) { + log(GENERAL, DEFAULT, message); + } + + + static void logf(MessageCategory category, MessageLevel level, const char *format, ...); + static void logf(MessageCategory category, const char *format, ...); + static void logf(MessageLevel level, const char *format, ...); + static void logf(const char *format, ...); +}; + +} +} + +#endif /* MAD_CORE_LOGGER_H_ */ diff --git a/src/Core/LoggerBase.h b/src/Core/LoggerBase.h new file mode 100644 index 0000000..49be833 --- /dev/null +++ b/src/Core/LoggerBase.h @@ -0,0 +1,79 @@ +/* + * LoggerBase.h + * + * Copyright (C) 2008 Johannes Thorn + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_LOGGERBASE_H_ +#define MAD_CORE_LOGGERBASE_H_ + +#include +#include + +namespace Mad { +namespace Core { + +class LoggerBase { + public: + enum MessageLevel { + CRITICAL, ERROR, WARNING, DEFAULT, VERBOSE, DEBUG + }; + + enum MessageCategory { + SYSTEM, NETWORK, DAEMON, USER, DISK, PROGRAM, GENERAL + }; + + protected: + std::bitset<16> categories; + MessageLevel level; + + LoggerBase() : level(DEFAULT) {setAllCategories();} + virtual ~LoggerBase() {} + + public: + void setCategory(MessageCategory newCategory) { + categories.set(newCategory); + } + + void unsetCategory(MessageCategory oldCategory) { + categories.reset(oldCategory); + } + + void setAllCategories() { + categories.set(); + } + + void unsetAllCategories() { + categories.reset(); + } + + bool isCategorySet(MessageCategory category) const { + return categories.test(category); + } + + MessageLevel getLevel() const { + return level; + } + + void setLevel(MessageLevel newLevel) { + level = newLevel; + } +}; + +} +} + +#endif /* MAD_CORE_LOGGERBASE_H_ */ diff --git a/src/Core/RemoteLogger.h b/src/Core/RemoteLogger.h new file mode 100644 index 0000000..f5c8a6b --- /dev/null +++ b/src/Core/RemoteLogger.h @@ -0,0 +1,42 @@ +/* + * LogRequestLogger.h + * + * Copyright (C) 2008 Johannes Thorn + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_REMOTELOGGER_H_ +#define MAD_CORE_REMOTELOGGER_H_ + +#include "LoggerBase.h" + +#include + +namespace Mad { +namespace Core { + +class LogManager; + +class RemoteLogger : public LoggerBase { + protected: + friend class LogManager; + + virtual void logMessage(MessageCategory category, MessageLevel level, time_t messageTimestamp, const std::string &message, const std::string &messageSource) = 0; +}; + +} +} + +#endif /* MAD_CORE_REMOTELOGGER_H_ */ diff --git a/src/Core/Signals.h b/src/Core/Signals.h new file mode 100644 index 0000000..3c129af --- /dev/null +++ b/src/Core/Signals.h @@ -0,0 +1,27 @@ +/* + * Signals.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_SIGNALS_SIGNALS_H_ +#define MAD_CORE_SIGNALS_SIGNALS_H_ + +#include "Signals/Signal0.h" +#include "Signals/Signal1.h" +#include "Signals/Signal2.h" + +#endif /* MAD_CORE_SIGNALS_SIGNALS_H_ */ diff --git a/src/Core/Signals/Connection.h b/src/Core/Signals/Connection.h new file mode 100644 index 0000000..5ce7b4a --- /dev/null +++ b/src/Core/Signals/Connection.h @@ -0,0 +1,53 @@ +/* + * Connection.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_SIGNALS_CONNECTION_H_ +#define MAD_CORE_SIGNALS_CONNECTION_H_ + +namespace Mad { +namespace Core { +namespace Signals { + +class SignalBase; + +class Connection { + private: + friend class SignalBase; + + SignalBase *signal; + unsigned long id; + + Connection(SignalBase *signal0, unsigned long id0) + : signal(signal0), id(id0) {} + + public: + bool operator==(const Connection &o) const { + return (signal == o.signal && id == o.id); + } + + bool operator<(const Connection &o) const { + return (signal != o.signal) ? (signal < o.signal) : (id < o.id); + } +}; + +} +} +} + +#endif /* MAD_CORE_SIGNALS_CONNECTION_H_ */ diff --git a/src/Core/Signals/GenericSignal.h b/src/Core/Signals/GenericSignal.h new file mode 100644 index 0000000..641f575 --- /dev/null +++ b/src/Core/Signals/GenericSignal.h @@ -0,0 +1,61 @@ +/* + * GenericSignal.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_SIGNALS_GENERICSIGNAL_H_ +#define MAD_CORE_SIGNALS_GENERICSIGNAL_H_ + +#include "SignalBase.h" + +#include +#include + +namespace Mad { +namespace Core { +namespace Signals { + +template +class GenericSignal : protected SignalBase { + public: + typedef FunctionType slot_type; + + protected: + std::map handlers; + + public: + Connection connect(const slot_type &slot) { + boost::lock_guard lock(mutex); + + Connection con = getNewConnection(); + handlers.insert(std::make_pair(con, slot)); + + return con; + } + + bool disconnect(const Connection &connection) { + boost::lock_guard lock(mutex); + + return handlers.erase(connection); + } +}; + +} +} +} + +#endif /* MAD_CORE_SIGNALS_GENERICSIGNAL_H_ */ diff --git a/src/Core/Signals/Signal0.h b/src/Core/Signals/Signal0.h new file mode 100644 index 0000000..ccfb548 --- /dev/null +++ b/src/Core/Signals/Signal0.h @@ -0,0 +1,46 @@ +/* + * Signal0.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_SIGNALS_SIGNAL0_H_ +#define MAD_CORE_SIGNALS_SIGNAL0_H_ + +#include "GenericSignal.h" +#include "../ThreadManager.h" + +#include + +namespace Mad { +namespace Core { +namespace Signals { + +class Signal0 : public GenericSignal > { + public: + void emit() { + boost::lock_guard lock(mutex); + + for(std::map::iterator handler = handlers.begin(); handler != handlers.end(); ++handler) + ThreadManager::get()->pushWork(handler->second); + } +}; + +} +} +} + +#endif /* MAD_CORE_SIGNALS_SIGNAL0_H_ */ diff --git a/src/Core/Signals/Signal1.h b/src/Core/Signals/Signal1.h new file mode 100644 index 0000000..e4a946c --- /dev/null +++ b/src/Core/Signals/Signal1.h @@ -0,0 +1,47 @@ +/* + * Signal1.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_SIGNALS_SIGNAL1_H_ +#define MAD_CORE_SIGNALS_SIGNAL1_H_ + +#include "GenericSignal.h" +#include "../ThreadManager.h" + +#include + +namespace Mad { +namespace Core { +namespace Signals { + +template +class Signal1 : public GenericSignal > { + public: + void emit(T1 arg1) { + boost::lock_guard lock(this->mutex); + + for(typename std::map >::slot_type>::iterator handler = this->handlers.begin(); handler != this->handlers.end(); ++handler) + ThreadManager::get()->pushWork(boost::bind(handler->second, arg1)); + } +}; + +} +} +} + +#endif /* MAD_CORE_SIGNALS_SIGNAL1_H_ */ diff --git a/src/Core/Signals/Signal2.h b/src/Core/Signals/Signal2.h new file mode 100644 index 0000000..41045d7 --- /dev/null +++ b/src/Core/Signals/Signal2.h @@ -0,0 +1,47 @@ +/* + * Signal2.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_SIGNALS_SIGNAL2_H_ +#define MAD_CORE_SIGNALS_SIGNAL2_H_ + +#include "GenericSignal.h" +#include "../ThreadManager.h" + +#include + +namespace Mad { +namespace Core { +namespace Signals { + +template +class Signal2 : public GenericSignal > { + public: + void emit(T1 arg1, T2 arg2) { + boost::lock_guard lock(this->mutex); + + for(typename std::map >::slot_type>::iterator handler = this->handlers.begin(); handler != this->handlers.end(); ++handler) + ThreadManager::get()->pushWork(boost::bind(handler->second, arg1, arg2)); + } +}; + +} +} +} + +#endif /* MAD_CORE_SIGNALS_SIGNAL2_H_ */ diff --git a/src/Core/Signals/SignalBase.h b/src/Core/Signals/SignalBase.h new file mode 100644 index 0000000..1a5d5a3 --- /dev/null +++ b/src/Core/Signals/SignalBase.h @@ -0,0 +1,53 @@ +/* + * SignalBase.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_SIGNALS_SIGNALBASE_H_ +#define MAD_CORE_SIGNALS_SIGNALBASE_H_ + +#include "Connection.h" + +#include +#include + +namespace Mad { +namespace Core { +namespace Signals { + +class SignalBase : private boost::noncopyable { + private: + friend class Connection; + + unsigned long connectionId; + + protected: + boost::mutex mutex; + + Connection getNewConnection() { + return Connection(this, connectionId++); + } + + SignalBase() : connectionId(0) {} + virtual ~SignalBase() {} +}; + +} +} +} + +#endif /* MAD_CORE_SIGNALS_SIGNALBASE_H_ */ diff --git a/src/Core/ThreadManager.cpp b/src/Core/ThreadManager.cpp new file mode 100644 index 0000000..abc0bc6 --- /dev/null +++ b/src/Core/ThreadManager.cpp @@ -0,0 +1,143 @@ +/* + * ThreadManager.cpp + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "ThreadManager.h" + +#include "Logger.h" +#include "LogManager.h" + +#include + +namespace Mad { +namespace Core { + +ThreadManager ThreadManager::threadManager; + + +void ThreadManager::workerFunc() { + while(true) { + boost::unique_lock lock(runLock); + + if(!running || !isThisWorkerThread()) + break; + + lock.unlock(); + + boost::unique_lock lock2(workLock); + while(work.empty()) { + workCond.wait(lock2); + + if(!running) + return; + } + + boost::function0 nextWork = work.front(); + work.pop(); + + lock2.unlock(); + + nextWork(); + } + + // And let the new worker thread join us... + pushWork(boost::bind(&ThreadManager::threadFinished, this, boost::this_thread::get_id())); +} + +void ThreadManager::detach() { + if(isThisMainThread()) { + Logger::log(Logger::CRITICAL, "Tried to detach main thread! This is just WRONG!"); + return; + } + + { + boost::lock_guard lock(runLock); + if(!running) + return; // There's no point in creating a new worker thread when we aren't running anymore + } + + threadLock.lock(); + + if(workerThread->get_id() == boost::this_thread::get_id()) {// Already detached? + threads.insert(std::make_pair(boost::this_thread::get_id(), workerThread)); + + workerThread.reset(new boost::thread(std::mem_fun(&ThreadManager::workerFunc), this)); + } + + threadLock.unlock(); +} + +void ThreadManager::pushWork(const boost::function0 &newWork) { + workLock.lock(); + work.push(newWork); + workLock.unlock(); + + workCond.notify_one(); +} + + +void ThreadManager::doInit() { + running = true; + + threadLock.lock(); + + ioWorker.reset(new boost::asio::io_service::work(ioService)); + + mainThreadId = boost::this_thread::get_id(); + workerThread.reset(new boost::thread(&ThreadManager::workerFunc, this)); + loggerThread.reset(new boost::thread(&LogManager::loggerThread, LogManager::get())); + ioThread.reset(new boost::thread((std::size_t(boost::asio::io_service::*)())&boost::asio::io_service::run, &ioService)); + + threadLock.unlock(); +} + +void ThreadManager::doDeinit() { + if(!isThisMainThread()) { + // TODO Critical error!!! + return; + } + + // First set running = false so the worker threads quit + boost::lock(runLock, workLock); + running = false; + + workLock.unlock(); + runLock.unlock(); + + workCond.notify_one(); + + // We don't have to lock threadLock as detach() won't change workerThread when running is false + workerThread->join(); + + // Now wait for all detached threads + while(!threads.empty()) { + threadFinished(threads.begin()->first); + } + + // IO thread is next + ioWorker.reset(); + ioService.stop(); + ioThread->join(); + + // Finally, the logger thread has to die + LogManager::get()->stopLoggerThread(); + loggerThread->join(); +} + +} +} diff --git a/src/Core/ThreadManager.h b/src/Core/ThreadManager.h new file mode 100644 index 0000000..b512ad4 --- /dev/null +++ b/src/Core/ThreadManager.h @@ -0,0 +1,113 @@ +/* + * ThreadManager.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_THREADMANAGER_H_ +#define MAD_CORE_THREADMANAGER_H_ + +#include + +#include "Initializable.h" + +#include +#include + +#include + +#include +#include +#include + +namespace Mad { +namespace Core { + +class ThreadManager : public Initializable { + private: + boost::thread::id mainThreadId; + boost::shared_ptr workerThread, loggerThread, ioThread; + std::map > threads; + + boost::mutex threadLock; + + boost::mutex runLock; + bool running; + + boost::mutex workLock; + boost::condition_variable workCond; + std::queue > work; + + boost::scoped_ptr ioWorker; + + boost::asio::io_service ioService; + + static ThreadManager threadManager; + + ThreadManager() {} + + void workerFunc(); + void ioFunc(); + + void threadFinished(boost::thread::id threadId) { + boost::shared_ptr thread; + + { + boost::lock_guard lock(threadLock); + + std::map >::iterator it = threads.find(threadId); + + if(it == threads.end()) + return; + + thread = it->second; + threads.erase(it); + } + + thread->join(); + } + + protected: + virtual void doInit(); + virtual void doDeinit(); + + public: + bool isThisMainThread() { + return (mainThreadId == boost::this_thread::get_id()); + } + + bool isThisWorkerThread() { + boost::lock_guard lock(threadLock); + return (workerThread->get_id() == boost::this_thread::get_id()); + } + + boost::asio::io_service& getIOService() { + return ioService; + } + + void detach(); + + void pushWork(const boost::function0 &newWork); + + static ThreadManager* get() { + return &threadManager; + } +}; + +} +} + +#endif /* MAD_CORE_THREADMANAGER_H_ */ diff --git a/src/Core/Tokenizer.cpp b/src/Core/Tokenizer.cpp new file mode 100644 index 0000000..a812ed1 --- /dev/null +++ b/src/Core/Tokenizer.cpp @@ -0,0 +1,131 @@ +/* + * Tokenizer.cpp + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "Tokenizer.h" + +namespace Mad { +namespace Core { + +const std::string Tokenizer::delimiters = " \t\n\"'\\"; + + +std::vector Tokenizer::split(const std::string &str) { + std::vector ret; + + for(size_t s = 0; s < str.length();) { + size_t index = str.find_first_of(delimiters, s); + size_t length = (index == std::string::npos) ? std::string::npos : index-s; + + ret.push_back(str.substr(s, length)); + + if(index != std::string::npos) { + size_t index2 = str.find_first_not_of(delimiters, index); + + length = (index2 == std::string::npos) ? std::string::npos : index2-index; + + ret.push_back(str.substr(index, length)); + + if(index2 != std::string::npos) + s = index2; + else + break; + } + else + break; + } + + return ret; +} + +bool Tokenizer::tokenize(const std::string &str, std::vector &out) { + std::vector splitString = split(str); + + bool singleQuotes = false, doubleQuotes = false, escape = false; + std::string token; + bool forceToken = false; + + out.clear(); + + for(std::vector::iterator s = splitString.begin(); s != splitString.end(); ++s) { + token += *s; + escape = false; + + if(++s == splitString.end()) + break; + + for(std::string::iterator c = s->begin(); c != s->end(); ++c) { + if(*c == '\n' && escape) { + escape = false; + + if(doubleQuotes) + continue; + } + + if(escape || (singleQuotes && *c != '\'')) { + token += *c; + + escape = false; + continue; + } + + switch(*c) { + case ' ': + case '\t': + case '\n': + if(doubleQuotes) { + token += *c; + } + else { + if(!token.empty() || forceToken) { + out.push_back(token); + token.clear(); + forceToken = false; + } + } + break; + + case '"': + doubleQuotes = !doubleQuotes; + forceToken = true; + break; + + case '\'': + if(doubleQuotes) { + token += *c; + } + else { + singleQuotes = !singleQuotes; + forceToken = true; + } + break; + + case '\\': + escape = true; + } + } + } + + if(!token.empty() || forceToken) + out.push_back(token); + + return !(singleQuotes || doubleQuotes || escape); +} + +} +} diff --git a/src/Core/Tokenizer.h b/src/Core/Tokenizer.h new file mode 100644 index 0000000..7b29fed --- /dev/null +++ b/src/Core/Tokenizer.h @@ -0,0 +1,44 @@ +/* + * Tokenizer.h + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_TOKENIZER_H_ +#define MAD_CORE_TOKENIZER_H_ + +#include +#include + +namespace Mad { +namespace Core { + +class Tokenizer { + private: + static const std::string delimiters; + + Tokenizer(); + + static std::vector split(const std::string &str); + + public: + static bool tokenize(const std::string &str, std::vector &out); +}; + +} +} + +#endif /* MAD_CORE_TOKENIZER_H_ */ diff --git a/src/Daemon/Backends/NetworkLogger.h b/src/Daemon/Backends/NetworkLogger.h index 10f5d25..c0b123a 100644 --- a/src/Daemon/Backends/NetworkLogger.h +++ b/src/Daemon/Backends/NetworkLogger.h @@ -20,7 +20,7 @@ #ifndef MAD_DAEMON_BACKENDS_NETWORKLOGGER_H_ #define MAD_DAEMON_BACKENDS_NETWORKLOGGER_H_ -#include +#include #include #include @@ -28,12 +28,12 @@ namespace Mad { namespace Daemon { namespace Backends { -class NetworkLogger : public Common::Logger { +class NetworkLogger : public Core::Logger { private: Common::Connection *connection; protected: - virtual void logMessage(Common::Logger::MessageCategory category, Common::Logger::MessageLevel level, time_t messageTimestamp, const std::string &message) { + virtual void logMessage(Core::Logger::MessageCategory category, Core::Logger::MessageLevel level, time_t messageTimestamp, const std::string &message) { boost::shared_ptr request(new Requests::LogRequest(category, level, messageTimestamp, message)); Common::RequestManager::get()->sendRequest(connection, request); } diff --git a/src/Daemon/Requests/LogRequest.h b/src/Daemon/Requests/LogRequest.h index bf675e3..6ee3b3d 100644 --- a/src/Daemon/Requests/LogRequest.h +++ b/src/Daemon/Requests/LogRequest.h @@ -21,7 +21,7 @@ #define MAD_DAEMON_REQUESTS_LOGREQUEST_H_ #include -#include +#include #include namespace Mad { @@ -30,8 +30,8 @@ namespace Requests { class LogRequest : public Common::Request { private: - Common::Logger::MessageCategory category; - Common::Logger::MessageLevel level; + Core::Logger::MessageCategory category; + Core::Logger::MessageLevel level; time_t messageTimestamp; std::string message; @@ -39,7 +39,7 @@ class LogRequest : public Common::Request { virtual void sendRequest(); public: - LogRequest(Common::Logger::MessageCategory category0, Common::Logger::MessageLevel level0, time_t messageTimestamp0, const std::string &message0) + LogRequest(Core::Logger::MessageCategory category0, Core::Logger::MessageLevel level0, time_t messageTimestamp0, const std::string &message0) : category(category0), level(level0), messageTimestamp(messageTimestamp0), message(message0) {} }; diff --git a/src/Net/CMakeLists.txt b/src/Net/CMakeLists.txt index 6bf700e..298aebe 100644 --- a/src/Net/CMakeLists.txt +++ b/src/Net/CMakeLists.txt @@ -1,20 +1,9 @@ include_directories(${INCLUDES}) -link_directories(${Boost_LIBRARY_DIRS}) add_library(Net - Signals/Connection.h - Signals/GenericSignal.h - Signals/Signal0.h - Signals/Signal1.h - Signals/Signal2.h - Signals/SignalBase.h - Signals/Signals.h - ClientConnection.cpp ClientConnection.h Connection.cpp Connection.h - Exception.cpp Exception.h Listener.cpp Listener.h Packet.cpp Packet.h - ThreadManager.cpp ThreadManager.h ) -target_link_libraries(Net ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES}) +target_link_libraries(Net Core ${OPENSSL_LIBRARIES}) diff --git a/src/Net/ClientConnection.cpp b/src/Net/ClientConnection.cpp index 07a9121..9196d89 100644 --- a/src/Net/ClientConnection.cpp +++ b/src/Net/ClientConnection.cpp @@ -19,8 +19,6 @@ #include "ClientConnection.h" -#include - namespace Mad { namespace Net { @@ -36,7 +34,7 @@ void ClientConnection::handleConnect(const boost::system::error_code& error) { socket.async_handshake(boost::asio::ssl::stream_base::client, boost::bind(&ClientConnection::handleHandshake, this, boost::asio::placeholders::error)); } -void ClientConnection::connect(const boost::asio::ip::tcp::endpoint &address) throw(Exception) { +void ClientConnection::connect(const boost::asio::ip::tcp::endpoint &address) throw(Core::Exception) { boost::lock_guard lock(connectionLock); if(_isConnected()) { diff --git a/src/Net/ClientConnection.h b/src/Net/ClientConnection.h index f93c2fc..dcb6906 100644 --- a/src/Net/ClientConnection.h +++ b/src/Net/ClientConnection.h @@ -21,7 +21,7 @@ #define MAD_NET_CLIENTCONNECTION_H_ #include "Connection.h" -#include "Exception.h" +#include #include @@ -37,13 +37,13 @@ class ClientConnection : private boost::base_from_member(boost::ref(Connection::ioService), boost::asio::ssl::context::sslv23), + : boost::base_from_member(boost::ref(Core::ThreadManager::get()->getIOService()), boost::asio::ssl::context::sslv23), Connection(member) { member.set_verify_mode(boost::asio::ssl::context::verify_none); } - void connect(const boost::asio::ip::tcp::endpoint &address) throw(Exception); + void connect(const boost::asio::ip::tcp::endpoint &address) throw(Core::Exception); }; } diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp index d9ff994..81f4c64 100644 --- a/src/Net/Connection.cpp +++ b/src/Net/Connection.cpp @@ -19,7 +19,7 @@ #include "Connection.h" -#include +#include #include #include @@ -27,8 +27,6 @@ namespace Mad { namespace Net { -boost::asio::io_service Connection::ioService; - Connection::~Connection() { if(_isConnected()) @@ -37,7 +35,7 @@ Connection::~Connection() { void Connection::handleHandshake(const boost::system::error_code& error) { if(error) { - Common::Logger::logf("Error: %s", error.message().c_str()); + Core::Logger::logf("Error: %s", error.message().c_str()); // TODO Error handling doDisconnect(); @@ -63,7 +61,7 @@ void Connection::handleShutdown(const boost::system::error_code& error) { boost::lock_guard lock(connectionLock); if(error) { - Common::Logger::logf(Common::Logger::VERBOSE, "Shutdown error: %s", error.message().c_str()); + Core::Logger::logf(Core::Logger::VERBOSE, "Shutdown error: %s", error.message().c_str()); } _setState(DISCONNECTED); @@ -112,7 +110,7 @@ void Connection::handleDataReceive(const std::vector &data) { void Connection::handleRead(const boost::system::error_code& error, std::size_t bytes_transferred, std::size_t length, const boost::function1& > ¬ify) { if(error || (bytes_transferred+received) < length) { - Common::Logger::logf(Common::Logger::VERBOSE, "Read error: %s", error.message().c_str()); + Core::Logger::logf(Core::Logger::VERBOSE, "Read error: %s", error.message().c_str()); // TODO Error doDisconnect(); @@ -185,7 +183,7 @@ void Connection::handleWrite(const boost::system::error_code& error, std::size_t } if(error) { - Common::Logger::logf(Common::Logger::VERBOSE, "Write error: %s", error.message().c_str()); + Core::Logger::logf(Core::Logger::VERBOSE, "Write error: %s", error.message().c_str()); // TODO Error doDisconnect(); diff --git a/src/Net/Connection.h b/src/Net/Connection.h index e1ca63b..6323164 100644 --- a/src/Net/Connection.h +++ b/src/Net/Connection.h @@ -23,7 +23,8 @@ #include #include "Packet.h" -#include "Signals/Signals.h" +#include +#include #include #include @@ -70,9 +71,9 @@ class Connection : boost::noncopyable { Packet::Data header; - Signals::Signal1 > receiveSignal; - Signals::Signal0 connectedSignal; - Signals::Signal0 disconnectedSignal; + Core::Signals::Signal1 > receiveSignal; + Core::Signals::Signal0 connectedSignal; + Core::Signals::Signal0 disconnectedSignal; bool receiving; unsigned long sending; @@ -91,8 +92,6 @@ class Connection : boost::noncopyable { void rawSend(const uint8_t *data, std::size_t length); protected: - static boost::asio::io_service ioService; - boost::shared_mutex connectionLock; boost::asio::ssl::stream socket; @@ -117,7 +116,7 @@ class Connection : boost::noncopyable { void doDisconnect(); Connection(boost::asio::ssl::context &sslContext) : - state(DISCONNECTED), receiveBuffer(1024*1024), socket(ioService, sslContext) {} + state(DISCONNECTED), receiveBuffer(1024*1024), socket(Core::ThreadManager::get()->getIOService(), sslContext) {} public: virtual ~Connection(); @@ -171,24 +170,24 @@ class Connection : boost::noncopyable { bool send(const Packet &packet); - Signals::Connection connectSignalReceive(const Signals::Signal1 >::slot_type &slot) { + Core::Signals::Connection connectSignalReceive(const Core::Signals::Signal1 >::slot_type &slot) { return receiveSignal.connect(slot); } - void disconnectSignalReceive(const Signals::Connection &connection) { + void disconnectSignalReceive(const Core::Signals::Connection &connection) { receiveSignal.disconnect(connection); } - Signals::Connection connectSignalConnected(const Signals::Signal0::slot_type &slot) { + Core::Signals::Connection connectSignalConnected(const Core::Signals::Signal0::slot_type &slot) { return connectedSignal.connect(slot); } - void disconnectSignalConnected(const Signals::Connection &connection) { + void disconnectSignalConnected(const Core::Signals::Connection &connection) { connectedSignal.disconnect(connection); } - Signals::Connection connectSignalDisconnected(const Signals::Signal0::slot_type &slot) { + Core::Signals::Connection connectSignalDisconnected(const Core::Signals::Signal0::slot_type &slot) { return disconnectedSignal.connect(slot); } - void disconnectSignalDisconnected(const Signals::Connection &connection) { + void disconnectSignalDisconnected(const Core::Signals::Connection &connection) { disconnectedSignal.disconnect(connection); } }; diff --git a/src/Net/Exception.cpp b/src/Net/Exception.cpp deleted file mode 100644 index e082948..0000000 --- a/src/Net/Exception.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Exception.cpp - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "Exception.h" - -#include - -namespace Mad { -namespace Net { - -std::string Exception::strerror() const { - std::string ret; - - if(!where.empty()) - ret = where + ": "; - - switch(errorCode) { - case SUCCESS: - return ret + "Success"; - case UNEXPECTED_PACKET: - return ret + "An unexpected packet was received"; - case INVALID_ACTION: - return ret + "The action is invalid"; - case NOT_AVAILABLE: - return ret + "Not available"; - case NOT_FINISHED: - return ret + "Not finished"; - case NOT_IMPLEMENTED: - return ret + "Not implemented"; - case INTERNAL_ERRNO: - return ret + std::strerror(subCode); - case INVALID_ADDRESS: - return ret + "Invalid address"; - case ALREADY_IDENTIFIED: - return ret + "The host is already identified"; - case UNKNOWN_DAEMON: - return ret + "The daemon is unknown"; - default: - return ret + "Unknown error"; - } -} - -} -} diff --git a/src/Net/Exception.h b/src/Net/Exception.h deleted file mode 100644 index 1ddf5f4..0000000 --- a/src/Net/Exception.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Exception.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_NET_EXCEPTION_H_ -#define MAD_NET_EXCEPTION_H_ - -#include - -namespace Mad { -namespace Net { - -class Exception { - public: - enum ErrorCode { - SUCCESS = 0x0000, UNEXPECTED_PACKET = 0x0001, INVALID_ACTION = 0x0002, NOT_AVAILABLE = 0x0003, NOT_FINISHED = 0x0004, NOT_IMPLEMENTED = 0x0005, - INTERNAL_ERRNO = 0x0010, - INVALID_ADDRESS = 0x0020, - ALREADY_IDENTIFIED = 0x0030, UNKNOWN_DAEMON = 0x0031 - }; - - private: - std::string where; - - ErrorCode errorCode; - long subCode; - long subSubCode; - - public: - Exception(const std::string &where0, ErrorCode errorCode0 = SUCCESS, long subCode0 = 0, long subSubCode0 = 0) - : where(where0), errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) {} - Exception(ErrorCode errorCode0 = SUCCESS, long subCode0 = 0, long subSubCode0 = 0) : errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) {} - virtual ~Exception() {} - - const std::string& getWhere() const {return where;} - ErrorCode getErrorCode() const {return errorCode;} - long getSubCode() const {return subCode;} - long getSubSubCode() const {return subSubCode;} - - std::string strerror() const; - - operator bool() const { - return (errorCode != SUCCESS); - } -}; - -} -} - -#endif /* MAD_NET_EXCEPTION_H_ */ diff --git a/src/Net/Listener.cpp b/src/Net/Listener.cpp index 6da2762..780e862 100644 --- a/src/Net/Listener.cpp +++ b/src/Net/Listener.cpp @@ -19,7 +19,8 @@ #include "Listener.h" -#include +#include +#include #include #include @@ -39,8 +40,8 @@ void Listener::handleAccept(const boost::system::error_code &error, boost::share con->_setState(Connection::CONNECT); - Signals::Connection con1 = con->connectSignalConnected(boost::bind(&Listener::handleConnect, this, con)); - Signals::Connection con2 = con->connectSignalDisconnected(boost::bind(&Listener::handleDisconnect, this, con)); + Core::Signals::Connection con1 = con->connectSignalConnected(boost::bind(&Listener::handleConnect, this, con)); + Core::Signals::Connection con2 = con->connectSignalDisconnected(boost::bind(&Listener::handleDisconnect, this, con)); connections.insert(std::make_pair(con, std::make_pair(con1, con2))); @@ -54,7 +55,7 @@ void Listener::handleAccept(const boost::system::error_code &error, boost::share void Listener::handleConnect(boost::shared_ptr con) { { boost::lock_guard lock(mutex); - std::map, std::pair >::iterator it = connections.find(con); + std::map, std::pair >::iterator it = connections.find(con); if(it == connections.end()) return; @@ -76,9 +77,9 @@ void Listener::handleDisconnect(boost::shared_ptr con) { Listener::Listener(const std::string &x905CertFile0, const std::string &x905KeyFile0, - const boost::asio::ip::tcp::endpoint &address0) throw(Exception) -: x905CertFile(x905CertFile0), x905KeyFile(x905KeyFile0), address(address0), acceptor(Connection::ioService, address), -sslContext(Connection::ioService, boost::asio::ssl::context::sslv23) + const boost::asio::ip::tcp::endpoint &address0) throw(Core::Exception) +: x905CertFile(x905CertFile0), x905KeyFile(x905KeyFile0), address(address0), acceptor(Core::ThreadManager::get()->getIOService(), address), +sslContext(Core::ThreadManager::get()->getIOService(), boost::asio::ssl::context::sslv23) { sslContext.set_options(boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2 @@ -93,9 +94,10 @@ sslContext(Connection::ioService, boost::asio::ssl::context::sslv23) } Listener::~Listener() { - for(std::map,std::pair >::iterator con = connections.begin(); con != connections.end(); ++con) { + for(std::map,std::pair >::iterator con = connections.begin(); con != connections.end(); ++con) { con->first->disconnect(); - // TODO wait... + con->first->waitWhileConnected(); + // TODO Optimize } } diff --git a/src/Net/Listener.h b/src/Net/Listener.h index 2e25051..af0888b 100644 --- a/src/Net/Listener.h +++ b/src/Net/Listener.h @@ -24,7 +24,7 @@ #include #include "Connection.h" -#include "Exception.h" +#include namespace Mad { namespace Net { @@ -38,9 +38,9 @@ class Listener : boost::noncopyable { boost::asio::ip::tcp::acceptor acceptor; boost::asio::ssl::context sslContext; - std::map, std::pair > connections; + std::map, std::pair > connections; - Signals::Signal1 > signal; + Core::Signals::Signal1 > signal; void handleAccept(const boost::system::error_code &error, boost::shared_ptr con); @@ -49,13 +49,13 @@ class Listener : boost::noncopyable { public: Listener(const std::string &x905CertFile0, const std::string &x905KeyFile0, - const boost::asio::ip::tcp::endpoint &address0 = boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 6666)) throw(Exception); + const boost::asio::ip::tcp::endpoint &address0 = boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 6666)) throw(Core::Exception); virtual ~Listener(); - Signals::Connection connectSignalNewConnection(const Signals::Signal1 >::slot_type &slot) { + Core::Signals::Connection connectSignalNewConnection(const Core::Signals::Signal1 >::slot_type &slot) { return signal.connect(slot); } - void disconnectSignalNewConnection(const Signals::Connection &connection) { + void disconnectSignalNewConnection(const Core::Signals::Connection &connection) { signal.disconnect(connection); } }; diff --git a/src/Net/Signals/Connection.h b/src/Net/Signals/Connection.h deleted file mode 100644 index 7730e13..0000000 --- a/src/Net/Signals/Connection.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Connection.h - * - * Copyright (C) 2009 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_NET_SIGNALS_CONNECTION_H_ -#define MAD_NET_SIGNALS_CONNECTION_H_ - -namespace Mad { -namespace Net { -namespace Signals { - -class SignalBase; - -class Connection { - private: - friend class SignalBase; - - SignalBase *signal; - unsigned long id; - - Connection(SignalBase *signal0, unsigned long id0) - : signal(signal0), id(id0) {} - - public: - bool operator==(const Connection &o) const { - return (signal == o.signal && id == o.id); - } - - bool operator<(const Connection &o) const { - return (signal != o.signal) ? (signal < o.signal) : (id < o.id); - } -}; - -} -} -} - -#endif /* MAD_NET_SIGNALS_CONNECTION_H_ */ diff --git a/src/Net/Signals/GenericSignal.h b/src/Net/Signals/GenericSignal.h deleted file mode 100644 index aa6cfa1..0000000 --- a/src/Net/Signals/GenericSignal.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * GenericSignal.h - * - * Copyright (C) 2009 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_NET_SIGNALS_GENERICSIGNAL_H_ -#define MAD_NET_SIGNALS_GENERICSIGNAL_H_ - -#include "SignalBase.h" - -#include -#include - -namespace Mad { -namespace Net { -namespace Signals { - -template -class GenericSignal : protected SignalBase { - public: - typedef FunctionType slot_type; - - protected: - std::map handlers; - - public: - Connection connect(const slot_type &slot) { - boost::lock_guard lock(mutex); - - Connection con = getNewConnection(); - handlers.insert(std::make_pair(con, slot)); - - return con; - } - - bool disconnect(const Connection &connection) { - boost::lock_guard lock(mutex); - - return handlers.erase(connection); - } -}; - -} -} -} - -#endif /* MAD_NET_SIGNALS_GENERICSIGNAL_H_ */ diff --git a/src/Net/Signals/Signal0.h b/src/Net/Signals/Signal0.h deleted file mode 100644 index 5c0a691..0000000 --- a/src/Net/Signals/Signal0.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Signal0.h - * - * Copyright (C) 2009 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_NET_SIGNALS_SIGNAL0_H_ -#define MAD_NET_SIGNALS_SIGNAL0_H_ - -#include "GenericSignal.h" -#include - -#include - -namespace Mad { -namespace Net { -namespace Signals { - -class Signal0 : public GenericSignal > { - public: - void emit() { - boost::lock_guard lock(mutex); - - for(std::map::iterator handler = handlers.begin(); handler != handlers.end(); ++handler) - Net::ThreadManager::get()->pushWork(handler->second); - } -}; - -} -} -} - -#endif /* MAD_NET_SIGNALS_SIGNAL0_H_ */ diff --git a/src/Net/Signals/Signal1.h b/src/Net/Signals/Signal1.h deleted file mode 100644 index 331c5cb..0000000 --- a/src/Net/Signals/Signal1.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Signal1.h - * - * Copyright (C) 2009 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_NET_SIGNALS_SIGNAL1_H_ -#define MAD_NET_SIGNALS_SIGNAL1_H_ - -#include "GenericSignal.h" -#include - -#include - -namespace Mad { -namespace Net { -namespace Signals { - -template -class Signal1 : public GenericSignal > { - public: - void emit(T1 arg1) { - boost::lock_guard lock(this->mutex); - - for(typename std::map >::slot_type>::iterator handler = this->handlers.begin(); handler != this->handlers.end(); ++handler) - Net::ThreadManager::get()->pushWork(boost::bind(handler->second, arg1)); - } -}; - -} -} -} - -#endif /* MAD_NET_SIGNALS_SIGNAL1_H_ */ diff --git a/src/Net/Signals/Signal2.h b/src/Net/Signals/Signal2.h deleted file mode 100644 index 3fb9315..0000000 --- a/src/Net/Signals/Signal2.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Signal2.h - * - * Copyright (C) 2009 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_NET_SIGNALS_SIGNAL2_H_ -#define MAD_NET_SIGNALS_SIGNAL2_H_ - -#include "GenericSignal.h" -#include - -#include - -namespace Mad { -namespace Net { -namespace Signals { - -template -class Signal2 : public GenericSignal > { - public: - void emit(T1 arg1, T2 arg2) { - boost::lock_guard lock(this->mutex); - - for(typename std::map >::slot_type>::iterator handler = this->handlers.begin(); handler != this->handlers.end(); ++handler) - Net::ThreadManager::get()->pushWork(boost::bind(handler->second, arg1, arg2)); - } -}; - -} -} -} - -#endif /* MAD_NET_SIGNALS_SIGNAL2_H_ */ diff --git a/src/Net/Signals/SignalBase.h b/src/Net/Signals/SignalBase.h deleted file mode 100644 index ea1b1e9..0000000 --- a/src/Net/Signals/SignalBase.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * SignalBase.h - * - * Copyright (C) 2009 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_SIGNALS_SIGNALBASE_H_ -#define MAD_SIGNALS_SIGNALBASE_H_ - -#include "Connection.h" - -#include -#include - -namespace Mad { -namespace Net { -namespace Signals { - -class SignalBase : private boost::noncopyable { - private: - friend class Connection; - - unsigned long connectionId; - - protected: - boost::mutex mutex; - - Connection getNewConnection() { - return Connection(this, connectionId++); - } - - SignalBase() : connectionId(0) {} - virtual ~SignalBase() {} -}; - -} -} -} - -#endif /* MAD_SIGNALS_SIGNALBASE_H_ */ diff --git a/src/Net/Signals/Signals.h b/src/Net/Signals/Signals.h deleted file mode 100644 index 4ee4f95..0000000 --- a/src/Net/Signals/Signals.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Signals.h - * - * Copyright (C) 2009 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_SIGNALS_SIGNALS_H_ -#define MAD_SIGNALS_SIGNALS_H_ - -#include "Signal0.h" -#include "Signal1.h" -#include "Signal2.h" - -#endif /* MAD_SIGNALS_SIGNALS_H_ */ diff --git a/src/Net/ThreadManager.cpp b/src/Net/ThreadManager.cpp deleted file mode 100644 index 377e6e1..0000000 --- a/src/Net/ThreadManager.cpp +++ /dev/null @@ -1,146 +0,0 @@ -/* - * ThreadManager.cpp - * - * Copyright (C) 2009 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "ThreadManager.h" -#include "Connection.h" - -#include -#include - -#include - -#include - -namespace Mad { -namespace Net { - -ThreadManager ThreadManager::threadManager; - - -void ThreadManager::workerFunc() { - while(true) { - boost::unique_lock lock(runLock); - - if(!running || !isThisWorkerThread()) - break; - - lock.unlock(); - - boost::unique_lock lock2(workLock); - while(work.empty()) { - workCond.wait(lock2); - - if(!running) - return; - } - - boost::function0 nextWork = work.front(); - work.pop(); - - lock2.unlock(); - - nextWork(); - } - - // And let the new worker thread join us... - pushWork(boost::bind(&ThreadManager::threadFinished, this, boost::this_thread::get_id())); -} - -void ThreadManager::detach() { - if(isThisMainThread()) { - Common::Logger::log(Common::Logger::CRITICAL, "Tried to detach main thread! This is just WRONG!"); - return; - } - - { - boost::lock_guard lock(runLock); - if(!running) - return; // There's no point in creating a new worker thread when we aren't running anymore - } - - threadLock.lock(); - - if(workerThread->get_id() == boost::this_thread::get_id()) {// Already detached? - threads.insert(std::make_pair(boost::this_thread::get_id(), workerThread)); - - workerThread.reset(new boost::thread(std::mem_fun(&ThreadManager::workerFunc), this)); - } - - threadLock.unlock(); -} - -void ThreadManager::pushWork(const boost::function0 &newWork) { - workLock.lock(); - work.push(newWork); - workLock.unlock(); - - workCond.notify_one(); -} - - -void ThreadManager::doInit() { - running = true; - - threadLock.lock(); - - ioWorker.reset(new boost::asio::io_service::work(Connection::ioService)); - - mainThreadId = boost::this_thread::get_id(); - workerThread.reset(new boost::thread(&ThreadManager::workerFunc, this)); - loggerThread.reset(new boost::thread(&Common::LogManager::loggerThread, Common::LogManager::get())); - ioThread.reset(new boost::thread((std::size_t(boost::asio::io_service::*)())&boost::asio::io_service::run, &Connection::ioService)); - - threadLock.unlock(); -} - -void ThreadManager::doDeinit() { - if(!isThisMainThread()) { - // TODO Critical error!!! - return; - } - - // First set running = false so the worker threads quit - boost::lock(runLock, workLock); - running = false; - - workLock.unlock(); - runLock.unlock(); - - workCond.notify_one(); - - // We don't have to lock threadLock as detach() won't change workerThread when running is false - workerThread->join(); - - // Now wait for all detached threads - while(!threads.empty()) { - threadFinished(threads.begin()->first); - } - - // IO thread is next - ioWorker.reset(); - Connection::ioService.stop(); - ioThread->join(); - - // Finally, the logger thread has to die - Common::LogManager::get()->stopLoggerThread(); - loggerThread->join(); -} - -} -} diff --git a/src/Net/ThreadManager.h b/src/Net/ThreadManager.h deleted file mode 100644 index b8c4bec..0000000 --- a/src/Net/ThreadManager.h +++ /dev/null @@ -1,107 +0,0 @@ -/* - * ThreadManager.h - * - * Copyright (C) 2009 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_NET_THREADMANAGER_H_ -#define MAD_NET_THREADMANAGER_H_ - -#include - -#include - -#include -#include - -#include - -#include -#include -#include - -namespace Mad { -namespace Net { - -class ThreadManager : public Common::Initializable { - private: - boost::thread::id mainThreadId; - boost::shared_ptr workerThread, loggerThread, ioThread; - std::map > threads; - - boost::mutex threadLock; - - boost::mutex runLock; - bool running; - - boost::mutex workLock; - boost::condition_variable workCond; - std::queue > work; - - boost::scoped_ptr ioWorker; - - static ThreadManager threadManager; - - ThreadManager() {} - - void workerFunc(); - void ioFunc(); - - void threadFinished(boost::thread::id threadId) { - boost::shared_ptr thread; - - { - boost::lock_guard lock(threadLock); - - std::map >::iterator it = threads.find(threadId); - - if(it == threads.end()) - return; - - thread = it->second; - threads.erase(it); - } - - thread->join(); - } - - protected: - virtual void doInit(); - virtual void doDeinit(); - - public: - bool isThisMainThread() { - return (mainThreadId == boost::this_thread::get_id()); - } - - bool isThisWorkerThread() { - boost::lock_guard lock(threadLock); - return (workerThread->get_id() == boost::this_thread::get_id()); - } - - void detach(); - - void pushWork(const boost::function0 &newWork); - - static ThreadManager* get() { - return &threadManager; - } -}; - -} -} - -#endif /* MAD_NET_THREADMANAGER_H_ */ diff --git a/src/Server/ConnectionManager.cpp b/src/Server/ConnectionManager.cpp index f568f74..a15a516 100644 --- a/src/Server/ConnectionManager.cpp +++ b/src/Server/ConnectionManager.cpp @@ -18,16 +18,13 @@ */ #include "ConnectionManager.h" -#include -#include -#include +#include +#include +#include #include #include #include "Requests/DaemonStateUpdateRequest.h" -//#include "RequestHandlers/DaemonCommandRequestHandler.h" -//#include "RequestHandlers/DaemonFSInfoRequestHandler.h" #include "RequestHandlers/DaemonListRequestHandler.h" -//#include "RequestHandlers/DaemonStatusRequestHandler.h" //#include "RequestHandlers/GSSAPIAuthRequestHandler.h" #include "RequestHandlers/IdentifyRequestHandler.h" #include "RequestHandlers/LogRequestHandler.h" @@ -89,7 +86,7 @@ void ConnectionManager::updateState(Common::HostInfo *hostInfo, Common::HostInfo } } -bool ConnectionManager::handleConfigEntry(const Common::ConfigEntry &entry, bool handled) { +bool ConnectionManager::handleConfigEntry(const Core::ConfigEntry &entry, bool handled) { if(handled) return false; @@ -97,7 +94,7 @@ bool ConnectionManager::handleConfigEntry(const Common::ConfigEntry &entry, bool try { listenerAddresses.push_back(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(entry[0][0]), 6666)); } - catch(Net::Exception &e) { + catch(Core::Exception &e) { // TODO Log error } @@ -148,7 +145,7 @@ void ConnectionManager::configFinished() { listener->connectSignalNewConnection(boost::bind(&ConnectionManager::handleNewConnection, this, _1)); listeners.push_back(listener); } - catch(Net::Exception &e) { + catch(Core::Exception &e) { // TODO Log error } } @@ -159,7 +156,7 @@ void ConnectionManager::configFinished() { listener->connectSignalNewConnection(boost::bind(&ConnectionManager::handleNewConnection, this, _1)); listeners.push_back(listener); } - catch(Net::Exception &e) { + catch(Core::Exception &e) { // TODO Log error } } @@ -220,14 +217,14 @@ void ConnectionManager::doDeinit() { Common::RequestManager::get()->unregisterPacketType("Log"); } -boost::shared_ptr ConnectionManager::getDaemonConnection(const std::string &name) const throw (Net::Exception&) { +boost::shared_ptr ConnectionManager::getDaemonConnection(const std::string &name) const throw (Core::Exception&) { const Common::HostInfo *hostInfo; try { hostInfo = &daemonInfo.at(name); } catch(std::out_of_range&) { - throw Net::Exception(Net::Exception::UNKNOWN_DAEMON); + throw Core::Exception(Core::Exception::UNKNOWN_DAEMON); } if(hostInfo->getState() != Common::HostInfo::INACTIVE) { @@ -238,56 +235,56 @@ boost::shared_ptr ConnectionManager::getDaemonConnection(con } } - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); } -std::string ConnectionManager::getDaemonName(const Common::Connection *con) const throw (Net::Exception&) { +std::string ConnectionManager::getDaemonName(const Common::Connection *con) const throw (Core::Exception&) { const ServerConnection *connection = dynamic_cast(con); if(connection && connection->getConnectionType() == ServerConnection::DAEMON) return connection->getHostInfo()->getName(); - throw Net::Exception(Net::Exception::UNKNOWN_DAEMON); + throw Core::Exception(Core::Exception::UNKNOWN_DAEMON); } -void ConnectionManager::identifyDaemonConnection(Common::Connection *con, const std::string &name) throw (Net::Exception&) { +void ConnectionManager::identifyDaemonConnection(Common::Connection *con, const std::string &name) throw (Core::Exception&) { // TODO Logging ServerConnection *connection = dynamic_cast(con); if(!connection) - throw Net::Exception(Net::Exception::INVALID_ACTION); + throw Core::Exception(Core::Exception::INVALID_ACTION); if(connection->isIdentified()) - throw Net::Exception(Net::Exception::ALREADY_IDENTIFIED); + throw Core::Exception(Core::Exception::ALREADY_IDENTIFIED); if(daemonInfo.count(name) == 0) - throw Net::Exception(Net::Exception::UNKNOWN_DAEMON); + throw Core::Exception(Core::Exception::UNKNOWN_DAEMON); Common::HostInfo *hostInfo = &daemonInfo[name]; if(hostInfo->getState() != Common::HostInfo::INACTIVE) { try { getDaemonConnection(name)->disconnect(); - Common::Logger::log(Common::Logger::WARNING, "Disconnecting old connection."); + Core::Logger::log(Core::Logger::WARNING, "Disconnecting old connection."); } - catch(Net::Exception&) {} + catch(Core::Exception&) {} } connection->identify(hostInfo); updateState(hostInfo, Common::HostInfo::RUNNING); - Common::Logger::logf("Identified as '%s'.", name.c_str()); + Core::Logger::logf("Identified as '%s'.", name.c_str()); } -void ConnectionManager::identifyClientConnection(Common::Connection *con) throw (Net::Exception&) { +void ConnectionManager::identifyClientConnection(Common::Connection *con) throw (Core::Exception&) { ServerConnection *connection = dynamic_cast(con); if(!connection) - throw Net::Exception(Net::Exception::INVALID_ACTION); + throw Core::Exception(Core::Exception::INVALID_ACTION); if(connection->isIdentified()) - throw Net::Exception(Net::Exception::ALREADY_IDENTIFIED); + throw Core::Exception(Core::Exception::ALREADY_IDENTIFIED); connection->identify(); } diff --git a/src/Server/ConnectionManager.h b/src/Server/ConnectionManager.h index 6573883..4cc12a5 100644 --- a/src/Server/ConnectionManager.h +++ b/src/Server/ConnectionManager.h @@ -20,9 +20,9 @@ #ifndef MAD_SERVER_CONNECTIONMANAGER_H_ #define MAD_SERVER_CONNECTIONMANAGER_H_ -#include +#include +#include #include -#include #include #include @@ -42,7 +42,7 @@ class Packet; namespace Server { -class ConnectionManager : public Common::Configurable, public Common::Initializable, boost::noncopyable { +class ConnectionManager : public Core::Configurable, public Core::Initializable, boost::noncopyable { private: class ServerConnection : public Common::Connection { public: @@ -111,7 +111,7 @@ class ConnectionManager : public Common::Configurable, public Common::Initializa void handleDisconnect(boost::shared_ptr con); protected: - virtual bool handleConfigEntry(const Common::ConfigEntry &entry, bool handled); + virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool handled); virtual void configFinished(); virtual void doInit(); @@ -122,11 +122,11 @@ class ConnectionManager : public Common::Configurable, public Common::Initializa return &connectionManager; } - boost::shared_ptr getDaemonConnection(const std::string &name) const throw (Net::Exception&); - std::string getDaemonName(const Common::Connection *con) const throw (Net::Exception&); + boost::shared_ptr getDaemonConnection(const std::string &name) const throw (Core::Exception&); + std::string getDaemonName(const Common::Connection *con) const throw (Core::Exception&); - void identifyDaemonConnection(Common::Connection *con, const std::string &name) throw (Net::Exception&); - void identifyClientConnection(Common::Connection *con) throw (Net::Exception&); + void identifyDaemonConnection(Common::Connection *con, const std::string &name) throw (Core::Exception&); + void identifyClientConnection(Common::Connection *con) throw (Core::Exception&); std::vector getDaemonList() const; }; diff --git a/src/Server/RequestHandlers/DaemonListRequestHandler.cpp b/src/Server/RequestHandlers/DaemonListRequestHandler.cpp index 6c17ce1..fc6900c 100644 --- a/src/Server/RequestHandlers/DaemonListRequestHandler.cpp +++ b/src/Server/RequestHandlers/DaemonListRequestHandler.cpp @@ -19,7 +19,6 @@ #include "DaemonListRequestHandler.h" #include "../ConnectionManager.h" -#include namespace Mad { namespace Server { diff --git a/src/Server/RequestHandlers/DaemonRequestHandlerGroup.cpp b/src/Server/RequestHandlers/DaemonRequestHandlerGroup.cpp index 4ff8c40..0e21fdd 100644 --- a/src/Server/RequestHandlers/DaemonRequestHandlerGroup.cpp +++ b/src/Server/RequestHandlers/DaemonRequestHandlerGroup.cpp @@ -21,7 +21,7 @@ #include "../ConnectionManager.h" #include "../Requests/CommandRequest.h" -#include +#include #include #include @@ -31,11 +31,11 @@ namespace RequestHandlers { void DaemonRequestHandlerGroup::DaemonRequestHandler::handlePacket(boost::shared_ptr packet) { if(packet->getType() != type) { - Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); + Core::Logger::log(Core::Logger::ERROR, "Received an unexpected packet."); Common::XmlPacket ret; ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); + ret.add("ErrorCode", Core::Exception::UNEXPECTED_PACKET); sendPacket(ret); @@ -59,7 +59,7 @@ void DaemonRequestHandlerGroup::DaemonRequestHandler::handlePacket(boost::shared request->connectSignalFinished(boost::bind(&DaemonRequestHandlerGroup::DaemonRequestHandler::requestFinished, this, _1, _2)); Common::RequestManager::get()->sendRequest(daemonCon.get(), request); } - catch(Net::Exception &e) { + catch(Core::Exception &e) { Common::XmlPacket ret; ret.setType("Error"); ret.add("ErrorCode", e.getErrorCode()); @@ -72,7 +72,7 @@ void DaemonRequestHandlerGroup::DaemonRequestHandler::handlePacket(boost::shared } } -void DaemonRequestHandlerGroup::DaemonRequestHandler::requestFinished(boost::shared_ptr packet, Net::Exception error) { +void DaemonRequestHandlerGroup::DaemonRequestHandler::requestFinished(boost::shared_ptr packet, Core::Exception error) { if(error) { Common::XmlPacket ret; ret.setType("Error"); diff --git a/src/Server/RequestHandlers/DaemonRequestHandlerGroup.h b/src/Server/RequestHandlers/DaemonRequestHandlerGroup.h index 7c0b127..7a709f4 100644 --- a/src/Server/RequestHandlers/DaemonRequestHandlerGroup.h +++ b/src/Server/RequestHandlers/DaemonRequestHandlerGroup.h @@ -21,7 +21,7 @@ #define MAD_SERVER_REQUESTHANDLERS_DAEMONREQUESTHANDLERGROUP_H_ #include -#include +#include namespace Mad { namespace Server { @@ -33,7 +33,7 @@ class DaemonRequestHandlerGroup : public Common::RequestHandlerGroup { private: std::string type; - void requestFinished(boost::shared_ptr packet, Net::Exception error); + void requestFinished(boost::shared_ptr packet, Core::Exception error); protected: virtual void handlePacket(boost::shared_ptr packet); diff --git a/src/Server/RequestHandlers/IdentifyRequestHandler.cpp b/src/Server/RequestHandlers/IdentifyRequestHandler.cpp index abee878..e48563b 100644 --- a/src/Server/RequestHandlers/IdentifyRequestHandler.cpp +++ b/src/Server/RequestHandlers/IdentifyRequestHandler.cpp @@ -19,7 +19,6 @@ #include "IdentifyRequestHandler.h" #include "../ConnectionManager.h" -#include namespace Mad { diff --git a/src/Server/RequestHandlers/LogRequestHandler.cpp b/src/Server/RequestHandlers/LogRequestHandler.cpp index 1e3f5b4..f33aad4 100644 --- a/src/Server/RequestHandlers/LogRequestHandler.cpp +++ b/src/Server/RequestHandlers/LogRequestHandler.cpp @@ -18,8 +18,7 @@ */ #include "LogRequestHandler.h" -#include -#include +#include #include "../ConnectionManager.h" namespace Mad { @@ -30,11 +29,11 @@ void LogRequestHandler::handleRequest(boost::shared_ptr // TODO Require authentication try { - Common::LogManager::get()->log((*packet)["category"], (*packet)["level"], (*packet)["timestamp"], (*packet)["message"], + Core::LogManager::get()->log((*packet)["category"], (*packet)["level"], (*packet)["timestamp"], (*packet)["message"], ConnectionManager::get()->getDaemonName(getConnection())); } - catch(Net::Exception &e) { - Common::Logger::logf(Common::Logger::ERROR, "Can't determine daemon name: %s", e.strerror().c_str()); + catch(Core::Exception &e) { + Core::Logger::logf(Core::Logger::ERROR, "Can't determine daemon name: %s", e.strerror().c_str()); } ret->setType("OK"); diff --git a/src/Server/RequestHandlers/UserRequestHandlerGroup.cpp b/src/Server/RequestHandlers/UserRequestHandlerGroup.cpp index 1ff8883..7836c34 100644 --- a/src/Server/RequestHandlers/UserRequestHandlerGroup.cpp +++ b/src/Server/RequestHandlers/UserRequestHandlerGroup.cpp @@ -18,8 +18,6 @@ */ #include "UserRequestHandlerGroup.h" - -#include #include "../UserManager.h" namespace Mad { diff --git a/src/Server/UserBackend.h b/src/Server/UserBackend.h index c2f340e..4688bc7 100644 --- a/src/Server/UserBackend.h +++ b/src/Server/UserBackend.h @@ -22,11 +22,11 @@ #include +#include + #include #include -#include - #include #include #include @@ -45,45 +45,45 @@ class UserBackend { UserBackend() {} - virtual boost::shared_ptr > getUserList() throw(Net::Exception) { - throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); + virtual boost::shared_ptr > getUserList() throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } - virtual boost::shared_ptr getUserInfo(unsigned long uid _UNUSED_PARAMETER_) throw(Net::Exception) { - throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); + virtual boost::shared_ptr getUserInfo(unsigned long uid _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } - virtual boost::shared_ptr getUserInfoByName(const std::string &name _UNUSED_PARAMETER_) throw(Net::Exception) { - throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); + virtual boost::shared_ptr getUserInfoByName(const std::string &name _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } - virtual boost::shared_ptr > getUserGroupList(unsigned long uid _UNUSED_PARAMETER_) throw(Net::Exception) { - throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); + virtual boost::shared_ptr > getUserGroupList(unsigned long uid _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } - virtual boost::shared_ptr > getGroupList() throw(Net::Exception) { - throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); + virtual boost::shared_ptr > getGroupList() throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } - virtual std::string getGroupName(unsigned long gid _UNUSED_PARAMETER_) throw(Net::Exception) { - throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); + virtual std::string getGroupName(unsigned long gid _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } - virtual unsigned long getGroupId(const std::string &name _UNUSED_PARAMETER_) throw(Net::Exception) { - throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); + virtual unsigned long getGroupId(const std::string &name _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } - virtual boost::shared_ptr > getGroupUserList(unsigned long gid _UNUSED_PARAMETER_) throw(Net::Exception) { - throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); + virtual boost::shared_ptr > getGroupUserList(unsigned long gid _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } - virtual void setPassword(unsigned long uid _UNUSED_PARAMETER_, const std::string &password _UNUSED_PARAMETER_) throw(Net::Exception) { - throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); + virtual void setPassword(unsigned long uid _UNUSED_PARAMETER_, const std::string &password _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } - virtual void addUser(const Common::UserInfo &userInfo _UNUSED_PARAMETER_) throw(Net::Exception) { - throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); + virtual void addUser(const Common::UserInfo &userInfo _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } virtual int getPriority() const { diff --git a/src/Server/UserManager.cpp b/src/Server/UserManager.cpp index 5f30a6b..0c45b78 100644 --- a/src/Server/UserManager.cpp +++ b/src/Server/UserManager.cpp @@ -34,15 +34,15 @@ bool UserManager::Compare::operator() (boost::shared_ptr b1, boost: } -boost::shared_ptr > UserManager::getUserList() throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +boost::shared_ptr > UserManager::getUserList() throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->getUserList(); } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } @@ -50,15 +50,15 @@ boost::shared_ptr > UserManager::getUs throw e; } -boost::shared_ptr UserManager::getUserInfo(unsigned long uid) throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +boost::shared_ptr UserManager::getUserInfo(unsigned long uid) throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->getUserInfo(uid); } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } @@ -66,15 +66,15 @@ boost::shared_ptr UserManager::getUserInfo(unsigned long uid) throw e; } -boost::shared_ptr UserManager::getUserInfoByName(const std::string &name) throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +boost::shared_ptr UserManager::getUserInfoByName(const std::string &name) throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->getUserInfoByName(name); } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } @@ -82,15 +82,15 @@ boost::shared_ptr UserManager::getUserInfoByName(const std::st throw e; } -boost::shared_ptr > UserManager::getUserGroupList(unsigned long uid) throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +boost::shared_ptr > UserManager::getUserGroupList(unsigned long uid) throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->getUserGroupList(uid); } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } @@ -98,15 +98,15 @@ boost::shared_ptr > UserManager::getUserGroupList(unsign throw e; } -boost::shared_ptr > UserManager::getGroupList() throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +boost::shared_ptr > UserManager::getGroupList() throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->getGroupList(); } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } @@ -114,15 +114,15 @@ boost::shared_ptr > UserManager::getG throw e; } -std::string UserManager::getGroupName(unsigned long gid) throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +std::string UserManager::getGroupName(unsigned long gid) throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->getGroupName(gid); } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } @@ -130,15 +130,15 @@ std::string UserManager::getGroupName(unsigned long gid) throw(Net::Exception) { throw e; } -unsigned long UserManager::getGroupId(const std::string &name) throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +unsigned long UserManager::getGroupId(const std::string &name) throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->getGroupId(name); } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } @@ -146,15 +146,15 @@ unsigned long UserManager::getGroupId(const std::string &name) throw(Net::Except throw e; } -boost::shared_ptr > UserManager::getGroupUserList(unsigned long gid) throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +boost::shared_ptr > UserManager::getGroupUserList(unsigned long gid) throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->getGroupUserList(gid); } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } @@ -162,15 +162,15 @@ boost::shared_ptr > UserManager::getGroupUserList(unsign throw e; } -void UserManager::setPassword(unsigned long uid, const std::string &password) throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +void UserManager::setPassword(unsigned long uid, const std::string &password) throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->setPassword(uid, password); } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } @@ -178,15 +178,15 @@ void UserManager::setPassword(unsigned long uid, const std::string &password) th throw e; } -void UserManager::addUser(const Common::UserInfo &userInfo) throw(Net::Exception) { - Net::Exception e(Net::Exception::NOT_IMPLEMENTED); +void UserManager::addUser(const Common::UserInfo &userInfo) throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->addUser(userInfo); } - catch(Net::Exception e2) { - if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } diff --git a/src/Server/UserManager.h b/src/Server/UserManager.h index 47c7318..31f50d5 100644 --- a/src/Server/UserManager.h +++ b/src/Server/UserManager.h @@ -23,7 +23,7 @@ #include #include -#include +#include #include #include @@ -57,19 +57,19 @@ class UserManager : boost::noncopyable { backends.erase(backend); } - boost::shared_ptr > getUserList() throw(Net::Exception); - boost::shared_ptr getUserInfo(unsigned long uid) throw(Net::Exception); - boost::shared_ptr getUserInfoByName(const std::string &name) throw(Net::Exception); - boost::shared_ptr > getUserGroupList(unsigned long uid) throw(Net::Exception); + boost::shared_ptr > getUserList() throw(Core::Exception); + boost::shared_ptr getUserInfo(unsigned long uid) throw(Core::Exception); + boost::shared_ptr getUserInfoByName(const std::string &name) throw(Core::Exception); + boost::shared_ptr > getUserGroupList(unsigned long uid) throw(Core::Exception); - boost::shared_ptr > getGroupList() throw(Net::Exception); - std::string getGroupName(unsigned long gid) throw(Net::Exception); - unsigned long getGroupId(const std::string &name) throw(Net::Exception); - boost::shared_ptr > getGroupUserList(unsigned long gid) throw(Net::Exception); + boost::shared_ptr > getGroupList() throw(Core::Exception); + std::string getGroupName(unsigned long gid) throw(Core::Exception); + unsigned long getGroupId(const std::string &name) throw(Core::Exception); + boost::shared_ptr > getGroupUserList(unsigned long gid) throw(Core::Exception); - void setPassword(unsigned long uid, const std::string &password) throw(Net::Exception); + void setPassword(unsigned long uid, const std::string &password) throw(Core::Exception); - void addUser(const Common::UserInfo &userInfo) throw(Net::Exception); + void addUser(const Common::UserInfo &userInfo) throw(Core::Exception); static UserManager *get() { return &userManager; diff --git a/src/mad-server.cpp b/src/mad-server.cpp index d55c049..a00f33d 100644 --- a/src/mad-server.cpp +++ b/src/mad-server.cpp @@ -17,16 +17,16 @@ * with this program. If not, see . */ -#include "Common/ConfigManager.h" +#include "Core/ConfigManager.h" +#include "Core/ThreadManager.h" #include "Common/ModuleManager.h" -#include "Net/ThreadManager.h" #include "Server/ConnectionManager.h" using namespace Mad; int main() { - Net::ThreadManager::get()->init(); + Core::ThreadManager::get()->init(); Server::ConnectionManager::get()->init(); Common::ModuleManager::get()->loadModule("FileLogger"); @@ -35,13 +35,13 @@ int main() { Common::ModuleManager::get()->loadModule("UserBackendMysql"); - Common::ConfigManager::get()->loadFile("mad-server.conf"); - Common::ConfigManager::get()->finish(); + Core::ConfigManager::get()->loadFile("mad-server.conf"); + Core::ConfigManager::get()->finish(); while(true) sleep(1000); - Common::Initializable::deinit(); + Core::Initializable::deinit(); return 0; } diff --git a/src/mad.cpp b/src/mad.cpp index 42afc87..107f240 100644 --- a/src/mad.cpp +++ b/src/mad.cpp @@ -17,10 +17,9 @@ * with this program. If not, see . */ -#include "Net/ThreadManager.h" -#include "Common/ConfigManager.h" -#include "Common/LogManager.h" -#include "Common/Logger.h" +#include "Core/ThreadManager.h" +#include "Core/ConfigManager.h" +#include "Core/LogManager.h" #include "Common/ModuleManager.h" #include "Common/RequestManager.h" #include "Common/ClientConnection.h" @@ -36,13 +35,13 @@ using namespace Mad; int main() { - Net::ThreadManager::get()->init(); + Core::ThreadManager::get()->init(); Common::ModuleManager::get()->loadModule("FileLogger"); Common::ModuleManager::get()->loadModule("SystemBackendPosix"); Common::ModuleManager::get()->loadModule("SystemBackendProc"); - Common::ConfigManager::get()->finish(); + Core::ConfigManager::get()->finish(); Common::RequestManager::get()->registerPacketType("Command"); Common::RequestManager::get()->registerPacketType("FSInfo"); @@ -58,8 +57,8 @@ int main() { Common::RequestManager::get()->registerConnection(connection); - Common::Logger *networkLogger = new Daemon::Backends::NetworkLogger(connection); - Common::LogManager::get()->registerLogger(networkLogger); + Core::Logger *networkLogger = new Daemon::Backends::NetworkLogger(connection); + Core::LogManager::get()->registerLogger(networkLogger); //char hostname[256]; @@ -71,16 +70,16 @@ int main() { Common::RequestManager::get()->sendRequest(connection, request); request->wait(); } - Common::Logger::log("Identified."); + Core::Logger::log("Identified."); connection->waitWhileConnected(); - Common::LogManager::get()->unregisterLogger(networkLogger); + Core::LogManager::get()->unregisterLogger(networkLogger); Common::RequestManager::get()->unregisterConnection(connection); } - catch(Net::Exception &e) { - Common::Logger::logf(Common::Logger::CRITICAL, "Connection error: %s", e.strerror().c_str()); + catch(Core::Exception &e) { + Core::Logger::logf(Core::Logger::CRITICAL, "Connection error: %s", e.strerror().c_str()); } delete connection; @@ -89,7 +88,7 @@ int main() { Common::RequestManager::get()->unregisterPacketType("FSInfo"); Common::RequestManager::get()->unregisterPacketType("GetStatus"); - Common::Initializable::deinit(); + Core::Initializable::deinit(); return 0; } diff --git a/src/madc.cpp b/src/madc.cpp index 64078c8..ea32f2f 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -17,12 +17,13 @@ * with this program. If not, see . */ -#include "Net/ThreadManager.h" +#include "Core/ConfigManager.h" +#include "Core/Logger.h" +#include "Core/ThreadManager.h" + #include "Common/ClientConnection.h" -#include "Common/ConfigManager.h" -#include "Common/Requests/IdentifyRequest.h" -#include "Common/Logger.h" #include "Common/RequestManager.h" +#include "Common/Requests/IdentifyRequest.h" #include "Client/CommandParser.h" #include "Client/InformationManager.h" @@ -47,10 +48,10 @@ int main(int argc, char *argv[]) { std::exit(1); } - Net::ThreadManager::get()->init(); + Core::ThreadManager::get()->init(); Client::InformationManager::get()->init(); - Common::ConfigManager::get()->finish(); + Core::ConfigManager::get()->finish(); Common::ClientConnection *connection = new Common::ClientConnection; @@ -96,13 +97,13 @@ int main(int argc, char *argv[]) { Common::RequestManager::get()->unregisterConnection(connection); } - catch(Net::Exception &e) { - Common::Logger::logf(Common::Logger::CRITICAL, "Connection error: %s", e.strerror().c_str()); + catch(Core::Exception &e) { + Core::Logger::logf(Core::Logger::CRITICAL, "Connection error: %s", e.strerror().c_str()); } delete connection; - Common::Initializable::deinit(); + Core::Initializable::deinit(); return 0; } diff --git a/src/modules/FileLogger/FileLogger.cpp b/src/modules/FileLogger/FileLogger.cpp index 4655002..a78ab36 100644 --- a/src/modules/FileLogger/FileLogger.cpp +++ b/src/modules/FileLogger/FileLogger.cpp @@ -19,7 +19,7 @@ #include "FileLogger.h" -#include +#include namespace Mad { @@ -29,7 +29,7 @@ FileLogger::ConfigHelper FileLogger::configHelper; std::set FileLogger::loggers; -bool FileLogger::ConfigHelper::handleConfigEntry(const Common::ConfigEntry &entry, bool handled) { +bool FileLogger::ConfigHelper::handleConfigEntry(const Core::ConfigEntry &entry, bool handled) { if(handled) return false; @@ -40,7 +40,7 @@ bool FileLogger::ConfigHelper::handleConfigEntry(const Common::ConfigEntry &entr FileLogger *logger = new FileLogger(entry[0][1]); loggers.insert(logger); - Common::LogManager::get()->registerLogger(static_cast(logger)); + Core::LogManager::get()->registerLogger(static_cast(logger)); } else { Logger::logf(Logger::WARNING, "FileLogger: no filename given."); diff --git a/src/modules/FileLogger/FileLogger.h b/src/modules/FileLogger/FileLogger.h index 2def986..04e2126 100644 --- a/src/modules/FileLogger/FileLogger.h +++ b/src/modules/FileLogger/FileLogger.h @@ -20,20 +20,20 @@ #ifndef MAD_MODULES_FILELOGGER_H_ #define MAD_MODULES_FILELOGGER_H_ -#include -#include -#include +#include +#include +#include #include namespace Mad { namespace Modules { -class FileLogger : private Common::Logger, private Common::RemoteLogger { +class FileLogger : private Core::Logger, private Core::RemoteLogger { private: - class ConfigHelper : public Common::Configurable { + class ConfigHelper : public Core::Configurable { protected: - virtual bool handleConfigEntry(const Common::ConfigEntry &entry, bool handled); + virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool handled); public: virtual int getPriority() const {return 1;} @@ -57,11 +57,11 @@ class FileLogger : private Common::Logger, private Common::RemoteLogger { : file(filename.c_str(), std::ios::out|std::ios::app) {} static void registerConfigHelper() { - Common::ConfigManager::get()->registerConfigurable(&configHelper); + Core::ConfigManager::get()->registerConfigurable(&configHelper); } static void unregisterConfigHelper() { - Common::ConfigManager::get()->unregisterConfigurable(&configHelper); + Core::ConfigManager::get()->unregisterConfigurable(&configHelper); for(std::set::iterator logger = loggers.begin(); logger != loggers.end(); ++logger) { delete *logger; diff --git a/src/modules/SystemBackendPosix/SystemBackendPosix.cpp b/src/modules/SystemBackendPosix/SystemBackendPosix.cpp index 35e6c4f..4eb5807 100644 --- a/src/modules/SystemBackendPosix/SystemBackendPosix.cpp +++ b/src/modules/SystemBackendPosix/SystemBackendPosix.cpp @@ -19,7 +19,7 @@ #include "SystemBackendPosix.h" -#include +#include #include #include @@ -29,12 +29,12 @@ namespace Modules { boost::shared_ptr SystemBackendPosix::backend; -void SystemBackendPosix::getFSInfo(std::vector *fsInfo) throw(Net::Exception) { - Net::ThreadManager::get()->detach(); +void SystemBackendPosix::getFSInfo(std::vector *fsInfo) throw(Core::Exception) { + Core::ThreadManager::get()->detach(); FILE *pipe = popen("/bin/df -P -k", "r"); if(!pipe) - throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); + throw(Core::Exception(Core::Exception::NOT_AVAILABLE)); char buffer[1024]; std::string output; @@ -78,18 +78,18 @@ void SystemBackendPosix::getFSInfo(std::vector *f return; } -void SystemBackendPosix::shutdown() throw(Net::Exception) { - Net::ThreadManager::get()->detach(); +void SystemBackendPosix::shutdown() throw(Core::Exception) { + Core::ThreadManager::get()->detach(); if(system("/sbin/halt") != 0) - throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); + throw(Core::Exception(Core::Exception::NOT_AVAILABLE)); } -void SystemBackendPosix::reboot() throw(Net::Exception) { - Net::ThreadManager::get()->detach(); +void SystemBackendPosix::reboot() throw(Core::Exception) { + Core::ThreadManager::get()->detach(); if(system("/sbin/reboot") != 0) - throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); + throw(Core::Exception(Core::Exception::NOT_AVAILABLE)); } } diff --git a/src/modules/SystemBackendPosix/SystemBackendPosix.h b/src/modules/SystemBackendPosix/SystemBackendPosix.h index e462b43..4aec7dd 100644 --- a/src/modules/SystemBackendPosix/SystemBackendPosix.h +++ b/src/modules/SystemBackendPosix/SystemBackendPosix.h @@ -38,10 +38,10 @@ class SystemBackendPosix : public Common::SystemBackend { static boost::shared_ptr backend; protected: - virtual void getFSInfo(std::vector *fsInfo) throw(Net::Exception); + virtual void getFSInfo(std::vector *fsInfo) throw(Core::Exception); - virtual void shutdown() throw(Net::Exception); - virtual void reboot() throw(Net::Exception); + virtual void shutdown() throw(Core::Exception); + virtual void reboot() throw(Core::Exception); public: static void registerBackend() { diff --git a/src/modules/SystemBackendProc/SystemBackendProc.cpp b/src/modules/SystemBackendProc/SystemBackendProc.cpp index ab074b2..e8b45cd 100644 --- a/src/modules/SystemBackendProc/SystemBackendProc.cpp +++ b/src/modules/SystemBackendProc/SystemBackendProc.cpp @@ -19,7 +19,7 @@ #include "SystemBackendProc.h" -#include +#include #include #include @@ -29,18 +29,18 @@ namespace Modules { boost::shared_ptr SystemBackendProc::backend; -void SystemBackendProc::getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) throw(Net::Exception) { - Net::ThreadManager::get()->detach(); +void SystemBackendProc::getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) throw(Core::Exception) { + Core::ThreadManager::get()->detach(); uptimeFile.seekg(0, std::ios::beg); if(!uptimeFile.good()) - throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); + throw(Core::Exception(Core::Exception::NOT_AVAILABLE)); float f; uptimeFile >> f; if(!uptimeFile.good()) - throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); + throw(Core::Exception(Core::Exception::NOT_AVAILABLE)); if(uptime) *uptime = (unsigned long)f; @@ -50,8 +50,8 @@ void SystemBackendProc::getUptimeInfo(unsigned long *uptime, unsigned long *idle *idleTime = (unsigned long)f; } -void SystemBackendProc::getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) throw(Net::Exception) { - Net::ThreadManager::get()->detach(); +void SystemBackendProc::getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) throw(Core::Exception) { + Core::ThreadManager::get()->detach(); if(totalMem) *totalMem = 0; @@ -65,7 +65,7 @@ void SystemBackendProc::getMemoryInfo(unsigned long *totalMem, unsigned long *fr meminfoFile.seekg(0, std::ios::beg); if(!meminfoFile.good()) - throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); + throw(Core::Exception(Core::Exception::NOT_AVAILABLE)); while(!meminfoFile.eof() && meminfoFile.good()) { std::string line; @@ -92,8 +92,8 @@ void SystemBackendProc::getMemoryInfo(unsigned long *totalMem, unsigned long *fr } } -void SystemBackendProc::getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) throw(Net::Exception) { - Net::ThreadManager::get()->detach(); +void SystemBackendProc::getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) throw(Core::Exception) { + Core::ThreadManager::get()->detach(); unsigned long currentLoadValue = 0, nProcessesValue = 0; float loadAvg1Value = 0, loadAvg5Value = 0, loadAvg15Value = 0; @@ -101,13 +101,13 @@ void SystemBackendProc::getLoadInfo(unsigned long *currentLoad, unsigned long *n loadFile.seekg(0, std::ios::beg); if(!loadFile.good()) - throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); + throw(Core::Exception(Core::Exception::NOT_AVAILABLE)); std::string line; std::getline(loadFile, line); if(line.empty()) - throw(Net::Exception(Net::Exception::NOT_AVAILABLE)); + throw(Core::Exception(Core::Exception::NOT_AVAILABLE)); std::sscanf(line.c_str(), "%f %f %f %lu/%lu", &loadAvg1Value, &loadAvg5Value, &loadAvg15Value, ¤tLoadValue, &nProcessesValue); diff --git a/src/modules/SystemBackendProc/SystemBackendProc.h b/src/modules/SystemBackendProc/SystemBackendProc.h index eb2f8a6..9a0cb71 100644 --- a/src/modules/SystemBackendProc/SystemBackendProc.h +++ b/src/modules/SystemBackendProc/SystemBackendProc.h @@ -39,9 +39,9 @@ class SystemBackendProc : public Common::SystemBackend, boost::noncopyable { SystemBackendProc() : uptimeFile("/proc/uptime"), meminfoFile("/proc/meminfo"), loadFile("/proc/loadavg") {} protected: - virtual void getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) throw(Net::Exception); - virtual void getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) throw(Net::Exception); - virtual void getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) throw(Net::Exception); + virtual void getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) throw(Core::Exception); + virtual void getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) throw(Core::Exception); + virtual void getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) throw(Core::Exception); public: static void registerBackend() { diff --git a/src/modules/UserBackendMysql/UserBackendMysql.cpp b/src/modules/UserBackendMysql/UserBackendMysql.cpp index b020310..f0e9b2e 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.cpp +++ b/src/modules/UserBackendMysql/UserBackendMysql.cpp @@ -20,10 +20,10 @@ #include #include "UserBackendMysql.h" -#include -#include -#include -#include +#include +#include +#include +#include #include @@ -36,7 +36,7 @@ namespace Modules { boost::shared_ptr UserBackendMysql::backend; -bool UserBackendMysql::handleConfigEntry(const Common::ConfigEntry &entry, bool handled) { +bool UserBackendMysql::handleConfigEntry(const Core::ConfigEntry &entry, bool handled) { if(handled) return false; @@ -65,7 +65,7 @@ bool UserBackendMysql::handleConfigEntry(const Common::ConfigEntry &entry, bool val = strtol(entry[1][0].c_str(), &endptr, 10); if(endptr != 0 || val < 0 || val > 65535) - Common::Logger::log(Common::Logger::WARNING, "UserBackendMysql: Invalid port"); + Core::Logger::log(Core::Logger::WARNING, "UserBackendMysql: Invalid port"); else port = val; } @@ -121,7 +121,7 @@ bool UserBackendMysql::handleConfigEntry(const Common::ConfigEntry &entry, bool void UserBackendMysql::configFinished() { if(db.empty()) { - Common::Logger::log(Common::Logger::ERROR, "UserBackendMysql: No database name given"); + Core::Logger::log(Core::Logger::ERROR, "UserBackendMysql: No database name given"); return; } @@ -132,17 +132,17 @@ void UserBackendMysql::configFinished() { } -boost::shared_ptr > UserBackendMysql::getUserList() throw(Net::Exception) { - Net::ThreadManager::get()->detach(); +boost::shared_ptr > UserBackendMysql::getUserList() throw(Core::Exception) { + Core::ThreadManager::get()->detach(); if(mysql_ping(mysql)) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); mysql_real_query(mysql, queryListUsers.c_str(), queryListUsers.length()); MYSQL_RES *result = mysql_use_result(mysql); if(!result || mysql_num_fields(result) < 4) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); boost::shared_ptr > users(new std::map()); @@ -158,11 +158,11 @@ boost::shared_ptr > UserBackendMysql:: return users; } -boost::shared_ptr UserBackendMysql::getUserInfo(unsigned long uid) throw(Net::Exception) { - Net::ThreadManager::get()->detach(); +boost::shared_ptr UserBackendMysql::getUserInfo(unsigned long uid) throw(Core::Exception) { + Core::ThreadManager::get()->detach(); if(mysql_ping(mysql)) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); std::string query = queryUserById; @@ -177,7 +177,7 @@ boost::shared_ptr UserBackendMysql::getUserInfo(unsigned long MYSQL_RES *result = mysql_use_result(mysql); if(!result || mysql_num_fields(result) < 4) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); MYSQL_ROW row = mysql_fetch_row(result); @@ -192,14 +192,14 @@ boost::shared_ptr UserBackendMysql::getUserInfo(unsigned long return user; } - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); } -boost::shared_ptr UserBackendMysql::getUserInfoByName(const std::string &name) throw(Net::Exception) { - Net::ThreadManager::get()->detach(); +boost::shared_ptr UserBackendMysql::getUserInfoByName(const std::string &name) throw(Core::Exception) { + Core::ThreadManager::get()->detach(); if(mysql_ping(mysql)) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); boost::scoped_array escapedName(new char[name.length()*2+1]); @@ -211,7 +211,7 @@ boost::shared_ptr UserBackendMysql::getUserInfoByName(const st MYSQL_RES *result = mysql_use_result(mysql); if(!result || mysql_num_fields(result) < 4) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); MYSQL_ROW row = mysql_fetch_row(result); @@ -226,14 +226,14 @@ boost::shared_ptr UserBackendMysql::getUserInfoByName(const st return user; } - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); } -boost::shared_ptr > UserBackendMysql::getUserGroupList(unsigned long uid) throw(Net::Exception) { - Net::ThreadManager::get()->detach(); +boost::shared_ptr > UserBackendMysql::getUserGroupList(unsigned long uid) throw(Core::Exception) { + Core::ThreadManager::get()->detach(); if(mysql_ping(mysql)) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); std::ostringstream tmp; tmp << '"'; @@ -246,7 +246,7 @@ boost::shared_ptr > UserBackendMysql::getUserGroupList(u MYSQL_RES *result = mysql_use_result(mysql); if(!result || mysql_num_fields(result) < 1) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); boost::shared_ptr > groups(new std::set); @@ -257,17 +257,17 @@ boost::shared_ptr > UserBackendMysql::getUserGroupList(u } -boost::shared_ptr > UserBackendMysql::getGroupList() throw(Net::Exception) { - Net::ThreadManager::get()->detach(); +boost::shared_ptr > UserBackendMysql::getGroupList() throw(Core::Exception) { + Core::ThreadManager::get()->detach(); if(mysql_ping(mysql)) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); mysql_real_query(mysql, queryListGroups.c_str(), queryListGroups.length()); MYSQL_RES *result = mysql_use_result(mysql); if(!result || mysql_num_fields(result) < 2) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); boost::shared_ptr > groups(new std::map()); @@ -280,11 +280,11 @@ boost::shared_ptr > UserBackendMysql: return groups; } -std::string UserBackendMysql::getGroupName(unsigned long gid) throw(Net::Exception) { - Net::ThreadManager::get()->detach(); +std::string UserBackendMysql::getGroupName(unsigned long gid) throw(Core::Exception) { + Core::ThreadManager::get()->detach(); if(mysql_ping(mysql)) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); std::string query = queryGroupById; @@ -299,21 +299,21 @@ std::string UserBackendMysql::getGroupName(unsigned long gid) throw(Net::Excepti MYSQL_RES *result = mysql_use_result(mysql); if(!result || mysql_num_fields(result) < 2) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); MYSQL_ROW row = mysql_fetch_row(result); if(row) return row[1]; - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); } -unsigned long UserBackendMysql::getGroupId(const std::string &name) throw(Net::Exception) { - Net::ThreadManager::get()->detach(); +unsigned long UserBackendMysql::getGroupId(const std::string &name) throw(Core::Exception) { + Core::ThreadManager::get()->detach(); if(mysql_ping(mysql)) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); boost::scoped_array escapedName(new char[name.length()*2+1]); @@ -325,21 +325,21 @@ unsigned long UserBackendMysql::getGroupId(const std::string &name) throw(Net::E MYSQL_RES *result = mysql_use_result(mysql); if(!result || mysql_num_fields(result) < 2) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); MYSQL_ROW row = mysql_fetch_row(result); if(row) return strtoul(row[0], 0, 10); - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); } -boost::shared_ptr > UserBackendMysql::getGroupUserList(unsigned long gid) throw(Net::Exception) { - Net::ThreadManager::get()->detach(); +boost::shared_ptr > UserBackendMysql::getGroupUserList(unsigned long gid) throw(Core::Exception) { + Core::ThreadManager::get()->detach(); if(mysql_ping(mysql)) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); std::ostringstream tmp; tmp << '"'; @@ -352,7 +352,7 @@ boost::shared_ptr > UserBackendMysql::getGroupUserList(u MYSQL_RES *result = mysql_use_result(mysql); if(!result || mysql_num_fields(result) < 1) - throw Net::Exception(Net::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_AVAILABLE); boost::shared_ptr > users(new std::set); @@ -368,14 +368,14 @@ void UserBackendMysql::registerBackend() { return; backend.reset(new UserBackendMysql()); - Common::ConfigManager::get()->registerConfigurable(backend.get()); + Core::ConfigManager::get()->registerConfigurable(backend.get()); } void UserBackendMysql::unregisterBackend() { if(!backend) return; - Common::ConfigManager::get()->unregisterConfigurable(backend.get()); + Core::ConfigManager::get()->unregisterConfigurable(backend.get()); Server::UserManager::get()->unregisterBackend(backend); backend.reset(); diff --git a/src/modules/UserBackendMysql/UserBackendMysql.h b/src/modules/UserBackendMysql/UserBackendMysql.h index 26c7ef3..523806f 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.h +++ b/src/modules/UserBackendMysql/UserBackendMysql.h @@ -22,7 +22,7 @@ #include #include -#include +#include #include @@ -30,7 +30,7 @@ namespace Mad { namespace Modules { -class UserBackendMysql : public Server::UserBackend, private Common::Configurable { +class UserBackendMysql : public Server::UserBackend, private Core::Configurable { private: static boost::shared_ptr backend; @@ -47,18 +47,18 @@ class UserBackendMysql : public Server::UserBackend, private Common::Configurabl UserBackendMysql() : port(0), mysql(0) {} protected: - virtual bool handleConfigEntry(const Common::ConfigEntry &entry, bool); + virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool); virtual void configFinished(); - virtual boost::shared_ptr > getUserList() throw(Net::Exception); - virtual boost::shared_ptr getUserInfo(unsigned long uid) throw(Net::Exception); - virtual boost::shared_ptr getUserInfoByName(const std::string &name) throw(Net::Exception); - virtual boost::shared_ptr > getUserGroupList(unsigned long uid) throw(Net::Exception); + virtual boost::shared_ptr > getUserList() throw(Core::Exception); + virtual boost::shared_ptr getUserInfo(unsigned long uid) throw(Core::Exception); + virtual boost::shared_ptr getUserInfoByName(const std::string &name) throw(Core::Exception); + virtual boost::shared_ptr > getUserGroupList(unsigned long uid) throw(Core::Exception); - virtual boost::shared_ptr > getGroupList() throw(Net::Exception); - virtual std::string getGroupName(unsigned long gid) throw(Net::Exception); - virtual unsigned long getGroupId(const std::string &name) throw(Net::Exception); - virtual boost::shared_ptr > getGroupUserList(unsigned long gid) throw(Net::Exception); + virtual boost::shared_ptr > getGroupList() throw(Core::Exception); + virtual std::string getGroupName(unsigned long gid) throw(Core::Exception); + virtual unsigned long getGroupId(const std::string &name) throw(Core::Exception); + virtual boost::shared_ptr > getGroupUserList(unsigned long gid) throw(Core::Exception); public: virtual ~UserBackendMysql() { -- cgit v1.2.3