📄 indexed.cpp
字号:
//// This file is part of the aMule Project.//// Copyright (c) 2004-2008 Angel Vidal (Kry) ( kry@amule.org )// Copyright (c) 2004-2008 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 "Indexed.h"#include <protocol/Protocols.h>#include <protocol/ed2k/Constants.h>#include <protocol/kad/Constants.h>#include <protocol/kad/Client2Client/UDP.h>#include <protocol/kad2/Client2Client/UDP.h>#include <common/Macros.h>#include <tags/FileTags.h>#include "../routing/Contact.h"#include "../net/KademliaUDPListener.h"#include "../utils/KadUDPKey.h"#include "../../CFile.h"#include "../../MemFile.h"#include "../../amule.h"#include "../../Preferences.h"#include "../../Logger.h"////////////////////////////////////////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; m_totalIndexNotes = 0; m_totalIndexLoad = 0; ReadFile();}void CIndexed::ReadFile(){ try { uint32_t totalLoad = 0; uint32_t totalSource = 0; uint32_t totalKeyword = 0; CFile load_file; if (CPath::FileExists(m_loadfilename) && load_file.Open(m_loadfilename, CFile::read)) { uint32_t version = load_file.ReadUInt32(); if (version < 2) { /*time_t savetime =*/ load_file.ReadUInt32(); // Savetime is unused now uint32_t numLoad = load_file.ReadUInt32(); while (numLoad) { CUInt128 keyID = load_file.ReadUInt128(); if (AddLoad(keyID, load_file.ReadUInt32())) { totalLoad++; } numLoad--; } } load_file.Close(); } CFile k_file; if (CPath::FileExists(m_kfilename) && k_file.Open(m_kfilename, CFile::read)) { uint32_t version = k_file.ReadUInt32(); if (version < 4) { time_t savetime = k_file.ReadUInt32(); if (savetime > time(NULL)) { CUInt128 id = k_file.ReadUInt128(); if (Kademlia::CKademlia::GetPrefs()->GetKadID() == id) { uint32_t numKeys = k_file.ReadUInt32(); while (numKeys) { CUInt128 keyID = k_file.ReadUInt128(); uint32_t numSource = k_file.ReadUInt32(); while (numSource) { CUInt128 sourceID = k_file.ReadUInt128(); uint32_t numName = k_file.ReadUInt32(); while (numName) { Kademlia::CKeyEntry* toAdd = new Kademlia::CKeyEntry(); toAdd->m_uKeyID = keyID; toAdd->m_uSourceID = sourceID; toAdd->m_bSource = false; toAdd->m_tLifeTime = k_file.ReadUInt32(); if (version >= 3) { toAdd->ReadPublishTrackingDataFromFile(&k_file); } uint32_t tagList = k_file.ReadUInt8(); while (tagList) { CTag* tag = k_file.ReadTag(); if (tag) { if (!tag->GetName().Cmp(TAG_FILENAME)) { if (toAdd->GetCommonFileName().IsEmpty()) { toAdd->SetFileName(tag->GetStr()); } delete tag; } else if (!tag->GetName().Cmp(TAG_FILESIZE)) { if (tag->IsBsob() && (tag->GetBsobSize() == 8)) { // We've previously wrongly saved BSOB uint64s to key_index.dat, // so we'll have to handle those here as well. Too bad ... toAdd->m_uSize = PeekUInt64(tag->GetBsob()); } else { toAdd->m_uSize = tag->GetInt(); } delete tag; } else if (!tag->GetName().Cmp(TAG_SOURCEIP)) { toAdd->m_uIP = tag->GetInt(); toAdd->AddTag(tag); } else if (!tag->GetName().Cmp(TAG_SOURCEPORT)) { toAdd->m_uTCPport = tag->GetInt(); toAdd->AddTag(tag); } else if (!tag->GetName().Cmp(TAG_SOURCEUPORT)) { toAdd->m_uUDPport = tag->GetInt(); toAdd->AddTag(tag); } else { toAdd->AddTag(tag); } } tagList--; } uint8_t load; if (AddKeyword(keyID, sourceID, toAdd, load)) { totalKeyword++; } else { delete toAdd; } numName--; } numSource--; } numKeys--; } } } } k_file.Close(); } CFile s_file; if (CPath::FileExists(m_sfilename) && s_file.Open(m_sfilename, CFile::read)) { uint32_t version = s_file.ReadUInt32(); if (version < 3) { time_t savetime = s_file.ReadUInt32(); if (savetime > time(NULL)) { uint32_t numKeys = s_file.ReadUInt32(); while (numKeys) { CUInt128 keyID = s_file.ReadUInt128(); uint32_t numSource = s_file.ReadUInt32(); while (numSource) { CUInt128 sourceID = s_file.ReadUInt128(); uint32_t numName = s_file.ReadUInt32(); while (numName) { Kademlia::CEntry* toAdd = new Kademlia::CEntry(); toAdd->m_bSource = true; toAdd->m_tLifeTime = s_file.ReadUInt32(); uint32_t tagList = s_file.ReadUInt8(); while (tagList) { CTag* tag = s_file.ReadTag(); if (tag) { if (!tag->GetName().Cmp(TAG_SOURCEIP)) { toAdd->m_uIP = tag->GetInt(); toAdd->AddTag(tag); } else if (!tag->GetName().Cmp(TAG_SOURCEPORT)) { toAdd->m_uTCPport = tag->GetInt(); toAdd->AddTag(tag); } else if (!tag->GetName().Cmp(TAG_SOURCEUPORT)) { toAdd->m_uUDPport = tag->GetInt(); toAdd->AddTag(tag); } else { toAdd->AddTag(tag); } } tagList--; } toAdd->m_uKeyID.SetValue(keyID); toAdd->m_uSourceID.SetValue(sourceID); uint8_t load; if (AddSources(keyID, sourceID, toAdd, load)) { totalSource++; } else { delete toAdd; } numName--; } numSource--; } numKeys--; } } } s_file.Close(); m_totalIndexSource = totalSource; m_totalIndexKeyword = totalKeyword; m_totalIndexLoad = totalLoad; AddDebugLogLineM(false, logKadIndex, wxString::Format(wxT("Read %u source, %u keyword, and %u load entries"),totalSource,totalKeyword,totalLoad)); } } catch (const CSafeIOException& err) { AddDebugLogLineM(true, logKadIndex, wxT("CSafeIOException in CIndexed::readFile: ") + err.what()); } catch (const CInvalidPacket& err) { AddDebugLogLineM(true, logKadIndex, wxT("CInvalidPacket Exception in CIndexed::readFile: ") + err.what()); } catch (const wxString& e) { AddDebugLogLineM(true, logKadIndex, wxT("Exception in CIndexed::readFile: ") + e); }}CIndexed::~CIndexed(){ try { time_t now = time(NULL); uint32_t s_total = 0; uint32_t k_total = 0; uint32_t l_total = 0; CFile load_file; if (load_file.Open(m_loadfilename, CFile::write)) { load_file.WriteUInt32(1); // version load_file.WriteUInt32(now); wxASSERT(m_Load_map.size() < 0xFFFFFFFF); load_file.WriteUInt32((uint32_t)m_Load_map.size()); for (LoadMap::iterator it = m_Load_map.begin(); 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(); } CFile s_file; if (s_file.Open(m_sfilename, CFile::write)) { s_file.WriteUInt32(2); // version s_file.WriteUInt32(now + KADEMLIAREPUBLISHTIMES); wxASSERT(m_Sources_map.size() < 0xFFFFFFFF); s_file.WriteUInt32((uint32_t)m_Sources_map.size()); for (SrcHashMap::iterator itSrcHash = m_Sources_map.begin(); itSrcHash != m_Sources_map.end(); ++itSrcHash ) { SrcHash* currSrcHash = itSrcHash->second; s_file.WriteUInt128(currSrcHash->keyID); CKadSourcePtrList& KeyHashSrcMap = currSrcHash->m_Source_map; wxASSERT(KeyHashSrcMap.size() < 0xFFFFFFFF); s_file.WriteUInt32((uint32_t)KeyHashSrcMap.size()); for (CKadSourcePtrList::iterator itSource = KeyHashSrcMap.begin(); itSource != KeyHashSrcMap.end(); ++itSource) { Source* currSource = *itSource; s_file.WriteUInt128(currSource->sourceID); CKadEntryPtrList& SrcEntryList = currSource->entryList; wxASSERT(SrcEntryList.size() < 0xFFFFFFFF); s_file.WriteUInt32((uint32_t)SrcEntryList.size()); for (CKadEntryPtrList::iterator itEntry = SrcEntryList.begin(); itEntry != SrcEntryList.end(); ++itEntry) { Kademlia::CEntry* currName = *itEntry; s_file.WriteUInt32(currName->m_tLifeTime); currName->WriteTagList(&s_file); delete currName; s_total++; } delete currSource; } delete currSrcHash; } s_file.Close(); } CFile k_file; if (k_file.Open(m_kfilename, CFile::write)) { k_file.WriteUInt32(3); // version k_file.WriteUInt32(now + KADEMLIAREPUBLISHTIMEK); k_file.WriteUInt128(Kademlia::CKademlia::GetPrefs()->GetKadID()); wxASSERT(m_Keyword_map.size() < 0xFFFFFFFF); k_file.WriteUInt32((uint32_t)m_Keyword_map.size()); for (KeyHashMap::iterator itKeyHash = m_Keyword_map.begin(); itKeyHash != m_Keyword_map.end(); ++itKeyHash ) { KeyHash* currKeyHash = itKeyHash->second; k_file.WriteUInt128(currKeyHash->keyID); CSourceKeyMap& KeyHashSrcMap = currKeyHash->m_Source_map; wxASSERT(KeyHashSrcMap.size() < 0xFFFFFFFF); k_file.WriteUInt32((uint32_t)KeyHashSrcMap.size()); for (CSourceKeyMap::iterator itSource = KeyHashSrcMap.begin(); itSource != KeyHashSrcMap.end(); ++itSource ) { Source* currSource = itSource->second; k_file.WriteUInt128(currSource->sourceID); CKadEntryPtrList& SrcEntryList = currSource->entryList; wxASSERT(SrcEntryList.size() < 0xFFFFFFFF); k_file.WriteUInt32((uint32_t)SrcEntryList.size());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -