QrtWriter.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 "QrtWriter.h"
00025 #include "QueryRoutingTable.h"
00026 #include "Imports.cpp"
00027 
00028 using namespace Gnutella::PacketProcessing::QueryRouting;
00029 
00030 namespace Gnutella {
00031 namespace PacketProcessing {
00032 namespace QueryRouting {
00033 
00034 enum Constants
00035 {
00036     PatchSize = 1024    
00037 };
00038 
00039 class QrtWriterPrivate
00040 {
00041     REFERENCE_OBJECT (QrtWriterPrivate)
00042 
00043 public:
00044     PacketSession       *session;
00045     QueryRoutingTable   oldTable;
00046     quint8              patchEntryBits;
00047 
00048     QrtWriterPrivate() :    session (0), oldTable(), patchEntryBits (0) {}
00049 };
00050 
00051 } // QueryRouting
00052 } // PacketProcessing
00053 } // Gnutella
00054 
00055 QrtWriter::QrtWriter (PacketSession *session, quint8 patchEntryBits)
00056  :  d (new QrtWriterPrivate)
00057 {
00058     d->session = session;
00059     d->patchEntryBits = patchEntryBits;
00060 }
00061 
00062 QrtWriter::~QrtWriter()
00063 {
00064     delete d;
00065 }
00066 
00067 void QrtWriter::write (const QueryRoutingTable &newTable)
00068 {
00069     if (d->oldTable.size() != newTable.size()) {
00070         QueryRoutingReset reset (newTable.size(), 2);
00071         d->session->sendPacket (reset);
00072         d->oldTable.reset (newTable.size());
00073     }
00074     QByteArray patchBuffer = d->oldTable.makePatchTo (newTable,
00075                                                       d->patchEntryBits);
00076     d->oldTable = newTable;
00077     patchBuffer = compress (patchBuffer);
00078 
00079     int patchSize = PatchSize;
00080     int seqSize = patchBuffer.size() / patchSize;
00081     if (patchBuffer.size() % patchSize != 0)
00082         seqSize++;
00083 
00084     char *patchStart = patchBuffer.data();
00085     char *patchEnd = patchStart + patchBuffer.size();
00086     int seqNo;
00087     for (seqNo = 0; seqNo < seqSize && patchStart + patchSize <= patchEnd;
00088         seqNo++, patchStart += patchSize) {
00089         QByteArray patchPiece (patchStart, patchSize);
00090         QueryRoutingPatch patch (seqNo, seqSize, QueryRoutingPatch::ZLIB,
00091                                  d->patchEntryBits, patchPiece);
00092         d->session->sendPacket (patch);
00093     }
00094     if (patchStart != patchEnd) {
00095         QByteArray patchPiece (patchStart, patchEnd - patchStart);
00096         QueryRoutingPatch patch (seqNo, seqSize, QueryRoutingPatch::ZLIB,
00097                                  d->patchEntryBits, patchPiece);
00098         d->session->sendPacket (patch);
00099     }
00100 }
00101