📄 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 "resource.h"
#include "SearchManager.h"
#include "Search.h"
#include "Kademlia.h"
#include "../../OpCodes.h"
#include "Defines.h"
#include "Tag.h"
#include "../routing/Contact.h"
#include "../utils/UInt128.h"
#include "../utils/MD4.h"
#include "../io/ByteIO.h"
#include "../io/IOException.h"
#include "../kademlia/prefs.h"
#include "SafeFile.h"
#include "OtherFunctions.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
LPCTSTR _aszInvKadKeywordChars = " ()[]{}<>,._-!?";
////////////////////////////////////////
using namespace Kademlia;
////////////////////////////////////////
uint32 CSearchManager::m_nextID = 0;
SearchMap CSearchManager::m_searches;
CCriticalSection CSearchManager::m_critical;
void CSearchManager::stopSearch(uint32 searchID, bool delayDelete)
{
m_critical.Lock();
try
{
SearchMap::iterator it = m_searches.begin();
while (it != m_searches.end())
{
if (it->second->m_searchID == searchID)
{
if(delayDelete)
{
it->second->prepareToStop();
it++;
}
else
{
delete it->second;
it = m_searches.erase(it);
}
}
else
it++;
}
}
catch (...)
{
CKademlia::debugLine("Exception in CSearchManager::stopSearch");
}
m_critical.Unlock();
}
bool CSearchManager::isNodeSearch(const CUInt128 &target)
{
// Timer should be the only thing that can delete these..
// Timer should be the only thing calling this...
// Therefore there shouldn't be a sync issue at first glance.
// m_critical.Lock();
try
{
SearchMap::iterator it = m_searches.begin();
while (it != m_searches.end())
{
if (it->second->m_target == target)
{
if( it->second->m_type == CSearch::NODE || it->second->m_type == CSearch::NODECOMPLETE )
return true;
else
return false;
}
else
it++;
}
}
catch (...)
{
CKademlia::debugLine("Exception in CSearchManager::isNodeSearch");
}
return false;
// m_critical.Unlock();
}
void CSearchManager::stopAllSearches(void)
{
m_critical.Lock();
try
{
SearchMap::iterator it;
for (it = m_searches.begin(); it != m_searches.end(); it++)
delete it->second;
m_searches.clear();
}
catch (...)
{
CKademlia::debugLine("Exception in CSearchManager::stopAllSearches");
}
m_critical.Unlock();
}
bool CSearchManager::startSearch(CSearch* pSearch)
{
if (alreadySearchingFor(pSearch->m_target))
{
delete pSearch;
return false;
}
m_searches[pSearch->m_target] = pSearch;
pSearch->go();
return true;
}
void CSearchManager::deleteSearch(CSearch* pSearch)
{
delete pSearch;
}
CSearch* CSearchManager::prepareFindKeywords(uint32 type, bool start, LPCSTR keyword1, UINT uSearchTermsSize, LPBYTE pucSearchTermsData)
{
CSearch *s = new CSearch;
try
{
s->m_type = type;
getWords(keyword1, &s->m_words);
if (s->m_words.size() == 0)
{
delete s;
throw GetResString(IDS_KAD_SEARCH_KEYWORD_TOO_SHORT);
}
CString k = s->m_words.front();
CMD4::hash((byte*)k.GetBuffer(0), k.GetLength(), &s->m_target);
if (alreadySearchingFor(s->m_target))
{
delete s;
CString strError;
strError.Format(GetResString(IDS_KAD_SEARCH_KEYWORD_ALREADY_SEARCHING), k);
throw strError;
}
// Write complete packet
s->m_searchTerms = new CSafeMemFile();
s->m_searchTerms->WriteUInt128(&s->m_target);
if (uSearchTermsSize == 0)
s->m_searchTerms->WriteUInt8(0);
else
{
s->m_searchTerms->WriteUInt8(1);
s->m_searchTerms->Write(pucSearchTermsData, uSearchTermsSize);
}
s->m_searchID = ++m_nextID;
if( start )
{
m_searches[s->m_target] = s;
s->go();
}
}
catch (CIOException* ioe)
{
CString strError;
strError.Format(_T("IO-Exception in %s: Error %u"), __FUNCTION__, ioe->m_cause);
ioe->Delete();
delete s;
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 %s: %s"), __FUNCTION__, szError);
e->Delete();
delete s;
throw strError;
}
catch (CString strException)
{
throw strException;
}
catch (...)
{
CString strError;
strError.Format(_T("Unknown exception in %s"), __FUNCTION__);
delete s;
throw strError;
}
return s;
}
CSearch* CSearchManager::prepareFindFile(uint32 type, bool start, const CUInt128 &id)
{
if (alreadySearchingFor(id))
return 0;
CSearch *s = new CSearch;
try
{
s->m_type = type;
s->m_target = id;
// Write complete packet
s->m_searchTerms = new CSafeMemFile();
s->m_searchTerms->WriteUInt128(&s->m_target);
s->m_searchTerms->WriteUInt8(1);
s->m_searchID = ++m_nextID;
if( start )
{
m_searches[s->m_target] = s;
s->go();
}
}
catch ( CIOException *ioe )
{
CKademlia::debugMsg("Exception in CSearchManager::prepareFindFile (IO error(%i))", ioe->m_cause);
ioe->Delete();
delete s;
s = NULL;
}
catch (...)
{
CKademlia::debugLine("Exception in CSearchManager::prepareFindFile");
delete s;
s = NULL;
}
return s;
}
// as long as this function is only called from the CTimer thread itself, there should be no
// sync/deadlocks problems. if it is called from the emule thread it has to be changed in
// the same way as findFile and findKeywords.
void CSearchManager::findNode(const CUInt128 &id)
{
if (alreadySearchingFor(id))
return;
// CString idStr;
// id.toHexString(&idStr);
// CKademlia::debugMsg("Find Node: %s", idStr);
try
{
CSearch *s = new CSearch;
s->m_type = CSearch::NODE;
s->m_target = id;
s->m_searchTerms = NULL;
s->m_searchID = 0;
m_searches[s->m_target] = s;
s->go();
}
catch (...)
{
CKademlia::debugLine("Exception in CSearchManager::findNode");
}
return;
}
void CSearchManager::findNodeComplete(const CUInt128 &id)
{
if (alreadySearchingFor(id))
return;
// CString idStr;
// id.toHexString(&idStr);
// CKademlia::debugMsg("Find Node: %s", idStr);
try
{
CSearch *s = new CSearch;
s->m_type = CSearch::NODECOMPLETE;
s->m_target = id;
s->m_searchTerms = NULL;
s->m_searchID = 0;
m_searches[s->m_target] = s;
s->go();
}
catch (...)
{
CKademlia::debugLine("Exception in CSearchManager::findNodeComplete");
}
return;
}
bool CSearchManager::alreadySearchingFor(const CUInt128 &target)
{
bool retVal = false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -