📄 searchmanager.cpp
字号:
/*
Copyright (C)2003 Barry Dunne (http://www.emule-project.net)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
// Note To Mods //
/*
Please do not change anything here and release it..
There is going to be a new forum created just for the Kademlia side of the client..
If you feel there is an error or a way to improve something, please
post it in the forum first and let us look at it.. If it is a real improvement,
it will be added to the offical client.. Changing something without knowing
what all it does can cause great harm to the network if released in mass form..
Any mod that changes anything within the Kademlia side will not be allowed to advertise
there client on the eMule forum..
*/
#include "stdafx.h"
#include "./SearchManager.h"
#include "./Tag.h"
#include "./Defines.h"
#include "./Kademlia.h"
#include "./Indexed.h"
#include "./prefs.h"
#include "../io/IOException.h"
#include "../../resource.h"
#include "../../SafeFile.h"
#include "../../Log.h"
#include "../../emule.h"
#include "../../emuledlg.h"
#include "../../kademliawnd.h"
#include "../../KadSearchListCtrl.h"
#include "../../SearchDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
LPCSTR _aszInvKadKeywordCharsA = INV_KAD_KEYWORD_CHARS;
LPCTSTR _aszInvKadKeywordChars = _T(INV_KAD_KEYWORD_CHARS);
LPCWSTR _awszInvKadKeywordChars = L" ()[]{}<>,._-!?:;\\/";
using namespace Kademlia;
uint32 CSearchManager::m_uNextID = 0;
SearchMap CSearchManager::m_mapSearches;
bool CSearchManager::IsSearching(uint32 uSearchID)
{
// Check if this searchID is within the searches.
for (SearchMap::const_iterator itSearchMap = m_mapSearches.begin(); itSearchMap != m_mapSearches.end(); ++itSearchMap)
{
if (itSearchMap->second->m_uSearchID == uSearchID)
return true;
}
return false;
}
void CSearchManager::StopSearch(uint32 uSearchID, bool bDelayDelete)
{
// Stop a specific searchID
for (SearchMap::iterator itSearchMap = m_mapSearches.begin(); itSearchMap != m_mapSearches.end(); ++itSearchMap)
{
if (itSearchMap->second->m_uSearchID == uSearchID)
{
// Do not delete as we want to get a chance for late packets to be processed.
if(bDelayDelete)
itSearchMap->second->PrepareToStop();
else
{
// Delete this search now.
delete itSearchMap->second;
itSearchMap = m_mapSearches.erase(itSearchMap);
}
return;
}
}
}
void CSearchManager::StopAllSearches()
{
// Stop and delete all searches.
for (SearchMap::iterator itSearchMap = m_mapSearches.begin(); itSearchMap != m_mapSearches.end(); ++itSearchMap)
delete itSearchMap->second;
m_mapSearches.clear();
}
bool CSearchManager::StartSearch(CSearch* pSearch)
{
// A search object was created, now try to start the search.
if (AlreadySearchingFor(pSearch->m_uTarget))
{
// There was already a search in progress with this target.
delete pSearch;
return false;
}
// Add to the search map
m_mapSearches[pSearch->m_uTarget] = pSearch;
// Start the search.
pSearch->Go();
return true;
}
CSearch* CSearchManager::PrepareFindKeywords(bool bUnicode, LPCTSTR szKeyword, UINT uSearchTermsSize, LPBYTE pucSearchTermsData)
{
// Create a keyword search object.
CSearch *pSearch = new CSearch;
try
{
// Set search to a keyword type.
pSearch->m_uType = CSearch::KEYWORD;
// Make sure we have a keyword list.
GetWords(szKeyword, &pSearch->m_listWords);
if (pSearch->m_listWords.size() == 0)
{
delete pSearch;
throw GetResString(IDS_KAD_SEARCH_KEYWORD_TOO_SHORT);
}
// Get the targetID based on the primary keyword.
CStringW wstrKeyword = pSearch->m_listWords.front();
if (bUnicode)
KadGetKeywordHash(wstrKeyword, &pSearch->m_uTarget);
else
{
// backward compatibility: use local ACP encoding
// TODO: to be removed in some months (when majority of nodes are Unicode compatible)
CStringA strA(wstrKeyword);
KadGetKeywordHash(strA, &pSearch->m_uTarget);
}
// Verify that we are not already searching for this target.
if (AlreadySearchingFor(pSearch->m_uTarget))
{
delete pSearch;
CString strError;
strError.Format(GetResString(IDS_KAD_SEARCH_KEYWORD_ALREADY_SEARCHING), CString(wstrKeyword));
throw strError;
}
// Write complete packet
pSearch->m_pfileSearchTerms = new CSafeMemFile();
pSearch->m_pfileSearchTerms->WriteUInt128(&pSearch->m_uTarget);
/*if(bKad2) // Need to add a swtich between Kad1.0 and Kad2.0
{
if (uSearchTermsSize == 0)
{
// JOHNTODO - Need to add ability to change start position.
// Start position range (0x0 to 0x7FFF)
pSearch->m_pfileSearchTerms->WriteUInt16((uint16)0x0000);
}
else
{
// JOHNTODO - Need to add ability to change start position.
// Start position range (0x8000 to 0xFFFF)
pSearch->m_pfileSearchTerms->WriteUInt16((uint16)0x8000);
pSearch->m_pfileSearchTerms->Write(pucSearchTermsData, uSearchTermsSize);
}
}
else*/
{
if (uSearchTermsSize == 0)
{
pSearch->m_pfileSearchTerms->WriteUInt8(0);
// We send this extra byte to flag we handle large files.
pSearch->m_pfileSearchTerms->WriteUInt8(0);
}
else
{
// Set to 2 to flag we handle handle large files.
pSearch->m_pfileSearchTerms->WriteUInt8(2);
pSearch->m_pfileSearchTerms->Write(pucSearchTermsData, uSearchTermsSize);
}
}
// Inc our searchID
pSearch->m_uSearchID = ++m_uNextID;
// Insert search into map.
m_mapSearches[pSearch->m_uTarget] = pSearch;
// Start search.
pSearch->Go();
}
catch (CIOException* ioe)
{
CString strError;
strError.Format(_T("IO-Exception in %hs: Error %u"), __FUNCTION__, ioe->m_iCause);
ioe->Delete();
delete pSearch;
throw strError;
}
catch (CFileException* e)
{
TCHAR szError[MAX_CFEXP_ERRORMSG];
e->m_strFileName = "search packet";
e->GetErrorMessage(szError, ARRSIZE(szError));
CString strError;
strError.Format(_T("Exception in %hs: %s"), __FUNCTION__, szError);
e->Delete();
delete pSearch;
throw strError;
}
catch (CString strException)
{
throw strException;
}
catch (...)
{
CString strError;
strError.Format(_T("Unknown exception in %hs"), __FUNCTION__);
delete pSearch;
throw strError;
}
return pSearch;
}
CSearch* CSearchManager::PrepareLookup(uint32 uType, bool bStart, const CUInt128 &uID)
{
// Prepare a kad lookup.
// Make sure this target is not already in progress.
if(AlreadySearchingFor(uID))
return NULL;
// Create a new search.
CSearch *pSearch = new CSearch;
// Set type and target.
pSearch->m_uType = uType;
pSearch->m_uTarget = uID;
try
{
switch(pSearch->m_uType)
{
case CSearch::STOREKEYWORD:
if(!Kademlia::CKademlia::GetIndexed()->SendStoreRequest(uID))
{
// Keyword Store was determined to be a overloaded node, abort store.
delete pSearch;
return NULL;
}
break;
case CSearch::FINDBUDDY:
pSearch->m_pfileSearchTerms = new CSafeMemFile();
// Send the ID we used to find our buddy. Used for checks later and allows users to callback someone if they change buddies.
pSearch->m_pfileSearchTerms->WriteUInt128(&pSearch->m_uTarget);
// Send client hash so they can do a callback.
pSearch->m_pfileSearchTerms->WriteUInt128(&CKademlia::GetPrefs()->GetClientHash());
// Send client port so they can do a callback
pSearch->m_pfileSearchTerms->WriteUInt16(thePrefs.GetPort());
break;
}
// Inc search ID.
pSearch->m_uSearchID = ++m_uNextID;
if( bStart )
{
// Auto start this search.
m_mapSearches[pSearch->m_uTarget] = pSearch;
pSearch->Go();
}
}
catch ( CIOException *ioe )
{
AddDebugLogLine( false, _T("Exception in CSearchManager::PrepareLookup (IO error(%i))"), ioe->m_iCause);
ioe->Delete();
delete pSearch;
return NULL;
}
catch (...)
{
AddDebugLogLine(false, _T("Exception in CSearchManager::PrepareLookup"));
delete pSearch;
return NULL;
}
return pSearch;
}
void CSearchManager::FindNode(const CUInt128 &uID, bool bComplete)
{
// Do a node lookup.
CSearch *pSearch = new CSearch;
if(bComplete)
pSearch->m_uType = CSearch::NODECOMPLETE;
else
pSearch->m_uType = CSearch::NODE;
pSearch->m_uTarget = uID;
pSearch->m_pfileSearchTerms = NULL;
StartSearch(pSearch);
}
bool CSearchManager::AlreadySearchingFor(const CUInt128 &uTarget)
{
// Check if this target is in the search map.
return (m_mapSearches.count(uTarget) > 0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -