📄 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 "OpCodes.h"
#include "Defines.h"
#include "Prefs.h"
#include "../routing/RoutingZone.h"
#include "../routing/Contact.h"
#include "../net/KademliaUDPListener.h"
#include "../io/ByteIO.h"
#include "otherfunctions.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
////////////////////////////////////////
using namespace Kademlia;
////////////////////////////////////////
CSearch::CSearch()
{
m_created = time(NULL);
m_searchTerms = NULL;
m_lenSearchTerms = 0;
m_type = -1;
m_count = 0;
m_keywordcount = 0;
m_callbackID = NULL;
m_callbackKeyword = NULL;
m_searchID = -1;
m_keywordPublish = NULL;
m_fileName = "";
Kademlia::CKademlia::reportSearchAdd(this);
}
CSearch::~CSearch()
{
Kademlia::CKademlia::reportSearchRem(this);
if (m_searchTerms != NULL)
delete m_searchTerms;
ContactList::const_iterator it;
for (it = m_delete.begin(); it != m_delete.end(); it++)
delete *it;
Kademlia::CKademlia::reportSearchRem(this);
}
void CSearch::go(void)
{
Kademlia::CKademlia::reportSearchRef(this);
// Start with a lot of possible contacts, this is a fallback in case search stalls due to dead contacts
if (m_possible.empty())
{
CRoutingZone *routingZone = CKademlia::getRoutingZone();
ASSERT(routingZone != NULL);
CPrefs *prefs = CKademlia::getPrefs();
ASSERT(prefs != NULL);
// Modified 2004.5.20 after see emule'forum
//CUInt128 distance(prefs->getClientID());
//distance.xor(m_target);
//routingZone->getClosestTo(1, distance, 50, &m_possible);
routingZone->getClosestTo(1, m_target, 50, &m_possible);
}
if (m_possible.empty())
return;
// Take top 3 possible
int count = min(ALPHA_QUERY, (int)m_possible.size());
CContact *c;
ContactMap::iterator it;
for (int i=0; i<count; i++)
{
it = m_possible.begin();
c = it->second;
// Move to tried
m_tried[it->first] = c;
m_possible.erase(it);
// Send request
c->setType(c->getType()+1);
CUInt128 check;
c->getClientID(&check);
sendFindValue(m_target, check, c->getIPAddress(), c->getUDPPort());
if(m_type == NODE)
{
break;
}
}
}
void CSearch::jumpStart(void)
{
if (m_possible.empty())
{
if ( m_created + SEARCHNODE_LIFETIME - time(NULL) > (15*ONE_SEC) )
m_created = time(NULL) - SEARCHNODE_LIFETIME - (15*ONE_SEC);
return;
}
CUInt128 best;
// Remove any obsolete possible contacts
if (!m_responded.empty())
{
best = m_responded.begin()->first;
ContactMap::iterator it = m_possible.begin();
while (it != m_possible.end())
{
if (it->first < best)
it = m_possible.erase(it);
else
it++;
}
}
if (m_possible.empty())
return;
// Move to tried
CContact *c = m_possible.begin()->second;
m_tried[m_possible.begin()->first] = c;
m_possible.erase(m_possible.begin());
// Send request
c->setType(c->getType()+1);
CUInt128 check;
c->getClientID(&check);
sendFindValue(m_target, check, c->getIPAddress(), c->getUDPPort());
}
void CSearch::processResponse(const CUInt128 &target, uint32 fromIP, uint16 fromPort, ContactList *results)
{
// Remember the contacts to be deleted when finished
ContactList::iterator response;
for (response = results->begin(); response != results->end(); response++)
m_delete.push_back(*response);
// Not interested in responses for FIND_NODE, will be added to contacts by udp listener
if (m_type == NODE)
{
m_count++;
m_possible.clear();
delete results;
CKademlia::reportSearchRef(this);
return;
}
ContactMap::const_iterator tried;
CContact *c;
CContact *from;
CUInt128 distance;
CUInt128 fromDistance;
bool returnedCloser;
try
{
// Find the person who sent this
returnedCloser = false;
// Added by RevConn 04/1/2, copy m_tried, for loop maybe invalid `tried` iterator
ContactMap m_tried_copy(m_tried);
for (tried = m_tried_copy.begin(); tried != m_tried_copy.end(); tried++)
// for (tried = m_tried.begin(); tried != m_tried.end(); tried++)
{
fromDistance = tried->first;
from = tried->second;
if ((from->getIPAddress() == fromIP) && (from->getUDPPort() == fromPort))
{
// Add to list of people who responded
m_responded[fromDistance] = from;
returnedCloser = false;
// Loop through their responses
for (response = results->begin(); response != results->end(); response++)
{
c = *response;
// Only interested in type 0
// if (c->getType() != 0)
// continue;
c->getClientID(&distance);
distance.xor(target);
// Ignore if already tried
if (m_tried.count(distance) > 0)
continue;
if (distance < fromDistance)
{
returnedCloser = true;
// If in top 3 responses
bool top = false;
if (m_best.size() < ALPHA_QUERY)
top = true;
else
{
ContactMap::iterator it = m_best.end();
it--;
if (distance < it->first)
{
m_best.erase(it);
m_best[distance] = c;
top = true;
}
}
if (top)
{
// Add to tried
m_tried[distance] = c;
// Send request
c->setType(c->getType()+1);
CUInt128 check;
c->getClientID(&check);
sendFindValue(m_target, check, c->getIPAddress(), c->getUDPPort());
}
else
{
// Add to possible
m_possible[distance] = c;
}
}
// break;
}
CString dist;
fromDistance.toBinaryString(&dist);
// We don't want anything from these people, so just increment the counter.
if( m_type == NODECOMPLETE )
{
m_count++;
CKademlia::reportSearchRef(this);
}
// Ask 'from' for the file if closest
else if (!returnedCloser && dist.Left(5) == "00000"){
switch(m_type){
case FILE:
case KEYWORD:
case STOREFILE:
case STOREKEYWORD:
{
CKademliaUDPListener *udpListner = CKademlia::getUDPListener();
ASSERT(udpListner != NULL);
udpListner->sendPacket(m_searchTerms, m_lenSearchTerms, from->getIPAddress(), from->getUDPPort());
Kademlia::CKademlia::reportSearchRef(this);
break;
}
}
}
}
}
} catch (...) {}
delete results;
}
void CSearch::processResult(const CUInt128 &target, uint32 fromIP, uint16 fromPort, const CUInt128 &answer, TagList *info)
{
// m_count++;
switch(m_type)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -