From 58c5d4eefdf1cdee0651f7c74ffd1501adbdc9c3 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 8 Oct 2008 23:08:21 +0200 Subject: fsinfo-Befehl implementiert --- src/Client/CommandManager.cpp | 83 +++++++++++++++++++-- src/Client/CommandManager.h | 6 +- src/Client/CommandParser.cpp | 21 +++++- src/Client/CommandParser.h | 1 + src/Client/Requests/CoreStatusRequest.cpp | 42 ----------- src/Client/Requests/CoreStatusRequest.h | 43 ----------- src/Client/Requests/DaemonFSInfoRequest.cpp | 47 ++++++++++++ src/Client/Requests/DaemonFSInfoRequest.h | 48 ++++++++++++ src/Client/Requests/Makefile.am | 4 +- src/Client/Requests/Makefile.in | 10 +-- src/Common/Backends/SystemBackendPosix.cpp | 31 +++++++- .../RequestHandlers/FSInfoRequestHandler.cpp | 57 ++++++++++++++ src/Common/RequestHandlers/FSInfoRequestHandler.h | 50 +++++++++++++ src/Common/RequestHandlers/Makefile.am | 4 +- src/Common/RequestHandlers/Makefile.in | 7 +- src/Common/Requests/FSInfoRequest.cpp | 45 +++++++++++ src/Common/Requests/FSInfoRequest.h | 43 +++++++++++ src/Common/Requests/Makefile.am | 4 +- src/Common/Requests/Makefile.in | 9 ++- src/Common/Requests/StatusRequest.cpp | 42 +++++++++++ src/Common/Requests/StatusRequest.h | 43 +++++++++++ src/Core/ConnectionManager.cpp | 4 + .../RequestHandlers/DaemonFSInfoRequestHandler.cpp | 71 ++++++++++++++++++ .../RequestHandlers/DaemonFSInfoRequestHandler.h | 56 ++++++++++++++ .../RequestHandlers/DaemonStatusRequestHandler.cpp | 4 +- .../RequestHandlers/DaemonStatusRequestHandler.h | 3 - src/Core/RequestHandlers/Makefile.am | 6 +- src/Core/RequestHandlers/Makefile.in | 15 ++-- src/Core/Requests/DaemonStatusRequest.cpp | 42 ----------- src/Core/Requests/DaemonStatusRequest.h | 43 ----------- src/Core/Requests/Makefile.am | 4 +- src/Core/Requests/Makefile.in | 7 +- src/Net/Connection.cpp | 2 + src/Net/Packet.cpp | 24 ++++++ src/Net/Packet.h | 6 +- src/Net/Packets/FSInfoPacket.cpp | 87 ++++++++++++++++++++++ src/Net/Packets/FSInfoPacket.h | 81 ++++++++++++++++++++ src/Net/Packets/HostListPacket.cpp | 10 +-- src/Net/Packets/Makefile.am | 4 +- src/Net/Packets/Makefile.in | 10 ++- src/mad.cpp | 2 + 41 files changed, 890 insertions(+), 231 deletions(-) delete mode 100644 src/Client/Requests/CoreStatusRequest.cpp delete mode 100644 src/Client/Requests/CoreStatusRequest.h create mode 100644 src/Client/Requests/DaemonFSInfoRequest.cpp create mode 100644 src/Client/Requests/DaemonFSInfoRequest.h create mode 100644 src/Common/RequestHandlers/FSInfoRequestHandler.cpp create mode 100644 src/Common/RequestHandlers/FSInfoRequestHandler.h create mode 100644 src/Common/Requests/FSInfoRequest.cpp create mode 100644 src/Common/Requests/FSInfoRequest.h create mode 100644 src/Common/Requests/StatusRequest.cpp create mode 100644 src/Common/Requests/StatusRequest.h create mode 100644 src/Core/RequestHandlers/DaemonFSInfoRequestHandler.cpp create mode 100644 src/Core/RequestHandlers/DaemonFSInfoRequestHandler.h delete mode 100644 src/Core/Requests/DaemonStatusRequest.cpp delete mode 100644 src/Core/Requests/DaemonStatusRequest.h create mode 100644 src/Net/Packets/FSInfoPacket.cpp create mode 100644 src/Net/Packets/FSInfoPacket.h (limited to 'src') diff --git a/src/Client/CommandManager.cpp b/src/Client/CommandManager.cpp index 2a20b29..08a2780 100644 --- a/src/Client/CommandManager.cpp +++ b/src/Client/CommandManager.cpp @@ -20,9 +20,11 @@ #include "CommandManager.h" #include #include +#include #include #include +#include #include #include @@ -30,6 +32,48 @@ namespace Mad { namespace Client { +void CommandManager::printFSInfo(const Net::Packets::FSInfoPacket &packet) { + const std::string units[] = { + "kB", "MB", "GB", "TB", "" + }; + + const std::vector& fsList = packet.getFSInfo(); + + for(std::vector::const_iterator fs = fsList.begin(); fs != fsList.end(); ++fs) { + unsigned usedUnit = 0, totalUnit = 0; + + float used = fs->used; + float total = fs->total; + float available = fs->available; + + while(used >= 1024 && !units[usedUnit+1].empty()) { + ++usedUnit; + used /= 1024; + available /= 1024; + } + + while(total >= 1024 && !units[totalUnit+1].empty()) { + ++totalUnit; + total /= 1024; + } + + std::string nameString = fs->mountedOn + " (" + fs->fsName + ")"; + + if(nameString.length() < 32) { + nameString.resize(32, ' '); + } + else { + nameString += '\n'; + nameString.resize(nameString.length() + 32, ' '); + } + + std::printf("\t%s%.*f%s", nameString.c_str(), (used < 10) ? 2 : 1, used, (usedUnit == totalUnit) ? "" : (" " + units[usedUnit]).c_str()); + std::printf("/%.*f %s (%.1f%%)\n", (total < 10) ? 2 : 1, total, units[totalUnit].c_str(), std::min(100*used/(used+available), 100.0f)); + } + + std::printf("\n"); +} + void CommandManager::printHostStatus(const Net::Packets::HostStatusPacket &packet) { if(packet.getUptime()) { unsigned long days = packet.getUptime()/86400; @@ -82,11 +126,10 @@ void CommandManager::printHostStatus(const Net::Packets::HostStatusPacket &packe } } -void CommandManager::coreStatusRequestFinished(const Common::Request &request) { + +void CommandManager::daemonCommandRequestFinished(const Common::Request<> &request) { try { - const Net::Packets::HostStatusPacket &packet = request.getResult(); - std::cout << "Server status:" << std::endl; - printHostStatus(packet); + request.getResult(); } catch(Common::Exception &exception) { Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); @@ -95,9 +138,11 @@ void CommandManager::coreStatusRequestFinished(const Common::Request &request) { +void CommandManager::daemonFSInfoRequestFinished(const Common::Request &request) { try { - request.getResult(); + const Net::Packets::FSInfoPacket &packet = request.getResult(); + std::cout << "Host file system usage:" << std::endl; + printFSInfo(packet); } catch(Common::Exception &exception) { Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); @@ -131,5 +176,31 @@ void CommandManager::disconnectRequestFinished(const Common::Request<> &request) requestFinished(); } +void CommandManager::fsInfoRequestFinished(const Common::Request &request) { + try { + const Net::Packets::FSInfoPacket &packet = request.getResult(); + std::cout << "Server file system usage:" << std::endl; + printFSInfo(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::statusRequestFinished(const Common::Request &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(); +} + } } diff --git a/src/Client/CommandManager.h b/src/Client/CommandManager.h index f93d07f..1937c0f 100644 --- a/src/Client/CommandManager.h +++ b/src/Client/CommandManager.h @@ -26,6 +26,7 @@ namespace Mad { namespace Net { namespace Packets { +class FSInfoPacket; class HostStatusPacket; class HostListPacket; } @@ -49,12 +50,15 @@ class CommandManager { finished(); } + void printFSInfo(const Net::Packets::FSInfoPacket &packet); void printHostStatus(const Net::Packets::HostStatusPacket &packet); - void coreStatusRequestFinished(const Common::Request &request); void daemonCommandRequestFinished(const Common::Request<> &request); + void daemonFSInfoRequestFinished(const Common::Request &request); void daemonStatusRequestFinished(const Common::Request &request); void disconnectRequestFinished(const Common::Request<> &request); + void fsInfoRequestFinished(const Common::Request &request); + void statusRequestFinished(const Common::Request &request); public: CommandManager() : activeRequests(0), disconnect(false) {} diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 135f771..ab3dd50 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -19,13 +19,15 @@ #include "CommandParser.h" #include "InformationManager.h" -#include "Requests/CoreStatusRequest.h" +#include "Requests/DaemonFSInfoRequest.h" #include "Requests/DaemonCommandRequest.h" #include "Requests/DaemonStatusRequest.h" #include #include #include +#include #include +#include #include #include @@ -37,6 +39,7 @@ namespace Mad { namespace Client { const CommandParser::Command CommandParser::commands[] = { + {{"fsinfo", 0}, "fsinfo [host]", "Displays file system usage information", "Displays file system usage information of a host or the server (if no host is given).", &CommandParser::fsinfoCommand}, {{"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 host", "Reboots hosts. * will reboot all hosts.", &CommandParser::rebootCommand}, @@ -102,6 +105,20 @@ std::map CommandParser::parseHostList(const std:: } +void CommandParser::fsinfoCommand(const std::vector &args) { + if(args.size() == 1) + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr(new Common::Requests::FSInfoRequest(sigc::mem_fun(commandManager, &CommandManager::fsInfoRequestFinished)))); + else if(args.size() == 2) + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr(new Requests::DaemonFSInfoRequest(args[1], sigc::mem_fun(commandManager, &CommandManager::daemonFSInfoRequestFinished)))); + else { + Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + printUsage("fsinfo"); + return; + } + + commandManager.activeRequests++; +} + void CommandParser::helpCommand(const std::vector &args) { if(args.size() == 1) { std::cout << "Available commands:" << std::endl << std::endl; @@ -221,7 +238,7 @@ void CommandParser::shutdownCommand(const std::vector &args) { 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(commandManager, &CommandManager::coreStatusRequestFinished)))); + Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr(new Common::Requests::StatusRequest(sigc::mem_fun(commandManager, &CommandManager::statusRequestFinished)))); else if(args.size() == 2) Common::RequestManager::getRequestManager()->sendRequest(connection, std::auto_ptr(new Requests::DaemonStatusRequest(args[1], sigc::mem_fun(commandManager, &CommandManager::daemonStatusRequestFinished)))); else { diff --git a/src/Client/CommandParser.h b/src/Client/CommandParser.h index daca0b9..660f548 100644 --- a/src/Client/CommandParser.h +++ b/src/Client/CommandParser.h @@ -60,6 +60,7 @@ class CommandParser { std::map parseHostList(const std::vector &args, bool mustBeActive = false); + void fsinfoCommand(const std::vector &args); void helpCommand(const std::vector &args); void listHostsCommand(const std::vector &args); void rebootCommand(const std::vector &args); diff --git a/src/Client/Requests/CoreStatusRequest.cpp b/src/Client/Requests/CoreStatusRequest.cpp deleted file mode 100644 index 69e3ecf..0000000 --- a/src/Client/Requests/CoreStatusRequest.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/* - * CoreStatusRequest.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 "CoreStatusRequest.h" -#include - -namespace Mad { -namespace Client { -namespace Requests { - -void CoreStatusRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(Net::Packet::STATUS, requestId)); -} - -void CoreStatusRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) { - finishWithError(Common::Exception(Common::Exception::UNEXPECTED_PACKET)); - return; // TODO Logging - } - - finish(Net::Packets::HostStatusPacket(packet)); -} - -} -} -} diff --git a/src/Client/Requests/CoreStatusRequest.h b/src/Client/Requests/CoreStatusRequest.h deleted file mode 100644 index f57f4bf..0000000 --- a/src/Client/Requests/CoreStatusRequest.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * CoreStatusRequest.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_REQUESTS_CORESTATUSREQUEST_H_ -#define MAD_CLIENT_REQUESTS_CORESTATUSREQUEST_H_ - -#include -#include - -namespace Mad { -namespace Client { -namespace Requests { - -class CoreStatusRequest : public Common::Request { - protected: - virtual void sendRequest(Net::Connection *connection, uint16_t requestId); - virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); - - public: - CoreStatusRequest(slot_type slot) : Common::Request(slot) {} -}; - -} -} -} - -#endif /* MAD_CLIENT_REQUESTS_CORESTATUSREQUEST_H_ */ diff --git a/src/Client/Requests/DaemonFSInfoRequest.cpp b/src/Client/Requests/DaemonFSInfoRequest.cpp new file mode 100644 index 0000000..eb26cf4 --- /dev/null +++ b/src/Client/Requests/DaemonFSInfoRequest.cpp @@ -0,0 +1,47 @@ +/* + * DaemonFSInfoRequest.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 "DaemonFSInfoRequest.h" +#include +#include + +namespace Mad { +namespace Client { +namespace Requests { + +void DaemonFSInfoRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { + connection->send(Net::Packet(Net::Packet::DAEMON_FS_INFO, requestId, daemon.c_str(), daemon.length())); +} + +void DaemonFSInfoRequest::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(Net::Packets::FSInfoPacket(packet)); +} + +} +} +} diff --git a/src/Client/Requests/DaemonFSInfoRequest.h b/src/Client/Requests/DaemonFSInfoRequest.h new file mode 100644 index 0000000..d966b8d --- /dev/null +++ b/src/Client/Requests/DaemonFSInfoRequest.h @@ -0,0 +1,48 @@ +/* + * DaemonFSInfoRequest.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_REQUESTS_DAEMONFSINFOREQUEST_H_ +#define MAD_CLIENT_REQUESTS_DAEMONFSINFOREQUEST_H_ + +#include +#include + +#include + +namespace Mad { +namespace Client { +namespace Requests { + +class DaemonFSInfoRequest : public Common::Request { + private: + std::string daemon; + + protected: + virtual void sendRequest(Net::Connection *connection, uint16_t requestId); + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + + public: + DaemonFSInfoRequest(const std::string &daemon0, slot_type slot) : Common::Request(slot), daemon(daemon0) {} +}; + +} +} +} + +#endif /* MAD_CLIENT_REQUESTS_DAEMONFSINFOREQUEST_H_ */ diff --git a/src/Client/Requests/Makefile.am b/src/Client/Requests/Makefile.am index 8c4f542..a96b265 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 DaemonCommandRequest.cpp DaemonListRequest.cpp DaemonStatusRequest.cpp +librequests_la_SOURCES = DaemonCommandRequest.cpp DaemonFSInfoRequest.cpp DaemonListRequest.cpp DaemonStatusRequest.cpp -noinst_HEADERS = CoreStatusRequest.h DaemonCommandRequest.h DaemonListRequest.h DaemonStatusRequest.h +noinst_HEADERS = DaemonCommandRequest.h DaemonFSInfoRequest.h DaemonListRequest.h DaemonStatusRequest.h diff --git a/src/Client/Requests/Makefile.in b/src/Client/Requests/Makefile.in index 9336dbb..a074523 100644 --- a/src/Client/Requests/Makefile.in +++ b/src/Client/Requests/Makefile.in @@ -48,8 +48,8 @@ CONFIG_HEADER = $(top_builddir)/src/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) librequests_la_LIBADD = -am_librequests_la_OBJECTS = CoreStatusRequest.lo \ - DaemonCommandRequest.lo DaemonListRequest.lo \ +am_librequests_la_OBJECTS = DaemonCommandRequest.lo \ + DaemonFSInfoRequest.lo DaemonListRequest.lo \ DaemonStatusRequest.lo librequests_la_OBJECTS = $(am_librequests_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src @@ -189,8 +189,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = librequests.la -librequests_la_SOURCES = CoreStatusRequest.cpp DaemonCommandRequest.cpp DaemonListRequest.cpp DaemonStatusRequest.cpp -noinst_HEADERS = CoreStatusRequest.h DaemonCommandRequest.h DaemonListRequest.h DaemonStatusRequest.h +librequests_la_SOURCES = DaemonCommandRequest.cpp DaemonFSInfoRequest.cpp DaemonListRequest.cpp DaemonStatusRequest.cpp +noinst_HEADERS = DaemonCommandRequest.h DaemonFSInfoRequest.h DaemonListRequest.h DaemonStatusRequest.h all: all-am .SUFFIXES: @@ -242,8 +242,8 @@ mostlyclean-compile: 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)/DaemonFSInfoRequest.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@ diff --git a/src/Common/Backends/SystemBackendPosix.cpp b/src/Common/Backends/SystemBackendPosix.cpp index ca8b440..acb6bb6 100644 --- a/src/Common/Backends/SystemBackendPosix.cpp +++ b/src/Common/Backends/SystemBackendPosix.cpp @@ -19,8 +19,10 @@ #include "SystemBackendPosix.h" +#include #include #include +#include #include #include @@ -62,9 +64,32 @@ SystemBackendPosix::~SystemBackendPosix() { void SystemBackendPosix::fsInfoCallback(int, const std::string &output, const sigc::slot& > &callback) { - // TODO Process df output + std::vector ret; + std::istringstream stream(output); + std::string str; - callback(std::vector()); + std::getline(stream, str); // ignore first line + + while(!stream.eof()) { + std::getline(stream, str); + + char *fsName = new char[str.length()+1]; + char *mountedOn = new char[str.length()+1]; + + FSInfo info; + + if(std::sscanf(str.c_str(), "%s %lld %lld %lld %*d%% %s", fsName, &info.total, &info.used, &info.available, mountedOn) == 5) { + info.fsName = fsName; + info.mountedOn = mountedOn; + + ret.push_back(info); + } + + delete [] fsName; + delete [] mountedOn; + } + + callback(ret); } bool SystemBackendPosix::fsInfo(const sigc::slot& > &callback) { @@ -200,7 +225,7 @@ bool SystemBackendPosix::execWithOutput(const sigc::slot + * + * 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 "FSInfoRequestHandler.h" +#include "../Logger.h" +#include +#include +#include + +namespace Mad { +namespace Common { +namespace RequestHandlers { + +void FSInfoRequestHandler::handlePacket(Net::Connection *con, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::FS_INFO) { + Logger::log(Logger::ERROR, "Received an unexpected packet."); + con->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Exception(Exception::UNEXPECTED_PACKET))); + + signalFinished().emit(); + return; + } + + // TODO Require authentication + + connection = con; + requestId = packet.getRequestId(); + + if(!SystemBackend::getFSInfo(sigc::mem_fun(this, &FSInfoRequestHandler::fsInfoHandler))) { + con->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Exception(Exception::NOT_IMPLEMENTED))); + signalFinished().emit(); + } +} + +void FSInfoRequestHandler::fsInfoHandler(const std::vector &info) { + connection->send(Net::Packets::FSInfoPacket(Net::Packet::OK, requestId, info)); + signalFinished().emit(); +} + +} +} +} diff --git a/src/Common/RequestHandlers/FSInfoRequestHandler.h b/src/Common/RequestHandlers/FSInfoRequestHandler.h new file mode 100644 index 0000000..8de89ce --- /dev/null +++ b/src/Common/RequestHandlers/FSInfoRequestHandler.h @@ -0,0 +1,50 @@ +/* + * FSInfoRequestHandler.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_COMMON_REQUESTHANDLERS_FSINFOREQUESTHANDLER_H_ +#define MAD_COMMON_REQUESTHANDLERS_FSINFOREQUESTHANDLER_H_ + +#include "../RequestHandler.h" +#include "../SystemBackend.h" + +#include + +namespace Mad { +namespace Common { +namespace RequestHandlers { + +class FSInfoRequestHandler : public RequestHandler { + private: + Net::Connection *connection; + uint16_t requestId; + + void fsInfoHandler(const std::vector &info); + + protected: + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + + public: + FSInfoRequestHandler() {} +}; + +} +} +} + +#endif /* MAD_COMMON_REQUESTHANDLERS_FSINFOREQUESTHANDLER_H_ */ diff --git a/src/Common/RequestHandlers/Makefile.am b/src/Common/RequestHandlers/Makefile.am index 4a266e8..0572641 100644 --- a/src/Common/RequestHandlers/Makefile.am +++ b/src/Common/RequestHandlers/Makefile.am @@ -1,5 +1,5 @@ noinst_LTLIBRARIES = librequesthandlers.la -librequesthandlers_la_SOURCES = DisconnectRequestHandler.cpp StatusRequestHandler.cpp +librequesthandlers_la_SOURCES = DisconnectRequestHandler.cpp FSInfoRequestHandler.cpp StatusRequestHandler.cpp -noinst_HEADERS = DisconnectRequestHandler.h StatusRequestHandler.h +noinst_HEADERS = DisconnectRequestHandler.h FSInfoRequestHandler.h StatusRequestHandler.h diff --git a/src/Common/RequestHandlers/Makefile.in b/src/Common/RequestHandlers/Makefile.in index 8139c28..a332620 100644 --- a/src/Common/RequestHandlers/Makefile.in +++ b/src/Common/RequestHandlers/Makefile.in @@ -49,7 +49,7 @@ CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) librequesthandlers_la_LIBADD = am_librequesthandlers_la_OBJECTS = DisconnectRequestHandler.lo \ - StatusRequestHandler.lo + FSInfoRequestHandler.lo StatusRequestHandler.lo librequesthandlers_la_OBJECTS = $(am_librequesthandlers_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -188,8 +188,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = librequesthandlers.la -librequesthandlers_la_SOURCES = DisconnectRequestHandler.cpp StatusRequestHandler.cpp -noinst_HEADERS = DisconnectRequestHandler.h StatusRequestHandler.h +librequesthandlers_la_SOURCES = DisconnectRequestHandler.cpp FSInfoRequestHandler.cpp StatusRequestHandler.cpp +noinst_HEADERS = DisconnectRequestHandler.h FSInfoRequestHandler.h StatusRequestHandler.h all: all-am .SUFFIXES: @@ -242,6 +242,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DisconnectRequestHandler.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FSInfoRequestHandler.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/StatusRequestHandler.Plo@am__quote@ .cpp.o: diff --git a/src/Common/Requests/FSInfoRequest.cpp b/src/Common/Requests/FSInfoRequest.cpp new file mode 100644 index 0000000..c8492ff --- /dev/null +++ b/src/Common/Requests/FSInfoRequest.cpp @@ -0,0 +1,45 @@ +/* + * FSInfoRequest.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 "FSInfoRequest.h" +#include + +#include + +namespace Mad { +namespace Common { +namespace Requests { + +void FSInfoRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { + connection->send(Net::Packet(Net::Packet::FS_INFO, requestId)); +} + +void FSInfoRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::OK) { + finishWithError(Exception(Exception::UNEXPECTED_PACKET)); + return; // TODO Logging + } + + finish(Net::Packets::FSInfoPacket(packet)); +} + +} +} +} + diff --git a/src/Common/Requests/FSInfoRequest.h b/src/Common/Requests/FSInfoRequest.h new file mode 100644 index 0000000..9ae7673 --- /dev/null +++ b/src/Common/Requests/FSInfoRequest.h @@ -0,0 +1,43 @@ +/* + * FSInfoRequest.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_COMMON_REQUESTS_FSINFOREQUEST_H_ +#define MAD_COMMON_REQUESTS_FSINFOREQUEST_H_ + +#include "../Request.h" +#include + +namespace Mad { +namespace Common { +namespace Requests { + +class FSInfoRequest : public Request { + protected: + virtual void sendRequest(Net::Connection *connection, uint16_t requestId); + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + + public: + FSInfoRequest(slot_type slot) : Request(slot) {} +}; + +} +} +} + +#endif /* MAD_COMMON_REQUESTS_FSINFOREQUEST_H_ */ diff --git a/src/Common/Requests/Makefile.am b/src/Common/Requests/Makefile.am index d455da6..ff116fc 100644 --- a/src/Common/Requests/Makefile.am +++ b/src/Common/Requests/Makefile.am @@ -1,4 +1,4 @@ noinst_LTLIBRARIES = librequests.la -librequests_la_SOURCES = DisconnectRequest.cpp GSSAPIAuthRequest.cpp +librequests_la_SOURCES = DisconnectRequest.cpp FSInfoRequest.cpp GSSAPIAuthRequest.cpp StatusRequest.cpp -noinst_HEADERS = DisconnectRequest.h GSSAPIAuthRequest.h +noinst_HEADERS = DisconnectRequest.h FSInfoRequest.h GSSAPIAuthRequest.h StatusRequest.h diff --git a/src/Common/Requests/Makefile.in b/src/Common/Requests/Makefile.in index 6de55d0..5b5e2fe 100644 --- a/src/Common/Requests/Makefile.in +++ b/src/Common/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 = DisconnectRequest.lo GSSAPIAuthRequest.lo +am_librequests_la_OBJECTS = DisconnectRequest.lo FSInfoRequest.lo \ + GSSAPIAuthRequest.lo StatusRequest.lo librequests_la_OBJECTS = $(am_librequests_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -187,8 +188,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = librequests.la -librequests_la_SOURCES = DisconnectRequest.cpp GSSAPIAuthRequest.cpp -noinst_HEADERS = DisconnectRequest.h GSSAPIAuthRequest.h +librequests_la_SOURCES = DisconnectRequest.cpp FSInfoRequest.cpp GSSAPIAuthRequest.cpp StatusRequest.cpp +noinst_HEADERS = DisconnectRequest.h FSInfoRequest.h GSSAPIAuthRequest.h StatusRequest.h all: all-am .SUFFIXES: @@ -241,7 +242,9 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DisconnectRequest.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FSInfoRequest.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GSSAPIAuthRequest.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/StatusRequest.Plo@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/src/Common/Requests/StatusRequest.cpp b/src/Common/Requests/StatusRequest.cpp new file mode 100644 index 0000000..dbaba23 --- /dev/null +++ b/src/Common/Requests/StatusRequest.cpp @@ -0,0 +1,42 @@ +/* + * CoreStatusRequest.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 "StatusRequest.h" +#include + +namespace Mad { +namespace Common { +namespace Requests { + +void StatusRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { + connection->send(Net::Packet(Net::Packet::STATUS, requestId)); +} + +void StatusRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::OK) { + finishWithError(Exception(Exception::UNEXPECTED_PACKET)); + return; // TODO Logging + } + + finish(Net::Packets::HostStatusPacket(packet)); +} + +} +} +} diff --git a/src/Common/Requests/StatusRequest.h b/src/Common/Requests/StatusRequest.h new file mode 100644 index 0000000..5712404 --- /dev/null +++ b/src/Common/Requests/StatusRequest.h @@ -0,0 +1,43 @@ +/* + * StatusRequest.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_COMMON_REQUESTS_STATUSREQUEST_H_ +#define MAD_COMMON_REQUESTS_STATUSREQUEST_H_ + +#include "../Request.h" +#include + +namespace Mad { +namespace Common { +namespace Requests { + +class StatusRequest : public Request { + protected: + virtual void sendRequest(Net::Connection *connection, uint16_t requestId); + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + + public: + StatusRequest(slot_type slot) : Request(slot) {} +}; + +} +} +} + +#endif /* MAD_COMMON_REQUESTS_STATUSREQUEST_H_ */ diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index 93d6d3f..a41b024 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -20,9 +20,11 @@ #include "ConnectionManager.h" #include "ConfigManager.h" #include +#include #include #include "Requests/DaemonStateUpdateRequest.h" #include "RequestHandlers/DaemonCommandRequestHandler.h" +#include "RequestHandlers/DaemonFSInfoRequestHandler.h" #include "RequestHandlers/DaemonListRequestHandler.h" #include "RequestHandlers/DaemonStatusRequestHandler.h" #include "RequestHandlers/GSSAPIAuthRequestHandler.h" @@ -78,9 +80,11 @@ void ConnectionManager::updateState(const std::string &name, Common::HostInfo::S ConnectionManager::ConnectionManager() { Common::RequestManager::init(true); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::FS_INFO); Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::STATUS); Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_COMMAND_REBOOT); Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_COMMAND_SHUTDOWN); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_FS_INFO); Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::LIST_DAEMONS); Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_STATUS); Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::GSSAPI_AUTH); diff --git a/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.cpp b/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.cpp new file mode 100644 index 0000000..1267520 --- /dev/null +++ b/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.cpp @@ -0,0 +1,71 @@ +/* + * DaemonFSInfoRequestHandler.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 "DaemonFSInfoRequestHandler.h" +#include "../ConnectionManager.h" +#include +#include +#include +#include + + +namespace Mad { +namespace Core { +namespace RequestHandlers { + +void DaemonFSInfoRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::DAEMON_FS_INFO) { + 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 + + con = connection; + requestId = packet.getRequestId(); + + std::string daemonName((char*)packet.getData(), packet.getLength()); + + try { + Net::Connection *daemonCon = ConnectionManager::getConnectionManager()->getDaemonConnection(daemonName); + Common::RequestManager::getRequestManager()->sendRequest(daemonCon, std::auto_ptr(new Common::Requests::FSInfoRequest(sigc::mem_fun(this, &DaemonFSInfoRequestHandler::requestFinished)))); + } + catch(Common::Exception &e) { + connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), e)); + } +} + +void DaemonFSInfoRequestHandler::requestFinished(const Common::Request &request) { + try { + const Net::Packet &packet = request.getResult(); + con->send(Net::Packet(Net::Packet::OK, requestId, packet.getData(), packet.getLength())); + } + catch(Common::Exception &e) { + con->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, requestId, e)); + } + + signalFinished().emit(); +} + +} +} +} diff --git a/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.h b/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.h new file mode 100644 index 0000000..2f6c69c --- /dev/null +++ b/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.h @@ -0,0 +1,56 @@ +/* + * DaemonFSInfoRequestHandler.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_CORE_REQUESTHANDLERS_DAEMONFSINFOREQUESTHANDLER_H_ +#define MAD_CORE_REQUESTHANDLERS_DAEMONFSINFOREQUESTHANDLER_H_ + +#include +#include +#include + +namespace Mad { + +namespace Net { +namespace Packets { +class FSInfoPacket; +} +} + +namespace Core { +namespace RequestHandlers { + +class DaemonFSInfoRequestHandler : public Common::RequestHandler { + private: + Net::Connection *con; + uint16_t requestId; + + void requestFinished(const Common::Request &request); + + protected: + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + + public: + DaemonFSInfoRequestHandler() {} +}; + +} +} +} + +#endif /* MAD_CORE_REQUESTHANDLERS_DAEMONFSINFOREQUESTHANDLER_H_ */ diff --git a/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp b/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp index 7fcf9fe..4add098 100644 --- a/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp +++ b/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp @@ -20,7 +20,7 @@ #include "DaemonStatusRequestHandler.h" #include "../ConnectionManager.h" #include -#include +#include #include #include @@ -47,7 +47,7 @@ void DaemonStatusRequestHandler::handlePacket(Net::Connection *connection, const try { Net::Connection *daemonCon = ConnectionManager::getConnectionManager()->getDaemonConnection(daemonName); - Common::RequestManager::getRequestManager()->sendRequest(daemonCon, std::auto_ptr(new Requests::DaemonStatusRequest(sigc::mem_fun(this, &DaemonStatusRequestHandler::requestFinished)))); + Common::RequestManager::getRequestManager()->sendRequest(daemonCon, std::auto_ptr(new Common::Requests::StatusRequest(sigc::mem_fun(this, &DaemonStatusRequestHandler::requestFinished)))); } catch(Common::Exception &e) { connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), e)); diff --git a/src/Core/RequestHandlers/DaemonStatusRequestHandler.h b/src/Core/RequestHandlers/DaemonStatusRequestHandler.h index d8edc41..36fabc0 100644 --- a/src/Core/RequestHandlers/DaemonStatusRequestHandler.h +++ b/src/Core/RequestHandlers/DaemonStatusRequestHandler.h @@ -33,9 +33,6 @@ class HostStatusPacket; } namespace Core { - -class ConnectionManager; - namespace RequestHandlers { class DaemonStatusRequestHandler : public Common::RequestHandler { diff --git a/src/Core/RequestHandlers/Makefile.am b/src/Core/RequestHandlers/Makefile.am index 484df59..79aef5e 100644 --- a/src/Core/RequestHandlers/Makefile.am +++ b/src/Core/RequestHandlers/Makefile.am @@ -1,4 +1,6 @@ noinst_LTLIBRARIES = librequesthandlers.la -librequesthandlers_la_SOURCES = DaemonCommandRequestHandler.cpp DaemonListRequestHandler.cpp DaemonStatusRequestHandler.cpp GSSAPIAuthRequestHandler.cpp IdentifyRequestHandler.cpp LogRequestHandler.cpp +librequesthandlers_la_SOURCES = DaemonCommandRequestHandler.cpp DaemonFSInfoRequestHandler.cpp DaemonListRequestHandler.cpp DaemonStatusRequestHandler.cpp GSSAPIAuthRequestHandler.cpp \ + IdentifyRequestHandler.cpp LogRequestHandler.cpp -noinst_HEADERS = DaemonCommandRequestHandler.h DaemonListRequestHandler.h DaemonStatusRequestHandler.h GSSAPIAuthRequestHandler.h IdentifyRequestHandler.h LogRequestHandler.h +noinst_HEADERS = DaemonCommandRequestHandler.h DaemonFSInfoRequestHandler.h DaemonListRequestHandler.h DaemonStatusRequestHandler.h GSSAPIAuthRequestHandler.h \ + IdentifyRequestHandler.h LogRequestHandler.h diff --git a/src/Core/RequestHandlers/Makefile.in b/src/Core/RequestHandlers/Makefile.in index e0dc6f2..46c1381 100644 --- a/src/Core/RequestHandlers/Makefile.in +++ b/src/Core/RequestHandlers/Makefile.in @@ -49,9 +49,9 @@ CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) librequesthandlers_la_LIBADD = am_librequesthandlers_la_OBJECTS = DaemonCommandRequestHandler.lo \ - DaemonListRequestHandler.lo DaemonStatusRequestHandler.lo \ - GSSAPIAuthRequestHandler.lo IdentifyRequestHandler.lo \ - LogRequestHandler.lo + DaemonFSInfoRequestHandler.lo DaemonListRequestHandler.lo \ + DaemonStatusRequestHandler.lo GSSAPIAuthRequestHandler.lo \ + IdentifyRequestHandler.lo LogRequestHandler.lo librequesthandlers_la_OBJECTS = $(am_librequesthandlers_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -190,8 +190,12 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = librequesthandlers.la -librequesthandlers_la_SOURCES = DaemonCommandRequestHandler.cpp DaemonListRequestHandler.cpp DaemonStatusRequestHandler.cpp GSSAPIAuthRequestHandler.cpp IdentifyRequestHandler.cpp LogRequestHandler.cpp -noinst_HEADERS = DaemonCommandRequestHandler.h DaemonListRequestHandler.h DaemonStatusRequestHandler.h GSSAPIAuthRequestHandler.h IdentifyRequestHandler.h LogRequestHandler.h +librequesthandlers_la_SOURCES = DaemonCommandRequestHandler.cpp DaemonFSInfoRequestHandler.cpp DaemonListRequestHandler.cpp DaemonStatusRequestHandler.cpp GSSAPIAuthRequestHandler.cpp \ + IdentifyRequestHandler.cpp LogRequestHandler.cpp + +noinst_HEADERS = DaemonCommandRequestHandler.h DaemonFSInfoRequestHandler.h DaemonListRequestHandler.h DaemonStatusRequestHandler.h GSSAPIAuthRequestHandler.h \ + IdentifyRequestHandler.h LogRequestHandler.h + all: all-am .SUFFIXES: @@ -244,6 +248,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonCommandRequestHandler.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonFSInfoRequestHandler.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonListRequestHandler.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonStatusRequestHandler.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GSSAPIAuthRequestHandler.Plo@am__quote@ diff --git a/src/Core/Requests/DaemonStatusRequest.cpp b/src/Core/Requests/DaemonStatusRequest.cpp deleted file mode 100644 index 3b028c9..0000000 --- a/src/Core/Requests/DaemonStatusRequest.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/* - * DaemonStatusRequest.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 "DaemonStatusRequest.h" -#include - -namespace Mad { -namespace Core { -namespace Requests { - -void DaemonStatusRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(Net::Packet::STATUS, requestId)); -} - -void DaemonStatusRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) { - finishWithError(Common::Exception(Common::Exception::UNEXPECTED_PACKET)); - return; // TODO Logging - } - - finish(Net::Packets::HostStatusPacket(packet)); -} - -} -} -} diff --git a/src/Core/Requests/DaemonStatusRequest.h b/src/Core/Requests/DaemonStatusRequest.h deleted file mode 100644 index 09cbc2b..0000000 --- a/src/Core/Requests/DaemonStatusRequest.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * DaemonStatusRequest.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_CORE_REQUESTS_DAEMONSTATUSREQUEST_H_ -#define MAD_CORE_REQUESTS_DAEMONSTATUSREQUEST_H_ - -#include -#include - -namespace Mad { -namespace Core { -namespace Requests { - -class DaemonStatusRequest : public Common::Request { - protected: - virtual void sendRequest(Net::Connection *connection, uint16_t requestId); - virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); - - public: - DaemonStatusRequest(slot_type slot) : Common::Request(slot) {} -}; - -} -} -} - -#endif /* MAD_CORE_REQUESTS_DAEMONSTATUSREQUEST_H_ */ diff --git a/src/Core/Requests/Makefile.am b/src/Core/Requests/Makefile.am index e69088b..f101b84 100644 --- a/src/Core/Requests/Makefile.am +++ b/src/Core/Requests/Makefile.am @@ -1,4 +1,4 @@ noinst_LTLIBRARIES = librequests.la -librequests_la_SOURCES = CommandRequest.cpp DaemonStateUpdateRequest.cpp DaemonStatusRequest.cpp +librequests_la_SOURCES = CommandRequest.cpp DaemonStateUpdateRequest.cpp -noinst_HEADERS = CommandRequest.h DaemonStateUpdateRequest.h DaemonStatusRequest.h +noinst_HEADERS = CommandRequest.h DaemonStateUpdateRequest.h diff --git a/src/Core/Requests/Makefile.in b/src/Core/Requests/Makefile.in index e6367a2..26aa0fe 100644 --- a/src/Core/Requests/Makefile.in +++ b/src/Core/Requests/Makefile.in @@ -49,7 +49,7 @@ CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) librequests_la_LIBADD = am_librequests_la_OBJECTS = CommandRequest.lo \ - DaemonStateUpdateRequest.lo DaemonStatusRequest.lo + DaemonStateUpdateRequest.lo librequests_la_OBJECTS = $(am_librequests_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -188,8 +188,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = librequests.la -librequests_la_SOURCES = CommandRequest.cpp DaemonStateUpdateRequest.cpp DaemonStatusRequest.cpp -noinst_HEADERS = CommandRequest.h DaemonStateUpdateRequest.h DaemonStatusRequest.h +librequests_la_SOURCES = CommandRequest.cpp DaemonStateUpdateRequest.cpp +noinst_HEADERS = CommandRequest.h DaemonStateUpdateRequest.h all: all-am .SUFFIXES: @@ -243,7 +243,6 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CommandRequest.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonStateUpdateRequest.Plo@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonStatusRequest.Plo@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp index 8eee0cc..03e9f73 100644 --- a/src/Net/Connection.cpp +++ b/src/Net/Connection.cpp @@ -225,6 +225,8 @@ bool Connection::rawSend(const uint8_t *data, unsigned long length) { std::memcpy(trans.data, data, length); transS.push(trans); + doSend(); + return true; } diff --git a/src/Net/Packet.cpp b/src/Net/Packet.cpp index 9e05f24..976fd65 100644 --- a/src/Net/Packet.cpp +++ b/src/Net/Packet.cpp @@ -46,5 +46,29 @@ Packet& Packet::operator=(const Packet &p) { return *this; } + +uint64_t Packet::htonll(uint64_t val) { + union { + uint32_t u32[2]; + uint64_t u64; + } ret; + + ret.u32[0] = htonl(val >> 32); + ret.u32[1] = htonl(val); + + return ret.u64; +} + +uint64_t Packet::ntohll(uint64_t val) { + union { + uint32_t u32[2]; + uint64_t u64; + } v; + + v.u64 = val; + + return (((uint64_t)ntohl(v.u32[0])) << 32) | ntohl(v.u32[1]); +} + } } diff --git a/src/Net/Packet.h b/src/Net/Packet.h index 366746b..2ec1634 100644 --- a/src/Net/Packet.h +++ b/src/Net/Packet.h @@ -34,7 +34,7 @@ class Packet { OK = 0x0000, ERROR = 0x0001, DISCONNECT = 0x0002, LOG = 0x0003, GSSAPI_AUTH = 0x0010, IDENTIFY = 0x0011, LIST_DAEMONS = 0x0020, - STATUS = 0x0030, DAEMON_STATUS = 0x0031, + STATUS = 0x0030, DAEMON_STATUS = 0x0031, FS_INFO = 0x0032, DAEMON_FS_INFO = 0x0033, COMMAND_SHUTDOWN = 0x0040, COMMAND_REBOOT = 0x0041, DAEMON_COMMAND_SHUTDOWN = 0x0050, DAEMON_COMMAND_REBOOT = 0x0051, DAEMON_STATE_UPDATE = 0x0060 @@ -94,6 +94,10 @@ class Packet { unsigned long getRawDataLength() const { return sizeof(Data) + ntohs(rawData->length); } + + + static uint64_t htonll(uint64_t val); + static uint64_t ntohll(uint64_t val); }; } diff --git a/src/Net/Packets/FSInfoPacket.cpp b/src/Net/Packets/FSInfoPacket.cpp new file mode 100644 index 0000000..c328f8a --- /dev/null +++ b/src/Net/Packets/FSInfoPacket.cpp @@ -0,0 +1,87 @@ +/* + * FSInfoPacket.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 "FSInfoPacket.h" +#include +#include + +namespace Mad { +namespace Net { +namespace Packets { + +void FSInfoPacket::assemblePacket() { + std::string str; + + for(std::vector::iterator fs = fsList.begin(); fs != fsList.end(); ++fs) + str += fs->fsName + "\n" + fs->mountedOn + "\n"; + + setLength(sizeof(uint16_t) + sizeof(FSData)*fsList.size() + str.length()); + + nFS = (uint16_t*)rawData->data; + fsData = (FSData*)(rawData->data + sizeof(uint16_t)); + charData = (char*)(rawData->data + sizeof(uint16_t) + sizeof(FSData)*fsList.size()); + + std::memcpy(charData, str.c_str(), str.length()); + + *nFS = htons(fsList.size()); + + for(size_t i = 0; i < fsList.size(); ++i) { + fsData[i].total = htonll(fsList[i].total); + fsData[i].used = htonll(fsList[i].used); + fsData[i].available = htonll(fsList[i].available); + } +} + +void FSInfoPacket::parsePacket() { + fsList.clear(); + + if(getLength() < sizeof(uint16_t)) + return; + + nFS = (uint16_t*)rawData->data; + fsList.resize(ntohs(*nFS)); + + if(getLength() < sizeof(uint16_t) + sizeof(FSData)*fsList.size()) + setLength(sizeof(uint16_t) + sizeof(FSData)*fsList.size()); + + nFS = (uint16_t*)rawData->data; + fsData = (FSData*)(rawData->data + sizeof(uint16_t)); + charData = (char*)(rawData->data + sizeof(uint16_t) + sizeof(FSData)*fsList.size()); + + std::istringstream stream(std::string(charData, getLength() - (sizeof(uint16_t)+sizeof(FSData)*fsList.size()))); + + for(size_t i = 0; i < fsList.size(); ++i) { + fsList[i].total = ntohll(fsData[i].total); + fsList[i].used = ntohll(fsData[i].used); + fsList[i].available = ntohll(fsData[i].available); + + if(!stream.eof()) { + std::string str; + + std::getline(stream, str); + fsList[i].fsName = str; + std::getline(stream, str); + fsList[i].mountedOn = str; + } + } +} + +} +} +} diff --git a/src/Net/Packets/FSInfoPacket.h b/src/Net/Packets/FSInfoPacket.h new file mode 100644 index 0000000..4e973aa --- /dev/null +++ b/src/Net/Packets/FSInfoPacket.h @@ -0,0 +1,81 @@ +/* + * FSInfoPacket.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_NET_PACKETS_FSINFOPACKET_H_ +#define MAD_NET_PACKETS_FSINFOPACKET_H_ + +#include "../Packet.h" +#include + +#include + +namespace Mad { +namespace Net { +namespace Packets { + +class FSInfoPacket : public Packet { + protected: + struct FSData { + uint64_t total; + uint64_t used; + uint64_t available; + }; + + uint16_t *nFS; + FSData *fsData; + char *charData; + + std::vector fsList; + + void assemblePacket(); + void parsePacket(); + + public: + FSInfoPacket(Type type, uint16_t requestId, const std::vector &fs) : Packet(type, requestId), fsList(fs) { + assemblePacket(); + } + + FSInfoPacket(const Packet &p) : Packet(p) { + parsePacket(); + } + + FSInfoPacket& operator=(const Packet &p) { + Packet::operator=(p); + parsePacket(); + + return *this; + } + + FSInfoPacket& operator=(const FSInfoPacket &p) { + Packet::operator=(p); + parsePacket(); + + return *this; + } + + const std::vector& getFSInfo() const { + return fsList; + } +}; + +} +} +} + +#endif /* MAD_NET_PACKETS_FSINFOPACKET_H_ */ diff --git a/src/Net/Packets/HostListPacket.cpp b/src/Net/Packets/HostListPacket.cpp index 8c3d395..659b621 100644 --- a/src/Net/Packets/HostListPacket.cpp +++ b/src/Net/Packets/HostListPacket.cpp @@ -26,14 +26,10 @@ namespace Net { namespace Packets { void HostListPacket::assemblePacket() { - std::ostringstream stream; + std::string str; - for(std::vector::iterator host = hostList.begin(); host != hostList.end(); ++host) { - stream << host->getName() << std::endl; - stream << host->getIP() << std::endl; - } - - std::string str = stream.str(); + for(std::vector::iterator host = hostList.begin(); host != hostList.end(); ++host) + str += host->getName() + "\n" + host->getIP() + "\n"; setLength(sizeof(uint16_t) + sizeof(HostData)*hostList.size() + str.length()); diff --git a/src/Net/Packets/Makefile.am b/src/Net/Packets/Makefile.am index 19dcc7c..311d9da 100644 --- a/src/Net/Packets/Makefile.am +++ b/src/Net/Packets/Makefile.am @@ -1,4 +1,4 @@ noinst_LTLIBRARIES = libpackets.la -libpackets_la_SOURCES = ErrorPacket.cpp HostListPacket.cpp HostStatePacket.cpp HostStatusPacket.cpp LogPacket.cpp +libpackets_la_SOURCES = ErrorPacket.cpp FSInfoPacket.cpp HostListPacket.cpp HostStatePacket.cpp HostStatusPacket.cpp LogPacket.cpp -noinst_HEADERS = ErrorPacket.h HostListPacket.h HostStatePacket.h HostStatusPacket.h LogPacket.h +noinst_HEADERS = ErrorPacket.h FSInfoPacket.h HostListPacket.h HostStatePacket.h HostStatusPacket.h LogPacket.h diff --git a/src/Net/Packets/Makefile.in b/src/Net/Packets/Makefile.in index b500da6..587a36c 100644 --- a/src/Net/Packets/Makefile.in +++ b/src/Net/Packets/Makefile.in @@ -48,8 +48,9 @@ CONFIG_HEADER = $(top_builddir)/src/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libpackets_la_LIBADD = -am_libpackets_la_OBJECTS = ErrorPacket.lo HostListPacket.lo \ - HostStatePacket.lo HostStatusPacket.lo LogPacket.lo +am_libpackets_la_OBJECTS = ErrorPacket.lo FSInfoPacket.lo \ + HostListPacket.lo HostStatePacket.lo HostStatusPacket.lo \ + LogPacket.lo libpackets_la_OBJECTS = $(am_libpackets_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -188,8 +189,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = libpackets.la -libpackets_la_SOURCES = ErrorPacket.cpp HostListPacket.cpp HostStatePacket.cpp HostStatusPacket.cpp LogPacket.cpp -noinst_HEADERS = ErrorPacket.h HostListPacket.h HostStatePacket.h HostStatusPacket.h LogPacket.h +libpackets_la_SOURCES = ErrorPacket.cpp FSInfoPacket.cpp HostListPacket.cpp HostStatePacket.cpp HostStatusPacket.cpp LogPacket.cpp +noinst_HEADERS = ErrorPacket.h FSInfoPacket.h HostListPacket.h HostStatePacket.h HostStatusPacket.h LogPacket.h all: all-am .SUFFIXES: @@ -242,6 +243,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ErrorPacket.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FSInfoPacket.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HostListPacket.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HostStatePacket.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HostStatusPacket.Plo@am__quote@ diff --git a/src/mad.cpp b/src/mad.cpp index 47dbfec..d6e0ae3 100644 --- a/src/mad.cpp +++ b/src/mad.cpp @@ -25,6 +25,7 @@ #include "Common/Backends/ConsoleLogger.h" #include "Common/Request.h" #include "Common/RequestManager.h" +#include "Common/RequestHandlers/FSInfoRequestHandler.h" #include "Common/RequestHandlers/StatusRequestHandler.h" #include "Daemon/Backends/NetworkLogger.h" #include "Daemon/Requests/IdentifyRequest.h" @@ -46,6 +47,7 @@ int main() { Net::Connection::init(); Common::RequestManager::init(false); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::FS_INFO); Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::STATUS); Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::COMMAND_REBOOT); Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::COMMAND_SHUTDOWN); -- cgit v1.2.3