DailyUptime.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 "DailyUptime.h"
00025 
00026 using Gnutella::Packets::Extensions::Ggep;
00027 using Gnutella::Packets::Extensions::GgepBlock;
00028 using Gnutella::Packets::Extensions::Ggeps::DailyUptime;
00029 
00030 const Ggep::GgepId DailyUptime::Id ("DU", 2);   
00031 
00032 DailyUptime::DailyUptime (const GgepId &id, int flags, int dataSize)
00033 : Ggep (id, flags, dataSize)
00034 {
00035     Q_ASSERT (id == Id);
00036     dailyUptime_ = 0;
00037 }
00038 
00039 DailyUptime::DailyUptime (int dailyUptime)
00040 : Ggep (Id)
00041 {
00042     dailyUptime_ = dailyUptime;
00043 }
00044 
00045 const DailyUptime * DailyUptime::findIn (const GgepBlock &ggepBlock)
00046 {
00047     const DailyUptime *res = 0;
00048     foreach (const Ggep *extension, ggepBlock.extensions()) {
00049         res = dynamic_cast <const DailyUptime *> (extension);
00050         if (res != 0)
00051             return res;
00052     }
00053     return 0;
00054 }
00055 
00056 DailyUptime * DailyUptime::copy() const
00057 {
00058     return new DailyUptime (*this);
00059 }
00060 
00061 bool DailyUptime::prepareReadData (const QByteArray &rawData)
00062 {
00063     return rawData.length() >= 1 && rawData.length() <= 4;
00064 }
00065 
00066 void DailyUptime::readData (QDataStream &stream)
00067 {
00068     dailyUptime_ = 0;
00069     for (int i = 0; i < dataLength(); i++)
00070         stream >> *(reinterpret_cast <uchar *> (&dailyUptime_) + i);
00071 
00072     if (dailyUptime_ > 86400) {
00073         qDebug() << "ggep bad uptime " << dailyUptime_;
00074         dailyUptime_ = -1;
00075     }
00076 }
00077 
00078 int DailyUptime::prepareWriteData() const
00079 {
00080     int bytesCount = 0;
00081     int du = dailyUptime_;
00082     for (; du > 0; du >>= 8)
00083         bytesCount++;
00084     return bytesCount;
00085 }
00086 
00087 void DailyUptime::writeData (QDataStream &stream) const
00088 {
00089     for (int i = 0; i < dataLength(); i++)
00090         stream << *(reinterpret_cast <const uchar *> (&dailyUptime_) + i);
00091 }