diff options
Diffstat (limited to 'src/Net/Packets')
-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 |
5 files changed, 179 insertions, 13 deletions
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@ |