diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-10-08 23:08:21 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-10-08 23:08:21 +0200 |
commit | 58c5d4eefdf1cdee0651f7c74ffd1501adbdc9c3 (patch) | |
tree | 340e4f8ccfd172e197db6aa20cd6cfc7bec0f40e /src/Net | |
parent | 3bb4da8601bd4d6de56d57507faf7fa115a15037 (diff) | |
download | mad-58c5d4eefdf1cdee0651f7c74ffd1501adbdc9c3.tar mad-58c5d4eefdf1cdee0651f7c74ffd1501adbdc9c3.zip |
fsinfo-Befehl implementiert
Diffstat (limited to 'src/Net')
-rw-r--r-- | src/Net/Connection.cpp | 2 | ||||
-rw-r--r-- | src/Net/Packet.cpp | 24 | ||||
-rw-r--r-- | src/Net/Packet.h | 6 | ||||
-rw-r--r-- | src/Net/Packets/FSInfoPacket.cpp | 87 | ||||
-rw-r--r-- | src/Net/Packets/FSInfoPacket.h | 81 | ||||
-rw-r--r-- | src/Net/Packets/HostListPacket.cpp | 10 | ||||
-rw-r--r-- | src/Net/Packets/Makefile.am | 4 | ||||
-rw-r--r-- | src/Net/Packets/Makefile.in | 10 |
8 files changed, 210 insertions, 14 deletions
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 <matthias@gamezock.de> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include "FSInfoPacket.h" +#include <cstring> +#include <sstream> + +namespace Mad { +namespace Net { +namespace Packets { + +void FSInfoPacket::assemblePacket() { + std::string str; + + for(std::vector<Common::SystemBackend::FSInfo>::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 <matthias@gamezock.de> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef MAD_NET_PACKETS_FSINFOPACKET_H_ +#define MAD_NET_PACKETS_FSINFOPACKET_H_ + +#include "../Packet.h" +#include <Common/SystemBackend.h> + +#include <vector> + +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<Common::SystemBackend::FSInfo> fsList; + + void assemblePacket(); + void parsePacket(); + + public: + FSInfoPacket(Type type, uint16_t requestId, const std::vector<Common::SystemBackend::FSInfo> &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<Common::SystemBackend::FSInfo>& 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<Common::HostInfo>::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<Common::HostInfo>::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@ |