Ultrapeer.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 "Ultrapeer.h"
00025 #include "../GgepBlock.h"
00026
00027 using Gnutella::Packets::Extensions::Ggep;
00028 using Gnutella::Packets::Extensions::GgepBlock;
00029 using Gnutella::Packets::Extensions::Ggeps::Ultrapeer;
00030 using Utils::Version;
00031
00032 const Ggep::GgepId Ultrapeer::Id ("UP", 2);
00033
00034 Ultrapeer::Ultrapeer (const GgepId &id, int flags, int dataSize)
00035 : Ggep (id, flags, dataSize)
00036 {
00037 Q_ASSERT (id == Id);
00038 d = new Data;
00039
00040 d->ref.init (1);
00041
00042
00043 d->ultrapeerSlots = 0;
00044 d->leafSlots = 0;
00045 }
00046
00047 Ultrapeer::Ultrapeer (Version version, int ultrapeerSlots, int leafSlots)
00048 : Ggep (Id)
00049 {
00050 d = new Data;
00051
00052 d->ref.init (1);
00053
00054 d->version = version;
00055 d->ultrapeerSlots = std::min (ultrapeerSlots, 255);
00056 d->leafSlots = std::min (leafSlots, 255);
00057 }
00058
00059 Ultrapeer::~Ultrapeer()
00060 {
00061 if (!d->ref.deref())
00062 delete d;
00063 }
00064
00065 Ultrapeer & Ultrapeer::operator= (const Ultrapeer &other)
00066 {
00067 qAtomicAssign (d, other.d);
00068 return *this;
00069 }
00070
00071 const Ultrapeer * Ultrapeer::findIn (const GgepBlock &ggepBlock)
00072 {
00073 foreach (const Ggep *extension, ggepBlock.extensions()) {
00074 if (const Ultrapeer *res = dynamic_cast <const Ultrapeer *> (extension))
00075 return res;
00076 }
00077 return 0;
00078 }
00079
00080 Ultrapeer * Ultrapeer::copy() const
00081 {
00082 return new Ultrapeer (*this);
00083 }
00084
00085 bool Ultrapeer::prepareReadData (const QByteArray &rawData)
00086 {
00087 return rawData.length() == 3;
00088 }
00089
00090 void Ultrapeer::readData (QDataStream &in)
00091 {
00092 uchar guessStyleVersion;
00093
00094 in >> guessStyleVersion;
00095 in >> d->ultrapeerSlots;
00096 in >> d->leafSlots;
00097
00098 d->version = Version (guessStyleVersion >> 4, guessStyleVersion & 0x0F);
00099 }
00100
00101 int Ultrapeer::prepareWriteData() const
00102 {
00103 return 3;
00104 }
00105
00106 void Ultrapeer::writeData (QDataStream &out) const
00107 {
00108 uchar guessStyleVersion = (d->version.majorVersion() << 4)
00109 | (d->version.minorVersion() & 0x0F);
00110 out << guessStyleVersion << d->ultrapeerSlots << d->leafSlots;
00111 }