📄 indexed.cpp
字号:
/*
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 "Indexed.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 "../utils/MiscUtils.h"
#include "otherfunctions.h"
#include "../kademlia/tag.h"
#include "../io/FileIO.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
////////////////////////////////////////
using namespace Kademlia;
////////////////////////////////////////
CString CIndexed::m_filename = "";
CIndexed::CIndexed()
{
m_filename = CMiscUtils::getAppDir();
m_filename.Append(_T(CONFIGFOLDER));
m_filename.Append("index.dat");
readFile();
m_lastClean = time(NULL) + (60*30);
}
void CIndexed::readFile(void)
{
try
{
uint32 total = 0;
uint32 numKeys = 0;
uint32 numSource = 0;
uint32 numName = 0;
uint32 tagList = 0;
time_t now = time(NULL) - (KADEMLIAINDEXCLEAN);
CFileIO file;
if (file.Open(m_filename.GetBuffer(0), CFile::modeRead))
{
numKeys = file.readUInt32();
CUInt128 id;
while( numKeys )
{
CUInt128 keyID;
file.readUInt128(&keyID);
numSource = file.readUInt32();
while( numSource )
{
CUInt128 sourceID;
file.readUInt128(&sourceID);
numName = file.readUInt32();
while( numName )
{
Kademlia::CEntry* toaddN = new Kademlia::CEntry();
uint32 test = file.readUInt32();
toaddN->lifetime = test;
//tagList = file.readUInt32LE();
tagList = file.readByte();
bool source = true;
while( tagList )
{
CTag* tag = file.readTag();
if (!tag->m_name.Compare(TAG_SOURCETYPE))
{
toaddN->source = true;
}
if (!tag->m_name.Compare(TAG_NAME))
{
toaddN->fileName = tag->GetStr();
source = false;
// NOTE: always add the 'name' tag, even if it's stored separately in 'fileName'. the tag is still needed for answering search request
toaddN->taglist.push_back(tag);
}
else if (!tag->m_name.Compare(TAG_SIZE))
{
toaddN->size = tag->GetInt();
// NOTE: always add the 'size' tag, even if it's stored separately in 'size'. the tag is still needed for answering search request
toaddN->taglist.push_back(tag);
}
else if (!tag->m_name.Compare(TAG_SOURCEIP))
{
toaddN->ip = tag->GetInt();
toaddN->taglist.push_back(tag);
}
else if (!tag->m_name.Compare(TAG_SOURCEPORT))
{
toaddN->tcpport = tag->GetInt();
toaddN->taglist.push_back(tag);
}
else if (!tag->m_name.Compare(TAG_SOURCEUPORT))
{
toaddN->udpport = tag->GetInt();
toaddN->taglist.push_back(tag);
}
else
{
toaddN->taglist.push_back(tag);
}
tagList--;
}
if( toaddN->lifetime < now)
{
delete toaddN;
}
else
{
toaddN->keyID.setValue(keyID);
toaddN->sourceID.setValue(sourceID);
IndexedAdd(keyID, sourceID, toaddN);
total++;
}
numName--;
}
numSource--;
}
numKeys--;
}
file.Close();
CKademlia::debugMsg("Read %u indexed entries", total);
}
} catch (...) {TRACE("\n************Error reading Entries!\n");ASSERT(0);}
}
void CIndexed::writeFile(void)
{
try
{
uint32 total = 0;
uint32 numKeys = 0;
uint32 numSource = 0;
uint32 numName = 0;
uint32 tagList = 0;
CFileIO file;
if (file.Open(m_filename.GetBuffer(0), CFile::modeWrite | CFile::modeCreate))
{
numKeys = this->keywordHashList.GetCount();
file.writeUInt32(numKeys);
for(POSITION pos = keywordHashList.GetHeadPosition(); pos != NULL; )
{
KeywordHash* currKeywordHash = keywordHashList.GetNext(pos);
file.writeUInt128(currKeywordHash->keyID);
numSource = currKeywordHash->sourceList.GetCount();
file.writeUInt32(numSource);
for(POSITION pos3 = currKeywordHash->sourceList.GetHeadPosition(); pos3 != NULL; )
{
Source* currSource = currKeywordHash->sourceList.GetNext(pos3);
file.writeUInt128(currSource->sourceID);
numName = currSource->entryList.GetCount();
file.writeUInt32(numName);
for(POSITION pos5 = currSource->entryList.GetHeadPosition(); pos5 != NULL; )
{
Kademlia::CEntry* currName = currSource->entryList.GetNext(pos5);
uint32 test = (uint32)currName->lifetime;
file.writeUInt32(test);
file.writeTagList(currName->taglist);
total++;
}
}
}
file.Close();
CKademlia::debugMsg("Wrote %u indexed entries", total);
}
}
catch (...) {ASSERT(0);}
}
void CIndexed::clean(void)
{
try
{
uint32 total = 0;
time_t expire = time(NULL) - (KADEMLIAINDEXCLEAN);
if( m_lastClean > time(NULL) )
{
return;
}
for(POSITION pos = keywordHashList.GetHeadPosition(); pos != NULL; )
{
POSITION pos2 = pos;
KeywordHash* currKeywordHash = keywordHashList.GetNext(pos);
for(POSITION pos3 = currKeywordHash->sourceList.GetHeadPosition(); pos3 != NULL; )
{
POSITION pos4 = pos3;
Source* currSource = currKeywordHash->sourceList.GetNext(pos3);
for(POSITION pos5 = currSource->entryList.GetHeadPosition(); pos5 != NULL; )
{
POSITION pos6 = pos5;
Kademlia::CEntry* currName = currSource->entryList.GetNext(pos5);
if( currName->lifetime < expire)
{
currSource->entryList.RemoveAt(pos6);
delete currName;
total++;
}
}
if( currSource->entryList.IsEmpty())
{
currKeywordHash->sourceList.RemoveAt(pos4);
delete currSource;
}
}
if( currKeywordHash->sourceList.IsEmpty())
{
keywordHashList.RemoveAt(pos2);
delete currKeywordHash;
}
}
CKademlia::debugMsg("Removed %u old indexed entries", total);
m_lastClean = time(NULL) + (60*30);
} catch(...){ASSERT(0);}
}
CIndexed::~CIndexed()
{
try
{
writeFile();
while( !keywordHashList.IsEmpty()){
KeywordHash* currKeywordHash = keywordHashList.GetHead();
while( !currKeywordHash->sourceList.IsEmpty()){
Source* currSource = currKeywordHash->sourceList.GetHead();
while( !currSource->entryList.IsEmpty() ){
Kademlia::CEntry* del1 = currSource->entryList.GetHead();
currSource->entryList.RemoveHead();
delete del1;
}
Source* del2 = currKeywordHash->sourceList.GetHead();
currKeywordHash->sourceList.RemoveHead();
delete del2;
}
KeywordHash* del3 = keywordHashList.GetHead();
keywordHashList.RemoveHead();
delete del3;
}
} catch(...){ASSERT(0);}
}
bool CIndexed::IndexedAdd(Kademlia::CUInt128 keyWordID, Kademlia::CUInt128 sourceID, Kademlia::CEntry* entry){
try
{
if(keywordHashList.IsEmpty()){
KeywordHash* toaddH = new KeywordHash;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -