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 "QrtExchanger.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 DefaultQrtSize = 65536 00037 }; 00038 00039 class QrtExchangerPrivate 00040 { 00041 REFERENCE_OBJECT (QrtExchangerPrivate) 00042 00043 public: 00044 typedef QList <PacketSession *> Sessions; 00045 00046 Sessions leafSessions; 00047 Sessions peerSessions; 00048 QueryRoutingTable ownTable; 00049 QueryRoutingTable combinedTable; 00050 00051 QrtExchangerPrivate() 00052 : leafSessions(), peerSessions(), ownTable(), combinedTable() 00053 {} 00054 }; 00055 00056 } // QueryRouting 00057 } // PacketProcessing 00058 } // Gnutella 00059 00060 QrtExchanger::QrtExchanger() 00061 : p (new QrtExchangerPrivate) 00062 { 00063 p->ownTable.reset (DefaultQrtSize); 00064 p->combinedTable.reset (DefaultQrtSize); 00065 } 00066 00067 QrtExchanger::~QrtExchanger() 00068 { 00069 delete p; 00070 } 00071 00072 void QrtExchanger::addSession (PacketSession *session) 00073 { 00074 NodeType nodeType = session->nodeInfo().type; 00075 if (nodeType == Gnutella::TypePeer) 00076 p->peerSessions.append (session); 00077 else if (nodeType == Gnutella::TypeLeaf) 00078 p->leafSessions.append (session); 00079 else if (nodeType == Gnutella::TypeUltrapeer) 00080 ; // \todo p->ultrapeerSessions.append (session); 00081 else 00082 Q_ASSERT (false); 00083 } 00084 00085 void QrtExchanger::removeSession (PacketSession *session) 00086 { 00087 NodeType nodeType = session->nodeInfo().type; 00088 if (nodeType == Gnutella::TypePeer) 00089 p->peerSessions.removeAll (session); 00090 else if (nodeType == Gnutella::TypeLeaf) 00091 p->leafSessions.removeAll (session); 00092 else if (nodeType == Gnutella::TypeUltrapeer) 00093 ; // \todo p->leafSessions.removeAll (session); 00094 else 00095 Q_ASSERT (false); 00096 } 00097 00098 void QrtExchanger::qrtRead (PacketSession *session) 00099 { 00100 NodeType nodeType = session->nodeInfo().type; 00101 if (nodeType == TypeLeaf) { 00102 p->combinedTable = p->ownTable; 00103 foreach (PacketSession *leafSession, p->leafSessions) 00104 p->combinedTable.addTable (leafSession->queryRoutingTable()); 00108 foreach (PacketSession *peerSession, p->peerSessions) 00109 peerSession->sendQrt (p->combinedTable); 00110 } 00111 }