⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indexed.cpp

📁 电驴的源程序,想学网络编程的可以下载来看看.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*
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 "./Entry.h"
#include "./Prefs.h"
#include "../net/KademliaUDPListener.h"
#include "../utils/MiscUtils.h"
#include "../io/BufferedFileIO.h"
#include "../io/IOException.h"
#include "../io/ByteIO.h"
#include "../../Preferences.h"
#include "../../Log.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

using namespace Kademlia;

void DebugSend(LPCTSTR pszMsg, uint32 uIP, uint16 uPort);

CString CIndexed::m_sKeyFileName;
CString CIndexed::m_sSourceFileName;
CString CIndexed::m_sLoadFileName;

CIndexed::CIndexed()
{
	m_mapKeyword.InitHashTable(1031);
	m_mapNotes.InitHashTable(1031);
	m_mapLoad.InitHashTable(1031);
	m_mapSources.InitHashTable(1031);
	m_sSourceFileName = CMiscUtils::GetAppDir();
	m_sSourceFileName.Append(CONFIGFOLDER);
	m_sSourceFileName.Append(_T("src_index.dat"));
	m_sKeyFileName = CMiscUtils::GetAppDir();
	m_sKeyFileName.Append(CONFIGFOLDER);
	m_sKeyFileName.Append(_T("key_index.dat"));
	m_sLoadFileName = CMiscUtils::GetAppDir();
	m_sLoadFileName.Append(CONFIGFOLDER);
	m_sLoadFileName.Append(_T("load_index.dat"));
	m_tLastClean = time(NULL) + (60*30);
	m_uTotalIndexSource = 0;
	m_uTotalIndexKeyword = 0;
	m_uTotalIndexNotes = 0;
	m_uTotalIndexLoad = 0;
	ReadFile();
}

void CIndexed::ReadFile(void)
{
	try
	{
		uint32 uTotalLoad = 0;
		uint32 uTotalSource = 0;
		uint32 uTotalKeyword = 0;
		CUInt128 uKeyID, uID, uSourceID;

		CBufferedFileIO fileLoad;
		if(fileLoad.Open(m_sLoadFileName, CFile::modeRead | CFile::typeBinary | CFile::shareDenyWrite))
		{
			setvbuf(fileLoad.m_pStream, NULL, _IOFBF, 32768);
			uint32 uVersion = fileLoad.ReadUInt32();
			if(uVersion<2)
			{
				/*time_t tSaveTime = */fileLoad.ReadUInt32();
				uint32 uNumLoad = fileLoad.ReadUInt32();
				while(uNumLoad)
				{
					fileLoad.ReadUInt128(&uKeyID);
					if(AddLoad(uKeyID, fileLoad.ReadUInt32()))
						uTotalLoad++;
					uNumLoad--;
				}
			}
			fileLoad.Close();
		}

		CBufferedFileIO fileKey;
		if (fileKey.Open(m_sKeyFileName, CFile::modeRead | CFile::typeBinary | CFile::shareDenyWrite))
		{
			setvbuf(fileKey.m_pStream, NULL, _IOFBF, 32768);

			uint32 uVersion = fileKey.ReadUInt32();
			if( uVersion < 3 )
			{
				time_t tSaveTime = fileKey.ReadUInt32();
				if( tSaveTime > time(NULL) )
				{
					fileKey.ReadUInt128(&uID);
					if( Kademlia::CKademlia::GetPrefs()->GetKadID() == uID )
					{
						uint32 uNumKeys = fileKey.ReadUInt32();
						while( uNumKeys )
						{
							fileKey.ReadUInt128(&uKeyID);
							uint32 uNumSource = fileKey.ReadUInt32();
							while( uNumSource )
							{
								fileKey.ReadUInt128(&uSourceID);
								uint32 uNumName = fileKey.ReadUInt32();
								while( uNumName )
								{
									CEntry* pToAdd = new Kademlia::CEntry();
									pToAdd->m_bSource = false;
									pToAdd->m_tLifetime = fileKey.ReadUInt32();
									uint32 uTotalTags = fileKey.ReadByte();
									while( uTotalTags )
									{
										CKadTag* pTag = fileKey.ReadTag();
										if(pTag)
										{
											if (!pTag->m_name.Compare(TAG_FILENAME))
											{
												pToAdd->m_fileName = pTag->GetStr();
												KadTagStrMakeLower(pToAdd->m_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
												pToAdd->m_listTag.push_back(pTag);
											}
											else if (!pTag->m_name.Compare(TAG_FILESIZE))
											{
												pToAdd->m_uSize = pTag->GetInt();
												// NOTE: always add the 'size' tag, even if it's stored separately in 'size'. the tag is still needed for answering search request
												pToAdd->m_listTag.push_back(pTag);
											}
											else if (!pTag->m_name.Compare(TAG_SOURCEIP))
											{
												pToAdd->m_uIP = (uint32)pTag->GetInt();
												pToAdd->m_listTag.push_back(pTag);
											}
											else if (!pTag->m_name.Compare(TAG_SOURCEPORT))
											{
												pToAdd->m_uTCPPort = (uint16)pTag->GetInt();
												pToAdd->m_listTag.push_back(pTag);
											}
											else if (!pTag->m_name.Compare(TAG_SOURCEUPORT))
											{
												pToAdd->m_uUDPPort = (uint16)pTag->GetInt();
												pToAdd->m_listTag.push_back(pTag);
											}
											else
											{
												pToAdd->m_listTag.push_back(pTag);
											}
										}
										uTotalTags--;
									}
									pToAdd->m_uKeyID.SetValue(uKeyID);
									pToAdd->m_uSourceID.SetValue(uSourceID);
									uint8 uLoad;
									if(AddKeyword(uKeyID, uSourceID, pToAdd, uLoad))
										uTotalKeyword++;
									else
										delete pToAdd;
									uNumName--;
								}
								uNumSource--;
							}
							uNumKeys--;
						}
					}
				}
			}
			fileKey.Close();
		}

		CBufferedFileIO fileSource;
		if (fileSource.Open(m_sSourceFileName, CFile::modeRead | CFile::typeBinary | CFile::shareDenyWrite))
		{
			setvbuf(fileSource.m_pStream, NULL, _IOFBF, 32768);

			uint32 uVersion = fileSource.ReadUInt32();
			if( uVersion < 3 )
			{
				time_t tSaveTime = fileSource.ReadUInt32();
				if( tSaveTime > time(NULL) )
				{
					uint32 uNumKeys = fileSource.ReadUInt32();
					while( uNumKeys )
					{
						fileSource.ReadUInt128(&uKeyID);
						uint32 uNumSource = fileSource.ReadUInt32();
						while( uNumSource )
						{
							fileSource.ReadUInt128(&uSourceID);
							uint32 uNumName = fileSource.ReadUInt32();
							while( uNumName )
							{
								CEntry* pToAdd = new Kademlia::CEntry();
								pToAdd->m_bSource = true;
								pToAdd->m_tLifetime = fileSource.ReadUInt32();
								uint32 uTotalTags = fileSource.ReadByte();
								while( uTotalTags )
								{
									CKadTag* pTag = fileSource.ReadTag();
									if(pTag)
									{
										if (!pTag->m_name.Compare(TAG_SOURCEIP))
										{
											pToAdd->m_uIP = (uint32)pTag->GetInt();
											pToAdd->m_listTag.push_back(pTag);
										}
										else if (!pTag->m_name.Compare(TAG_SOURCEPORT))
										{
											pToAdd->m_uTCPPort = (uint16)pTag->GetInt();
											pToAdd->m_listTag.push_back(pTag);
										}
										else if (!pTag->m_name.Compare(TAG_SOURCEUPORT))
										{
											pToAdd->m_uUDPPort = (uint16)pTag->GetInt();
											pToAdd->m_listTag.push_back(pTag);
										}
										else
										{
											pToAdd->m_listTag.push_back(pTag);
										}
									}
									uTotalTags--;
								}
								pToAdd->m_uKeyID.SetValue(uKeyID);
								pToAdd->m_uSourceID.SetValue(uSourceID);
								uint8 uLoad;
								if(AddSources(uKeyID, uSourceID, pToAdd, uLoad))
									uTotalSource++;
								else
									delete pToAdd;
								uNumName--;
							}
							uNumSource--;
						}
						uNumKeys--;
					}
				}
			}
			fileSource.Close();

			m_uTotalIndexSource = uTotalSource;
			m_uTotalIndexKeyword = uTotalKeyword;
			m_uTotalIndexLoad = uTotalLoad;
			AddDebugLogLine( false, _T("Read %u source, %u keyword, and %u load entries"), uTotalSource, uTotalKeyword, uTotalLoad);
		}
	}
	catch ( CIOException *ioe )
	{
		AddDebugLogLine( false, _T("Exception in CIndexed::readFile (IO error(%i))"), ioe->m_iCause);
		ioe->Delete();
	}
	catch (...)
	{
		AddDebugLogLine(false, _T("Exception in CIndexed::readFile"));
	}
}

CIndexed::~CIndexed()
{
	try
	{
		uint32 uTotalSource = 0;
		uint32 uTotalKey = 0;
		uint32 uTotalLoad = 0;

		CBufferedFileIO fileLoad;
		if(fileLoad.Open(m_sLoadFileName, CFile::modeWrite | CFile::modeCreate | CFile::typeBinary | CFile::shareDenyWrite))
		{
			setvbuf(fileLoad.m_pStream, NULL, _IOFBF, 32768);
			uint32 uVersion = 1;
			fileLoad.WriteUInt32(uVersion);
			fileLoad.WriteUInt32(time(NULL));
			fileLoad.WriteUInt32(m_mapLoad.GetCount());
			POSITION pos1 = m_mapLoad.GetStartPosition();
			while( pos1 != NULL )
			{
				Load* pLoad;
				CCKey key1;
				m_mapLoad.GetNextAssoc( pos1, key1, pLoad );
				fileLoad.WriteUInt128(pLoad->uKeyID);
				fileLoad.WriteUInt32(pLoad->uTime);
				uTotalLoad++;
				delete pLoad;
			}
			fileLoad.Close();
		}

		CBufferedFileIO fileSource;
		if (fileSource.Open(m_sSourceFileName, CFile::modeWrite | CFile::modeCreate | CFile::typeBinary | CFile::shareDenyWrite))
		{
			setvbuf(fileSource.m_pStream, NULL, _IOFBF, 32768);
			uint32 uVersion = 2;
			fileSource.WriteUInt32(uVersion);
			fileSource.WriteUInt32(time(NULL)+KADEMLIAREPUBLISHTIMES);
			fileSource.WriteUInt32(m_mapSources.GetCount());
			POSITION pos1 = m_mapSources.GetStartPosition();
			while( pos1 != NULL )
			{
				CCKey key1;
				SrcHash* pCurrSrcHash;
				m_mapSources.GetNextAssoc( pos1, key1, pCurrSrcHash );
				fileSource.WriteUInt128(pCurrSrcHash->uKeyID);
				CKadSourcePtrList* keyHashSrcMap = &pCurrSrcHash->ptrlistSource;
				fileSource.WriteUInt32(keyHashSrcMap->GetCount());
				POSITION pos2 = keyHashSrcMap->GetHeadPosition();
				while( pos2 != NULL )
				{
					Source* pCurrSource = keyHashSrcMap->GetNext(pos2);
					fileSource.WriteUInt128(pCurrSource->uSourceID);
					CKadEntryPtrList* srcEntryList = &pCurrSource->ptrlEntryList;
					fileSource.WriteUInt32(srcEntryList->GetCount());
					for(POSITION pos3 = srcEntryList->GetHeadPosition(); pos3 != NULL; )
					{
						CEntry* pCurrName = srcEntryList->GetNext(pos3);
						fileSource.WriteUInt32(pCurrName->m_tLifetime);
						fileSource.WriteTagList(pCurrName->m_listTag);
						delete pCurrName;
						uTotalSource++;
					}
					delete pCurrSource;
				}
				delete pCurrSrcHash;
			}
			fileSource.Close();
		}

		CBufferedFileIO fileKey;
		if (fileKey.Open(m_sKeyFileName, CFile::modeWrite | CFile::modeCreate | CFile::typeBinary | CFile::shareDenyWrite))
		{
			setvbuf(fileKey.m_pStream, NULL, _IOFBF, 32768);
			uint32 uVersion = 2;
			fileKey.WriteUInt32(uVersion);
			fileKey.WriteUInt32(time(NULL)+KADEMLIAREPUBLISHTIMEK);
			fileKey.WriteUInt128(Kademlia::CKademlia::GetPrefs()->GetKadID());
			fileKey.WriteUInt32(m_mapKeyword.GetCount());
			POSITION pos1 = m_mapKeyword.GetStartPosition();
			while( pos1 != NULL )
			{
				CCKey key1;
				KeyHash* pCurrKeyHash;
				m_mapKeyword.GetNextAssoc( pos1, key1, pCurrKeyHash );
				fileKey.WriteUInt128(pCurrKeyHash->uKeyID);
				CSourceKeyMap* keySrcKeyMap = &pCurrKeyHash->mapSource;
				fileKey.WriteUInt32(keySrcKeyMap->GetCount());
				POSITION pos2 = keySrcKeyMap->GetStartPosition();
				while( pos2 != NULL )
				{
					Source* pCurrSource;
					CCKey key2;
					keySrcKeyMap->GetNextAssoc( pos2, key2, pCurrSource );
					fileKey.WriteUInt128(pCurrSource->uSourceID);
					CKadEntryPtrList* srcEntryList = &pCurrSource->ptrlEntryList;
					fileKey.WriteUInt32(srcEntryList->GetCount());
					for(POSITION pos3 = srcEntryList->GetHeadPosition(); pos3 != NULL; )
					{
						CEntry* pCurrName = srcEntryList->GetNext(pos3);
						fileKey.WriteUInt32(pCurrName->m_tLifetime);
						fileKey.WriteTagList(pCurrName->m_listTag);
						delete pCurrName;
						uTotalKey++;
					}
					delete pCurrSource;
				}
				delete pCurrKeyHash;
			}
			fileKey.Close();
		}
		AddDebugLogLine( false, _T("Wrote %u source, %u keyword, and %u load entries"), uTotalSource, uTotalKey, uTotalLoad);

		POSITION pos1 = m_mapNotes.GetStartPosition();
		while( pos1 != NULL )
		{
			CCKey key1;
			SrcHash* pCurrNoteHash;
			m_mapNotes.GetNextAssoc( pos1, key1, pCurrNoteHash );
			CKadSourcePtrList* keyHashNoteMap = &pCurrNoteHash->ptrlistSource;
			POSITION pos2 = keyHashNoteMap->GetHeadPosition();
			while( pos2 != NULL )
			{
				Source* pCurrNote = keyHashNoteMap->GetNext(pos2);
				CKadEntryPtrList* noteEntryList = &pCurrNote->ptrlEntryList;
				for(POSITION pos3 = noteEntryList->GetHeadPosition(); pos3 != NULL; )
				{
					delete noteEntryList->GetNext(pos3);
				}
				delete pCurrNote;
			}
			delete pCurrNoteHash;
		}
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -