PacketBase.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-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 #ifndef PROTOCOLS__GENERICS__PACKET_BASE_H
00024 #define PROTOCOLS__GENERICS__PACKET_BASE_H
00025 
00026 #include "Imports.h"
00027 //#include "DataBase.h"
00028 #include "Data.h" // \todo change to AbstractValue.h or move to Import.h
00029 
00030 namespace Protocols {
00031 namespace Generics {
00032 
00033 class PacketVisitor;
00034 class PacketFactory;
00035 FORWARD_DECLARE (Testing, class PacketTest)
00036 
00037 
00038 
00058 class PacketBase : public DataBase
00059 {
00060 public:
00061     virtual             ~PacketBase();
00062 
00063     // DataBase pure virtual functions:
00064     virtual auto_ptr <DataBase>
00065                         copy() const = 0;
00066     QByteArray          toRawBytes() const;
00067 
00068     // Own (new) functions:
00069     virtual QString     protocol() const = 0;
00070     virtual QString     name() const = 0;
00071     //virtual void      accept (PacketVisitor &); //! Infrastructure for the visitor pattern.
00072 
00073     bool                parse (const QByteArray &rawHeader,
00074                                const QByteArray &rawPayload);
00075 
00076     QByteArray          rawHeader() const;
00077     QByteArray          rawPayload() const;
00078     QByteArray          rawPacket() const;
00079 
00080     quint32             headerLength() const;
00081     quint32             payloadLength() const;
00082     quint32             packetLength() const;
00083 
00084 protected:
00085     class Data;         // Needed for ctor declaration.
00086                         PacketBase (Data *d);
00087 
00088     virtual void        invalidateHeader();
00089     virtual void        invalidatePayload();
00090 
00091     virtual bool        readHeader (const QByteArray &rawHeader) = 0;
00092     virtual bool        readPayload (const QByteArray &rawPayload) = 0;
00093 
00094     virtual QByteArray  writeHeader() const = 0;
00095     virtual QByteArray  writePayload() const = 0;
00096 
00098 
00112     class Data : public SharedData
00113     {
00114     public:
00115         Data() : rawHeader(), rawPayload(), rewriteHeader(), rewritePayload() {}
00116 
00117         mutable QByteArray  rawHeader;
00118         mutable QByteArray  rawPayload;
00119         mutable bool        rewriteHeader;
00120         mutable bool        rewritePayload;
00121     };
00122 
00124     SharedDataPointer       d_ptr;
00125 
00126 private:
00127     Q_DECLARE_SHARED_DATA (Data);
00128 
00129     friend class PacketFactory;
00130     friend class Testing::PacketTest; // \todo swith to CalitkoMocks??
00131 
00132     bool                doParse (const QByteArray &rawHeader,
00133                                  const QByteArray &rawPayload);
00134 };
00135 
00137 inline QByteArray PacketBase::rawPacket() const
00138 { return rawHeader() + rawPayload(); }
00139 
00141 
00145 inline quint32 PacketBase::headerLength() const
00146 { return rawHeader().length(); }
00147 
00149 
00153 inline quint32 PacketBase::payloadLength() const
00154 { return rawPayload().length(); }
00155 
00157 
00161 inline quint32 PacketBase::packetLength() const
00162 { return headerLength() + payloadLength(); }
00163 
00165 inline QByteArray PacketBase::toRawBytes() const
00166 { return rawPacket(); }
00167 
00168 } // namespace Generics
00169 } // namespace Protocols
00170 
00171 #endif // PROTOCOLS__GENERICS__PACKET_BASE_H