From b503a70fca019368399038cde649b3ef8df85bb9 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 18 Sep 2008 14:51:48 +0200 Subject: Flexiblere ?bertragung von Host-Listen --- src/Net/Packets/HostListPacket.cpp | 87 ++++++++++++++++++++++++++++++++++++++ src/Net/Packets/HostListPacket.h | 73 ++++++++++++++++++++++++++++++++ src/Net/Packets/Makefile.am | 4 +- src/Net/Packets/Makefile.in | 10 ++--- src/Net/Packets/NameListPacket.cpp | 56 ------------------------ src/Net/Packets/NameListPacket.h | 65 ---------------------------- 6 files changed, 167 insertions(+), 128 deletions(-) create mode 100644 src/Net/Packets/HostListPacket.cpp create mode 100644 src/Net/Packets/HostListPacket.h delete mode 100644 src/Net/Packets/NameListPacket.cpp delete mode 100644 src/Net/Packets/NameListPacket.h (limited to 'src/Net') diff --git a/src/Net/Packets/HostListPacket.cpp b/src/Net/Packets/HostListPacket.cpp new file mode 100644 index 0000000..30a33b3 --- /dev/null +++ b/src/Net/Packets/HostListPacket.cpp @@ -0,0 +1,87 @@ +/* + * HostListPacket.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 "HostListPacket.h" +#include +#include + +namespace Mad { +namespace Net { +namespace Packets { + +void HostListPacket::assemblePacket() { + std::ostringstream stream; + + 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(); + + setLength(sizeof(uint16_t) + sizeof(HostData)*hostList.size() + str.length()); + + nHosts = (uint16_t*)rawData->data; + hostData = (HostData*)(rawData->data + sizeof(uint16_t)); + charData = (char*)(rawData->data + sizeof(uint16_t) + sizeof(HostData)*hostList.size()); + + std::memcpy(charData, str.c_str(), str.length()); + + *nHosts = htons(hostList.size()); + + for(size_t i = 0; i < hostList.size(); ++i) { + hostData[i].status = htons((uint16_t)hostList[i].getStatus()); + } +} + +void HostListPacket::parsePacket() { + hostList.clear(); + + if(getLength() < sizeof(uint16_t)) + return; + + nHosts = (uint16_t*)rawData->data; + hostList.resize(ntohs(*nHosts)); + + if(getLength() < sizeof(uint16_t) + sizeof(HostData)*hostList.size()) + setLength(sizeof(uint16_t) + sizeof(HostData)*hostList.size()); + + nHosts = (uint16_t*)rawData->data; + hostData = (HostData*)(rawData->data + sizeof(uint16_t)); + charData = (char*)(rawData->data + sizeof(uint16_t) + sizeof(HostData)*hostList.size()); + + std::istringstream stream(std::string(charData, getLength() - (sizeof(uint16_t)+sizeof(HostData)*hostList.size()))); + + for(size_t i = 0; i < hostList.size(); ++i) { + hostList[i].setStatus((Common::HostInfo::Status)ntohs(hostData[i].status)); + + if(!stream.eof()) { + std::string str; + + std::getline(stream, str); + hostList[i].setName(str); + std::getline(stream, str); + hostList[i].setIP(str); + } + } +} + +} +} +} diff --git a/src/Net/Packets/HostListPacket.h b/src/Net/Packets/HostListPacket.h new file mode 100644 index 0000000..aba6461 --- /dev/null +++ b/src/Net/Packets/HostListPacket.h @@ -0,0 +1,73 @@ +/* + * HostListPacket.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_HOSTLISTPACKET_H_ +#define MAD_NET_PACKETS_HOSTLISTPACKET_H_ + +#include "../Packet.h" +#include + +#include + + +namespace Mad { +namespace Net { +namespace Packets { + +class HostListPacket : public Packet { + protected: + struct HostData { + uint16_t status; + }; + + uint16_t *nHosts; + HostData *hostData; + char *charData; + + std::vector hostList; + + void assemblePacket(); + void parsePacket(); + + public: + HostListPacket(Type type, uint16_t requestId, const std::vector &hosts) : Packet(type, requestId), hostList(hosts) { + assemblePacket(); + } + + HostListPacket(const Packet &p) : Packet(p) { + parsePacket(); + } + + HostListPacket& operator=(const Packet &p) { + Packet::operator=(p); + parsePacket(); + + return *this; + } + + const std::vector& getHostInfo() const { + return hostList; + } +}; + +} +} +} + +#endif /* MAD_NET_PACKETS_HOSTLISTPACKET_H_ */ diff --git a/src/Net/Packets/Makefile.am b/src/Net/Packets/Makefile.am index 4600c96..6402c10 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 HostStatusPacket.cpp NameListPacket.cpp +libpackets_la_SOURCES = ErrorPacket.cpp HostListPacket.cpp HostStatusPacket.cpp -noinst_HEADERS = ErrorPacket.cpp HostStatusPacket.h NameListPacket.h +noinst_HEADERS = ErrorPacket.h HostListPacket.h HostStatusPacket.h diff --git a/src/Net/Packets/Makefile.in b/src/Net/Packets/Makefile.in index 4553fab..8344b8e 100644 --- a/src/Net/Packets/Makefile.in +++ b/src/Net/Packets/Makefile.in @@ -45,8 +45,8 @@ CONFIG_HEADER = $(top_builddir)/src/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libpackets_la_LIBADD = -am_libpackets_la_OBJECTS = ErrorPacket.lo HostStatusPacket.lo \ - NameListPacket.lo +am_libpackets_la_OBJECTS = ErrorPacket.lo HostListPacket.lo \ + HostStatusPacket.lo libpackets_la_OBJECTS = $(am_libpackets_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -187,8 +187,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = libpackets.la -libpackets_la_SOURCES = ErrorPacket.cpp HostStatusPacket.cpp NameListPacket.cpp -noinst_HEADERS = ErrorPacket.cpp HostStatusPacket.h NameListPacket.h +libpackets_la_SOURCES = ErrorPacket.cpp HostListPacket.cpp HostStatusPacket.cpp +noinst_HEADERS = ErrorPacket.h HostListPacket.h HostStatusPacket.h all: all-am .SUFFIXES: @@ -241,8 +241,8 @@ distclean-compile: -rm -f *.tab.c @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)/HostStatusPacket.Plo@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/NameListPacket.Plo@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/src/Net/Packets/NameListPacket.cpp b/src/Net/Packets/NameListPacket.cpp deleted file mode 100644 index 19f588a..0000000 --- a/src/Net/Packets/NameListPacket.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * NameListPacket.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 "NameListPacket.h" -#include -#include - -namespace Mad { -namespace Net { -namespace Packets { - -void NameListPacket::assemblePacket() { - std::ostringstream stream; - - for(std::vector::iterator name = nameList.begin(); name != nameList.end(); ++name) - stream << *name << std::endl; - - std::string str = stream.str(); - - setLength(str.length()); - std::memcpy(rawData->data, str.c_str(), str.length()); -} - -void NameListPacket::parsePacket() { - nameList.clear(); - - std::istringstream stream(std::string((char*)getData(), getLength())); - - while(!stream.eof()) { - std::string str; - std::getline(stream, str); - - if(!str.empty()) - nameList.push_back(str); - } -} - -} -} -} diff --git a/src/Net/Packets/NameListPacket.h b/src/Net/Packets/NameListPacket.h deleted file mode 100644 index 89cc040..0000000 --- a/src/Net/Packets/NameListPacket.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * NameListPacket.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_NAMELISTPACKET_H_ -#define MAD_NET_PACKETS_NAMELISTPACKET_H_ - -#include "../Packet.h" - -#include -#include - - -namespace Mad { -namespace Net { -namespace Packets { - -class NameListPacket : public Packet { - protected: - std::vector nameList; - - void assemblePacket(); - void parsePacket(); - - public: - NameListPacket(Type type, uint16_t requestId, const std::vector &nameList0) : Packet(type, requestId), nameList(nameList0) { - assemblePacket(); - } - - NameListPacket(const Packet &p) : Packet(p) { - parsePacket(); - } - - NameListPacket& operator=(const Packet &p) { - Packet::operator=(p); - parsePacket(); - - return *this; - } - - const std::vector& getNameList() const { - return nameList; - } -}; - -} -} -} - -#endif /* MAD_NET_PACKETS_NAMELISTPACKET_H_ */ -- cgit v1.2.3