From 7234fe326d16d6bf9f4374a09ddc6ef790e6723f Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 18 Jun 2009 22:03:02 +0200 Subject: Globale Variablen durch Application-Klasse ersetzt --- src/Client/Application.cpp | 33 +++++++++++++ src/Client/Application.h | 46 ++++++++++++++++++ src/Client/CMakeLists.txt | 1 + src/Client/CommandParser.cpp | 45 ++++++++--------- src/Client/CommandParser.h | 27 ++++------- src/Client/InformationManager.cpp | 30 ++++++------ src/Client/InformationManager.h | 28 +++++------ src/Client/Requests/DaemonCommandRequest.h | 4 +- src/Client/Requests/DaemonFSInfoRequest.h | 2 +- src/Client/Requests/DaemonListRequest.h | 2 +- src/Client/Requests/DaemonStatusRequest.h | 2 +- src/Client/SystemCommands.cpp | 58 +++++++++++----------- src/Client/SystemCommands.h | 14 +++--- src/Client/UserCommands.cpp | 78 +++++++++++++++--------------- src/Client/UserCommands.h | 15 +++--- 15 files changed, 229 insertions(+), 156 deletions(-) create mode 100644 src/Client/Application.cpp create mode 100644 src/Client/Application.h (limited to 'src/Client') diff --git a/src/Client/Application.cpp b/src/Client/Application.cpp new file mode 100644 index 0000000..34a26bb --- /dev/null +++ b/src/Client/Application.cpp @@ -0,0 +1,33 @@ +/* + * Application.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 "Application.h" +#include "InformationManager.h" + +namespace Mad { +namespace Client { + +Application::Application() : Common::Application(false), informationManager(new InformationManager(this)) {} + +Application::~Application() { + delete informationManager; +} + +} +} diff --git a/src/Client/Application.h b/src/Client/Application.h new file mode 100644 index 0000000..db861c2 --- /dev/null +++ b/src/Client/Application.h @@ -0,0 +1,46 @@ +/* + * Application.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_CLIENT_APPLICATION_H_ +#define MAD_CLIENT_APPLICATION_H_ + +#include + +namespace Mad { +namespace Client { + +class InformationManager; + +class Application : public Common::Application { + private: + InformationManager *informationManager; + + public: + Application(); + virtual ~Application(); + + InformationManager* getInformationManager() const { + return informationManager; + } +}; + +} +} + +#endif /* MAD_CLIENT_APPLICATION_H_ */ diff --git a/src/Client/CMakeLists.txt b/src/Client/CMakeLists.txt index e663602..6d7ba4c 100644 --- a/src/Client/CMakeLists.txt +++ b/src/Client/CMakeLists.txt @@ -3,6 +3,7 @@ add_subdirectory(Requests) include_directories(${INCLUDES}) add_library(Client + Application.cpp Application.h CommandParser.cpp CommandParser.h InformationManager.cpp InformationManager.h SystemCommands.cpp SystemCommands.h diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index be2383a..f231082 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -18,11 +18,11 @@ */ #include "CommandParser.h" +#include "Application.h" #include "InformationManager.h" #include "SystemCommands.h" #include "UserCommands.h" -#include #include #include @@ -52,8 +52,6 @@ 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) { @@ -71,12 +69,12 @@ void CommandParser::printUsage(const std::string& command) { const CommandParser::Command *cmd = findCommand(command); if(cmd) - Core::Logger::logf("Usage: %s\n", cmd->cmdline); + std::cerr << "Usage: " << cmd->cmdline << std::endl; } std::map CommandParser::parseHostList(const std::vector &args, bool mustBeActive) { - const std::map& hosts = InformationManager::get()->getDaemons(); + const std::map& hosts = application->getInformationManager()->getDaemons(); std::map ret; for(std::vector::const_iterator arg = args.begin(); arg != args.end(); ++arg) { @@ -88,7 +86,7 @@ std::map CommandParser::parseHostList(const std:: } if(ret.empty()) - Core::Logger::log("No hosts active."); + std::cerr << "No hosts active." << std::endl; } else { ret = hosts; @@ -98,9 +96,9 @@ std::map CommandParser::parseHostList(const std:: std::map::const_iterator host = hosts.find(*arg); if(host == hosts.end()) - Core::Logger::logf(Core::Logger::ERROR, "Host '%s' doesn't exist.", arg->c_str()); + std::cerr << "Host '" << *arg << "' doesn't exist." << std::endl; else if(mustBeActive && host->second.getState() == Common::HostInfo::INACTIVE) - Core::Logger::logf(Core::Logger::WARNING, "Host '%s' is inactive.", arg->c_str()); + std::cerr << "Host '" << *arg << "' is inactive." << std::endl; else ret.insert(*host); } @@ -109,7 +107,7 @@ std::map CommandParser::parseHostList(const std:: return ret; } -void CommandParser::helpCommand(const std::vector &args, Common::Connection *connection _UNUSED_PARAMETER_) { +void CommandParser::helpCommand(const std::vector &args) { if(args.size() == 1) { std::cout << "Available commands:" << std::endl << std::endl; @@ -130,16 +128,16 @@ void CommandParser::helpCommand(const std::vector &args, Common::Co std::cout << command->longdesc << std::endl << std::endl; } else - Core::Logger::logf(Core::Logger::WARNING, "%s: Command '%s' doesn't exist.", args[0].c_str(), args[1].c_str()); + std::cerr << args[0] << ": Command '" << args[1] << "' doesn't exist." << std::endl; } else { - Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + std::cerr << args[0] << ": Too many arguments." << std::endl; printUsage("help"); } } -void CommandParser::listHostsCommand(const std::vector &args, Common::Connection *connection _UNUSED_PARAMETER_) { - const std::map& hosts = InformationManager::get()->getDaemons(); +void CommandParser::listHostsCommand(const std::vector &args) { + const std::map& hosts = application->getInformationManager()->getDaemons(); if(args.size() == 1) { if(hosts.empty()) { @@ -167,7 +165,7 @@ void CommandParser::listHostsCommand(const std::vector &args, Commo std::cout << std::endl; } else if(args.size() > 2) { - Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + std::cerr << args[0] << ": Too many arguments." << std::endl; printUsage("list_hosts"); } else if(args[1] == "-a") { @@ -185,23 +183,23 @@ void CommandParser::listHostsCommand(const std::vector &args, Commo std::cout << std::endl; } else { - Core::Logger::logf(Core::Logger::ERROR, "%s: Don't understand argument '%s'.", args[0].c_str(), args[1].c_str()); + std::cerr << args[0] << ": Don't understand argument '" << args[1] << "'." << std::endl; printUsage("list_hosts"); } } -void CommandParser::exitCommand(const std::vector &args _UNUSED_PARAMETER_, Common::Connection *connection) { - boost::shared_ptr request(new Common::Requests::DisconnectRequest); +void CommandParser::exitCommand(const std::vector &args _UNUSED_PARAMETER_) { + boost::shared_ptr request(new Common::Requests::DisconnectRequest(application)); - Common::RequestManager::get()->sendRequest(connection, request); + application->getRequestManager()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(result.second) - Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; else - commandParser.disconnect = true; + disconnect = true; } bool CommandParser::parse(const std::string &cmd) { @@ -215,12 +213,15 @@ bool CommandParser::parse(const std::string &cmd) { const Command* command = findCommand(splitCmd[0]); if(command) - command->function(splitCmd, connection); + command->function(this, splitCmd); else - Core::Logger::logf(Core::Logger::ERROR, "Unknown command '%s'.", splitCmd[0].c_str()); + std::cerr << "Unknown command '" << splitCmd[0] << "'." << std::endl; return true; } +CommandParser::CommandParser(Application *application0, Common::Connection *connection0) +: application(application0), connection(connection0), disconnect(false) {} + } } diff --git a/src/Client/CommandParser.h b/src/Client/CommandParser.h index a7157ed..64dab1f 100644 --- a/src/Client/CommandParser.h +++ b/src/Client/CommandParser.h @@ -38,6 +38,7 @@ class Connection; namespace Client { +class Application; class SystemCommands; class UserCommands; @@ -52,12 +53,12 @@ class CommandParser { const char* desc; const char* longdesc; - boost::function2, Common::Connection*> function; + boost::function2 > function; }; static const Command commands[]; - static CommandParser commandParser; + Application *application; Common::Connection *connection; @@ -65,32 +66,24 @@ class CommandParser { static const Command* findCommand(const std::string& command); - static void printUsage(const std::string& command); - static std::map parseHostList(const std::vector &args, bool mustBeActive = false); + void printUsage(const std::string& command); + std::map parseHostList(const std::vector &args, bool mustBeActive = false); - static void helpCommand(const std::vector &args, Common::Connection *connection); - static void listHostsCommand(const std::vector &args, Common::Connection *connection); - static void exitCommand(const std::vector &args, Common::Connection *connection); - - CommandParser() : connection(0), disconnect(false) {} + void helpCommand(const std::vector &args); + void listHostsCommand(const std::vector &args); + void exitCommand(const std::vector &args); public: - static CommandParser *get() { - return &commandParser; - } + CommandParser(Application *application0, Common::Connection *connection0); Common::Connection *getConnection() const { return connection; } - void setConnection(Common::Connection *con) { - connection = con; - } - bool parse(const std::string &cmd); void requestDisconnect() { - exitCommand(std::vector(), connection); + exitCommand(std::vector()); } bool willDisconnect() const { diff --git a/src/Client/InformationManager.cpp b/src/Client/InformationManager.cpp index c5ff9f8..a4a63fb 100644 --- a/src/Client/InformationManager.cpp +++ b/src/Client/InformationManager.cpp @@ -18,40 +18,40 @@ */ #include "InformationManager.h" +#include "Application.h" #include "Requests/DaemonListRequest.h" -#include + #include namespace Mad { namespace Client { -InformationManager InformationManager::informationManager; - - void InformationManager::DaemonStateUpdateRequestHandler::handleRequest(boost::shared_ptr packet, Common::XmlPacket *ret) { // TODO Require authentication + InformationManager *informationManager = dynamic_cast(*getApplication()).getInformationManager(); + { - boost::lock_guard lock(informationManager.mutex); + boost::lock_guard lock(informationManager->mutex); - std::map::iterator host = informationManager.daemons.find((*packet)["name"]); - if(host != informationManager.daemons.end()) + std::map::iterator host = informationManager->daemons.find((*packet)["name"]); + if(host != informationManager->daemons.end()) host->second.setState((*packet)["state"]); else - Core::Logger::log(Core::Logger::WARNING, "Received a state update for an unknown host."); + getApplication()->log(Core::LoggerBase::WARNING, "Received a state update for an unknown host."); } ret->setType("OK"); } -void InformationManager::doInit() { - Common::RequestManager::get()->registerPacketType("UpdateHostState"); +InformationManager::InformationManager(Application *application0) : application(application0), updating(false) { + application->getRequestManager()->registerPacketType("UpdateHostState"); } -void InformationManager::doDeinit() { - Common::RequestManager::get()->unregisterPacketType("UpdateHostState"); +InformationManager::~InformationManager() { + application->getRequestManager()->unregisterPacketType("UpdateHostState"); } void InformationManager::updateDaemonList(Common::Connection *con) { @@ -60,10 +60,10 @@ void InformationManager::updateDaemonList(Common::Connection *con) { if(updating) return; - updateRequest = boost::shared_ptr(new Requests::DaemonListRequest); + updateRequest = boost::shared_ptr(new Requests::DaemonListRequest(application)); updateRequest->connectSignalFinished(boost::bind(&InformationManager::daemonListRequestFinished, this, _1, _2)); - Common::RequestManager::get()->sendRequest(con, updateRequest); + application->getRequestManager()->sendRequest(con, updateRequest); updating = true; } @@ -72,7 +72,7 @@ void InformationManager::daemonListRequestFinished(boost::shared_ptr lock(mutex); if(!packet || error) { - Core::Logger::logf(Core::Logger::CRITICAL, "Host list request failed: %s", error.strerror().c_str()); + application->logf(Core::LoggerBase::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 ffc24cf..ab84e3c 100644 --- a/src/Client/InformationManager.h +++ b/src/Client/InformationManager.h @@ -23,10 +23,9 @@ #include #include -#include - #include #include +#include #include #include @@ -36,17 +35,23 @@ namespace Mad { namespace Client { -class InformationManager : public Core::Initializable, private boost::noncopyable { +class Application; + +class InformationManager : private boost::noncopyable { private: class DaemonStateUpdateRequestHandler : public Common::RequestHandlers::SimpleRequestHandler { private: - static void handleRequest(boost::shared_ptr packet, Common::XmlPacket *ret); + void handleRequest(boost::shared_ptr packet, Common::XmlPacket *ret); public: - DaemonStateUpdateRequestHandler() : Common::RequestHandlers::SimpleRequestHandler("UpdateHostState", &DaemonStateUpdateRequestHandler::handleRequest) {} + DaemonStateUpdateRequestHandler(Common::Application *application) + : Common::RequestHandlers::SimpleRequestHandler(application, "UpdateHostState", + boost::bind(&DaemonStateUpdateRequestHandler::handleRequest, this, _1, _2)) {} }; - static InformationManager informationManager; + friend class Application; + + Application *application; boost::mutex mutex; @@ -56,19 +61,12 @@ class InformationManager : public Core::Initializable, private boost::noncopyabl bool updating; - InformationManager() : updating(false) {} - void daemonListRequestFinished(boost::shared_ptr packet, Core::Exception error); - protected: - virtual void doInit(); - virtual void doDeinit(); + InformationManager(Application *application0); + ~InformationManager(); public: - static InformationManager *get() { - return &informationManager; - } - void updateDaemonList(Common::Connection *con); bool isUpdating() { diff --git a/src/Client/Requests/DaemonCommandRequest.h b/src/Client/Requests/DaemonCommandRequest.h index 8559f87..5f01e1f 100644 --- a/src/Client/Requests/DaemonCommandRequest.h +++ b/src/Client/Requests/DaemonCommandRequest.h @@ -36,8 +36,8 @@ class DaemonCommandRequest : public Common::Request { virtual void sendRequest(); public: - DaemonCommandRequest(const std::string &daemon0, bool reboot0) - : daemon(daemon0), reboot(reboot0) {} + DaemonCommandRequest(Common::Application *application, const std::string &daemon0, bool reboot0) + : Common::Request(application), daemon(daemon0), reboot(reboot0) {} }; } diff --git a/src/Client/Requests/DaemonFSInfoRequest.h b/src/Client/Requests/DaemonFSInfoRequest.h index 58ca9a6..5be504f 100644 --- a/src/Client/Requests/DaemonFSInfoRequest.h +++ b/src/Client/Requests/DaemonFSInfoRequest.h @@ -36,7 +36,7 @@ class DaemonFSInfoRequest : public Common::Request { virtual void sendRequest(); public: - DaemonFSInfoRequest(const std::string &daemon0) : daemon(daemon0) {} + DaemonFSInfoRequest(Common::Application *application, const std::string &daemon0) : Common::Request(application), daemon(daemon0) {} }; } diff --git a/src/Client/Requests/DaemonListRequest.h b/src/Client/Requests/DaemonListRequest.h index d10efab..566fee7 100644 --- a/src/Client/Requests/DaemonListRequest.h +++ b/src/Client/Requests/DaemonListRequest.h @@ -28,7 +28,7 @@ namespace Requests { class DaemonListRequest : public Common::Requests::SimpleRequest { public: - DaemonListRequest() : SimpleRequest("ListHosts") {} + DaemonListRequest(Common::Application *application) : SimpleRequest(application, "ListHosts") {} }; } diff --git a/src/Client/Requests/DaemonStatusRequest.h b/src/Client/Requests/DaemonStatusRequest.h index 0642187..4035276 100644 --- a/src/Client/Requests/DaemonStatusRequest.h +++ b/src/Client/Requests/DaemonStatusRequest.h @@ -36,7 +36,7 @@ class DaemonStatusRequest : public Common::Request { virtual void sendRequest(); public: - DaemonStatusRequest(const std::string &daemon0) : daemon(daemon0) {} + DaemonStatusRequest(Common::Application *application, const std::string &daemon0) : Common::Request(application), daemon(daemon0) {} }; } diff --git a/src/Client/SystemCommands.cpp b/src/Client/SystemCommands.cpp index 490f278..5b2d4e5 100644 --- a/src/Client/SystemCommands.cpp +++ b/src/Client/SystemCommands.cpp @@ -18,9 +18,9 @@ */ #include "SystemCommands.h" +#include "Application.h" #include "CommandParser.h" -#include #include #include "Requests/DaemonFSInfoRequest.h" @@ -138,26 +138,26 @@ void SystemCommands::printHostStatus(boost::shared_ptr } -void SystemCommands::fsinfoCommand(const std::vector &args, Common::Connection *connection) { +void SystemCommands::fsinfoCommand(CommandParser *commandParser, const std::vector &args) { boost::shared_ptr request; if(args.size() == 1) - request = boost::shared_ptr(new Common::Requests::FSInfoRequest); + request = boost::shared_ptr(new Common::Requests::FSInfoRequest(commandParser->application)); else if(args.size() == 2) - request = boost::shared_ptr(new Requests::DaemonFSInfoRequest(args[1])); + request = boost::shared_ptr(new Requests::DaemonFSInfoRequest(commandParser->application, args[1])); else { - Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); - CommandParser::printUsage("fsinfo"); + std::cerr << args[0] << ": Too many arguments." << std::endl; + commandParser->printUsage("fsinfo"); return; } - Common::RequestManager::get()->sendRequest(connection, request); + commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } else { if(args.size() == 1) @@ -169,20 +169,20 @@ void SystemCommands::fsinfoCommand(const std::vector &args, Common: } } -void SystemCommands::rebootCommand(const std::vector &args, Common::Connection *connection) { +void SystemCommands::rebootCommand(CommandParser *commandParser, const std::vector &args) { if(args.size() < 2) { - Core::Logger::logf(Core::Logger::ERROR, "%s: No host given.", args[0].c_str()); - CommandParser::printUsage("reboot"); + std::cerr << args[0] << ": No host given." << std::endl; + commandParser->printUsage("reboot"); return; } - std::map hosts = CommandParser::parseHostList(std::vector(args.begin()+1, args.end()), true); + std::map hosts = commandParser->parseHostList(std::vector(args.begin()+1, args.end()), true); std::vector > requests; for(std::map::iterator host = hosts.begin(); host != hosts.end(); ++host) { - boost::shared_ptr request(new Requests::DaemonCommandRequest(host->first, true)); - Common::RequestManager::get()->sendRequest(connection, request); + boost::shared_ptr request(new Requests::DaemonCommandRequest(commandParser->application, host->first, true)); + commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request); requests.push_back(request); } @@ -194,24 +194,24 @@ void SystemCommands::rebootCommand(const std::vector &args, Common: std::pair, Core::Exception> result = (*request)->getResult(); if(result.second) - Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } } -void SystemCommands::shutdownCommand(const std::vector &args, Common::Connection *connection) { +void SystemCommands::shutdownCommand(CommandParser *commandParser, const std::vector &args) { if(args.size() < 2) { - Core::Logger::logf(Core::Logger::ERROR, "%s: No host given.", args[0].c_str()); - CommandParser::printUsage("shutdown"); + std::cerr << args[0] << ": No host given." << std::endl; + commandParser->printUsage("shutdown"); return; } - std::map hosts = CommandParser::parseHostList(std::vector(args.begin()+1, args.end()), true); + std::map hosts = commandParser->parseHostList(std::vector(args.begin()+1, args.end()), true); std::vector > requests; for(std::map::iterator host = hosts.begin(); host != hosts.end(); ++host) { - boost::shared_ptr request(new Requests::DaemonCommandRequest(host->first, false)); - Common::RequestManager::get()->sendRequest(connection, request); + boost::shared_ptr request(new Requests::DaemonCommandRequest(commandParser->application, host->first, false)); + commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request); requests.push_back(request); } @@ -223,30 +223,30 @@ void SystemCommands::shutdownCommand(const std::vector &args, Commo std::pair, Core::Exception> result = (*request)->getResult(); if(result.second) - Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } } -void SystemCommands::statusCommand(const std::vector &args, Common::Connection *connection) { +void SystemCommands::statusCommand(CommandParser *commandParser, const std::vector &args) { boost::shared_ptr request; if(args.size() == 1) - request = boost::shared_ptr(new Common::Requests::StatusRequest); + request = boost::shared_ptr(new Common::Requests::StatusRequest(commandParser->application)); else if(args.size() == 2) - request = boost::shared_ptr(new Requests::DaemonStatusRequest(args[1])); + request = boost::shared_ptr(new Requests::DaemonStatusRequest(commandParser->application, args[1])); else { - Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); - CommandParser::printUsage("status"); + std::cerr << args[0] << ": Too many arguments." << std::endl; + commandParser->printUsage("status"); return; } - Common::RequestManager::get()->sendRequest(connection, request); + commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } else { if(args.size() == 1) diff --git a/src/Client/SystemCommands.h b/src/Client/SystemCommands.h index 142ebdc..a647bed 100644 --- a/src/Client/SystemCommands.h +++ b/src/Client/SystemCommands.h @@ -20,15 +20,17 @@ #ifndef MAD_CLIENT_SYSTEMCOMMANDS_H_ #define MAD_CLIENT_SYSTEMCOMMANDS_H_ -#include +#include -#include #include +#include namespace Mad { namespace Client { +class CommandParser; + class SystemCommands { private: SystemCommands(); @@ -37,10 +39,10 @@ class SystemCommands { static void printHostStatus(boost::shared_ptr &packet); public: - static void fsinfoCommand(const std::vector &args, Common::Connection *connection); - static void rebootCommand(const std::vector &args, Common::Connection *connection); - static void shutdownCommand(const std::vector &args, Common::Connection *connection); - static void statusCommand(const std::vector &args, Common::Connection *connection); + static void fsinfoCommand(CommandParser *commandParser, const std::vector &args); + static void rebootCommand(CommandParser *commandParser, const std::vector &args); + static void shutdownCommand(CommandParser *commandParser, const std::vector &args); + static void statusCommand(CommandParser *commandParser, const std::vector &args); }; } diff --git a/src/Client/UserCommands.cpp b/src/Client/UserCommands.cpp index 97ea27b..a1cfd0f 100644 --- a/src/Client/UserCommands.cpp +++ b/src/Client/UserCommands.cpp @@ -18,9 +18,9 @@ */ #include "UserCommands.h" +#include "Application.h" #include "CommandParser.h" -#include #include #include @@ -35,34 +35,34 @@ namespace Mad { namespace Client { -void UserCommands::userInfoCommand(const std::vector &args, Common::Connection *connection) { +void UserCommands::userInfoCommand(CommandParser *commandParser, const std::vector &args) { if(args.size() == 1) { - Core::Logger::logf(Core::Logger::ERROR, "%s: No user id given.", args[0].c_str()); - CommandParser::printUsage("user_info"); + std::cerr << args[0] << ": No user id given." << std::endl; + commandParser->printUsage("user_info"); return; } if(args.size() > 2) { - Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); - CommandParser::printUsage("user_info"); + std::cerr << args[0] << ": Too many arguments." << std::endl; + commandParser->printUsage("user_info"); return; } char *endptr; unsigned long uid = std::strtoul(args[1].c_str(), &endptr, 10); if(args[1].empty() || *endptr != '\0') { - Core::Logger::logf(Core::Logger::ERROR, "%s: Unable to parse user id.", args[0].c_str()); - CommandParser::printUsage("user_info"); + std::cerr << args[0] << ": Unable to parse user id." << std::endl; + commandParser->printUsage("user_info"); return; } - boost::shared_ptr request(new Common::Requests::UserInfoRequest(uid)); - Common::RequestManager::get()->sendRequest(connection, request); + boost::shared_ptr request(new Common::Requests::UserInfoRequest(commandParser->application, uid)); + commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } else { const Common::XmlPacket &packet = *result.first; @@ -72,16 +72,16 @@ void UserCommands::userInfoCommand(const std::vector &args, Common: } } -void UserCommands::listUsersCommand(const std::vector &args _UNUSED_PARAMETER_, Common::Connection *connection) { - boost::shared_ptr request(new Common::Requests::UserListRequest); +void UserCommands::listUsersCommand(CommandParser *commandParser, const std::vector &args _UNUSED_PARAMETER_) { + boost::shared_ptr request(new Common::Requests::UserListRequest(commandParser->application)); - Common::RequestManager::get()->sendRequest(connection, request); + commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } else { const Common::XmlPacket::Element &users = (*result.first)["users"]; @@ -101,34 +101,34 @@ void UserCommands::listUsersCommand(const std::vector &args _UNUSED } } -void UserCommands::listUserGroupsCommand(const std::vector &args, Common::Connection *connection) { +void UserCommands::listUserGroupsCommand(CommandParser *commandParser, const std::vector &args) { if(args.size() == 1) { - Core::Logger::logf(Core::Logger::ERROR, "%s: No user id given.", args[0].c_str()); - CommandParser::printUsage("list_user_groups"); + std::cerr << args[0] << ": No user id given." << std::endl; + commandParser->printUsage("list_user_groups"); return; } if(args.size() > 2) { - Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); - CommandParser::printUsage("list_user_groups"); + std::cerr << args[0] << ": Too many arguments." << std::endl; + commandParser->printUsage("list_user_groups"); return; } char *endptr; unsigned long uid = std::strtoul(args[1].c_str(), &endptr, 10); if(args[1].empty() || *endptr != '\0') { - Core::Logger::logf(Core::Logger::ERROR, "%s: Unable to parse user id.", args[0].c_str()); - CommandParser::printUsage("list_user_groups"); + std::cerr << args[0] << ": Unable to parse user id." << std::endl; + commandParser->printUsage("list_user_groups"); return; } - boost::shared_ptr request(new Common::Requests::UserGroupListRequest(uid)); - Common::RequestManager::get()->sendRequest(connection, request); + boost::shared_ptr request(new Common::Requests::UserGroupListRequest(commandParser->application, uid)); + commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } else { const Common::XmlPacket::Element &groups = (*result.first)["groups"]; @@ -149,16 +149,16 @@ void UserCommands::listUserGroupsCommand(const std::vector &args, C } } -void UserCommands::listGroupsCommand(const std::vector &args _UNUSED_PARAMETER_, Common::Connection *connection) { - boost::shared_ptr request(new Common::Requests::GroupListRequest); +void UserCommands::listGroupsCommand(CommandParser *commandParser, const std::vector &args _UNUSED_PARAMETER_) { + boost::shared_ptr request(new Common::Requests::GroupListRequest(commandParser->application)); - Common::RequestManager::get()->sendRequest(connection, request); + commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } else { const Common::XmlPacket::Element &groups = (*result.first)["groups"]; @@ -178,34 +178,34 @@ void UserCommands::listGroupsCommand(const std::vector &args _UNUSE } } -void UserCommands::listGroupUsersCommand(const std::vector &args, Common::Connection *connection) { +void UserCommands::listGroupUsersCommand(CommandParser *commandParser, const std::vector &args) { if(args.size() == 1) { - Core::Logger::logf(Core::Logger::ERROR, "%s: No group id given.", args[0].c_str()); - CommandParser::printUsage("list_group_users"); + std::cerr << args[0] << ": No group id given." << std::endl; + commandParser->printUsage("list_group_users"); return; } if(args.size() > 2) { - Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); - CommandParser::printUsage("list_group_users"); + std::cerr << args[0] << ": Too many arguments." << std::endl; + commandParser->printUsage("list_group_users"); return; } char *endptr; unsigned long gid = std::strtoul(args[1].c_str(), &endptr, 10); if(args[1].empty() || *endptr != '\0') { - Core::Logger::logf(Core::Logger::ERROR, "%s: Unable to parse group id.", args[0].c_str()); - CommandParser::printUsage("list_group_users"); + std::cerr << args[0] << ": Unable to parse group id." << std::endl; + commandParser->printUsage("list_group_users"); return; } - boost::shared_ptr request(new Common::Requests::GroupUserListRequest(gid)); - Common::RequestManager::get()->sendRequest(connection, request); + boost::shared_ptr request(new Common::Requests::GroupUserListRequest(commandParser->application, gid)); + commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); + std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } else { const Common::XmlPacket::Element &users = (*result.first)["users"]; diff --git a/src/Client/UserCommands.h b/src/Client/UserCommands.h index c4cd399..f3dd31c 100644 --- a/src/Client/UserCommands.h +++ b/src/Client/UserCommands.h @@ -20,25 +20,24 @@ #ifndef MAD_CLIENT_USERCOMMANDS_H_ #define MAD_CLIENT_USERCOMMANDS_H_ -#include - #include #include - namespace Mad { namespace Client { +class CommandParser; + class UserCommands { private: UserCommands(); public: - static void userInfoCommand(const std::vector &args, Common::Connection *connection); - static void listUsersCommand(const std::vector &args, Common::Connection *connection); - static void listUserGroupsCommand(const std::vector &args, Common::Connection *connection); - static void listGroupsCommand(const std::vector &args, Common::Connection *connection); - static void listGroupUsersCommand(const std::vector &args, Common::Connection *connection); + static void userInfoCommand(CommandParser *commandParser, const std::vector &args); + static void listUsersCommand(CommandParser *commandParser, const std::vector &args); + static void listUserGroupsCommand(CommandParser *commandParser, const std::vector &args); + static void listGroupsCommand(CommandParser *commandParser, const std::vector &args); + static void listGroupUsersCommand(CommandParser *commandParser, const std::vector &args); }; } -- cgit v1.2.3