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 ++++ src/Core/ConnectionManager.cpp | 15 +++++- src/Core/ConnectionManager.h | 2 + src/Core/Requests/DaemonStateUpdateRequest.cpp | 43 +++++++++++++++ src/Core/Requests/DaemonStateUpdateRequest.h | 47 ++++++++++++++++ src/Core/Requests/Makefile.am | 4 +- src/Core/Requests/Makefile.in | 8 +-- src/Net/Packet.h | 3 +- src/Net/Packets/HostStatePacket.cpp | 50 +++++++++++++++++ src/Net/Packets/HostStatePacket.h | 74 ++++++++++++++++++++++++++ src/Net/Packets/Makefile.am | 4 +- src/Net/Packets/Makefile.in | 7 +-- 14 files changed, 295 insertions(+), 13 deletions(-) create mode 100644 src/Core/Requests/DaemonStateUpdateRequest.cpp create mode 100644 src/Core/Requests/DaemonStateUpdateRequest.h create mode 100644 src/Net/Packets/HostStatePacket.cpp create mode 100644 src/Net/Packets/HostStatePacket.h (limited to 'src') 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(); } diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index 506a7d4..c83f3aa 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -21,6 +21,7 @@ #include "ConfigManager.h" #include #include +#include "Requests/DaemonStateUpdateRequest.h" #include "RequestHandlers/DaemonCommandRequestHandler.h" #include "RequestHandlers/DaemonListRequestHandler.h" #include "RequestHandlers/DaemonStatusRequestHandler.h" @@ -64,6 +65,16 @@ void ConnectionManager::refreshPollfds() { } } +void ConnectionManager::updateState(const std::string &name, Common::HostInfo::State state) { + daemonInfo[name].setState(state); + + for(std::list::iterator con = clientConnections.begin(); con != clientConnections.end(); ++con) { + Common::RequestManager::getRequestManager()->sendRequest(*con, std::auto_ptr( + new Requests::DaemonStateUpdateRequest(name, state) + )); + } +} + ConnectionManager::ConnectionManager() { Common::RequestManager::init(true); @@ -137,7 +148,7 @@ void ConnectionManager::handleConnections(std::list& con if(idCon->second == *con) { idCon->second = 0; - daemonInfo[idCon->first].setState(Common::HostInfo::INACTIVE); + updateState(idCon->first, Common::HostInfo::INACTIVE); break; } } @@ -200,7 +211,7 @@ void ConnectionManager::identifyDaemonConnection(Net::Connection *connection, co } idCon->second = *con; - daemonInfo[idCon->first].setState(Common::HostInfo::RUNNING); + updateState(idCon->first, Common::HostInfo::RUNNING); connection->setIdentified(); Common::Logger::logf("Identified as '%s'.", name.c_str()); diff --git a/src/Core/ConnectionManager.h b/src/Core/ConnectionManager.h index 9b8615c..cd470d9 100644 --- a/src/Core/ConnectionManager.h +++ b/src/Core/ConnectionManager.h @@ -66,6 +66,8 @@ class ConnectionManager { void handleConnections(std::list &connections); + void updateState(const std::string &name, Common::HostInfo::State state); + public: static ConnectionManager* getConnectionManager() { return connectionManager.get(); diff --git a/src/Core/Requests/DaemonStateUpdateRequest.cpp b/src/Core/Requests/DaemonStateUpdateRequest.cpp new file mode 100644 index 0000000..00e0bdf --- /dev/null +++ b/src/Core/Requests/DaemonStateUpdateRequest.cpp @@ -0,0 +1,43 @@ +/* + * DaemonStateUpdateRequest.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 "DaemonStateUpdateRequest.h" +#include +#include + +namespace Mad { +namespace Core { +namespace Requests { + +void DaemonStateUpdateRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { + connection->send(Net::Packets::HostStatePacket(Net::Packet::DAEMON_STATE_UPDATE, requestId, name, state)); +} + +void DaemonStateUpdateRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::OK) { + finishWithError(Common::Exception(Common::Exception::UNEXPECTED_PACKET)); + return; // TODO Logging + } + + finish(); +} + +} +} +} diff --git a/src/Core/Requests/DaemonStateUpdateRequest.h b/src/Core/Requests/DaemonStateUpdateRequest.h new file mode 100644 index 0000000..9a1c8f0 --- /dev/null +++ b/src/Core/Requests/DaemonStateUpdateRequest.h @@ -0,0 +1,47 @@ +/* + * DaemonStateUpdateRequest.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_DAEMONSTATEUPDATEREQUEST_H_ +#define MAD_CORE_REQUESTS_DAEMONSTATEUPDATEREQUEST_H_ + +#include +#include + +namespace Mad { +namespace Core { +namespace Requests { + +class DaemonStateUpdateRequest : public Common::Request<> { + private: + std::string name; + Common::HostInfo::State state; + + protected: + virtual void sendRequest(Net::Connection *connection, uint16_t requestId); + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + + public: + DaemonStateUpdateRequest(const std::string &name0, Common::HostInfo::State state0) : Common::Request<>(slot_type()), name(name0), state(state0) {} +}; + +} +} +} + +#endif /* MAD_CORE_REQUESTS_DAEMONSTATEUPDATEREQUEST_H_ */ diff --git a/src/Core/Requests/Makefile.am b/src/Core/Requests/Makefile.am index 2d08477..e69088b 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 DaemonStatusRequest.cpp +librequests_la_SOURCES = CommandRequest.cpp DaemonStateUpdateRequest.cpp DaemonStatusRequest.cpp -noinst_HEADERS = CommandRequest.h DaemonStatusRequest.h +noinst_HEADERS = CommandRequest.h DaemonStateUpdateRequest.h DaemonStatusRequest.h diff --git a/src/Core/Requests/Makefile.in b/src/Core/Requests/Makefile.in index 22ac701..e6367a2 100644 --- a/src/Core/Requests/Makefile.in +++ b/src/Core/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 = CommandRequest.lo DaemonStatusRequest.lo +am_librequests_la_OBJECTS = CommandRequest.lo \ + DaemonStateUpdateRequest.lo DaemonStatusRequest.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 = CommandRequest.cpp DaemonStatusRequest.cpp -noinst_HEADERS = CommandRequest.h DaemonStatusRequest.h +librequests_la_SOURCES = CommandRequest.cpp DaemonStateUpdateRequest.cpp DaemonStatusRequest.cpp +noinst_HEADERS = CommandRequest.h DaemonStateUpdateRequest.h DaemonStatusRequest.h all: all-am .SUFFIXES: @@ -241,6 +242,7 @@ distclean-compile: -rm -f *.tab.c @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: diff --git a/src/Net/Packet.h b/src/Net/Packet.h index a3fe524..366746b 100644 --- a/src/Net/Packet.h +++ b/src/Net/Packet.h @@ -36,7 +36,8 @@ class Packet { LIST_DAEMONS = 0x0020, STATUS = 0x0030, DAEMON_STATUS = 0x0031, COMMAND_SHUTDOWN = 0x0040, COMMAND_REBOOT = 0x0041, - DAEMON_COMMAND_SHUTDOWN = 0x0050, DAEMON_COMMAND_REBOOT = 0x0051 + DAEMON_COMMAND_SHUTDOWN = 0x0050, DAEMON_COMMAND_REBOOT = 0x0051, + DAEMON_STATE_UPDATE = 0x0060 }; struct Data { diff --git a/src/Net/Packets/HostStatePacket.cpp b/src/Net/Packets/HostStatePacket.cpp new file mode 100644 index 0000000..d34d9d5 --- /dev/null +++ b/src/Net/Packets/HostStatePacket.cpp @@ -0,0 +1,50 @@ +/* + * HostStatePacket.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 "HostStatePacket.h" + +namespace Mad { +namespace Net { +namespace Packets { + +HostStatePacket::HostStatePacket(Type type, uint16_t requestId, const std::string &name, Common::HostInfo::State state) +: Packet(type, requestId) +{ + setLength(sizeof(HostStateData) + name.length()); + hostStateData = (HostStateData*)&rawData->data; + + hostStateData->state = htons(state); + + std::memcpy(hostStateData->name, name.c_str(), name.length()); +} + +HostStatePacket& HostStatePacket::operator=(const Packet &p) { + Packet::operator=(p); + + if(getLength() < sizeof(HostStateData)) + setLength(sizeof(HostStateData)); + + hostStateData = (HostStateData*)&rawData->data; + + return *this; +} + +} +} +} diff --git a/src/Net/Packets/HostStatePacket.h b/src/Net/Packets/HostStatePacket.h new file mode 100644 index 0000000..5a34275 --- /dev/null +++ b/src/Net/Packets/HostStatePacket.h @@ -0,0 +1,74 @@ +/* + * HostStatePacket.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_HOSTSTATEPACKET_H_ +#define MAD_NET_PACKETS_HOSTSTATEPACKET_H_ + +#include "../Packet.h" +#include + +#include + +namespace Mad { +namespace Net { +namespace Packets { + +class HostStatePacket : public Packet { + protected: + struct HostStateData { + uint16_t state; + uint8_t name[0]; + }; + + HostStateData *hostStateData; + + public: + HostStatePacket(Type type, uint16_t requestId, const std::string &name, Common::HostInfo::State state); + + HostStatePacket(const Packet &p) : Packet(p) { + if(getLength() < sizeof(HostStateData)) + setLength(sizeof(HostStateData)); + + hostStateData = (HostStateData*)&rawData->data; + } + + HostStatePacket(const HostStatePacket &p) : Packet(p) { + hostStateData = (HostStateData*)&rawData->data; + } + + HostStatePacket& operator=(const Packet &p); + + HostStatePacket& operator=(const HostStatePacket &p) { + return (*this = (Packet)p); + } + + std::string getName() const { + return std::string((char*)hostStateData->name, getLength()-sizeof(HostStateData)); + } + + Common::HostInfo::State getState() const { + return (Common::HostInfo::State)ntohs(hostStateData->state); + } +}; + +} +} +} + +#endif /* MAD_NET_PACKETS_HOSTSTATEPACKET_H_ */ diff --git a/src/Net/Packets/Makefile.am b/src/Net/Packets/Makefile.am index 0b44f6d..19dcc7c 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 HostStatusPacket.cpp LogPacket.cpp +libpackets_la_SOURCES = ErrorPacket.cpp HostListPacket.cpp HostStatePacket.cpp HostStatusPacket.cpp LogPacket.cpp -noinst_HEADERS = ErrorPacket.h HostListPacket.h HostStatusPacket.h LogPacket.h +noinst_HEADERS = ErrorPacket.h HostListPacket.h HostStatePacket.h HostStatusPacket.h LogPacket.h diff --git a/src/Net/Packets/Makefile.in b/src/Net/Packets/Makefile.in index 9109103..b500da6 100644 --- a/src/Net/Packets/Makefile.in +++ b/src/Net/Packets/Makefile.in @@ -49,7 +49,7 @@ CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libpackets_la_LIBADD = am_libpackets_la_OBJECTS = ErrorPacket.lo HostListPacket.lo \ - HostStatusPacket.lo LogPacket.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 +188,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = libpackets.la -libpackets_la_SOURCES = ErrorPacket.cpp HostListPacket.cpp HostStatusPacket.cpp LogPacket.cpp -noinst_HEADERS = ErrorPacket.h HostListPacket.h HostStatusPacket.h LogPacket.h +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 all: all-am .SUFFIXES: @@ -243,6 +243,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ErrorPacket.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@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/LogPacket.Plo@am__quote@ -- cgit v1.2.3