www.gusucode.com > VC_C++源码,界面编程,网页爬虫源码程序 > VC_C++源码,界面编程,网页爬虫源码程序/code/webpageloader_SourceCode/Session.cpp
// Session.cpp: implementation of the CSession class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "WebPageLoader.h" #include "Session.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// IMPLEMENT_DYNCREATE(CSession, CWinThread) LONG CSession::m_iUniqueCount = 0; CSession::CSession() { m_State = m_OldState = STATE_UNSCHEDULED; m_Preferences = NULL; m_nStartIndex = m_nStopIndex = 0; m_bSleepRequest = m_bStopRequest = m_bKillRequest = false; m_bRunning = false; // We'll here we assign a unique ID. // Not a cool way to do it, but InterlockedIncrement() returns // crap on NT3.51 / win95 ::InterlockedIncrement(&m_iUniqueCount); m_iUniqueID = m_iUniqueCount; } CSession::~CSession() { for( int i=0; i<m_aDelFolders.GetSize(); i++ ) ::RemoveDirectory(m_aDelFolders[i]); } ////////////////////////////////////////////////////////////////////// // Implementation void CSession::Create(SessionType Type, CSessionSettings Info, CPreferences *Prefs) { ASSERT(Prefs); m_Type = Type; m_Settings = Info; m_OrigSettings = Info; m_Preferences = Prefs; }; void CSession::Start() { CSingleLock lock( &m_cs, TRUE ); // Extract the server URL DWORD dwType; INTERNET_PORT nPort; AfxParseURL( m_sURL, dwType, m_Info.m_sServer, m_Info.m_sPage, nPort ); if( m_Info.m_sPage.Right(1) != '/' ) { int iPos = m_Info.m_sPage.ReverseFind('/'); if( iPos > 0 ) m_Info.m_sPage = m_Info.m_sPage.Left(iPos + 1); } m_Info.m_tStarted = CTime::GetCurrentTime(); m_State = STATE_CONNECTING; m_bRunning = true; ::PostMessage(m_Preferences->m_hwndFrame, WM_SCHEDULE, 0, 0L); ::PostMessage(m_Preferences->m_hwndFrame, WM_REFRESHITEMS, m_iUniqueID, 0L); Log(LOGTYPE_LOG, IDS_LOG_SESSIONSTARTED); }; void CSession::Done() { CSingleLock lock( &m_cs, TRUE ); m_Info.m_tStopped = CTime::GetCurrentTime(); m_State = STATE_STOPPED; m_bRunning = false; ::PostMessage(m_Preferences->m_hwndFrame, WM_SCHEDULE, 0, 0L); ::PostMessage(m_Preferences->m_hwndFrame, WM_REFRESHITEMS, m_iUniqueID, 0L); Log(LOGTYPE_LOG, IDS_LOG_SESSIONDONE); }; BOOL CSession::ScheduleFiles() // Using the update class attributes, we schedule a // new download session. If this is a Image Scan then // we create file downloads for all files. { if( m_Files.GetCount()>0 ) return FALSE; BOOL bRes = TRUE; CSingleLock lock( &m_cs, TRUE ); switch( m_Type ) { case TYPE_HTMLSCAN: { CDownloadFile *pFile = new CDownloadFile; pFile->Create(m_sURL, FALSE); m_Files.AddHead(pFile); } break; case TYPE_IMAGESCAN: { // Set some default settings // (they are not displayed in the property sheets for image // scans) m_Settings.m_Duplicates = DUP_OVERWRITE; m_Settings.m_bUseFileNameFilter = FALSE; m_Settings.m_bUseFileSizeFilter = FALSE; // Add image files for( int i=m_nStopIndex; i>=m_nStartIndex; i-- ) { CString sURL; sURL.Format(m_sFormat, i); CDownloadFile *pFile = new CDownloadFile; pFile->Create(sURL, TRUE); m_Files.AddHead(pFile); } } break; default: // Error: invalid type bRes = FALSE; }; return bRes; }; void CSession::Log(LogType Type, LPCTSTR szText) // Add to the log. { if( !BfxIsValidSession(this) ) return; ASSERT(AfxIsValidString(szText)); if( szText==NULL ) return; CString s; CTime t; t = CTime::GetCurrentTime(); s = t.Format(_T("%X")); s += _T(" - "); s += szText; m_Info.m_Log.Add(s); // Make sure to update bottom pane with new info ASSERT(m_Preferences->m_hwndFrame); ::PostMessage(m_Preferences->m_hwndFrame, WM_REFRESHNODES, m_iUniqueID, 0); }; void CSession::Log(LogType Type, UINT nText, ...) { if( !BfxIsValidSession(this) ) return; CString sFormat; sFormat.LoadString(nText); CString sTxt; va_list args; va_start(args, nText); sTxt.FormatV((LPCSTR)sFormat, args); Log(Type, sTxt); }; void CSession::SafeLog(LogType Type, UINT nText, ...) { CSingleLock lock( &m_cs, TRUE ); if( !BfxIsValidSession(this) ) return; CString sFormat; sFormat.LoadString(nText); CString sTxt; va_list args; va_start(args, nText); sTxt.FormatV((LPCSTR)sFormat, args); Log(Type, sTxt); }; void CSession::SetState(SessionState State) { if( !BfxIsValidSession(this) ) return; CSingleLock lock( &m_cs, TRUE ); m_State = State; ASSERT(m_Preferences->m_hwndFrame); ::PostMessage(m_Preferences->m_hwndFrame, WM_SCHEDULE, 0, 0); ::PostMessage(m_Preferences->m_hwndFrame, WM_REFRESHITEMS, m_iUniqueID, 0); }; CString CSession::GetBaseDomain() const { // Extract the server part of the URL CString sServer, sPage; DWORD dwType; INTERNET_PORT nPort; AfxParseURL( m_sURL, dwType, sServer, sPage, nPort ); int nDots = 0; for( int i = 0; i < sServer.GetLength(); i++ ) if( sServer.GetAt(i) == '.' ) ++nDots; while( nDots >= 2 ) { int iPos = sServer.Find('.'); sServer = sServer.Mid(iPos + 1); nDots = 0; for( int i = 0; i < sServer.GetLength(); i++ ) if( sServer.GetAt(i) == '.' ) ++nDots; } return sServer; }