diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-09-25 21:15:48 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-09-25 21:15:48 +0200 |
commit | 37b452c361d99ca809c699b6968df3723f0cadb9 (patch) | |
tree | 4c22b433a0c8876aaf7677efd44fe976ad342af8 | |
parent | af0f84058d322d68cbbff98ef272dd2bcd0f692c (diff) | |
download | mad-37b452c361d99ca809c699b6968df3723f0cadb9.tar mad-37b452c361d99ca809c699b6968df3723f0cadb9.zip |
Teile vom CommandParser als CommandManager abgespalten
-rw-r--r-- | src/Client/CommandManager.cpp | 175 | ||||
-rw-r--r-- | src/Client/CommandManager.h | 72 | ||||
-rw-r--r-- | src/Client/CommandParser.cpp | 165 | ||||
-rw-r--r-- | src/Client/CommandParser.h | 40 | ||||
-rw-r--r-- | src/Client/Makefile.am | 4 | ||||
-rw-r--r-- | src/Client/Makefile.in | 7 |
6 files changed, 272 insertions, 191 deletions
diff --git a/src/Client/CommandManager.cpp b/src/Client/CommandManager.cpp new file mode 100644 index 0000000..8a82a82 --- /dev/null +++ b/src/Client/CommandManager.cpp @@ -0,0 +1,175 @@ +/* + * CommandManager.cpp + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include "CommandManager.h" +#include <Common/HostInfo.h> +#include <Common/Logger.h> +#include <Net/Packets/HostListPacket.h> +#include <Net/Packets/HostStatusPacket.h> + +#include <iostream> +#include <vector> + + +namespace Mad { +namespace Client { + +void CommandManager::printHostStatus(const Net::Packets::HostStatusPacket &packet) { + if(packet.getUptime()) { + unsigned long days = packet.getUptime()/86400; + unsigned long hours = (packet.getUptime()%86400)/3600; + unsigned long minutes = (packet.getUptime()%3600)/60; + + std::printf("\tUptime:\t\t"); + + if(days) std::printf("%lu days ", days); + + std::printf("%lu:%02lu", hours, minutes); + + std::printf(" (load average: %.2f %.2f %.2f, %lu processes)", packet.getLoadAverage1(), packet.getLoadAverage5(), packet.getLoadAverage15(), (unsigned long)packet.getProcessNumber()); + + std::printf("\n\n"); + } + + if(packet.getTotalMem() && packet.getFreeMem()) { + const std::string units[] = { + "kB", "MB", "GB", "TB", "" + }; + + unsigned unit = 0; + float totalMem = packet.getTotalMem(), usedMem = packet.getTotalMem()-packet.getFreeMem(); + + while(totalMem >= 1024 && !units[unit+1].empty()) { + ++unit; + totalMem /= 1024; + usedMem /= 1024; + } + + std::printf("\tMemory usage:\t%.*f/%.*f %s", (usedMem < 10) ? 2 : 1, usedMem, (totalMem < 10) ? 2 : 1, totalMem, units[unit].c_str()); + std::printf(" (%.1f%%)\n", usedMem*100.0f/totalMem); + + if(packet.getTotalSwap() && packet.getFreeSwap()) { + unit = 0; + totalMem = packet.getTotalSwap(); usedMem = packet.getTotalSwap()-packet.getFreeSwap(); + + while(totalMem >= 1024 && !units[unit+1].empty()) { + ++unit; + totalMem /= 1024; + usedMem /= 1024; + } + + std::printf("\tSwap usage:\t%.*f/%.*f %s", (usedMem < 10) ? 2 : 1, usedMem, (totalMem < 10) ? 2 : 1, totalMem, units[unit].c_str()); + std::printf(" (%.1f%%)\n", usedMem*100.0f/totalMem); + } + + std::printf("\n"); + } +} + +void CommandManager::coreStatusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request) { + try { + const Net::Packets::HostStatusPacket &packet = request.getResult(); + std::cout << "Server status:" << std::endl; + printHostStatus(packet); + } + catch(Common::Exception &exception) { + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); + } + + requestFinished(); +} + +void CommandManager::daemonCommandRequestFinished(const Common::Request<> &request) { + try { + request.getResult(); + } + catch(Common::Exception &exception) { + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); + } + + requestFinished(); +} + +void CommandManager::daemonListRequestFinished(const Common::Request<Net::Packets::HostListPacket> &request, bool all) { + try { + const std::vector<Common::HostInfo>& hosts = request.getResult().getHostInfo(); + + if(hosts.empty()) { + std::cout << "The host list is empty." << std::endl << std::endl; + } + else { + bool output = false; + + for(std::vector<Common::HostInfo>::const_iterator host = hosts.begin(); host != hosts.end(); ++host) { + if(host->getStatus() == Common::HostInfo::INACTIVE && !all) + continue; + + if(!output) { + std::cout << (all ? "Host list:" : "Active hosts:") << std::endl; + output = true; + } + + std::cout << " " << host->getName(); + + if(all) + std::cout << " (" << (host->getStatus() == Common::HostInfo::RUNNING ? "running" : "inactive") << ")"; + + std::cout << std::endl; + } + + if(!output) + std::cout << "No active hosts." << std::endl; + + std::cout << std::endl; + } + } + catch(Common::Exception &exception) { + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); + } + + requestFinished(); +} + +void CommandManager::daemonStatusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request) { + try { + const Net::Packets::HostStatusPacket &packet = request.getResult(); + std::cout << "Host status:" << std::endl; + printHostStatus(packet); + } + catch(Common::Exception &exception) { + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); + } + + requestFinished(); +} + +void CommandManager::disconnectRequestFinished(const Common::Request<> &request) { + try { + request.getResult(); + disconnect = true; + } + catch(Common::Exception &exception) { + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); + } + + requestFinished(); +} + +} +} diff --git a/src/Client/CommandManager.h b/src/Client/CommandManager.h new file mode 100644 index 0000000..6aea724 --- /dev/null +++ b/src/Client/CommandManager.h @@ -0,0 +1,72 @@ +/* + * CommandManager.h + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef MAD_CLIENT_COMMANDMANAGER_H_ +#define MAD_CLIENT_COMMANDMANAGER_H_ + +#include <Common/Request.h> + +namespace Mad { + +namespace Net { +namespace Packets { +class HostStatusPacket; +class HostListPacket; +} +} + +namespace Client { + +class CommandManager { + private: + friend class CommandParser; + + unsigned int activeRequests; + + sigc::signal<void> finished; + + bool disconnect; + + void requestFinished() { + activeRequests--; + + finished(); + } + + void printHostStatus(const Net::Packets::HostStatusPacket &packet); + + void coreStatusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request); + void daemonCommandRequestFinished(const Common::Request<> &request); + void daemonListRequestFinished(const Common::Request<Net::Packets::HostListPacket> &request, bool all); + void daemonStatusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request); + void disconnectRequestFinished(const Common::Request<> &request); + + public: + CommandManager() : activeRequests(0), disconnect(false) {} + + bool requestsActive() {return (activeRequests > 0);} + bool willDisconnect() {return disconnect;} + + sigc::signal<void> signalFinished() const {return finished;} +}; + +} +} + +#endif /* MAD_CLIENT_COMMANDMANAGER_H_ */ diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 74b5710..1a5a96c 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -65,58 +65,6 @@ void CommandParser::printUsage(const std::string& command) { Common::Logger::logf("Usage: %s\n", cmd->cmdline); } -void CommandParser::printHostStatus(const Net::Packets::HostStatusPacket &packet) { - if(packet.getUptime()) { - unsigned long days = packet.getUptime()/86400; - unsigned long hours = (packet.getUptime()%86400)/3600; - unsigned long minutes = (packet.getUptime()%3600)/60; - - std::printf("\tUptime:\t\t"); - - if(days) std::printf("%lu days ", days); - - std::printf("%lu:%02lu", hours, minutes); - - std::printf(" (load average: %.2f %.2f %.2f, %lu processes)", packet.getLoadAverage1(), packet.getLoadAverage5(), packet.getLoadAverage15(), (unsigned long)packet.getProcessNumber()); - - std::printf("\n\n"); - } - - if(packet.getTotalMem() && packet.getFreeMem()) { - const std::string units[] = { - "kB", "MB", "GB", "TB", "" - }; - - unsigned unit = 0; - float totalMem = packet.getTotalMem(), usedMem = packet.getTotalMem()-packet.getFreeMem(); - - while(totalMem >= 1024 && !units[unit+1].empty()) { - ++unit; - totalMem /= 1024; - usedMem /= 1024; - } - - std::printf("\tMemory usage:\t%.*f/%.*f %s", (usedMem < 10) ? 2 : 1, usedMem, (totalMem < 10) ? 2 : 1, totalMem, units[unit].c_str()); - std::printf(" (%.1f%%)\n", usedMem*100.0f/totalMem); - - if(packet.getTotalSwap() && packet.getFreeSwap()) { - unit = 0; - totalMem = packet.getTotalSwap(); usedMem = packet.getTotalSwap()-packet.getFreeSwap(); - - while(totalMem >= 1024 && !units[unit+1].empty()) { - ++unit; - totalMem /= 1024; - usedMem /= 1024; - } - - std::printf("\tSwap usage:\t%.*f/%.*f %s", (usedMem < 10) ? 2 : 1, usedMem, (totalMem < 10) ? 2 : 1, totalMem, units[unit].c_str()); - std::printf(" (%.1f%%)\n", usedMem*100.0f/totalMem); - } - - std::printf("\n"); - } -} - void CommandParser::helpCommand(const std::vector<std::string> &args) { if(args.size() == 1) { std::cout << "Available commands:" << std::endl << std::endl; @@ -151,7 +99,7 @@ void CommandParser::listHostsCommand(const std::vector<std::string> &args) { Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>( new Requests::DaemonListRequest( - sigc::bind(sigc::mem_fun(this, &CommandParser::daemonListRequestFinished), false) + sigc::bind(sigc::mem_fun(commandManager, &CommandManager::daemonListRequestFinished), false) ) ) ); @@ -165,7 +113,7 @@ void CommandParser::listHostsCommand(const std::vector<std::string> &args) { Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>( new Requests::DaemonListRequest( - sigc::bind(sigc::mem_fun(this, &CommandParser::daemonListRequestFinished), true) + sigc::bind(sigc::mem_fun(commandManager, &CommandManager::daemonListRequestFinished), true) ) ) ); @@ -176,7 +124,7 @@ void CommandParser::listHostsCommand(const std::vector<std::string> &args) { return; } - activeRequests++; + commandManager.activeRequests++; } void CommandParser::rebootCommand(const std::vector<std::string> &args) { @@ -192,10 +140,10 @@ void CommandParser::rebootCommand(const std::vector<std::string> &args) { } Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>( - new Requests::DaemonCommandRequest(args[1], true, sigc::mem_fun(this, &CommandParser::daemonCommandRequestFinished)) + new Requests::DaemonCommandRequest(args[1], true, sigc::mem_fun(commandManager, &CommandManager::daemonCommandRequestFinished)) )); - activeRequests++; + commandManager.activeRequests++; } void CommandParser::shutdownCommand(const std::vector<std::string> &args) { @@ -211,119 +159,30 @@ void CommandParser::shutdownCommand(const std::vector<std::string> &args) { } Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>( - new Requests::DaemonCommandRequest(args[1], false, sigc::mem_fun(this, &CommandParser::daemonCommandRequestFinished)) + new Requests::DaemonCommandRequest(args[1], false, sigc::mem_fun(commandManager, &CommandManager::daemonCommandRequestFinished)) )); - activeRequests++; + commandManager.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 Requests::CoreStatusRequest(sigc::mem_fun(this, &CommandParser::coreStatusRequestFinished)))); + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::CoreStatusRequest(sigc::mem_fun(commandManager, &CommandManager::coreStatusRequestFinished)))); else if(args.size() == 2) - Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::DaemonStatusRequest(args[1], sigc::mem_fun(this, &CommandParser::daemonStatusRequestFinished)))); + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::DaemonStatusRequest(args[1], sigc::mem_fun(commandManager, &CommandManager::daemonStatusRequestFinished)))); else { Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); printUsage("status"); return; } - activeRequests++; + commandManager.activeRequests++; } void CommandParser::exitCommand(const std::vector<std::string>&) { - activeRequests++; - - Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::DisconnectRequest(sigc::mem_fun(this, &CommandParser::disconnectRequestFinished)))); -} - -void CommandParser::coreStatusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request) { - try { - const Net::Packets::HostStatusPacket &packet = request.getResult(); - std::cout << "Server status:" << std::endl; - printHostStatus(packet); - } - catch(Common::Exception &exception) { - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); - } - - requestFinished(); -} - -void CommandParser::daemonCommandRequestFinished(const Common::Request<> &request) { - try { - request.getResult(); - } - catch(Common::Exception &exception) { - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); - } - - requestFinished(); -} - -void CommandParser::daemonListRequestFinished(const Common::Request<Net::Packets::HostListPacket> &request, bool all) { - try { - const std::vector<Common::HostInfo>& hosts = request.getResult().getHostInfo(); - - if(hosts.empty()) { - std::cout << "The host list is empty." << std::endl << std::endl; - } - else { - bool output = false; - - for(std::vector<Common::HostInfo>::const_iterator host = hosts.begin(); host != hosts.end(); ++host) { - if(host->getStatus() == Common::HostInfo::INACTIVE && !all) - continue; - - if(!output) { - std::cout << (all ? "Host list:" : "Active hosts:") << std::endl; - output = true; - } - - std::cout << " " << host->getName(); - - if(all) - std::cout << " (" << (host->getStatus() == Common::HostInfo::RUNNING ? "running" : "inactive") << ")"; - - std::cout << std::endl; - } - - if(!output) - std::cout << "No active hosts." << std::endl; - - std::cout << std::endl; - } - } - catch(Common::Exception &exception) { - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); - } - - requestFinished(); -} - -void CommandParser::daemonStatusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request) { - try { - const Net::Packets::HostStatusPacket &packet = request.getResult(); - std::cout << "Host status:" << std::endl; - printHostStatus(packet); - } - catch(Common::Exception &exception) { - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); - } - - requestFinished(); -} - -void CommandParser::disconnectRequestFinished(const Common::Request<> &request) { - try { - request.getResult(); - disconnect = true; - } - catch(Common::Exception &exception) { - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); - } + commandManager.activeRequests++; - requestFinished(); + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::DisconnectRequest(sigc::mem_fun(commandManager, &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 9b94ef0..86be22f 100644 --- a/src/Client/CommandParser.h +++ b/src/Client/CommandParser.h @@ -20,6 +20,7 @@ #ifndef MAD_CLIENT_COMMANDPARSER_H_ #define MAD_CLIENT_COMMANDPARSER_H_ +#include "CommandManager.h" #include <Common/Request.h> #include <string> @@ -27,18 +28,8 @@ namespace Mad { -namespace Common { -class Exception; -} - namespace Net { class Connection; - -namespace Packets { -class HostStatusPacket; -class HostListPacket; -} - } namespace Client { @@ -56,20 +47,15 @@ class CommandParser { static const Command commands[]; - sigc::signal<void> finished; + CommandManager commandManager; Net::Connection *connection; - unsigned int activeRequests; - bool disconnect; - bool split(const std::string &str, std::vector<std::string> &ret); const Command* findCommand(const std::string& command); void printUsage(const std::string& command); - void printHostStatus(const Net::Packets::HostStatusPacket &packet); - void helpCommand(const std::vector<std::string> &args); void listHostsCommand(const std::vector<std::string> &args); void rebootCommand(const std::vector<std::string> &args); @@ -77,31 +63,19 @@ class CommandParser { void statusCommand(const std::vector<std::string> &args); void exitCommand(const std::vector<std::string>&); - void coreStatusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request); - void daemonCommandRequestFinished(const Common::Request<> &request); - void daemonListRequestFinished(const Common::Request<Net::Packets::HostListPacket> &request, bool all); - void daemonStatusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request); - void disconnectRequestFinished(const Common::Request<> &request); - - void requestFinished() { - activeRequests--; - - finished(); - } - public: - CommandParser(Net::Connection *connection0) : connection(connection0), activeRequests(0), disconnect(false) {} + CommandParser(Net::Connection *connection0) : connection(connection0) {} + + bool requestsActive() {return commandManager.requestsActive();} + bool willDisconnect() {return commandManager.willDisconnect();} - bool requestsActive() {return (activeRequests > 0);} - bool willDisconnect() {return disconnect;} + sigc::signal<void> signalFinished() const {return commandManager.signalFinished();} bool parse(const std::string &cmd); void requestDisconnect() { exitCommand(std::vector<std::string>()); } - - sigc::signal<void> signalFinished() const {return finished;} }; } diff --git a/src/Client/Makefile.am b/src/Client/Makefile.am index 6ee2248..cfbabd4 100644 --- a/src/Client/Makefile.am +++ b/src/Client/Makefile.am @@ -1,7 +1,7 @@ SUBDIRS = Requests noinst_LTLIBRARIES = libclient.la -libclient_la_SOURCES = CommandParser.cpp +libclient_la_SOURCES = CommandManager.cpp CommandParser.cpp libclient_la_LIBADD = Requests/librequests.la -noinst_HEADERS = CommandParser.h +noinst_HEADERS = CommandManager.h CommandParser.h diff --git a/src/Client/Makefile.in b/src/Client/Makefile.in index 7af2949..b9e2d98 100644 --- a/src/Client/Makefile.in +++ b/src/Client/Makefile.in @@ -48,7 +48,7 @@ CONFIG_HEADER = $(top_builddir)/src/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libclient_la_DEPENDENCIES = Requests/librequests.la -am_libclient_la_OBJECTS = CommandParser.lo +am_libclient_la_OBJECTS = CommandManager.lo CommandParser.lo libclient_la_OBJECTS = $(am_libclient_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -198,9 +198,9 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ SUBDIRS = Requests noinst_LTLIBRARIES = libclient.la -libclient_la_SOURCES = CommandParser.cpp +libclient_la_SOURCES = CommandManager.cpp CommandParser.cpp libclient_la_LIBADD = Requests/librequests.la -noinst_HEADERS = CommandParser.h +noinst_HEADERS = CommandManager.h CommandParser.h all: all-recursive .SUFFIXES: @@ -252,6 +252,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CommandManager.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CommandParser.Plo@am__quote@ .cpp.o: |