Ultrapeer.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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     //d->version            = Version();
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; // One byte for version, ultrapeer slots and leaf slots each.
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; // One byte for version, ultrapeer slots and leaf slots each.
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 }