PackedHostCaches.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 "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; // No special format to check.
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             // Still no properties are defined, but be ready to parse any.
00078             properties = address.right (address.length() - ampPos);
00079             address = address.left (ampPos);
00080         }
00081         int colonPos = address.indexOf (':');
00082         // \todo if no colonPos, then try with the default 6346
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 }