UdpHostCache.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 "UdpHostCache.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::UdpHostCache;
00030 
00031 const Ggep::GgepId UdpHostCache::Id ("UDPHC", 5);
00032 
00033 UdpHostCache::UdpHostCache (const GgepId &id, int flags, int dataSize)
00034 : Ggep (id, flags, dataSize)
00035 {
00036 }
00037 
00038 UdpHostCache::UdpHostCache (const QString &hostName)
00039 : Ggep (Id)
00040 {
00041     p.hostName = hostName;
00042 }
00043 
00044 
00045 const UdpHostCache * UdpHostCache::findIn (const GgepBlock &ggepBlock)
00046 {
00047     const UdpHostCache *res = 0;
00048     foreach (const Ggep *extension, ggepBlock.extensions()) {
00049         res = dynamic_cast <const UdpHostCache *> (extension);
00050         if (res != 0)
00051             return res;
00052     }
00053     return 0;
00054 }
00055 
00056 UdpHostCache * UdpHostCache::copy() const
00057 {
00058     return new UdpHostCache (*this);
00059 }
00060 
00061 bool UdpHostCache::prepareReadData (const QByteArray &)
00062 {
00063     return true; // The host address is optional.
00064 }
00065 
00066 void UdpHostCache::readData (QDataStream &stream)
00067 {
00068     p.rawBuffer.resize (dataLength());
00069     stream.readRawData (p.rawBuffer.data(), dataLength());
00070     p.hostName = QString::fromLatin1 (p.rawBuffer);
00071     p.rawBuffer.resize (0);
00072 }
00073 
00074 int UdpHostCache::prepareWriteData() const
00075 {
00076     p.rawBuffer = p.hostName.toLatin1();
00077     return p.rawBuffer.length();
00078 }
00079 
00080 void UdpHostCache::writeData (QDataStream &stream) const
00081 {
00082     stream.writeRawData (p.rawBuffer.data(), dataLength());
00083     p.rawBuffer.resize (0);
00084 }