📄 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 "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 "../utils/MiscUtils.h"
#include "../io/ByteIO.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
////////////////////////////////////////
using namespace Kademlia;
////////////////////////////////////////
uint32 CSearchManager::m_nextID = 0;
SearchMap CSearchManager::m_searches;
CCriticalSection CSearchManager::m_critical;
void CSearchManager::stopSearch(uint32 searchID)
{
m_critical.Lock();
try
{
SearchMap::iterator it = m_searches.begin();
while (it != m_searches.end())
{
if (it->second->m_searchID == searchID)
{
delete it->second;
it = m_searches.erase(it);
}
else
it++;
}
} catch (...) {}
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 == Kademlia::CSearch::NODE || it->second->m_type == Kademlia::CSearch::NODECOMPLETE )
return true;
else
return false;
}
else
it++;
}
} catch (...) {}
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 (...) {}
m_critical.Unlock();
}
void CSearchManager::startSearch(CSearch* pSearch)
{
if (alreadySearchingFor(pSearch->m_target))
{
delete pSearch;
return;
}
m_searches[pSearch->m_target] = pSearch;
pSearch->go();
}
void CSearchManager::deleteSearch(CSearch* pSearch)
{
delete pSearch;
}
/*
CSearch* CSearchManager::prepareFindKeywords(SEARCH_KEYWORD_CALLBACK callback, uint32 numParams, LPCSTR keyword1, ...)
{
//uint32 searchID = 0;
CSearch *s = new CSearch;
try
{
s->m_type = CSearch::KEYWORD;
s->m_callbackKeyword = callback;
getWords(keyword1, &s->m_words);
if (numParams > 1)
{
va_list args;
va_start(args, keyword1);
while (--numParams > 0)
getWords(va_arg(args, LPCSTR), &s->m_words);
va_end(args);
}
if (s->m_words.size() == 0)
{
delete s;
return 0;
}
CString k = s->m_words.front();
CMD4::hash((byte*)k.GetBuffer(0), k.GetLength(), &s->m_target);
//if (alreadySearchingFor(s->m_target)) //-- this may block!
//{
// delete s;
// return 0;
//}
// Build keyword message (skip first keyword)
int count = 0;
byte keywords[4096];
CByteIO kio(keywords, 4096);
WordList::const_iterator it = s->m_words.begin();
for (it++; it != s->m_words.end(); it++)
{
if (!(*it).Compare(SEARCH_IMAGE))
{
kio.writeTag("Image", "\x03");
count++;
}
else if (!(*it).Compare(SEARCH_AUDIO))
{
kio.writeTag("Audio", "\x03");
count++;
}
else if (!(*it).Compare(SEARCH_VIDEO))
{
kio.writeTag("Video", "\x03");
count++;
}
else if (!(*it).Compare(SEARCH_DOC))
{
kio.writeTag("Doc", "\x03");
count++;
}
else if (!(*it).Compare(SEARCH_PRO))
{
kio.writeTag("Pro", "\x03");
count++;
}
else
{
kio.writeTag(1, *it);
count++;
}
}
// Write complete packet
s->m_searchTerms = new byte[4096];
CByteIO bio(s->m_searchTerms, 4096);
bio.writeByte(OP_KADEMLIAHEADER);
bio.writeByte(KADEMLIA_SEARCH_REQ);
bio.writeUInt128(s->m_target);
if (count == 0)
bio.writeByte(0);
else
{
bio.writeByte(1);
for (int i=0; i<(count - 1); i++)
bio.writeUInt16(0); // These are the operator / logical ANDs
bio.writeArray(keywords, 4096 - kio.getAvailable());
}
s->m_lenSearchTerms = 4096 - bio.getAvailable();
//m_searches[s->m_target] = s;
//searchID = ++m_nextID;
//s->m_searchID = searchID;
// NOTE: the following line does only work because this function is (currently) invoked from emule thread only!
// If that function once gets called from a different thread, the handling with 'm_nextID' has to by synchronized.
s->m_searchID = ++m_nextID;
//s->go(); //-- this may block!
} catch (...) {
delete s;
s = NULL;
}
return s;
}
CSearch* CSearchManager::prepareFindKeywords(SEARCH_KEYWORD_CALLBACK callback, LPCSTR keyword1, UINT uSearchTermsSize, LPBYTE pucSearchTermsData)
{
CSearch *s = new CSearch;
try
{
s->m_type = CSearch::KEYWORD;
s->m_callbackKeyword = callback;
getWords(keyword1, &s->m_words);
if (s->m_words.size() == 0)
{
delete s;
return 0;
}
CString k = s->m_words.front();
CMD4::hash((byte*)k.GetBuffer(0), k.GetLength(), &s->m_target);
// Write complete packet
s->m_searchTerms = new byte[4096];
CByteIO bio(s->m_searchTerms, 4096);
bio.writeByte(OP_KADEMLIAHEADER);
bio.writeByte(KADEMLIA_SEARCH_REQ);
bio.writeUInt128(s->m_target);
if (uSearchTermsSize == 0)
bio.writeByte(0);
else
{
bio.writeByte(1);
bio.writeArray(pucSearchTermsData, uSearchTermsSize);
}
s->m_lenSearchTerms = 4096 - bio.getAvailable();
s->m_searchID = ++m_nextID;
} catch (...) {
delete s;
s = NULL;
}
return s;
}
*/
CSearch* CSearchManager::prepareFindKeywords(SEARCH_KEYWORD_CALLBACK callback, CUInt128& target, const std::vector<CString>& _keywords, int type, uint32 minsize, uint32 maxsize)
{
//uint32 searchID = 0;
CSearch *s = new CSearch;
try
{
s->m_type = CSearch::KEYWORD;
s->m_callbackKeyword = callback;
s->m_target = target;
s->m_words.assign(_keywords.begin(), _keywords.end());
// Build keyword message
int count = 0;
byte keywords[4096];
CByteIO kio(keywords, 4096);
// type
if(type){
char buf[32];
wsprintf(buf, "FT%d", type);
kio.writeTag(buf, "\x03");
count++;
}
for (std::vector<CString>::const_iterator it = _keywords.begin(); it != _keywords.end(); it++)
{
kio.writeTag(1, *it);
count++;
}
if (minsize > 0){
kio.writeByte(0x03);
kio.writeUInt32(minsize);
kio.writeByte(0x01);
kio.writeUInt16(0x0001);
kio.writeByte(0x02);
count++;
}
if (maxsize > 0){
kio.writeByte(0x03);
kio.writeUInt32(maxsize);
kio.writeByte(0x02);
kio.writeUInt16(0x0001);
kio.writeByte(0x02);
count++;
}
// Write complete packet
s->m_searchTerms = new byte[4096];
CByteIO bio(s->m_searchTerms, 4096);
bio.writeByte(OP_KADEMLIAHEADER);
bio.writeByte(KADEMLIA_SEARCH_REQ);
bio.writeUInt128(s->m_target);
if (count == 0)
bio.writeByte(0);
else
{
bio.writeByte(1);
for (int i=0; i<(count - 1); i++)
bio.writeUInt16(0); // These are the operator / logical ANDs
bio.writeArray(keywords, 4096 - kio.getAvailable());
}
s->m_lenSearchTerms = 4096 - bio.getAvailable();
// NOTE: the following line does only work because this function is (currently) invoked from emule thread only!
// If that function once gets called from a different thread, the handling with 'm_nextID' has to by synchronized.
s->m_searchID = ++m_nextID;
}
catch (...)
{
CKademlia::debugLine("Exception in CSearchManager::prepareFindKeywords");
delete s;
s = NULL;
}
return s;
}
CSearch* CSearchManager::prepareFindFile(SEARCH_ID_CALLBACK callback, const CUInt128 &id, uint32 _tth1, uint32 _tth2)
{
//if (alreadySearchingFor(id)) //-- this may block!
// return 0;
//uint32 searchID = 0;
CSearch *s = new CSearch;
try
{
s->m_type = CSearch::FILE;
s->m_target = id;
s->m_callbackID = callback;
s->tth[0] = _tth1;
s->tth[1] = _tth2;
// Write complete packet
s->m_searchTerms = new byte[4096];
CByteIO bio(s->m_searchTerms, 4096);
bio.writeByte(OP_KADEMLIAHEADER);
bio.writeByte(KADEMLIA_SEARCH_REQ);
bio.writeUInt128(s->m_target);
bio.writeByte(1);
s->m_lenSearchTerms = 4096 - bio.getAvailable();
//m_searches[s->m_target] = s;
//searchID = ++m_nextID;
//s->m_searchID = searchID;
// NOTE: the following line does only work because this function is (currently) invoked from emule thread only!
// If that function once gets called from a different thread, the handling with 'm_nextID' has to by synchronized.
s->m_searchID = ++m_nextID;
//s->go(); //-- this may block!
} catch (...) {
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_lenSearchTerms = 0;
s->m_searchID = 0;
m_searches[s->m_target] = s;
s->go();
} catch (...) {}
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -