PackedHostCaches.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "Qt.h"
00024 #include "PackedHostCaches.h"
00025 #include "Gnutella/Packets/Extensions/GgepBlock.h"
00026
00027 using Gnutella::Packets::Extensions::Ggep;
00028 using Gnutella::Packets::Extensions::GgepBlock;
00029 using Gnutella::Packets::Extensions::Ggeps::PackedHostCaches;
00030 using Protocols::Transports::NodeAddress;
00031
00032 const Ggep::GgepId PackedHostCaches::Id ("PHC", 3);
00033
00034 PackedHostCaches::PackedHostCaches (const GgepId &id, int flags, int dataSize)
00035 : Ggep (id, flags, dataSize)
00036 {
00037 }
00038
00039 PackedHostCaches::PackedHostCaches (const QList <NodeAddress> &addresses)
00040 : Ggep (Id)
00041 {
00042 p.addresses = addresses;
00043 }
00044
00045
00046 const PackedHostCaches * PackedHostCaches::findIn (const GgepBlock &ggepBlock)
00047 {
00048 const PackedHostCaches *res = 0;
00049 foreach (const Ggep *extension, ggepBlock.extensions()) {
00050 res = dynamic_cast <const PackedHostCaches *> (extension);
00051 if (res != 0)
00052 return res;
00053 }
00054 return 0;
00055 }
00056
00057 PackedHostCaches * PackedHostCaches::copy() const
00058 {
00059 return new PackedHostCaches (*this);
00060 }
00061
00062 bool PackedHostCaches::prepareReadData (const QByteArray &)
00063 {
00064 return true;
00065 }
00066
00067 void PackedHostCaches::readData (QDataStream &stream)
00068 {
00069 p.rawBuffer.resize (dataLength());
00070 stream.readRawData (p.rawBuffer.data(), dataLength());
00071
00072 QList <QByteArray> addresses = p.rawBuffer.split ('\n');
00073 foreach (QByteArray address, addresses) {
00074 int ampPos = address.indexOf ('&');
00075 QByteArray properties;
00076 if (ampPos != -1) {
00077
00078 properties = address.right (address.length() - ampPos);
00079 address = address.left (ampPos);
00080 }
00081 int colonPos = address.indexOf (':');
00082
00083 NodeAddress nodeAddress;
00084 nodeAddress.setHostName (QString::fromLatin1(address.left(colonPos)));
00085 nodeAddress.setHostPort (address.right (address.length() - colonPos - 1).toUShort());
00086 p.addresses.append (nodeAddress);
00087 }
00088 p.rawBuffer.resize (0);
00089 }
00090
00091 int PackedHostCaches::prepareWriteData() const
00092 {
00093 p.rawBuffer.resize (0);
00094 foreach (NodeAddress nodeAddress, p.addresses) {
00095 QString address = QString("%1:%2").arg (nodeAddress.hostAddress().toString())
00096 .arg (nodeAddress.hostPort());
00097 p.rawBuffer.append (address.toLatin1());
00098 p.rawBuffer.append ('\n');
00099 }
00100 p.rawBuffer.chop (1);
00101 return p.rawBuffer.length();
00102 }
00103
00104 void PackedHostCaches::writeData (QDataStream &stream) const
00105 {
00106 stream.writeRawData (p.rawBuffer.data(), dataLength());
00107 p.rawBuffer.resize (0);
00108 }