Exemplo n.º 1
0
void CRoutingZone::WriteFile()
{
	try
	{
		// Write a saved contact list.
		CUInt128 uID;
		//CSafeBufferedFile file;
		CBufferedFileIO file;
		CFileException fexp;
		if (file.Open(m_sFilename, CFile::modeWrite | CFile::modeCreate | CFile::typeBinary|CFile::shareDenyWrite, &fexp))
		{
			setvbuf(file.m_pStream, NULL, _IOFBF, 32768);

			// The bootstrap method gets a very nice sample of contacts to save.
			ContactList listContacts;
			GetBootstrapContacts(&listContacts, 200);
			// Start file with 0 to prevent older clients from reading it.
			file.WriteUInt32(0);
			// Now tag it with a version which happens to be 1.
			file.WriteUInt32(1);
			file.WriteUInt32((uint32)listContacts.size());
			for (ContactList::const_iterator itContactList = listContacts.begin(); itContactList != listContacts.end(); ++itContactList)
			{
				CContact* pContact = *itContactList;
				pContact->GetClientID(&uID);
				file.WriteUInt128(&uID);
				file.WriteUInt32(pContact->GetIPAddress());
				file.WriteUInt16(pContact->GetUDPPort());
				file.WriteUInt16(pContact->GetTCPPort());
				file.WriteUInt8(pContact->GetVersion());
			}
			file.Close();
			//AddDebugLogLine( false, _T("Wrote %ld contact%s to file."), listContacts.size(), ((listContacts.size() == 1) ? _T("") : _T("s")));
			TRACE(_T("Wrote %ld contact%s to file.\n"), listContacts.size(), ((listContacts.size() == 1) ? _T("") : _T("s")));
		}
	}
	catch (CFileException* e)
	{
		e->Delete();
		AddDebugLogLine(false, _T("CFileException in CRoutingZone::writeFile"));
	}
}
Exemplo n.º 2
0
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;
		}
	}
	catch ( CIOException *ioe )
	{
		AddDebugLogLine( false, _T("Exception in CIndexed::~CIndexed (IO error(%i))"), ioe->m_iCause);
		ioe->Delete();
	}
	catch (...)
	{
		AddDebugLogLine(false, _T("Exception in CIndexed::~CIndexed"));
	}
}