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

📄 crawlsession.cpp

📁 著名的下载软件核心Shareaza
💻 CPP
字号:
//
// CrawlSession.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2004.
// This file is part of SHAREAZA (www.shareaza.com)
//
// Shareaza 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.
//
// Shareaza 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 Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#include "StdAfx.h"
#include "Shareaza.h"
#include "Network.h"
#include "Datagrams.h"
#include "G2Packet.h"
#include "CrawlSession.h"

#include "Neighbours.h"
#include "Neighbour.h"

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

CCrawlSession CrawlSession;


//////////////////////////////////////////////////////////////////////
// CCrawlSession construction

CCrawlSession::CCrawlSession()
{
	m_bActive = FALSE;
}

CCrawlSession::~CCrawlSession()
{
	Clear();
}

//////////////////////////////////////////////////////////////////////
// CCrawlSession clear

void CCrawlSession::Clear()
{
	for ( POSITION pos = m_pNodes.GetHeadPosition() ; pos ; )
	{
		delete (CCrawlNode*)m_pNodes.GetNext( pos );
	}
	
	m_pNodes.RemoveAll();
}

//////////////////////////////////////////////////////////////////////
// CCrawlSession bootstrap

void CCrawlSession::Bootstrap()
{
	for ( POSITION pos = Neighbours.GetIterator() ; pos ; )
	{
		CNeighbour* pNeighbour = Neighbours.GetNext( pos );
		
		if ( pNeighbour->m_nNodeType != ntLeaf &&
			 pNeighbour->m_nProtocol == PROTOCOL_G2 )
		{
			SendCrawl( &pNeighbour->m_pHost );
		}
	}
}

//////////////////////////////////////////////////////////////////////
// CCrawlSession send a crawl request

void CCrawlSession::SendCrawl(SOCKADDR_IN* pHost)
{
	theApp.Message( MSG_TEMP, _T("CRAWL: Crawling host %s"),
		(LPCTSTR)CString( inet_ntoa( pHost->sin_addr ) ) );
	
	CG2Packet* pPacket = CG2Packet::New( G2_PACKET_CRAWL_REQ, TRUE );
	pPacket->WritePacket( "RLEAF", 0 );
	pPacket->WritePacket( "RNAME", 0 );
	pPacket->WritePacket( "RGPS", 0 );
	Datagrams.Send( pHost, pPacket );
}

//////////////////////////////////////////////////////////////////////
// CCrawlSession counts

int CCrawlSession::GetHubCount()
{
	int nCount = 0;
	
	for ( POSITION pos = m_pNodes.GetHeadPosition() ; pos ; )
	{
		CCrawlNode* pNode = (CCrawlNode*)m_pNodes.GetNext( pos );
		if ( pNode->m_nType == CCrawlNode::ntHub ) nCount ++;
	}
	
	return nCount;
}

int CCrawlSession::GetLeafCount()
{
	int nCount = 0;
	
	for ( POSITION pos = m_pNodes.GetHeadPosition() ; pos ; )
	{
		CCrawlNode* pNode = (CCrawlNode*)m_pNodes.GetNext( pos );
		if ( pNode->m_nType == CCrawlNode::ntLeaf ) nCount ++;
	}
	
	return nCount;
}

//////////////////////////////////////////////////////////////////////
// CCrawlSession run

void CCrawlSession::OnRun()
{
	if ( ! m_bActive ) return;
	
	DWORD tNow = time( NULL );
	
	for ( POSITION pos = m_pNodes.GetTailPosition() ; pos ; )
	{
		CCrawlNode* pNode = (CCrawlNode*)m_pNodes.GetPrev( pos );
		
		if ( pNode->m_nType == CCrawlNode::ntHub &&
			 pNode->m_tResponse == 0 &&
			 tNow - pNode->m_tCrawled >= 30 )
		{
			pNode->m_tCrawled = tNow;
			SendCrawl( &pNode->m_pHost );
			break;
		}
	}
}

//////////////////////////////////////////////////////////////////////
// CCrawlSession process a crawl reply

void CCrawlSession::OnCrawl(SOCKADDR_IN* pHost, CG2Packet* pPacket)
{
	if ( ! m_bActive ) return;
	
	theApp.Message( MSG_TEMP, _T("CRAWL: Response from %s"),
		(LPCTSTR)CString( inet_ntoa( pHost->sin_addr ) ) );
	
	CCrawlNode* pNode = Find( &pHost->sin_addr, TRUE );
	
	pNode->OnCrawl( this, pPacket );
	
	// pNode->Dump();
}

//////////////////////////////////////////////////////////////////////
// CCrawlSession find a crawled node

CCrawlNode* CCrawlSession::Find(IN_ADDR* pAddress, BOOL bCreate)
{
	for ( POSITION pos = m_pNodes.GetTailPosition() ; pos ; )
	{
		CCrawlNode* pNode = (CCrawlNode*)m_pNodes.GetPrev( pos );
		
		if ( pNode->m_pHost.sin_addr.S_un.S_addr == pAddress->S_un.S_addr )
		{
			return pNode;
		}
	}
	
	if ( ! bCreate ) return NULL;
	
	CCrawlNode* pNode = new CCrawlNode();
	pNode->m_nUnique = (DWORD)m_pNodes.AddTail( pNode );
	
	return pNode;
}


