VendorMessage.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 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 "VendorMessage.h"
00025 #include "Imports.cpp"
00026 
00030 VendorMessage::VendorMessage (PayloadDescriptor payloadDescriptor,
00031                               VendorCode vendorCode, quint16 messageType,
00032                               quint16 versionNumber)
00033     : Packet (payloadDescriptor)
00034 {
00035     setTtl (1); // Default TTL for vendor messages to be 1.
00036 
00037     p.vendorCode = vendorCode;
00038     p.messageType = messageType;
00039     p.versionNumber = versionNumber;
00040 }
00041 
00046 VendorMessage::VendorMessage (const QByteArray &rawHeader,
00047                               const QByteArray &rawPayload)
00048     : Packet (rawHeader, rawPayload)
00049 {
00050 }
00051 
00055 VendorMessage::~VendorMessage()
00056 {
00057 }
00058 
00060 
00063 bool VendorMessage::prepareReadPayload (const QByteArray &rawPayload)
00064 {
00065     return rawPayload.length() >= 8;
00066 }
00067 
00069 
00072 void VendorMessage::readPayload (QDataStream &stream)
00073 {
00074     stream >> p.vendorCode;
00075     stream >> p.messageType;
00076     stream >> p.versionNumber;
00077 }
00078 
00080 
00083 int VendorMessage::prepareWritePayload() const
00084 {
00085     return 8;
00086 }
00087 
00089 
00092 void VendorMessage::writePayload(QDataStream &stream) const
00093 {
00094     stream << p.vendorCode;
00095     stream << p.messageType;
00096     stream << p.versionNumber;
00097 }
00098 
00099 // \todo Bad, bad! Make nicer:
00100 #include "QueryStatusRequest.h"
00101 #include "QueryStatusResponse.h"
00102 #include "../UnknownPacket.h"
00103 
00104 using Gnutella::Packets::Packet;
00105 
00106 Packet * VendorMessage::fromRawData (const QByteArray &rawHeader, const QByteArray &rawPayload)
00107 {
00108     // \todo Do that nicer!!
00109     if (rawPayload.left (8) == QByteArray ("BEAR\013\0\01\0", 8))
00110         return new QueryStatusRequest (rawHeader, rawPayload);
00111     if (rawPayload.left (8) == QByteArray ("BEAR\014\0\01\0", 8))
00112         return new QueryStatusResponse (rawHeader, rawPayload);
00113     return new UnknownPacket (rawHeader, rawPayload);
00114 }