📄 search.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 "./Search.h"
#include "./Kademlia.h"
#include "./Entry.h"
#include "./Defines.h"
#include "./Prefs.h"
#include "./Indexed.h"
#include "./SearchManager.h"
#include "../io/IOException.h"
#include "../io/ByteIO.h"
#include "../routing/RoutingZone.h"
#include "../net/KademliaUDPListener.h"
#include "../../emule.h"
#include "../../sharedfilelist.h"
#include "../../Packets.h"
#include "../../partfile.h"
#include "../../emuledlg.h"
#include "../../KadSearchListCtrl.h"
#include "../../kademliawnd.h"
#include "../../DownloadQueue.h"
#include "../../SearchList.h"
#include "../../ClientList.h"
#include "../../UpDownClient.h"
#include "../../Log.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
using namespace Kademlia;
void DebugSend(LPCTSTR pszMsg, uint32 uIP, uint16 uUDPPort);
CSearch::CSearch()
{
m_tCreated = time(NULL);
m_pfileSearchTerms = NULL;
m_uType = (uint32)-1;
m_uAnswers = 0;
m_uTotalRequestAnswers = 0;
m_uKadPacketSent = 0;
m_uSearchID = (uint32)-1;
(void)m_sFileName;
m_bStoping = false;
m_uTotalLoad = 0;
m_uTotalLoadResponses = 0;
pbyIO1 = NULL;
pbyIO2 = NULL;
pbyIO3 = NULL;
theApp.emuledlg->kademliawnd->searchList->SearchAdd(this);
m_uLastResponse = time(NULL);
}
CSearch::~CSearch()
{
// Remove search from GUI
theApp.emuledlg->kademliawnd->searchList->SearchRem(this);
// Check if a source search is currently being done.
CPartFile* pPartFile = theApp.downloadqueue->GetFileByKadFileSearchID(GetSearchID());
// Reset the searchID if a source search is currently being done.
if(pPartFile)
pPartFile->SetKadFileSearchID(0);
// Delete any search terms.
delete m_pfileSearchTerms;
// Decrease the use count for any contacts that are in your contact list.
for (ContactMap::iterator itContactMap = m_mapInUse.begin(); itContactMap != m_mapInUse.end(); ++itContactMap)
((CContact*)itContactMap->second)->DecUse();
// Delete any temp contacts..
for (ContactList::const_iterator itContactList = m_listDelete.begin(); itContactList != m_listDelete.end(); ++itContactList)
delete *itContactList;
// Delete any storage packages
delete pbyIO1;
delete pbyIO2;
delete pbyIO3;
// Check if this search was contacting a overload node and adjust time of next time we use that node.
if(CKademlia::IsRunning() && GetNodeLoad() > 20)
{
switch(GetSearchTypes())
{
case CSearch::STOREKEYWORD:
Kademlia::CKademlia::GetIndexed()->AddLoad(GetTarget(), ((uint32)(DAY2S(7)*((double)GetNodeLoad()/100.0))+(uint32)time(NULL)));
break;
}
}
}
void CSearch::Go()
{
// Start with a lot of possible contacts, this is a fallback in case search stalls due to dead contacts
if (m_mapPossible.empty())
{
CUInt128 uDistance(CKademlia::GetPrefs()->GetKadID());
uDistance.Xor(m_uTarget);
CKademlia::GetRoutingZone()->GetClosestTo(3, m_uTarget, uDistance, 50, &m_mapPossible, true, true);
}
if (!m_mapPossible.empty())
{
//Lets keep our contact list entries in mind to dec the inUse flag.
for (ContactMap::iterator itContactMap = m_mapPossible.begin(); itContactMap != m_mapPossible.end(); ++itContactMap)
m_mapInUse[itContactMap->first] = itContactMap->second;
ASSERT(m_mapPossible.size() == m_mapInUse.size());
ASSERT(m_mapPossible.size() != 50);
// Take top ALPHA_QUERY to start search with.
int iCount;
if(m_uType == NODE)
iCount = 1;
else
iCount = min(ALPHA_QUERY, (int)m_mapPossible.size());
ContactMap::iterator itContactMap2 = m_mapPossible.begin();
// Send initial packets to start the search.
for (int iIndex=0; iIndex<iCount; iIndex++)
{
CContact* pContact = itContactMap2->second;
// Move to tried
m_mapTried[itContactMap2->first] = pContact;
// Mark contact that we tried to contact it.
// pContact->CheckingType();
// Send the KadID so other side can check if I think it has the right KadID. (Saftey net)
// Send request
SendFindValue(pContact);
++itContactMap2;
}
}
// Update search for the GUI
theApp.emuledlg->kademliawnd->searchList->SearchRef(this);
}
//If we allow about a 15 sec delay before deleting, we won't miss a lot of delayed returning packets.
void CSearch::PrepareToStop()
{
// Check if already stoping..
if( m_bStoping )
return;
// Set basetime by search type.
uint32 uBaseTime;
switch(m_uType)
{
case NODE:
case NODECOMPLETE:
uBaseTime = SEARCHNODE_LIFETIME;
break;
case FILE:
uBaseTime = SEARCHFILE_LIFETIME;
break;
case KEYWORD:
uBaseTime = SEARCHKEYWORD_LIFETIME;
break;
case NOTES:
uBaseTime = SEARCHNOTES_LIFETIME;
break;
case STOREFILE:
uBaseTime = SEARCHSTOREFILE_LIFETIME;
break;
case STOREKEYWORD:
uBaseTime = SEARCHSTOREKEYWORD_LIFETIME;
break;
case STORENOTES:
uBaseTime = SEARCHSTORENOTES_LIFETIME;
break;
case FINDBUDDY:
uBaseTime = SEARCHFINDBUDDY_LIFETIME;
break;
case FINDSOURCE:
uBaseTime = SEARCHFINDSOURCE_LIFETIME;
break;
default:
uBaseTime = SEARCH_LIFETIME;
}
// Adjust created time so that search will delete within 15 seconds.
// This gives late results time to be processed.
m_tCreated = time(NULL) - uBaseTime + SEC(15);
m_bStoping = true;
//Update search within GUI.
theApp.emuledlg->kademliawnd->searchList->SearchRef(this);
}
void CSearch::JumpStart()
{
// If we ran out of contacts, stop search.
if (m_mapPossible.empty())
{
PrepareToStop();
return;
}
// If we had a response within the last 3 seconds, no need to jumpstart the search.
if (m_uLastResponse + SEC(3) > (uint32)time(NULL))
return;
// Search for contacts that can be used to jumpstart a stalled search.
while(!m_mapPossible.empty())
{
// Get a contact closest to our target.
CContact* pContact = m_mapPossible.begin()->second;
// Have we already tried to contact this node.
if (m_mapTried.count(m_mapPossible.begin()->first) > 0)
{
// Did we get a response from this node, if so, try to store or get info.
if(m_mapResponded.count(m_mapPossible.begin()->first) > 0)
StorePacket();
// Remove from possible list.
m_mapPossible.erase(m_mapPossible.begin());
}
else
{
// Add to tried list.
m_mapTried[m_mapPossible.begin()->first] = pContact;
// Mark contact that we tried to contact it.
// pContact->CheckingType();
// Send the KadID so other side can check if I think it has the right KadID. (Saftey net)
// Send request
SendFindValue(pContact);
break;
}
}
}
void CSearch::ProcessResponse(uint32 uFromIP, uint16 uFromPort, ContactList *plistResults)
{
if(plistResults)
m_uLastResponse = time(NULL);
// Remember the contacts to be deleted when finished
for (ContactList::iterator itContactList = plistResults->begin(); itContactList != plistResults->end(); ++itContactList)
m_listDelete.push_back(*itContactList);
// Not interested in responses for FIND_NODE.
// Once we get a results we stop the search.
// These contacts are added to contacts by UDPListener.
if (m_uType == NODE)
{
// Note we got an answer
m_uAnswers++;
// We clear the possible list to force the search to stop.
// We do this so the user has time to visually see the results.
m_mapPossible.clear();
delete plistResults;
// Update search on the GUI.
theApp.emuledlg->kademliawnd->searchList->SearchRef(this);
return;
}
try
{
//Find contact that is responding.
for (ContactMap::const_iterator itContactMap = m_mapTried.begin(); itContactMap != m_mapTried.end(); ++itContactMap)
{
CUInt128 uFromDistance(itContactMap->first);
CContact* pFromContact = itContactMap->second;
if ((pFromContact->GetIPAddress() == uFromIP) && (pFromContact->GetUDPPort() == uFromPort))
{
// Add to list of people who responded
m_mapResponded[uFromDistance] = pFromContact;
// Loop through their responses
for (itContactList = plistResults->begin(); itContactList != plistResults->end(); ++itContactList)
{
// Get next result
CContact* pContact = *itContactList;
// Calc distance this result is to the target.
CUInt128 uDistance(pContact->GetClientID());
uDistance.Xor(m_uTarget);
// Ignore this contact if already know or tried it.
if (m_mapPossible.count(uDistance) > 0)
continue;
if (m_mapTried.count(uDistance) > 0)
continue;
// Add to possible
m_mapPossible[uDistance] = pContact;
// Verify if the result is closer to the target then the one we just checked.
if (uDistance < uFromDistance)
{
// The top APLPHA_QUERY of results are used to determine if we send a request.
bool bTop = false;
if (m_mapBest.size() < ALPHA_QUERY)
{
bTop = true;
m_mapBest[uDistance] = pContact;
}
else
{
ContactMap::iterator itContactMapBest = m_mapBest.end();
itContactMapBest--;
if (uDistance < itContactMapBest->first)
{
// Prevent having more then ALPHA_QUERY within the Best list.
m_mapBest.erase(itContactMapBest);
m_mapBest[uDistance] = pContact;
bTop = true;
}
}
if(bTop)
{
// We determined this contact is a canditate for a request.
// Add to the tried list.
m_mapTried[uDistance] = pContact;
// Mark contact that we tried to contact it.
// pContact->CheckingType();
// Send the KadID so other side can check if I think it has the right KadID. (Saftey net)
// Send request
SendFindValue(pContact);
}
}
}
// Complete node search, just increase the answers and update the GUI
if( m_uType == NODECOMPLETE )
{
m_uAnswers++;
theApp.emuledlg->kademliawnd->searchList->SearchRef(this);
}
break;
}
}
}
catch (...)
{
AddDebugLogLine(false, _T("Exception in CSearch::ProcessResponse"));
}
delete plistResults;
}
void CSearch::StorePacket()
{
ASSERT(!m_mapPossible.empty());
// This method is currently only called by jumpstart so only use best possible.
ContactMap::const_iterator itContactMap = m_mapPossible.begin();
CUInt128 uFromDistance(itContactMap->first);
CContact* pFromContact = itContactMap->second;
// Make sure this is a valid Node to store too.
// Shouldn't LAN IPs already be filtered?
if(thePrefs.FilterLANIPs() && uFromDistance.Get32BitChunk(0) > SEARCHTOLERANCE)
return;
// What kind of store are we doing?
switch(m_uType)
{
case FILE:
{
if(m_pfileSearchTerms)
delete m_pfileSearchTerms;
m_pfileSearchTerms = new CSafeMemFile();
m_pfileSearchTerms->WriteUInt128(&m_uTarget);
/*if(pFromContact->GetVersion()) // JOHNTODO -- Add ability to switch betweek Kad1.0 and Kad2.0
{
// JOHNTODO -- Add start position
// Start Position range (0x0 to 0x7FFF)
m_pfileSearchTerms->WriteUInt16(0);
// JOHNTODO -- Add File Size
m_pfileSearchTerms->WriteUInt64(0);
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugSend("KADEMLIA2_SEARCH_SOURCE_REQ", pFromContact->GetIPAddress(), pFromContact->GetUDPPort());
CKademlia::GetUDPListener()->SendPacket(m_pfileSearchTerms->GetBuffer(), (UINT)m_pfileSearchTerms->GetLength(), KADEMLIA2_SEARCH_SOURCE_REQ, pFromContact->GetIPAddress(), pFromContact->GetUDPPort());
}
else*/
{
m_pfileSearchTerms->WriteUInt8(1);
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugSend("KADEMLIA_SEARCH_REQ(File)", pFromContact->GetIPAddress(), pFromContact->GetUDPPort());
CKademlia::GetUDPListener()->SendPacket(m_pfileSearchTerms->GetBuffer(), (UINT)m_pfileSearchTerms->GetLength(), KADEMLIA_SEARCH_REQ, pFromContact->GetIPAddress(), pFromContact->GetUDPPort());
}
// Inc total request answers
m_uTotalRequestAnswers++;
// Update search in the GUI
theApp.emuledlg->kademliawnd->searchList->SearchRef(this);
break;
}
case KEYWORD:
{
//JOHNTODO -- We cannot precreate these packets as we do not know
// before hand if we are talking to Kad1.0 or Kad2.0..
/*if(pFromContact->GetVersion()) // JOHNTODO -- Add ability to switch betweek Kad1.0 and Kad2.0
{
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugSend("KADEMLIA2_SEARCH_KEY_REQ", pFromContact->GetIPAddress(), pFromContact->GetUDPPort());
CKademlia::GetUDPListener()->SendPacket(m_pfileSearchTerms->GetBuffer(), (UINT)m_pfileSearchTerms->GetLength(), KADEMLIA2_SEARCH_KEY_REQ, pFromContact->GetIPAddress(), pFromContact->GetUDPPort());
}
else*/
{
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugSend("KADEMLIA_SEARCH_REQ(KEYWORD)", pFromContact->GetIPAddress(), pFromContact->GetUDPPort());
CKademlia::GetUDPListener()->SendPacket(m_pfileSearchTerms->GetBuffer(), (UINT)m_pfileSearchTerms->GetLength(), KADEMLIA_SEARCH_REQ, pFromContact->GetIPAddress(), pFromContact->GetUDPPort());
}
// Inc total request answers
m_uTotalRequestAnswers++;
// Update search in the GUI
theApp.emuledlg->kademliawnd->searchList->SearchRef(this);
break;
}
case NOTES:
{
// Write complete packet
if(m_pfileSearchTerms)
delete m_pfileSearchTerms;
m_pfileSearchTerms = new CSafeMemFile();
m_pfileSearchTerms->WriteUInt128(&m_uTarget);
/*if(pFromContact->GetVersion()) // JOHNTODO -- Add ability to switch betweek Kad1.0 and Kad2.0
{
// JOHNTODO -- Add File Size
m_pfileSearchTerms->WriteUInt64(0);
if (thePrefs.GetDebugClientKadUDPLevel() > 0)
DebugSend("KADEMLIA2_SEARCH_NOTES_REQ", pFromContact->GetIPAddress(), pFromContact->GetUDPPort());
CKademlia::GetUDPListener()->SendPacket(m_pfileSearchTerms->GetBuffer(), (UINT)m_pfileSearchTerms->GetLength(), KADEMLIA2_SEARCH_NOTES_REQ, pFromContact->GetIPAddress(), pFromContact->GetUDPPort());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -