PingHandler.cpp

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 #include "Qt.h"
00024 #include "PingHandler.h"
00025 #include "UdpHostCache.h"
00026 #include "Imports.cpp"
00027 
00028 using namespace Gnutella::Bootstrapping;
00029 
00030 namespace Gnutella {
00031 namespace Bootstrapping {
00032 
00033 enum Constants
00034 {
00035     IPPAddresses    = 20
00036 };
00037 
00038 } // namespace Bootstrapping;
00039 } // namespace Gnutella;
00040 
00041 PingHandler::PingHandler (LocalPeer *localPeer)
00042  :  d()
00043 {
00044     d.localPeer         = localPeer;
00045     d.packetProcessor   = localPeer->packetProcessor();
00046     d.nodeCache         = localPeer->nodeCache();
00047 }
00048 
00049 PingHandler::~PingHandler()
00050 {
00051 }
00052 
00053 Pong PingHandler::makePongFor (const Ping &ping)
00054 {
00055     // Extract some info from the ping:
00056     const SupportsCachedPongs *scp = SupportsCachedPongs::findIn (ping.ggepBlock());
00057     int ttl = ping.hops() + ping.ttl(); // \todo Make sure ttl is not bigger than a maximum?
00058     int hops = 0;
00059     QList <Pong> pongs;
00060 
00061     // \todo Return a cached pong no older than some time. UHC makes no cacheing itself!
00062     Pong ownPong;
00063     // \todo Use constants instead of fixed numbers:
00064     ownPong.setTtl (ttl);
00065     ownPong.setHops (hops);
00066     ownPong.setSharedKilobytes (1 << 21); // \todo Square of two means we are an ultrapeer.
00067     ownPong.setSharedFiles (1<<10); // \todo Get from FileManager?
00068 
00069     GgepBlock ggepBlock;
00070     Ultrapeer ultrapeer (true, 10, 10); // \todo Ask SlotAllocator
00071 
00072     Packets::VendorCode myVendorCode ("CLTK");
00073     Version myVersion (0, 5); // \todo Get version from LocalPeer
00074     Packets::Extensions::Ggeps::VendorCode vendorCode (myVendorCode, myVersion);
00075 
00076     DailyUptime dailyUptime (34345); // \todo Calculate and store it.
00077 
00078     ggepBlock.addExtension (ultrapeer);
00079     ggepBlock.addExtension (vendorCode);
00080     ggepBlock.addExtension (dailyUptime);
00081     if (scp) {
00082         // \todo read the scp and use a predicate in getNodes! Move the predicates to a header!!!
00083         NodeSet nodes = d.nodeCache->getNodes (IPPAddresses,
00084                                               NodeCache::NodeAvailable,
00085                                               true);
00086         IpPort ipp (nodes.toList());
00087         ggepBlock.addExtension (ipp);
00088         // Should we return a list of UHCs?
00089     }
00090     ownPong.setGgepBlock (ggepBlock);
00091     return ownPong;
00092 }
00093 
00094 void PingHandler::processPing (const Ping &ping)
00095 {
00096     Pong pong = makePongFor (ping);
00097     d.packetProcessor->sendPacket (pong);
00098 }