📄 routingzone.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.
This work is based on the java implementation of the Kademlia protocol.
Kademlia: Peer-to-peer routing based on the XOR metric
Copyright (C) 2002 Petar Maymounkov [petar@post.harvard.edu]
http://kademlia.scs.cs.nyu.edu
*/
// 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..
*/
/**
* The *Zone* is just a node in a binary tree of *Zone*s.
* Each zone is either an internal node or a leaf node.
* Internal nodes have "bin == null" and "subZones[i] != null",
* leaf nodes have "subZones[i] == null" and "bin != null".
*
* All key unique id's are relative to the center (self), which
* is considered to be 000..000
*/
#include "stdafx.h"
#include <math.h>
#include "RoutingZone.h"
#include "Contact.h"
#include "RoutingBin.h"
#include "../utils/UInt128.h"
#include "../utils/MiscUtils.h"
#include "../kademlia/Kademlia.h"
#include "../kademlia/Prefs.h"
#include "../kademlia/SearchManager.h"
#include "../kademlia/Defines.h"
#include "../kademlia/Error.h"
#include "../net/KademliaUDPListener.h"
#include "../../otherfunctions.h"
#include "../../Opcodes.h"
#include "emule.h"
#include "emuledlg.h"
#include "KadContactListCtrl.h"
#include "kademliawnd.h"
#include "SafeFile.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
////////////////////////////////////////
using namespace Kademlia;
////////////////////////////////////////
void DebugSend(LPCTSTR pszMsg, uint32 ip, uint16 port);
// This is just a safety precaution
#define CONTACT_FILE_LIMIT 5000
CString CRoutingZone::m_filename = "";
CUInt128 CRoutingZone::me = (ULONG)0;
CRoutingZone::CRoutingZone()
{
// Can only create routing zone after prefs
CPrefs *prefs = CKademlia::getPrefs();
ASSERT(prefs != NULL);
prefs->getClientID(&me);
m_filename = CMiscUtils::getAppDir();
m_filename.Append(CONFIGFOLDER);
m_filename.Append("nodes.dat");
CUInt128 zero((ULONG)0);
init(NULL, 0, zero);
}
CRoutingZone::CRoutingZone(LPCSTR filename)
{
// Can only create routing zone after prefs
CPrefs *prefs = CKademlia::getPrefs();
ASSERT(prefs != NULL);
prefs->getClientID(&me);
m_filename = filename;
CUInt128 zero((ULONG)0);
init(NULL, 0, zero);
}
CRoutingZone::CRoutingZone(CRoutingZone *super_zone, int level, const CUInt128 &zone_index)
{
init(super_zone, level, zone_index);
}
void CRoutingZone::init(CRoutingZone *super_zone, int level, const CUInt128 &zone_index)
{
m_superZone = super_zone;
m_level = level;
m_zoneIndex = zone_index;
m_subZones[0] = NULL;
m_subZones[1] = NULL;
m_bin = new CRoutingBin();
m_nextSmallTimer = time(NULL) + m_zoneIndex.get32BitChunk(3);
if ((m_superZone == NULL) && (m_filename.GetLength() > 0))
readFile();
startTimer();
}
CRoutingZone::~CRoutingZone()
{
if ((m_superZone == NULL) && (m_filename.GetLength() > 0))
{
theApp.emuledlg->kademliawnd->contactList->Hide();
writeFile();
}
if (isLeaf())
delete m_bin;
else
{
delete m_subZones[0];
delete m_subZones[1];
}
if (m_superZone == NULL)
theApp.emuledlg->kademliawnd->contactList->Visable();
}
void CRoutingZone::readFile(void)
{
Lock();
try
{
theApp.emuledlg->kademliawnd->contactList->Hide();
uint32 numContacts = 0;
CSafeBufferedFile file;
CFileException fexp;
if (file.Open(m_filename, CFile::modeRead | CFile::osSequentialScan|CFile::typeBinary, &fexp))
{
setvbuf(file.m_pStream, NULL, _IOFBF, 32768);
numContacts = file.ReadUInt32();
CUInt128 id;
uint32 ip;
uint16 udpPort;
uint16 tcpPort;
byte type;
for (uint32 i=0; i<numContacts; i++)
{
file.ReadUInt128(&id);
ip = file.ReadUInt32();
udpPort = file.ReadUInt16();
tcpPort = file.ReadUInt16();
type = file.ReadUInt8();
if(::IsGoodIPPort(ntohl(ip),udpPort))
{
if( type < 2)
add(id, ip, udpPort, tcpPort, type);
}
}
file.Close();
CKademlia::logMsg(GetResString(IDS_KADCONTACTSREAD), numContacts);
}
if (numContacts == 0)
CKademlia::reportError(ERR_NO_CONTACTS, GetResString(IDS_ERR_KADCONTACTS));
}
//TODO: Make this catch an CFileException..
catch (...)
{
CKademlia::debugLine("Exception in CRoutingZone::readFile");
}
theApp.emuledlg->kademliawnd->contactList->Visable();
Unlock();
}
void CRoutingZone::writeFile(void)
{
Lock();
try
{
// dumpContents();
uint32 count = 0;
CContact *c;
CUInt128 id;
CSafeBufferedFile file;
CFileException fexp;
if (file.Open(m_filename, CFile::modeWrite | CFile::modeCreate | CFile::typeBinary, &fexp))
{
setvbuf(file.m_pStream, NULL, _IOFBF, 32768);
ContactList contacts;
getAllEntries(&contacts);
file.WriteUInt32((uint32)min(contacts.size(), CONTACT_FILE_LIMIT));
ContactList::const_iterator it;
for (it = contacts.begin(); it != contacts.end(); it++)
{
count++;
c = *it;
c->getClientID(&id);
file.WriteUInt128(&id);
file.WriteUInt32(c->getIPAddress());
file.WriteUInt16(c->getUDPPort());
file.WriteUInt16(c->getTCPPort());
file.WriteUInt8(c->getType());
if (count == CONTACT_FILE_LIMIT)
break;
}
file.Close();
}
CKademlia::debugMsg("Wrote %ld contact%s to file.", count, ((count == 1) ? "" : "s"));
}
//TODO: Make this catch an CFileException..
catch (...)
{
CKademlia::debugLine("Exception in CRoutingZone::writeFile");
}
Unlock();
}
bool CRoutingZone::canSplit(void) const
{
if (m_level >= 127)
return false;
/* Check if we are close to the center */
if ( (m_zoneIndex < KK || m_level < KBASE) && m_bin->getSize() == K)
return true;
return false;
/* Check if we need to split to keep the vicinity consistent */
// return areWeInFirstSubtreeOfEnoughSize();
}
bool CRoutingZone::areWeInFirstSubtreeOfEnoughSize(void) const
{
if (m_zoneIndex == 0)
{
CRoutingZone *z = m_subZones[0];
if (z == NULL || z->getNumContacts() < K)
return true;
else
return false;
}
else
return m_superZone->areWeInFirstSubtreeOfEnoughSize();
}
void CRoutingZone::consolidate(void)
{
Lock();
try
{
if (isLeaf() && m_superZone != NULL)
m_superZone->consolidate();
else if ((!isLeaf())
&& (m_subZones[0]->isLeaf() && m_subZones[1]->isLeaf())
&& (!canSplit())
&& (m_subZones[0]->m_bin->getRemaining() < 1 || m_subZones[1]->m_bin->getRemaining() < 1) )
{
if (m_superZone != NULL)
m_superZone->consolidate();
}
}
catch (...)
{
CKademlia::debugLine("Exception in CRoutingZone::consolidate");
}
Unlock();
}
bool CRoutingZone::add(const CUInt128 &id, uint32 ip, uint16 port, uint16 tport, byte type)
{
if (id == me)
return false;
bool retVal = false;
CUInt128 distance(me);
distance.xor(id);
CContact *c = NULL;
Lock();
try
{
if (!isLeaf())
retVal = m_subZones[distance.getBitNumber(m_level)]->add(id, ip, port, tport, type);
else
{
c = m_bin->getContact(id);
if (c != NULL)
{
c->setIPAddress(ip);
c->setUDPPort(port);
c->setTCPPort(tport);
retVal = true;
theApp.emuledlg->kademliawnd->contactList->ContactRef(c);
}
else if (m_bin->getRemaining() > 0)
{
c = new CContact(id, ip, port, tport, type);
retVal = m_bin->add(c);
if(retVal)
theApp.emuledlg->kademliawnd->contactList->ContactAdd(c);
}
else if (canSplit())
{
split();
retVal = m_subZones[distance.getBitNumber(m_level)]->add(id, ip, port, tport, type);
}
else
{
merge();
c = new CContact(id, ip, port, tport, type);
retVal = m_bin->add(c);
if(retVal)
theApp.emuledlg->kademliawnd->contactList->ContactAdd(c);
}
if (!retVal)
{
if (c != NULL)
delete c;
}
}
}
catch (...)
{
CKademlia::debugLine("Exception in CRoutingZone::add");
}
Unlock();
return retVal;
}
void CRoutingZone::setAlive(uint32 ip, uint16 port)
{
Lock();
try
{
if (isLeaf())
m_bin->setAlive(ip, port);
else
{
m_subZones[0]->setAlive(ip, port);
m_subZones[1]->setAlive(ip, port);
}
}
catch (...)
{
CKademlia::debugLine("Exception in CRoutingZone::setAlive");
}
Unlock();
}
bool CRoutingZone::contains(const CUInt128 &id) const
{
if (isLeaf())
return m_bin->contains(id);
else
return m_subZones[id.getBitNumber(m_level)]->contains(id);
}
CContact *CRoutingZone::getContact(const CUInt128 &id) const
{
if (isLeaf())
return m_bin->getContact(id);
else
return m_subZones[id.getBitNumber(m_level)]->getContact(id);
}
int CRoutingZone::getClosestTo(int maxType, const CUInt128 &target, int maxRequired, ContactMap *result, bool emptyFirst) const
{
// If leaf zone, do it here
if (isLeaf())
return m_bin->getClosestTo(maxType, target, maxRequired, result, emptyFirst);
// otherwise, recurse in the closer-to-the-target subzone first
int closer = target.getBitNumber(m_level);
int found = m_subZones[closer]->getClosestTo(maxType, target, maxRequired, result, emptyFirst);
// if still not enough tokens found, recurse in the other subzone too
if (found < maxRequired)
found += m_subZones[1-closer]->getClosestTo(maxType, target, maxRequired-found, result, false);
return found;
}
void CRoutingZone::getAllEntries(ContactList *result, bool emptyFirst)
{
if (isLeaf())
{
Lock();
try
{
m_bin->getEntries(result, emptyFirst);
}
catch (...)
{
CKademlia::debugLine("Exception in CRoutingZone::getAllEntries");
}
Unlock();
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -