00001 // 00002 // QueryHashMaster.cpp 00003 // 00004 // Copyright (c) Shareaza Development Team, 2002-2005. 00005 // This file is part of SHAREAZA (www.shareaza.com) 00006 // 00007 // Shareaza is free software; you can redistribute it 00008 // and/or modify it under the terms of the GNU General Public License 00009 // as published by the Free Software Foundation; either version 2 of 00010 // the License, or (at your option) any later version. 00011 // 00012 // Shareaza 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 Shareaza; if not, write to the Free Software 00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 // 00021 00022 #include "StdAfx.h" 00023 #include "Shareaza.h" 00024 #include "QueryHashMaster.h" 00025 #include "QueryHashGroup.h" 00026 #include "Neighbour.h" 00027 #include "Library.h" 00028 #include "LibraryDictionary.h" 00029 #include "Transfers.h" 00030 #include "Downloads.h" 00031 #include "Download.h" 00032 00033 #ifdef _DEBUG 00034 #undef THIS_FILE 00035 static char THIS_FILE[]=__FILE__; 00036 #define new DEBUG_NEW 00037 #endif 00038 00039 CQueryHashMaster QueryHashMaster; 00040 00041 00043 // CQueryHashMaster construction 00044 00045 CQueryHashMaster::CQueryHashMaster() 00046 { 00047 m_nPerGroup = 0; 00048 } 00049 00050 CQueryHashMaster::~CQueryHashMaster() 00051 { 00052 ASSERT( GetCount() == 0 ); 00053 } 00054 00056 // CQueryHashMaster create 00057 00058 void CQueryHashMaster::Create() 00059 { 00060 CQueryHashTable::Create(); 00061 00062 m_nPerGroup = 250; 00063 m_bValid = FALSE; 00064 m_bLive = FALSE; 00065 m_nCookie = 0; 00066 } 00067 00069 // CQueryHashMaster add neighbour 00070 00071 void CQueryHashMaster::Add(CQueryHashTable* pTable) 00072 { 00073 ASSERT( m_nPerGroup > 0 ); 00074 ASSERT( pTable != NULL ); 00075 ASSERT( pTable->m_nHash > 0 ); 00076 ASSERT( pTable->m_pGroup == NULL ); 00077 00078 for ( POSITION pos = GetIterator() ; pos ; ) 00079 { 00080 CQueryHashGroup* pGroup = GetNext( pos ); 00081 00082 if ( pGroup->m_nHash == pTable->m_nHash && 00083 pGroup->GetCount() < m_nPerGroup ) 00084 { 00085 pGroup->Add( pTable ); 00086 m_bValid = FALSE; 00087 return; 00088 } 00089 } 00090 00091 CQueryHashGroup* pGroup = new CQueryHashGroup( pTable->m_nHash ); 00092 m_pGroups.AddTail( pGroup ); 00093 pGroup->Add( pTable ); 00094 m_bValid = FALSE; 00095 } 00096 00098 // CQueryHashMaster remove neighbour 00099 00100 void CQueryHashMaster::Remove(CQueryHashTable* pTable) 00101 { 00102 ASSERT( pTable != NULL ); 00103 if( pTable->m_pGroup == NULL ) return; 00104 00105 CQueryHashGroup* pGroup = pTable->m_pGroup; 00106 pGroup->Remove( pTable ); 00107 00108 if ( pGroup->GetCount() == 0 ) 00109 { 00110 POSITION pos = m_pGroups.Find( pGroup ); 00111 ASSERT( pos != NULL ); 00112 m_pGroups.RemoveAt( pos ); 00113 delete pGroup; 00114 } 00115 00116 m_bValid = FALSE; 00117 } 00118 00120 // CQueryHashMaster build 00121 00122 void CQueryHashMaster::Build() 00123 { 00124 DWORD tNow = GetTickCount(); 00125 00126 if ( m_bValid ) 00127 { 00128 if ( tNow - m_nCookie < 600000 ) return; 00129 } 00130 else 00131 { 00132 if ( tNow - m_nCookie < 20000 ) return; 00133 } 00134 00135 { 00136 CSingleLock oLock( &Library.m_pSection ); 00137 if ( !oLock.Lock( 500 ) ) return; 00138 CQueryHashTable* pLocalTable = LibraryDictionary.GetHashTable(); 00139 if ( pLocalTable == NULL ) return; 00140 00141 Clear(); 00142 Merge( pLocalTable ); 00143 } 00144 00145 for ( POSITION pos = GetIterator() ; pos ; ) 00146 { 00147 CQueryHashGroup* pGroup = GetNext( pos ); 00148 Merge( pGroup ); 00149 } 00150 00151 if ( Transfers.m_pSection.Lock( 100 ) ) 00152 { 00153 for ( POSITION pos = Downloads.GetIterator() ; pos ; ) 00154 { 00155 CDownload* pDownload = Downloads.GetNext( pos ); 00156 00157 if ( pDownload->m_bSHA1 ) 00158 { 00159 AddString( CSHA::HashToString( &pDownload->m_pSHA1, TRUE ) ); 00160 } 00161 00162 if ( pDownload->m_bED2K ) 00163 { 00164 AddString( CED2K::HashToString( &pDownload->m_pED2K, TRUE ) ); 00165 } 00166 00167 if ( pDownload->m_bBTH ) 00168 { 00169 AddString( _T("BTIH") ); 00170 AddString( _T("urn:btih:") + CSHA::HashToString( &pDownload->m_pBTH, FALSE ) ); 00171 } 00172 } 00173 00174 Transfers.m_pSection.Unlock(); 00175 } 00176 00177 m_bValid = TRUE; 00178 m_bLive = TRUE; 00179 m_nCookie = tNow; 00180 }