00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "Network.h"
00026 #include "NeighboursWithG2.h"
00027 #include "G2Neighbour.h"
00028 #include "G2Packet.h"
00029 #include "HubHorizon.h"
00030 #include "RouteCache.h"
00031 #include "HostCache.h"
00032
00033 #ifdef _DEBUG
00034 #undef THIS_FILE
00035 static char THIS_FILE[]=__FILE__;
00036 #define new DEBUG_NEW
00037 #endif
00038
00039
00041
00042
00043 CNeighboursWithG2::CNeighboursWithG2()
00044 {
00045 }
00046
00047 CNeighboursWithG2::~CNeighboursWithG2()
00048 {
00049 }
00050
00052
00053
00054 void CNeighboursWithG2::Connect()
00055 {
00056 CNeighboursWithG1::Connect();
00057 HubHorizonPool.Setup();
00058 }
00059
00061
00062
00063 CG2Packet* CNeighboursWithG2::CreateQueryWeb(GGUID* pGUID, CNeighbour* pExcept)
00064 {
00065 CG2Packet* pPacket = CG2Packet::New( G2_PACKET_QUERY_ACK, TRUE );
00066
00067 DWORD tNow = time( NULL );
00068
00069 pPacket->WritePacket( "TS", 4 );
00070 pPacket->WriteLongBE( tNow );
00071
00072 theApp.Message( MSG_DEBUG, _T("Creating a query acknowledgement:") );
00073
00074 pPacket->WritePacket( "D", 8 );
00075 pPacket->WriteLongLE( Network.m_pHost.sin_addr.S_un.S_addr );
00076 pPacket->WriteShortBE( htons( Network.m_pHost.sin_port ) );
00077 pPacket->WriteShortBE( GetCount( PROTOCOL_G2, nrsConnected, ntLeaf ) );
00078
00079 for ( POSITION pos = GetIterator() ; pos ; )
00080 {
00081 CG2Neighbour* pNeighbour = (CG2Neighbour*)GetNext( pos );
00082
00083 if ( pNeighbour->m_nProtocol == PROTOCOL_G2 &&
00084 pNeighbour->m_nNodeType != ntLeaf &&
00085 pNeighbour->m_nState >= nrsConnected &&
00086 pNeighbour != pExcept )
00087 {
00088 pPacket->WritePacket( "D", 8 );
00089 pPacket->WriteLongLE( pNeighbour->m_pHost.sin_addr.S_un.S_addr );
00090 pPacket->WriteShortBE( htons( pNeighbour->m_pHost.sin_port ) );
00091 pPacket->WriteShortBE( (WORD)pNeighbour->m_nLeafCount );
00092
00093 theApp.Message( MSG_DEBUG, _T(" Done neighbour %s"),
00094 (LPCTSTR)pNeighbour->m_sAddress );
00095 }
00096 }
00097
00098 int nCount = ( pExcept == NULL ) ? 3 : 25;
00099
00100 for ( CHostCacheHost* pHost = HostCache.Gnutella2.GetNewest() ; pHost ; pHost = pHost->m_pPrevTime )
00101 {
00102 if ( pHost->CanQuote( tNow ) &&
00103 Get( &pHost->m_pAddress ) == NULL &&
00104 HubHorizonPool.Find( &pHost->m_pAddress ) == NULL )
00105 {
00106 pPacket->WritePacket( "S", 10 );
00107 pPacket->WriteLongLE( pHost->m_pAddress.S_un.S_addr );
00108 pPacket->WriteShortBE( pHost->m_nPort );
00109 pPacket->WriteLongBE( pHost->m_tSeen );
00110
00111 theApp.Message( MSG_DEBUG, _T(" Try cached hub %s"),
00112 (LPCTSTR)CString( inet_ntoa( pHost->m_pAddress ) ) );
00113
00114 if ( ! --nCount ) break;
00115 }
00116 }
00117
00118 HubHorizonPool.AddHorizonHubs( pPacket );
00119
00120 pPacket->WriteByte( 0 );
00121 pPacket->Write( pGUID, sizeof(GGUID) );
00122
00123 return pPacket;
00124 }
00125
00127
00128
00129 CG2Neighbour* CNeighboursWithG2::GetRandomHub(CG2Neighbour* pExcept, GGUID* pGUID)
00130 {
00131 CPtrArray pRandom;
00132
00133 for ( POSITION pos = GetIterator() ; pos ; )
00134 {
00135 CG2Neighbour* pNeighbour = (CG2Neighbour*)GetNext( pos );
00136
00137 if ( pNeighbour->m_nState == nrsConnected &&
00138 pNeighbour->m_nProtocol == PROTOCOL_G2 &&
00139 pNeighbour->m_nNodeType != ntLeaf &&
00140 pNeighbour != pExcept )
00141 {
00142 if ( pNeighbour->m_pGUIDCache->Lookup( pGUID ) == NULL )
00143 {
00144 pRandom.Add( pNeighbour );
00145 }
00146 }
00147 }
00148
00149 int nSize = pRandom.GetSize();
00150 if ( ! nSize ) return NULL;
00151
00152 nSize = rand() % nSize;
00153
00154 return (CG2Neighbour*)pRandom.GetAt( nSize );
00155 }
00156