diff options
Diffstat (limited to 'src/Client/CommandParser.cpp')
-rw-r--r-- | src/Client/CommandParser.cpp | 64 |
1 files changed, 58 insertions, 6 deletions
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 <Common/Exception.h> @@ -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<std::string> &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<std::string> &args) { activeRequests++; } +void CommandParser::rebootCommand(const std::vector<std::string> &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<Common::RequestBase>( + new Requests::DaemonCommandRequest(args[1], true, sigc::mem_fun(this, &CommandParser::daemonCommandRequestFinished)) + )); + + activeRequests++; +} + +void CommandParser::shutdownCommand(const std::vector<std::string> &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<Common::RequestBase>( + new Requests::DaemonCommandRequest(args[1], false, sigc::mem_fun(this, &CommandParser::daemonCommandRequestFinished)) + )); + + 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)))); @@ -203,7 +244,18 @@ void CommandParser::coreStatusRequestFinished(const Common::Request<Net::Packets printHostStatus(packet); } 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(); +} + +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(); @@ -243,7 +295,7 @@ void CommandParser::daemonListRequestFinished(const Common::Request<Net::Packets } } 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(); @@ -256,7 +308,7 @@ void CommandParser::daemonStatusRequestFinished(const Common::Request<Net::Packe printHostStatus(packet); } 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(); @@ -268,7 +320,7 @@ void CommandParser::disconnectRequestFinished(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; } |