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 "SupportsCachedPongs.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::SupportsCachedPongs; 00030 00031 const Ggep::GgepId SupportsCachedPongs::Id ("SCP", 3); 00032 00033 SupportsCachedPongs::SupportsCachedPongs (const GgepId &id, int flags, int dataSize) 00034 : Ggep (id, flags, dataSize) 00035 { 00036 } 00037 00038 SupportsCachedPongs::SupportsCachedPongs (PongPreference pongPreference) 00039 : Ggep (Id) 00040 { 00041 p.pongPreference = pongPreference; 00042 } 00043 00044 00045 const SupportsCachedPongs * SupportsCachedPongs::findIn (const GgepBlock &ggepBlock) 00046 { 00047 const SupportsCachedPongs *res = 0; 00048 foreach (const Ggep *extension, ggepBlock.extensions()) { 00049 res = dynamic_cast <const SupportsCachedPongs *> (extension); 00050 if (res != 0) 00051 return res; 00052 } 00053 return 0; 00054 } 00055 00056 SupportsCachedPongs * SupportsCachedPongs::copy() const 00057 { 00058 return new SupportsCachedPongs (*this); 00059 } 00060 00061 bool SupportsCachedPongs::prepareReadData (const QByteArray &) 00062 { 00063 return true; // Additional data is optional. 00064 } 00065 00066 void SupportsCachedPongs::readData (QDataStream &stream) 00067 { 00068 int totalCount = dataLength(); 00069 uchar temp; 00070 if (totalCount == 0) { 00071 p.pongPreference = NoPreference; 00072 return; 00073 } 00074 stream >> temp; 00075 if (temp & 0x01) 00076 p.pongPreference = UltrapeerPreference; 00077 else 00078 p.pongPreference = LeafPreference; 00079 00080 while (--totalCount > 0) 00081 stream >> temp; 00082 } 00083 00084 int SupportsCachedPongs::prepareWriteData() const 00085 { 00086 if (p.pongPreference == NoPreference) 00087 return 0; 00088 else 00089 return 1; 00090 } 00091 00092 void SupportsCachedPongs::writeData (QDataStream &stream) const 00093 { 00094 if (p.pongPreference == NoPreference) 00095 return; 00096 else if (p.pongPreference == UltrapeerPreference) 00097 stream << static_cast <uchar> (1); 00098 else 00099 stream << static_cast <uchar> (0); 00100 }