//////////////////////////////////////////////////////////////////////
// CCrawlNode construction

CCrawlNode::CCrawlNode()
{
	ZeroMemory( &m_pHost, sizeof(m_pHost) );
	
	m_nType			= ntUnknown;
	m_nLeaves		= 0;
	m_nLatitude		= 0;
	m_nLongitude	= 0;
	
	m_tDiscovered	= time( NULL );
	m_tCrawled		= 0;
	m_tResponse		= 0;
}

CCrawlNode::~CCrawlNode()
{
}

//////////////////////////////////////////////////////////////////////
// CCrawlNode process a crawl reply

void CCrawlNode::OnCrawl(CCrawlSession* pSession, CG2Packet* pPacket)
{
	BOOL bCompound;
	CHAR szType[9];
	DWORD nLength;
	
	m_tResponse = time( NULL );
	if ( m_tCrawled == 0 ) m_tCrawled = m_tResponse;
	
	while ( pPacket->ReadPacket( szType, nLength, &bCompound ) )
	{
		DWORD nNext = pPacket->m_nPosition + nLength;
		
		if ( strcmp( szType, "SELF" ) == 0 )
		{
			OnNode( pSession, pPacket, nLength, parseSelf );
		}
		else if ( strcmp( szType, "NH" ) == 0 )
		{
			OnNode( pSession, pPacket, nLength, parseHub );
		}
		else if ( strcmp( szType, "NL" ) == 0 )
		{
			OnNode( pSession, pPacket, nLength, parseLeaf );
		}
		
		pPacket->m_nPosition = nNext;
	}
}

//////////////////////////////////////////////////////////////////////
// CCrawlNode process a crawl reply node

void CCrawlNode::OnNode(CCrawlSession* pSession, CG2Packet* pPacket, DWORD nPacket, int nType)
{
	SOCKADDR_IN pHost;
	pHost.sin_family = PF_INET + 1;
	
	BOOL bHub = FALSE;
	int nLeafs = 0;
	
	CString strNick;
	float nLatitude = 0;
	float nLongitude = 0;
	
	CHAR szType[9];
	DWORD nLength;
	
	while ( pPacket->ReadPacket( szType, nLength ) )
	{
		DWORD nNext = pPacket->m_nPosition + nLength;
		
		if ( strcmp( szType, "NA" ) == 0 && nLength >= 6 )
		{
			pHost.sin_family = PF_INET;
			pHost.sin_addr.S_un.S_addr = pPacket->ReadLongLE();
			pHost.sin_port = htons( pPacket->ReadShortBE() );
		}
		else if ( strcmp( szType, "HS" ) == 0 && nLength >= 2 )
		{
			bHub = TRUE;
			nLeafs = (int)pPacket->ReadShortBE();
		}
		else if ( strcmp( szType, "NAME" ) == 0 )
		{
			strNick = pPacket->ReadString( nLength );
		}
		else if ( strcmp( szType, "GPS" ) == 0 && nLength >= 4 )
		{
			DWORD nGPS = pPacket->ReadLongBE();
			nLatitude	= (float)HIWORD( nGPS ) / 65535.0f * 180.0f - 90.0f;
			nLongitude	= (float)LOWORD( nGPS ) / 65535.0f * 360.0f - 180.0f;
		}
		
		pPacket->m_nPosition = nNext;
	}

	if ( pHost.sin_family != PF_INET ) return;

	if ( nType == parseSelf )
	{
		m_pHost			= pHost;
		m_nType			= bHub ? ntHub : ntLeaf;
		m_nLeaves		= nLeafs;
		m_sNick			= strNick;
		m_nLatitude		= nLatitude;
		m_nLongitude	= nLongitude;

		theApp.Message( MSG_TEMP, _T("CRAWL: Found %s, %s(%i), \"%s\", lat: %.3f, lon: %.3f :"),
			(LPCTSTR)CString( inet_ntoa( pHost.sin_addr ) ), bHub ? _T("hub") : _T("leaf"),
			nLeafs, (LPCTSTR)strNick, nLatitude, nLongitude );
	}
	else
	{
		CCrawlNode* pNode = pSession->Find( &pHost.sin_addr, TRUE );
		
		if ( pNode->m_tResponse == 0 )
		{
			pNode->m_pHost		= pHost;
			pNode->m_nType		= nType;
			pNode->m_nLeaves	= nLeafs;
			pNode->m_sNick		= strNick;
			pNode->m_nLatitude	= nLatitude;
			pNode->m_nLongitude	= nLongitude;
		}
		
		if ( m_pNeighbours.Find( pNode ) == NULL )
		{
			m_pNeighbours.AddTail( pNode );
		}
		
		theApp.Message( MSG_TEMP, _T("CRAWL:    %s, %s(%i), \"%s\", lat: %.3f, lon: %.3f"),
			(LPCTSTR)CString( inet_ntoa( pHost.sin_addr ) ), bHub ? _T("hub") : _T("leaf"),
			nLeafs, (LPCTSTR)strNick, nLatitude, nLongitude );
	}
}

⌨️ 快捷键说明

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