From 3086f10f53ced17ab4a237ab57da62395d259f0a Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 29 Sep 2008 18:16:20 +0200 Subject: InformationManager zur Verwaltung der Host-Liste hinzugef?gt --- src/Client/CommandParser.cpp | 2 +- src/Client/InformationManager.cpp | 56 +++++++++++++++++++++ src/Client/InformationManager.h | 78 +++++++++++++++++++++++++++++ src/Client/Makefile.am | 4 +- src/Client/Makefile.in | 8 +-- src/Client/Requests/CoreStatusRequest.cpp | 1 - src/Client/Requests/CoreStatusRequest.h | 8 +-- src/Client/Requests/DaemonListRequest.cpp | 1 - src/Client/Requests/DaemonListRequest.h | 8 +-- src/Client/Requests/DaemonStatusRequest.cpp | 1 - src/Client/Requests/DaemonStatusRequest.h | 8 +-- 11 files changed, 145 insertions(+), 30 deletions(-) create mode 100644 src/Client/InformationManager.cpp create mode 100644 src/Client/InformationManager.h (limited to 'src/Client') diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 1a5a96c..663c214 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -40,7 +40,7 @@ const CommandParser::Command CommandParser::commands[] = { {{"help", "?", 0}, "help [command]", "Displays usage information about commands", "Displays usage information about a command. If no command is given, a list of all available commands is displayed.", &CommandParser::helpCommand}, {{"list_hosts", "hosts", 0}, "list_hosts [-a]", "Lists the currently active hosts", "Lists the currently active hosts.\n\n -a\tAlso list inactive hosts", &CommandParser::listHostsCommand}, {{"reboot", 0}, "reboot host", "Reboots a host", "Reboots a host.", &CommandParser::rebootCommand}, - {{"shutdown", 0}, "shutdown host", "Shuts a host down", "Shuts a host down.", &CommandParser::shutdownCommand}, + {{"shutdown", "halt", 0}, "shutdown host", "Shuts a host down", "Shuts a host down.", &CommandParser::shutdownCommand}, {{"status", "st", 0}, "status [host]", "Displays status information", "Displays host status information. If no host is given, server status information is displayed.", &CommandParser::statusCommand}, {{"exit", "quit", 0}, "exit", "Closes the connection and quits the client", "Closes the connection and quits the client.", &CommandParser::exitCommand}, {{0}, 0, 0, 0, 0} diff --git a/src/Client/InformationManager.cpp b/src/Client/InformationManager.cpp new file mode 100644 index 0000000..3a8caab --- /dev/null +++ b/src/Client/InformationManager.cpp @@ -0,0 +1,56 @@ +/* + * InformationManager.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 "InformationManager.h" +#include "Requests/DaemonListRequest.h" +#include +#include + + +namespace Mad { +namespace Client { + +std::auto_ptr InformationManager::informationManager; + +InformationManager::InformationManager(Net::Connection *connection) : initFinished(false) { + Common::RequestManager::getRequestManager()->sendRequest(connection, + std::auto_ptr( + new Requests::DaemonListRequest(sigc::mem_fun(this, &InformationManager::daemonListRequestFinished)) + ) + ); +} + +void InformationManager::daemonListRequestFinished(const Common::Request &request) { + try { + const std::vector &hostInfo = request.getResult().getHostInfo(); + + for(std::vector::const_iterator daemon = hostInfo.begin(); daemon != hostInfo.end(); ++daemon) { + daemons.clear(); + daemons.insert(std::make_pair(daemon->getName(), *daemon)); + } + + initFinished = true; + } + catch(Common::Exception &e) { + Common::Logger::logf(Common::Logger::CRITICAL, "Host list request failed: %s", e.strerror().c_str()); + } +} + +} +} diff --git a/src/Client/InformationManager.h b/src/Client/InformationManager.h new file mode 100644 index 0000000..5670f8f --- /dev/null +++ b/src/Client/InformationManager.h @@ -0,0 +1,78 @@ +/* + * InformationManager.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_CLIENT_INFORMATIONMANAGER_H_ +#define MAD_CLIENT_INFORMATIONMANAGER_H_ + +#include +#include + +#include +#include + +namespace Mad { + +namespace Net { +class Connection; + +namespace Packets { +class HostListPacket; +} + +} + +namespace Client { + +class InformationManager { + private: + static std::auto_ptr informationManager; + + std::map daemons; + bool initFinished; + + // Prevent shallow copy + InformationManager(const InformationManager &o); + InformationManager& operator=(const InformationManager &o); + + InformationManager(Net::Connection *connection); + + void daemonListRequestFinished(const Common::Request &request); + + public: + static InformationManager* getInformationManager() { + return informationManager.get(); + } + + static void init(Net::Connection *connection) { + informationManager = std::auto_ptr(new InformationManager(connection)); + } + + bool isInitialised() const { + return initFinished; + } + + const std::map& getDaemons() const { + return daemons; + } +}; + +} +} + +#endif /* MAD_CLIENT_INFORMATIONMANAGER_H_ */ diff --git a/src/Client/Makefile.am b/src/Client/Makefile.am index cfbabd4..5de3bc6 100644 --- a/src/Client/Makefile.am +++ b/src/Client/Makefile.am @@ -1,7 +1,7 @@ SUBDIRS = Requests noinst_LTLIBRARIES = libclient.la -libclient_la_SOURCES = CommandManager.cpp CommandParser.cpp +libclient_la_SOURCES = CommandManager.cpp CommandParser.cpp InformationManager.cpp libclient_la_LIBADD = Requests/librequests.la -noinst_HEADERS = CommandManager.h CommandParser.h +noinst_HEADERS = CommandManager.h CommandParser.h InformationManager.h diff --git a/src/Client/Makefile.in b/src/Client/Makefile.in index b9e2d98..8d6b09e 100644 --- a/src/Client/Makefile.in +++ b/src/Client/Makefile.in @@ -48,7 +48,8 @@ CONFIG_HEADER = $(top_builddir)/src/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libclient_la_DEPENDENCIES = Requests/librequests.la -am_libclient_la_OBJECTS = CommandManager.lo CommandParser.lo +am_libclient_la_OBJECTS = CommandManager.lo CommandParser.lo \ + InformationManager.lo libclient_la_OBJECTS = $(am_libclient_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -198,9 +199,9 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ SUBDIRS = Requests noinst_LTLIBRARIES = libclient.la -libclient_la_SOURCES = CommandManager.cpp CommandParser.cpp +libclient_la_SOURCES = CommandManager.cpp CommandParser.cpp InformationManager.cpp libclient_la_LIBADD = Requests/librequests.la -noinst_HEADERS = CommandManager.h CommandParser.h +noinst_HEADERS = CommandManager.h CommandParser.h InformationManager.h all: all-recursive .SUFFIXES: @@ -254,6 +255,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CommandManager.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CommandParser.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/InformationManager.Plo@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/src/Client/Requests/CoreStatusRequest.cpp b/src/Client/Requests/CoreStatusRequest.cpp index 35a63e2..69e3ecf 100644 --- a/src/Client/Requests/CoreStatusRequest.cpp +++ b/src/Client/Requests/CoreStatusRequest.cpp @@ -19,7 +19,6 @@ #include "CoreStatusRequest.h" #include -#include namespace Mad { namespace Client { diff --git a/src/Client/Requests/CoreStatusRequest.h b/src/Client/Requests/CoreStatusRequest.h index 94aff9c..f57f4bf 100644 --- a/src/Client/Requests/CoreStatusRequest.h +++ b/src/Client/Requests/CoreStatusRequest.h @@ -21,15 +21,9 @@ #define MAD_CLIENT_REQUESTS_CORESTATUSREQUEST_H_ #include +#include namespace Mad { - -namespace Net { -namespace Packets { -class HostStatusPacket; -} -} - namespace Client { namespace Requests { diff --git a/src/Client/Requests/DaemonListRequest.cpp b/src/Client/Requests/DaemonListRequest.cpp index 279d19c..690aaea 100644 --- a/src/Client/Requests/DaemonListRequest.cpp +++ b/src/Client/Requests/DaemonListRequest.cpp @@ -19,7 +19,6 @@ #include "DaemonListRequest.h" #include -#include namespace Mad { namespace Client { diff --git a/src/Client/Requests/DaemonListRequest.h b/src/Client/Requests/DaemonListRequest.h index 625e430..fbf0e70 100644 --- a/src/Client/Requests/DaemonListRequest.h +++ b/src/Client/Requests/DaemonListRequest.h @@ -21,15 +21,9 @@ #define MAD_CLIENT_REQUEST_DAEMONLISTREQUEST_H_ #include +#include namespace Mad { - -namespace Net { -namespace Packets { -class HostListPacket; -} -} - namespace Client { namespace Requests { diff --git a/src/Client/Requests/DaemonStatusRequest.cpp b/src/Client/Requests/DaemonStatusRequest.cpp index 9238ed6..94407b4 100644 --- a/src/Client/Requests/DaemonStatusRequest.cpp +++ b/src/Client/Requests/DaemonStatusRequest.cpp @@ -20,7 +20,6 @@ #include "DaemonStatusRequest.h" #include #include -#include namespace Mad { namespace Client { diff --git a/src/Client/Requests/DaemonStatusRequest.h b/src/Client/Requests/DaemonStatusRequest.h index cd71dfb..3190ca1 100644 --- a/src/Client/Requests/DaemonStatusRequest.h +++ b/src/Client/Requests/DaemonStatusRequest.h @@ -21,17 +21,11 @@ #define MAD_CLIENT_REQUESTS_DAEMONSTATUSREQUEST_H_ #include +#include #include namespace Mad { - -namespace Net { -namespace Packets { -class HostStatusPacket; -} -} - namespace Client { namespace Requests { -- cgit v1.2.3 From d803fa64b8ecc94425043b4551c79273b6fee11e Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 29 Sep 2008 19:26:02 +0200 Subject: Benutzte InformationManager zur Auflistung der Hosts --- src/Client/CommandManager.cpp | 40 ---------------------------------- src/Client/CommandManager.h | 1 - src/Client/CommandParser.cpp | 50 +++++++++++++++++++++++++++---------------- 3 files changed, 31 insertions(+), 60 deletions(-) (limited to 'src/Client') diff --git a/src/Client/CommandManager.cpp b/src/Client/CommandManager.cpp index 8a82a82..2a20b29 100644 --- a/src/Client/CommandManager.cpp +++ b/src/Client/CommandManager.cpp @@ -106,46 +106,6 @@ void CommandManager::daemonCommandRequestFinished(const Common::Request<> &reque requestFinished(); } -void CommandManager::daemonListRequestFinished(const Common::Request &request, bool all) { - try { - const std::vector& 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::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 &request) { try { const Net::Packets::HostStatusPacket &packet = request.getResult(); diff --git a/src/Client/CommandManager.h b/src/Client/CommandManager.h index 6aea724..f93d07f 100644 --- a/src/Client/CommandManager.h +++ b/src/Client/CommandManager.h @@ -53,7 +53,6 @@ class CommandManager { void coreStatusRequestFinished(const Common::Request &request); void daemonCommandRequestFinished(const Common::Request<> &request); - void daemonListRequestFinished(const Common::Request &request, bool all); void daemonStatusRequestFinished(const Common::Request &request); void disconnectRequestFinished(const Common::Request<> &request); diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 663c214..4f1cb99 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -18,9 +18,9 @@ */ #include "CommandParser.h" +#include "InformationManager.h" #include "Requests/CoreStatusRequest.h" #include "Requests/DaemonCommandRequest.h" -#include "Requests/DaemonListRequest.h" #include "Requests/DaemonStatusRequest.h" #include #include @@ -95,36 +95,48 @@ void CommandParser::helpCommand(const std::vector &args) { } void CommandParser::listHostsCommand(const std::vector &args) { + const std::map& hosts = InformationManager::getInformationManager()->getDaemons(); + if(args.size() == 1) { - Common::RequestManager::getRequestManager()->sendRequest(connection, - std::auto_ptr( - new Requests::DaemonListRequest( - sigc::bind(sigc::mem_fun(commandManager, &CommandManager::daemonListRequestFinished), false) - ) - ) - ); + if(hosts.empty()) { + std::cout << "The host list is empty." << std::endl << std::endl; + return; + } + + bool output = false; + + for(std::map::const_iterator host = hosts.begin(); host != hosts.end(); ++host) { + if(host->second.getStatus() == Common::HostInfo::INACTIVE) + continue; + + if(!output) { + std::cout << "Active hosts:" << std::endl; + output = true; + } + + std::cout << " " << host->first << std::endl; + } } else if(args.size() > 2) { Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); printUsage("list_hosts"); - return; } else if(args[1] == "-a") { - Common::RequestManager::getRequestManager()->sendRequest(connection, - std::auto_ptr( - new Requests::DaemonListRequest( - sigc::bind(sigc::mem_fun(commandManager, &CommandManager::daemonListRequestFinished), true) - ) - ) - ); + if(hosts.empty()) { + std::cout << "The host list is empty." << std::endl << std::endl; + return; + } + + std::cout << "Host list:" << std::endl; + + for(std::map::const_iterator host = hosts.begin(); host != hosts.end(); ++host) { + std::cout << " " << host->first << " (" << (host->second.getStatus() == Common::HostInfo::RUNNING ? "running" : "inactive") << ")" << std::endl; + } } else { Common::Logger::logf(Common::Logger::ERROR, "%s: Don't unterstand argument '%s'.", args[0].c_str(), args[1].c_str()); printUsage("list_hosts"); - return; } - - commandManager.activeRequests++; } void CommandParser::rebootCommand(const std::vector &args) { -- cgit v1.2.3 From cc617dba41d5acfc43552847cdd747e7ea8c67da Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 29 Sep 2008 20:11:24 +0200 Subject: Schreibfehler korrigiert --- src/Client/CommandParser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Client') diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 4f1cb99..a271a7e 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -134,7 +134,7 @@ void CommandParser::listHostsCommand(const std::vector &args) { } } else { - Common::Logger::logf(Common::Logger::ERROR, "%s: Don't unterstand argument '%s'.", args[0].c_str(), args[1].c_str()); + Common::Logger::logf(Common::Logger::ERROR, "%s: Don't understand argument '%s'.", args[0].c_str(), args[1].c_str()); printUsage("list_hosts"); } } -- cgit v1.2.3 From 07768a871691433e2f3c490aa14c45cde40e00b4 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 29 Sep 2008 20:28:26 +0200 Subject: Status in HostInfo hei?t jetzt State --- src/Client/CommandParser.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Client') diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index a271a7e..bc179fa 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -106,7 +106,7 @@ void CommandParser::listHostsCommand(const std::vector &args) { bool output = false; for(std::map::const_iterator host = hosts.begin(); host != hosts.end(); ++host) { - if(host->second.getStatus() == Common::HostInfo::INACTIVE) + if(host->second.getState() == Common::HostInfo::INACTIVE) continue; if(!output) { @@ -130,7 +130,7 @@ void CommandParser::listHostsCommand(const std::vector &args) { std::cout << "Host list:" << std::endl; for(std::map::const_iterator host = hosts.begin(); host != hosts.end(); ++host) { - std::cout << " " << host->first << " (" << (host->second.getStatus() == Common::HostInfo::RUNNING ? "running" : "inactive") << ")" << std::endl; + std::cout << " " << host->first << " (" << (host->second.getState() == Common::HostInfo::RUNNING ? "running" : "inactive") << ")" << std::endl; } } else { -- cgit v1.2.3 From 13fd1bb4f19e4791e000cb71cca2065820184bdb Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 29 Sep 2008 22:25:04 +0200 Subject: Daemon-Liste wird jetzt vom Core aktualisiert --- src/Client/CommandParser.cpp | 7 +++++++ src/Client/InformationManager.cpp | 34 ++++++++++++++++++++++++++++++++++ src/Client/InformationManager.h | 10 ++++++++++ 3 files changed, 51 insertions(+) (limited to 'src/Client') diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index bc179fa..1ba986f 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -116,6 +116,11 @@ void CommandParser::listHostsCommand(const std::vector &args) { std::cout << " " << host->first << std::endl; } + + if(!output) + std::cout << "No active hosts." << std::endl; + + std::cout << std::endl; } else if(args.size() > 2) { Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); @@ -132,6 +137,8 @@ void CommandParser::listHostsCommand(const std::vector &args) { for(std::map::const_iterator host = hosts.begin(); host != hosts.end(); ++host) { std::cout << " " << host->first << " (" << (host->second.getState() == Common::HostInfo::RUNNING ? "running" : "inactive") << ")" << std::endl; } + + 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()); diff --git a/src/Client/InformationManager.cpp b/src/Client/InformationManager.cpp index 3a8caab..8a3227c 100644 --- a/src/Client/InformationManager.cpp +++ b/src/Client/InformationManager.cpp @@ -21,6 +21,8 @@ #include "Requests/DaemonListRequest.h" #include #include +#include +#include namespace Mad { @@ -28,12 +30,44 @@ namespace Client { std::auto_ptr InformationManager::informationManager; + +void InformationManager::DaemonStateUpdateRequest::handlePacket(Net::Connection *connection, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::DAEMON_STATE_UPDATE) { + Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); + connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::UNEXPECTED_PACKET))); + + signalFinished().emit(); + return; + } + + // TODO Require authentication + + Net::Packets::HostStatePacket hostStatePacket(packet); + + std::map::iterator host = informationManager->daemons.find(hostStatePacket.getName()); + if(host != informationManager->daemons.end()) + host->second.setState(hostStatePacket.getState()); + else + Common::Logger::log(Common::Logger::WARNING, "Received a state update for an unknown host."); + + connection->send(Net::Packet(Net::Packet::OK, packet.getRequestId())); + + signalFinished().emit(); +} + + InformationManager::InformationManager(Net::Connection *connection) : initFinished(false) { Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr( new Requests::DaemonListRequest(sigc::mem_fun(this, &InformationManager::daemonListRequestFinished)) ) ); + + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_STATE_UPDATE); +} + +InformationManager::~InformationManager() { + Common::RequestManager::getRequestManager()->unregisterPacketType(Net::Packet::DAEMON_STATE_UPDATE); } void InformationManager::daemonListRequestFinished(const Common::Request &request) { diff --git a/src/Client/InformationManager.h b/src/Client/InformationManager.h index 5670f8f..bf2d67a 100644 --- a/src/Client/InformationManager.h +++ b/src/Client/InformationManager.h @@ -41,6 +41,14 @@ namespace Client { class InformationManager { private: + class DaemonStateUpdateRequest : public Common::RequestHandler { + protected: + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + + public: + DaemonStateUpdateRequest() {} + }; + static std::auto_ptr informationManager; std::map daemons; @@ -55,6 +63,8 @@ class InformationManager { void daemonListRequestFinished(const Common::Request &request); public: + ~InformationManager(); + static InformationManager* getInformationManager() { return informationManager.get(); } -- cgit v1.2.3