00001 // 00002 // DownloadWithSearch.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 "Settings.h" 00025 #include "DownloadWithSearch.h" 00026 #include "SearchManager.h" 00027 #include "ManagedSearch.h" 00028 #include "QuerySearch.h" 00029 00030 #ifdef _DEBUG 00031 #undef THIS_FILE 00032 static char THIS_FILE[]=__FILE__; 00033 #define new DEBUG_NEW 00034 #endif 00035 00036 00038 // CDownloadWithSearch construction 00039 00040 CDownloadWithSearch::CDownloadWithSearch() 00041 { 00042 m_pSearch = NULL; 00043 m_tSearchTime = 0; 00044 m_tSearchCheck = 0; 00045 m_tLastED2KGlobal = 0; 00046 m_tLastED2KLocal = 0; 00047 } 00048 00049 CDownloadWithSearch::~CDownloadWithSearch() 00050 { 00051 if ( m_pSearch ) delete m_pSearch; 00052 } 00053 00054 00056 // CDownloadWithSearch Can Find Sources 00057 00058 BOOL CDownloadWithSearch::FindSourcesAllowed(DWORD tNow) const 00059 { 00060 if ( tNow > m_tSearchTime && tNow - m_tSearchTime > 15*1000 ) 00061 return TRUE; 00062 else 00063 return FALSE; 00064 } 00065 00067 // CDownloadWithSearch find additional sources 00068 00069 BOOL CDownloadWithSearch::FindMoreSources() 00070 { 00071 BOOL bSuccess = CDownloadWithTiger::FindMoreSources(); 00072 00073 if ( CanSearch() ) 00074 { 00075 DWORD tNow = GetTickCount(); 00076 if ( tNow - m_tSearchTime > ( Settings.Downloads.SearchPeriod / 4 ) ) 00077 { 00078 m_tSearchTime = tNow; 00079 if ( m_pSearch != NULL ) m_pSearch->Stop(); 00080 bSuccess = TRUE; 00081 } 00082 } 00083 00084 return bSuccess; 00085 } 00086 00088 // CDownloadWithSearch run process 00089 00090 void CDownloadWithSearch::RunSearch(DWORD tNow) 00091 { 00092 if ( ! CanSearch() ) 00093 { 00094 StopSearch(); 00095 return; 00096 } 00097 00098 if ( tNow > m_tSearchTime && tNow - m_tSearchTime < Settings.Downloads.SearchPeriod ) 00099 { 00100 StartManualSearch(); 00101 } 00102 else if ( tNow > m_tSearchCheck && tNow - m_tSearchCheck >= 1000 ) 00103 { 00104 BOOL bFewSources = GetSourceCount( FALSE, TRUE ) < Settings.Downloads.MinSources; 00105 BOOL bDataStarve = ( tNow > m_tReceived ? tNow - m_tReceived : 0 ) > Settings.Downloads.StarveTimeout * 1000; 00106 00107 m_tSearchCheck = tNow; 00108 00109 if ( IsPaused() == FALSE && ( bFewSources || bDataStarve ) ) 00110 { 00111 StartAutomaticSearch(); 00112 } 00113 else 00114 { 00115 StopSearch(); 00116 } 00117 } 00118 } 00119 00121 // CDownloadWithSearch start (or continue) a manual search 00122 00123 void CDownloadWithSearch::StartManualSearch() 00124 { 00125 CSingleLock pLock( &SearchManager.m_pSection ); 00126 if ( ! pLock.Lock( 50 ) ) return; 00127 00128 PrepareSearch(); 00129 00130 m_pSearch->m_nPriority = CManagedSearch::spHighest; 00131 m_pSearch->Start(); 00132 } 00133 00135 // CDownloadWithSearch start (or continue) an autoamtic search 00136 00137 void CDownloadWithSearch::StartAutomaticSearch() 00138 { 00139 CSingleLock pLock( &SearchManager.m_pSection ); 00140 if ( ! pLock.Lock( 10 ) ) return; 00141 00142 PrepareSearch(); 00143 00144 m_pSearch->m_nPriority = CManagedSearch::spLowest; 00145 m_pSearch->Start(); 00146 } 00147 00149 // CDownloadWithSearch check if we can actually search 00150 00151 BOOL CDownloadWithSearch::CanSearch() const 00152 { 00153 return ( ( m_pFile != NULL ) && ( m_bSHA1 || m_bTiger || m_bED2K || m_bBTH ) ); 00154 } 00155 00157 // CDownloadWithSearch prepare a managed search object 00158 00159 void CDownloadWithSearch::PrepareSearch() 00160 { 00161 if ( m_pSearch == NULL ) m_pSearch = new CManagedSearch(); 00162 CQuerySearch* pSearch = m_pSearch->m_pSearch; 00163 00164 if ( pSearch->m_bAndG1 ) 00165 { 00166 pSearch->m_sSearch = m_sRemoteName; 00167 pSearch->BuildWordList(); 00168 } 00169 00170 if ( m_bSHA1 ) 00171 { 00172 pSearch->m_bSHA1 = TRUE; 00173 pSearch->m_pSHA1 = m_pSHA1; 00174 } 00175 if ( m_bTiger ) 00176 { 00177 pSearch->m_bTiger = TRUE; 00178 pSearch->m_pTiger = m_pTiger; 00179 } 00180 if ( m_bED2K ) 00181 { 00182 pSearch->m_bED2K = TRUE; 00183 pSearch->m_pED2K = m_pED2K; 00184 m_pSearch->m_bAllowED2K = TRUE; 00185 } 00186 else 00187 { 00188 m_pSearch->m_bAllowED2K = FALSE; 00189 } 00190 if ( m_bBTH ) 00191 { 00192 pSearch->m_bBTH = TRUE; 00193 pSearch->m_pBTH = m_pBTH; 00194 } 00195 00196 pSearch->m_bWantURL = TRUE; 00197 pSearch->m_bWantDN = ( m_sRemoteName.GetLength() == 0 ); 00198 pSearch->m_bWantXML = FALSE; 00199 pSearch->m_bWantPFS = TRUE; 00200 pSearch->m_bWantCOM = FALSE; 00201 00202 if ( m_nSize == SIZE_UNKNOWN ) 00203 { 00204 pSearch->m_nMinSize = 0; 00205 pSearch->m_nMaxSize = SIZE_UNKNOWN; 00206 pSearch->m_bWantDN = TRUE; 00207 } 00208 else 00209 { 00210 pSearch->m_nMinSize = m_nSize; 00211 pSearch->m_nMaxSize = m_nSize; 00212 } 00213 } 00214 00216 // CDownloadWithSearch stop searching 00217 00218 void CDownloadWithSearch::StopSearch() 00219 { 00220 if ( m_pSearch != NULL ) m_pSearch->Stop(); 00221 }