📄 indexed.cpp
字号:
//// This file is part of the aMule Project.//// Copyright (c) 2004-2006 Angel Vidal (Kry) ( kry@amule.org )// Copyright (c) 2004-2006 aMule Team ( admin@amule.org / http://www.amule.org )// Copyright (c) 2003 Barry Dunne (http://www.emule-project.net)//// Any parts of this program derived from the xMule, lMule or eMule project,// or contributed by third-party developers are copyrighted by their// respective authors.//// 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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, pleasepost 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 knowingwhat 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 advertisethere client on the eMule forum..*///#include "stdafx.h"//#include "../utils/MiscUtils.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/UInt128.h"#include "../../OtherFunctions.h"#include "../kademlia/Tag.h"#include "../io/FileIO.h"#include "../io/IOException.h"#include "amule.h"#include "Preferences.h"#include "Logger.h"#include <wx/arrstr.h>#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif////////////////////////////////////////using namespace Kademlia;////////////////////////////////////////wxString CIndexed::m_kfilename;wxString CIndexed::m_sfilename;wxString CIndexed::m_loadfilename;CIndexed::CIndexed(){ m_sfilename = theApp.ConfigDir + wxT("src_index.dat"); m_kfilename = theApp.ConfigDir + wxT("key_index.dat"); m_loadfilename = theApp.ConfigDir + wxT("load_index.dat"); m_lastClean = time(NULL) + (60*30); m_totalIndexSource = 0; m_totalIndexKeyword = 0; readFile();}void CIndexed::readFile(void){ try { uint32 totalLoad = 0; uint32 totalSource = 0; uint32 totalKeyword = 0; uint32 numKeys = 0; uint32 numSource = 0; uint32 numName = 0; uint32 numLoad = 0; uint32 tagList = 0; CBufferedFileIO load_file; if(load_file.Open(m_loadfilename, CFile::read)) { uint32 version = load_file.readUInt32(); if(version<2) { /*time_t savetime =*/ load_file.readUInt32(); // Savetime is unused now numLoad = load_file.readUInt32(); while(numLoad) { CUInt128 keyID; load_file.readUInt128(&keyID); if(AddLoad(keyID, load_file.readUInt32())) { totalLoad++; } numLoad--; } } load_file.Close(); } CBufferedFileIO k_file; if (k_file.Open(m_kfilename, CFile::read)) { uint32 version = k_file.readUInt32(); if( version < 2 ) { time_t savetime = k_file.readUInt32(); if( savetime > time(NULL) ) { CUInt128 id; k_file.readUInt128(&id); if( !Kademlia::CKademlia::getPrefs()->getKadID().compareTo(id) ) { numKeys = k_file.readUInt32(); while( numKeys ) { CUInt128 keyID; k_file.readUInt128(&keyID); numSource = k_file.readUInt32(); while( numSource ) { CUInt128 sourceID; k_file.readUInt128(&sourceID); numName = k_file.readUInt32(); while( numName ) { Kademlia::CEntry* toaddN = new Kademlia::CEntry(); toaddN->source = false; uint32 expire = k_file.readUInt32(); toaddN->lifetime = expire; tagList = k_file.readByte(); while( tagList ) { CTag* tag = k_file.readTag(); if(tag) { if (!tag->m_name.Cmp(wxT(TAG_FILENAME))) { toaddN->fileName = tag->GetStr(); KadTagStrMakeLower(toaddN->fileName); // Make lowercase, the search code expects lower case strings! // 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.Cmp(wxT(TAG_FILESIZE))) { 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.Cmp(wxT(TAG_SOURCEIP))) { toaddN->ip = tag->GetInt(); toaddN->taglist.push_back(tag); } else if (!tag->m_name.Cmp(wxT(TAG_SOURCEPORT))) { toaddN->tcpport = tag->GetInt(); toaddN->taglist.push_back(tag); } else if (!tag->m_name.Cmp(wxT(TAG_SOURCEUPORT))) { toaddN->udpport = tag->GetInt(); toaddN->taglist.push_back(tag); } else { toaddN->taglist.push_back(tag); } } tagList--; } toaddN->keyID.setValue(keyID); toaddN->sourceID.setValue(sourceID); uint8 load = 0; if(AddKeyword(keyID, sourceID, toaddN, load)) { totalKeyword++; } else { delete toaddN; } numName--; } numSource--; } numKeys--; } } } } k_file.Close(); } CBufferedFileIO s_file; if (s_file.Open(m_sfilename, CFile::read)) { uint32 version = s_file.readUInt32(); if( version < 2 ) { time_t savetime = s_file.readUInt32(); if( savetime > time(NULL) ) { numKeys = s_file.readUInt32(); CUInt128 id; while( numKeys ) { CUInt128 keyID; s_file.readUInt128(&keyID); numSource = s_file.readUInt32(); while( numSource ) { CUInt128 sourceID; s_file.readUInt128(&sourceID); numName = s_file.readUInt32(); while( numName ) { Kademlia::CEntry* toaddN = new Kademlia::CEntry(); toaddN->source = true; uint32 test = s_file.readUInt32(); toaddN->lifetime = test; tagList = s_file.readByte(); while( tagList ) { CTag* tag = s_file.readTag(); if(tag) { if (!tag->m_name.Cmp(wxT(TAG_SOURCEIP))) { toaddN->ip = tag->GetInt(); toaddN->taglist.push_back(tag); } else if (!tag->m_name.Cmp(wxT(TAG_SOURCEPORT))) { toaddN->tcpport = tag->GetInt(); toaddN->taglist.push_back(tag); } else if (!tag->m_name.Cmp(wxT(TAG_SOURCEUPORT))) { toaddN->udpport = tag->GetInt(); toaddN->taglist.push_back(tag); } else { toaddN->taglist.push_back(tag); } } tagList--; } toaddN->keyID.setValue(keyID); toaddN->sourceID.setValue(sourceID); uint8 load = 0; if(AddSources(keyID, sourceID, toaddN, load)) { totalSource++; } else { delete toaddN; } numName--; } numSource--; } numKeys--; } } } s_file.Close(); m_totalIndexSource = totalSource; m_totalIndexKeyword = totalKeyword; AddDebugLogLineM( false, logKadIndex, wxString::Format(wxT("Read %u source, %u keyword, and %u load entries"),totalSource,totalKeyword,totalLoad)); } } catch (const CIOException& ioe) { AddDebugLogLineM(true, logKadIndex, wxString::Format(wxT("Exception in CIndexed::readFile (IO error(%i))"), ioe.m_cause)); } catch (const wxString& e) { AddDebugLogLineM(true, logKadIndex, wxT("Exception in CIndexed::readFile: ") + e); }}CIndexed::~CIndexed(){ try { uint32 s_total = 0; uint32 k_total = 0; uint32 l_total = 0; CBufferedFileIO load_file; if(load_file.Open(m_loadfilename, CFile::write)) { uint32 version = 1; load_file.writeUInt32(version); load_file.writeUInt32(time(NULL)); load_file.writeUInt32(m_Load_map.size()); LoadMap::iterator it = m_Load_map.begin(); for ( ; it != m_Load_map.end(); ++it ) { Load* load = it->second; wxASSERT(load); if (load) { load_file.writeUInt128(load->keyID); load_file.writeUInt32(load->time); l_total++; delete load; } } load_file.Close(); } CBufferedFileIO s_file; if (s_file.Open(m_sfilename, CFile::write)) { uint32 version = 1; s_file.writeUInt32(version); s_file.writeUInt32(time(NULL)+KADEMLIAREPUBLISHTIMES); s_file.writeUInt32(m_Sources_map.size()); SrcHashMap::iterator itSrcHash = m_Sources_map.begin(); for ( ; itSrcHash != m_Sources_map.end(); ++itSrcHash ) { SrcHash* currSrcHash = itSrcHash->second; s_file.writeUInt128(currSrcHash->keyID); CKadSourcePtrList& KeyHashSrcMap = currSrcHash->m_Source_map; s_file.writeUInt32(KeyHashSrcMap.size()); CKadSourcePtrList::iterator itSource = KeyHashSrcMap.begin(); for (; itSource != KeyHashSrcMap.end(); ++itSource) { Source* currSource = *itSource; s_file.writeUInt128(currSource->sourceID); CKadEntryPtrList& SrcEntryList = currSource->entryList; s_file.writeUInt32(SrcEntryList.size()); CKadEntryPtrList::iterator itEntry = SrcEntryList.begin(); for (; itEntry != SrcEntryList.end(); ++itEntry) { Kademlia::CEntry* currName = *itEntry; s_file.writeUInt32(currName->lifetime); s_file.writeTagList(currName->taglist); delete currName; s_total++; } delete currSource; } delete currSrcHash; } s_file.Close(); } CBufferedFileIO k_file; if (k_file.Open(m_kfilename, CFile::write)) { uint32 version = 1; k_file.writeUInt32(version); k_file.writeUInt32(time(NULL)+KADEMLIAREPUBLISHTIMEK); k_file.writeUInt128(Kademlia::CKademlia::getPrefs()->getKadID()); k_file.writeUInt32(m_Keyword_map.size()); KeyHashMap::iterator itKeyHash = m_Keyword_map.begin(); for ( ; itKeyHash != m_Keyword_map.end(); ++itKeyHash ) { KeyHash* currKeyHash = itKeyHash->second; k_file.writeUInt128(currKeyHash->keyID); CSourceKeyMap& KeyHashSrcMap = currKeyHash->m_Source_map; k_file.writeUInt32(KeyHashSrcMap.size()); CSourceKeyMap::iterator itSource = KeyHashSrcMap.begin(); for ( ; itSource != KeyHashSrcMap.end(); ++itSource ) { Source* currSource = itSource->second; k_file.writeUInt128(currSource->sourceID); CKadEntryPtrList& SrcEntryList = currSource->entryList; k_file.writeUInt32(SrcEntryList.size()); CKadEntryPtrList::iterator itEntry = SrcEntryList.begin(); for (; itEntry != SrcEntryList.end(); ++itEntry) { Kademlia::CEntry* currName = *itEntry; k_file.writeUInt32(currName->lifetime); k_file.writeTagList(currName->taglist); delete currName; k_total++; } delete currSource; } delete currKeyHash; } k_file.Close(); } AddDebugLogLineM( false, logKadIndex, wxString::Format(wxT("Wrote %u source, %u keyword, and %u load entries"), s_total, k_total, l_total)); SrcHashMap::iterator itNoteHash = m_Notes_map.begin(); for ( ; itNoteHash != m_Notes_map.end(); ++itNoteHash) { SrcHash* currNoteHash = itNoteHash->second; CKadSourcePtrList& KeyHashNoteMap = currNoteHash->m_Source_map;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -