PongCache.h

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 #ifndef PONGCACHE_H
00024 #define PONGCACHE_H
00025 
00026 #include "Gnutella/Namespace.h"
00027 #include "Gnutella/Packets/Ping.h"
00028 #include "Gnutella/Packets/Pong.h"
00029 #include "Gnutella/PacketProcessing/PacketProcessor.h"
00030 #include "Gnutella/PacketProcessing/PacketSession.h"
00031 #include "Gnutella/Handshaking/Namespace.h"
00032 
00033 // Open containing namespaces:
00034 namespace Gnutella {
00035 namespace PacketProcessing {
00037 
00038 using Gnutella::LocalPeer;
00039 using Gnutella::PacketProcessing::PacketProcessor;
00040 using Gnutella::PacketProcessing::PacketSession;
00041 using Gnutella::Packets::Ping;
00042 using Gnutella::Packets::Pong;
00043 using Gnutella::Handshaking::SlotAllocator;
00044 
00046 
00058 class PongCache : QObject
00059 {
00060     Q_OBJECT
00061     REFERENCE_OBJECT (PongCache)
00062 
00063 public:
00064     typedef PacketProcessor::Sessions Paths;
00065 
00066             PongCache (PacketProcessor *pp, LocalPeer *);
00067             ~PongCache();
00068 
00069     void    addSession (PacketSession *session);
00070     void    removeSession (PacketSession *session);
00071 
00072     void    cachePong (const Pong &, PacketSession *);
00073     void    sendCachedPongs (const Ping &, PacketSession *session);
00074     Paths   pingRoutePaths (const Ping &);
00075 
00076 private slots:
00077     void refreshCache();
00078 
00079 private:
00080 
00081     enum Constants
00082     {
00083         MinimalPingInterval     = 1000,
00084         PongExpirationTime      = 10000,
00085         MaximalPongHops         = 5,
00086         PongsPerHops            = 1
00087     };
00088 
00089     struct SessionData
00090     {
00091         Pong            directPong;
00092         Pong            neighborPong;
00093         QTime           lastInPingTime;
00094         QTime           lastOutPingTime;
00095 
00096         SessionData();
00097     };
00098 
00099     LocalPeer           *localPeer; // \todo mayber remove dependency
00100     PacketProcessor     *packetProcessor;
00101     SlotAllocator       *slotAllocator;
00102 
00103     Ping                directPing;
00104     Pong                directPong;
00105     Pong                hostPong;
00106     QTimer              pingTimer;
00107 
00108     typedef QPair <Pong, PacketSession *>           PongInfo;
00109     typedef QQueue <PongInfo *>                     PongForwardQueue;
00110     typedef QMap <PacketSession *, SessionData *>   Sessions;
00111 
00112     PongForwardQueue    pongForwardQueue[MaximalPongHops];
00113     PongForwardQueue    cachedPongForwardQueue[MaximalPongHops];
00114     Sessions            sessions;
00115 };
00116 
00117 // Close containing namespaces:
00118 } // namespace PacketProcessing
00119 } // namespace Gnutella
00121 
00122 #endif // PONGCACHE_H