diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-11-18 17:59:09 +0100 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-11-18 17:59:09 +0100 |
commit | 8f5086e03d80620ededcad052192e7247a91dcd8 (patch) | |
tree | c3afec572c7103b20f75c2601e694e2981959659 | |
parent | 62ca019532b28527facf6ab133742c2a190f15d6 (diff) | |
download | mad-8f5086e03d80620ededcad052192e7247a91dcd8.tar mad-8f5086e03d80620ededcad052192e7247a91dcd8.zip |
CommandManager und CommandParser als Singleton realisiert
-rw-r--r-- | src/Client/CommandManager.cpp | 3 | ||||
-rw-r--r-- | src/Client/CommandManager.h | 8 | ||||
-rw-r--r-- | src/Client/CommandParser.cpp | 26 | ||||
-rw-r--r-- | src/Client/CommandParser.h | 22 | ||||
-rw-r--r-- | src/Common/LogManager.h | 1 | ||||
-rw-r--r-- | src/madc.cpp | 17 |
6 files changed, 47 insertions, 30 deletions
diff --git a/src/Client/CommandManager.cpp b/src/Client/CommandManager.cpp index 08a2780..4b24782 100644 --- a/src/Client/CommandManager.cpp +++ b/src/Client/CommandManager.cpp @@ -32,6 +32,9 @@ namespace Mad { namespace Client { +CommandManager CommandManager::commandManager; + + void CommandManager::printFSInfo(const Net::Packets::FSInfoPacket &packet) { const std::string units[] = { "kB", "MB", "GB", "TB", "" diff --git a/src/Client/CommandManager.h b/src/Client/CommandManager.h index 1937c0f..25b3f2e 100644 --- a/src/Client/CommandManager.h +++ b/src/Client/CommandManager.h @@ -38,6 +38,8 @@ class CommandManager { private: friend class CommandParser; + static CommandManager commandManager; + unsigned int activeRequests; sigc::signal<void> finished; @@ -60,9 +62,13 @@ class CommandManager { void fsInfoRequestFinished(const Common::Request<Net::Packets::FSInfoPacket> &request); void statusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request); - public: CommandManager() : activeRequests(0), disconnect(false) {} + public: + static CommandManager *getCommandManager() { + return &commandManager; + } + bool requestsActive() {return (activeRequests > 0);} bool willDisconnect() {return disconnect;} diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index d75eb0a..c0c8d7f 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -50,6 +50,8 @@ const CommandParser::Command CommandParser::commands[] = { {{0}, 0, 0, 0, 0} }; +CommandParser CommandParser::commandParser; + const CommandParser::Command* CommandParser::findCommand(const std::string& command) { for(int i = 0; commands[i].commands[0] != 0; ++i) { @@ -108,16 +110,16 @@ std::map<std::string, Common::HostInfo> CommandParser::parseHostList(const std:: void CommandParser::fsinfoCommand(const std::vector<std::string> &args) { if(args.size() == 1) - Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::FSInfoRequest(sigc::mem_fun(commandManager, &CommandManager::fsInfoRequestFinished)))); + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::FSInfoRequest(sigc::mem_fun(CommandManager::getCommandManager(), &CommandManager::fsInfoRequestFinished)))); else if(args.size() == 2) - Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::DaemonFSInfoRequest(args[1], sigc::mem_fun(commandManager, &CommandManager::daemonFSInfoRequestFinished)))); + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::DaemonFSInfoRequest(args[1], sigc::mem_fun(CommandManager::getCommandManager(), &CommandManager::daemonFSInfoRequestFinished)))); else { Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); printUsage("fsinfo"); return; } - commandManager.activeRequests++; + ++CommandManager::getCommandManager()->activeRequests; } void CommandParser::helpCommand(const std::vector<std::string> &args) { @@ -212,10 +214,10 @@ void CommandParser::rebootCommand(const std::vector<std::string> &args) { for(std::map<std::string, Common::HostInfo>::iterator host = hosts.begin(); host != hosts.end(); ++host) { Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>( - new Requests::DaemonCommandRequest(host->first, true, sigc::mem_fun(commandManager, &CommandManager::daemonCommandRequestFinished)) + new Requests::DaemonCommandRequest(host->first, true, sigc::mem_fun(CommandManager::getCommandManager(), &CommandManager::daemonCommandRequestFinished)) )); - commandManager.activeRequests++; + ++CommandManager::getCommandManager()->activeRequests; } } @@ -230,31 +232,31 @@ void CommandParser::shutdownCommand(const std::vector<std::string> &args) { for(std::map<std::string, Common::HostInfo>::iterator host = hosts.begin(); host != hosts.end(); ++host) { Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>( - new Requests::DaemonCommandRequest(host->first, false, sigc::mem_fun(commandManager, &CommandManager::daemonCommandRequestFinished)) + new Requests::DaemonCommandRequest(host->first, false, sigc::mem_fun(CommandManager::getCommandManager(), &CommandManager::daemonCommandRequestFinished)) )); - commandManager.activeRequests++; + ++CommandManager::getCommandManager()->activeRequests; } } void CommandParser::statusCommand(const std::vector<std::string> &args) { if(args.size() == 1) - Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::StatusRequest(sigc::mem_fun(commandManager, &CommandManager::statusRequestFinished)))); + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::StatusRequest(sigc::mem_fun(CommandManager::getCommandManager(), &CommandManager::statusRequestFinished)))); else if(args.size() == 2) - Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::DaemonStatusRequest(args[1], sigc::mem_fun(commandManager, &CommandManager::daemonStatusRequestFinished)))); + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::DaemonStatusRequest(args[1], sigc::mem_fun(CommandManager::getCommandManager(), &CommandManager::daemonStatusRequestFinished)))); else { Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); printUsage("status"); return; } - commandManager.activeRequests++; + ++CommandManager::getCommandManager()->activeRequests; } void CommandParser::exitCommand(const std::vector<std::string>&) { - commandManager.activeRequests++; + ++CommandManager::getCommandManager()->activeRequests; - Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::DisconnectRequest(sigc::mem_fun(commandManager, &CommandManager::disconnectRequestFinished)))); + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::DisconnectRequest(sigc::mem_fun(CommandManager::getCommandManager(), &CommandManager::disconnectRequestFinished)))); } bool CommandParser::split(const std::string &str, std::vector<std::string> &ret) { diff --git a/src/Client/CommandParser.h b/src/Client/CommandParser.h index 660f548..e08c775 100644 --- a/src/Client/CommandParser.h +++ b/src/Client/CommandParser.h @@ -49,7 +49,7 @@ class CommandParser { static const Command commands[]; - CommandManager commandManager; + static CommandParser commandParser; Net::Connection *connection; @@ -68,13 +68,25 @@ class CommandParser { void statusCommand(const std::vector<std::string> &args); void exitCommand(const std::vector<std::string>&); + CommandParser() : connection(0) {} + public: - CommandParser(Net::Connection *connection0) : connection(connection0) {} + static CommandParser *getCommandParser() { + return &commandParser; + } + + Net::Connection *getConnection() const { + return connection; + } + + void setConnection(Net::Connection *con) { + connection = con; + } - bool requestsActive() {return commandManager.requestsActive();} - bool willDisconnect() {return commandManager.willDisconnect();} + bool requestsActive() {return CommandManager::getCommandManager()->requestsActive();} + bool willDisconnect() {return CommandManager::getCommandManager()->willDisconnect();} - sigc::signal<void> signalFinished() const {return commandManager.signalFinished();} + sigc::signal<void> signalFinished() const {return CommandManager::getCommandManager()->signalFinished();} bool parse(const std::string &cmd); diff --git a/src/Common/LogManager.h b/src/Common/LogManager.h index 846bc95..65fcf19 100644 --- a/src/Common/LogManager.h +++ b/src/Common/LogManager.h @@ -21,7 +21,6 @@ #define MAD_COMMON_LOGMANAGER_H_ #include "Configurable.h" -#include "Initializable.h" #include "Logger.h" #include "RemoteLogger.h" #include "SharedPtr.h" diff --git a/src/madc.cpp b/src/madc.cpp index f74f925..b758ec8 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -38,24 +38,21 @@ using namespace Mad; -static Client::CommandParser *parser; - - static void usage(const std::string &cmd) { std::cerr << "Usage: " << cmd << " address[:port]" << std::endl; } static void handleCommand(char *cmd) { if(!cmd) - parser->requestDisconnect(); + Client::CommandParser::getCommandParser()->requestDisconnect(); else if(!*cmd) return; else { - parser->parse(cmd); + Client::CommandParser::getCommandParser()->parse(cmd); add_history(cmd); } - if(parser->requestsActive()) { + if(Client::CommandParser::getCommandParser()->requestsActive()) { rl_callback_handler_remove(); Net::FdManager::getFdManager()->setFdEvents(STDIN_FILENO, 0); } @@ -67,7 +64,7 @@ static void charHandler(short events) { } static void activateReadline() { - if(parser->willDisconnect()) + if(Client::CommandParser::getCommandParser()->willDisconnect()) return; rl_callback_handler_install("mad> ", handleCommand); @@ -111,8 +108,8 @@ int main(int argc, char *argv[]) { std::cerr << " done." << std::endl << std::endl; - parser = new Mad::Client::CommandParser(connection); - parser->signalFinished().connect(sigc::ptr_fun(activateReadline)); + Client::CommandParser::getCommandParser()->setConnection(connection); + Client::CommandParser::getCommandParser()->signalFinished().connect(sigc::ptr_fun(activateReadline)); Net::FdManager::getFdManager()->registerFd(STDIN_FILENO, sigc::ptr_fun(charHandler)); @@ -123,8 +120,6 @@ int main(int argc, char *argv[]) { Net::FdManager::getFdManager()->unregisterFd(STDIN_FILENO); - delete parser; - Common::RequestManager::getRequestManager()->unregisterConnection(connection); } catch(Mad::Common::Exception &e) { |