From af0f84058d322d68cbbff98ef272dd2bcd0f692c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 25 Sep 2008 13:36:02 +0200 Subject: Daemon-Host kann jetzt heruntergefahren und neu gestartet werden --- src/Client/CommandParser.cpp | 64 +++++++++++++++++++++++++--- src/Client/CommandParser.h | 3 ++ src/Client/Requests/DaemonCommandRequest.cpp | 47 ++++++++++++++++++++ src/Client/Requests/DaemonCommandRequest.h | 47 ++++++++++++++++++++ src/Client/Requests/DaemonStatusRequest.h | 4 -- src/Client/Requests/Makefile.am | 4 +- src/Client/Requests/Makefile.in | 8 ++-- 7 files changed, 162 insertions(+), 15 deletions(-) create mode 100644 src/Client/Requests/DaemonCommandRequest.cpp create mode 100644 src/Client/Requests/DaemonCommandRequest.h (limited to 'src/Client') diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 87202b4..74b5710 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -19,6 +19,7 @@ #include "CommandParser.h" #include "Requests/CoreStatusRequest.h" +#include "Requests/DaemonCommandRequest.h" #include "Requests/DaemonListRequest.h" #include "Requests/DaemonStatusRequest.h" #include @@ -38,6 +39,8 @@ namespace Client { 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}, {{"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} @@ -135,7 +138,7 @@ void CommandParser::helpCommand(const std::vector &args) { std::cout << command->longdesc << std::endl << std::endl; } else - Common::Logger::logf(Common::Logger::WARNING,"%s: Command '%s' doesn't exist.\n", args[0].c_str(), args[1].c_str()); + Common::Logger::logf(Common::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()); @@ -176,6 +179,44 @@ void CommandParser::listHostsCommand(const std::vector &args) { activeRequests++; } +void CommandParser::rebootCommand(const std::vector &args) { + if(args.size() < 2) { + Common::Logger::logf(Common::Logger::ERROR, "%s: No host given.", args[0].c_str()); + printUsage("reboot"); + return; + } + else if(args.size() > 2) { + Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + printUsage("reboot"); + return; + } + + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr( + new Requests::DaemonCommandRequest(args[1], true, sigc::mem_fun(this, &CommandParser::daemonCommandRequestFinished)) + )); + + activeRequests++; +} + +void CommandParser::shutdownCommand(const std::vector &args) { + if(args.size() < 2) { + Common::Logger::logf(Common::Logger::ERROR, "%s: No host given.", args[0].c_str()); + printUsage("reboot"); + return; + } + else if(args.size() > 2) { + Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + printUsage("reboot"); + return; + } + + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr( + new Requests::DaemonCommandRequest(args[1], false, sigc::mem_fun(this, &CommandParser::daemonCommandRequestFinished)) + )); + + activeRequests++; +} + void CommandParser::statusCommand(const std::vector &args) { if(args.size() == 1) Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr(new Requests::CoreStatusRequest(sigc::mem_fun(this, &CommandParser::coreStatusRequestFinished)))); @@ -203,7 +244,18 @@ void CommandParser::coreStatusRequestFinished(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(); @@ -243,7 +295,7 @@ void CommandParser::daemonListRequestFinished(const Common::Request &request) disconnect = true; } catch(Common::Exception &exception) { - Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.\n", exception.strerror().c_str()); + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); } requestFinished(); @@ -344,7 +396,7 @@ bool CommandParser::parse(const std::string &cmd) { if(command) (this->*command->funcPtr)(splitCmd); else - Common::Logger::logf(Common::Logger::ERROR, "Unknown command '%s'.\n", splitCmd[0].c_str()); + Common::Logger::logf(Common::Logger::ERROR, "Unknown command '%s'.", splitCmd[0].c_str()); return true; } diff --git a/src/Client/CommandParser.h b/src/Client/CommandParser.h index 28abb12..9b94ef0 100644 --- a/src/Client/CommandParser.h +++ b/src/Client/CommandParser.h @@ -72,10 +72,13 @@ class CommandParser { void helpCommand(const std::vector &args); void listHostsCommand(const std::vector &args); + void rebootCommand(const std::vector &args); + void shutdownCommand(const std::vector &args); void statusCommand(const std::vector &args); void exitCommand(const std::vector&); 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/Requests/DaemonCommandRequest.cpp b/src/Client/Requests/DaemonCommandRequest.cpp new file mode 100644 index 0000000..d27c3fb --- /dev/null +++ b/src/Client/Requests/DaemonCommandRequest.cpp @@ -0,0 +1,47 @@ +/* + * DaemonCommandRequest.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 "DaemonCommandRequest.h" +#include +#include + +namespace Mad { +namespace Client { +namespace Requests { + +void DaemonCommandRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { + connection->send(Net::Packet(reboot ? Net::Packet::DAEMON_COMMAND_REBOOT : Net::Packet::DAEMON_COMMAND_SHUTDOWN, requestId, daemon.c_str(), daemon.length())); +} + +void DaemonCommandRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { + if(packet.getType() == Net::Packet::ERROR) { + finishWithError(Net::Packets::ErrorPacket(packet).getException()); + return; + } + else if(packet.getType() != Net::Packet::OK) { + finishWithError(Common::Exception(Common::Exception::UNEXPECTED_PACKET)); + return; // TODO Logging + } + + finish(); +} + +} +} +} diff --git a/src/Client/Requests/DaemonCommandRequest.h b/src/Client/Requests/DaemonCommandRequest.h new file mode 100644 index 0000000..439462f --- /dev/null +++ b/src/Client/Requests/DaemonCommandRequest.h @@ -0,0 +1,47 @@ +/* + * DaemonCommandRequest.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_REQUEST_DAEMONCOMMANDREQUEST_H_ +#define MAD_CLIENT_REQUEST_DAEMONCOMMANDREQUEST_H_ + +#include +#include + +namespace Mad { +namespace Client { +namespace Requests { + +class DaemonCommandRequest : public Common::Request<> { + private: + std::string daemon; + bool reboot; + + protected: + virtual void sendRequest(Net::Connection *connection, uint16_t requestId); + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + + public: + DaemonCommandRequest(const std::string &daemon0, bool reboot0, slot_type slot) : Common::Request<>(slot), daemon(daemon0), reboot(reboot0) {} +}; + +} +} +} + +#endif /* MAD_CLIENT_REQUEST_DAEMONCOMMANDREQUEST_H_ */ diff --git a/src/Client/Requests/DaemonStatusRequest.h b/src/Client/Requests/DaemonStatusRequest.h index 8f74ca4..cd71dfb 100644 --- a/src/Client/Requests/DaemonStatusRequest.h +++ b/src/Client/Requests/DaemonStatusRequest.h @@ -26,10 +26,6 @@ namespace Mad { -namespace Common { -class Exception; -} - namespace Net { namespace Packets { class HostStatusPacket; diff --git a/src/Client/Requests/Makefile.am b/src/Client/Requests/Makefile.am index e8e72d4..8c4f542 100644 --- a/src/Client/Requests/Makefile.am +++ b/src/Client/Requests/Makefile.am @@ -1,4 +1,4 @@ noinst_LTLIBRARIES = librequests.la -librequests_la_SOURCES = CoreStatusRequest.cpp DaemonListRequest.cpp DaemonStatusRequest.cpp +librequests_la_SOURCES = CoreStatusRequest.cpp DaemonCommandRequest.cpp DaemonListRequest.cpp DaemonStatusRequest.cpp -noinst_HEADERS = CoreStatusRequest.h DaemonListRequest.h DaemonStatusRequest.h +noinst_HEADERS = CoreStatusRequest.h DaemonCommandRequest.h DaemonListRequest.h DaemonStatusRequest.h diff --git a/src/Client/Requests/Makefile.in b/src/Client/Requests/Makefile.in index bb9a837..9336dbb 100644 --- a/src/Client/Requests/Makefile.in +++ b/src/Client/Requests/Makefile.in @@ -48,7 +48,8 @@ CONFIG_HEADER = $(top_builddir)/src/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) librequests_la_LIBADD = -am_librequests_la_OBJECTS = CoreStatusRequest.lo DaemonListRequest.lo \ +am_librequests_la_OBJECTS = CoreStatusRequest.lo \ + DaemonCommandRequest.lo DaemonListRequest.lo \ DaemonStatusRequest.lo librequests_la_OBJECTS = $(am_librequests_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src @@ -188,8 +189,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = librequests.la -librequests_la_SOURCES = CoreStatusRequest.cpp DaemonListRequest.cpp DaemonStatusRequest.cpp -noinst_HEADERS = CoreStatusRequest.h DaemonListRequest.h DaemonStatusRequest.h +librequests_la_SOURCES = CoreStatusRequest.cpp DaemonCommandRequest.cpp DaemonListRequest.cpp DaemonStatusRequest.cpp +noinst_HEADERS = CoreStatusRequest.h DaemonCommandRequest.h DaemonListRequest.h DaemonStatusRequest.h all: all-am .SUFFIXES: @@ -242,6 +243,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CoreStatusRequest.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonCommandRequest.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonListRequest.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonStatusRequest.Plo@am__quote@ -- cgit v1.2.3