#include "StdAfx.h"
|
#include "MBThreadDownloadDB.h"
|
#include "MBTransExtendEvents.h"
|
|
CMBThreadDownloadDB::CMBThreadDownloadDB(void)
|
{
|
m_pDownloadMgr = new CMBDownloadMgr();
|
m_bStop = false;
|
m_pTransMgr = NULL;
|
m_pTaskLoopMgr = NULL;
|
}
|
|
|
CMBThreadDownloadDB::~CMBThreadDownloadDB(void)
|
{
|
if(m_pDownloadMgr)
|
{
|
delete m_pDownloadMgr;
|
m_pDownloadMgr = NULL;
|
}
|
}
|
|
// ¿ªÊ¼Ïß³Ì
|
void CMBThreadDownloadDB::StartThread()
|
{
|
if(IsRunning())
|
return;
|
|
BeginThread();
|
}
|
|
// ½áÊøÏß³Ì
|
void CMBThreadDownloadDB::OverThread()
|
{
|
m_bStop = true;
|
|
m_pDownloadMgr->SetEventExe();
|
m_pDownloadMgr->SetEventShutdown();
|
|
|
if(!IsRunning())
|
return;
|
|
EndThread();
|
}
|
|
void CMBThreadDownloadDB::SetTransMgr(CMBTransMgr *pTransMgr)
|
{
|
m_pTransMgr = pTransMgr;
|
}
|
|
void CMBThreadDownloadDB::SetTaskLoopMgr(CMBTaskLoopMgr *pTaskLoopMgr)
|
{
|
m_pTaskLoopMgr = pTaskLoopMgr;
|
}
|
|
bool CMBThreadDownloadDB::AddDownloadMgr( CMBDownloadRootMgr *pDownloadRootMgr )
|
{
|
bool bRet = m_pDownloadMgr->AddDownloadRootMgr(pDownloadRootMgr);
|
|
m_pDownloadMgr->SetEventExe();
|
|
return bRet;
|
}
|
|
// ÏÂÔØÊ±£¬¹ØÁªµ±Ç°Îļþ
|
void CMBThreadDownloadDB::DownloadAssCurSelFile(CMBDownloadFileMgr *pDownloadFileMgr,BOOL &bThreadStop )
|
{
|
if( NULL == pDownloadFileMgr )
|
return ;
|
|
CString strErrInfo;
|
CMBDownloadFileInfoMap::iterator it;
|
CMBDownFileInfo *pDownFileInfo = NULL;
|
CString strCatalogPath,strFileSvr,
|
strCabinetID,strCabinetName,strFileName,strFileID,strFrom;
|
__int64 nFileSize = 0;
|
int nIsDir = 0;
|
EventTransReqList *pEvt = new EventTransReqList(NULL);
|
|
for( it = pDownloadFileMgr->m_mapDownloadFileInfo.begin();it != pDownloadFileMgr->m_mapDownloadFileInfo.end();it++ )
|
{
|
// ֹͣ
|
if(bThreadStop)
|
return;
|
|
pDownFileInfo = it->second;
|
|
if( !pDownFileInfo->IsGetDocInfo() ) // µ÷ÓÃGetDocInfo µÄ״̬£º 0 δµ÷Ó㬠1 ÕýÔÚµ÷Óã¬2 ÒÑÍê³É
|
{
|
m_pTaskLoopMgr->PostGetDocInfoTask( pDownFileInfo,true );
|
}
|
strFileName = pDownFileInfo->GetDownloadFileName();
|
strFileSvr = pDownFileInfo->m_strServerFlag;
|
nFileSize = pDownFileInfo->m_nFileSize;
|
strFileID = pDownFileInfo->m_strFileID;
|
strCatalogPath = pDownFileInfo->m_strCatalogPath;
|
strCabinetID = pDownFileInfo->m_strCabinetID;
|
strCabinetName = pDownFileInfo->m_strCabinetName;
|
strFrom = pDownFileInfo->m_strFrom;
|
nIsDir = pDownFileInfo->m_nIsDir;
|
|
// Èç¹ûÊÇ Èí¼þÉÕÖÆ¹¦Äܵã
|
if( strFrom == FCTN_FIRE )
|
{
|
bool bExsit = false;
|
CString strFullFileName = pDownFileInfo->GetFullFileName();
|
if(!m_pTransMgr->IsFireInSqliteDownloadFile( strFullFileName,strFileID,strFrom,bExsit,strErrInfo ))
|
{
|
return ;
|
}
|
|
if( bExsit || CStrFileUtils::IsExist(strFullFileName) )
|
{
|
continue;
|
}
|
}
|
// Ö±µ½µ÷ÓÃÍê³É
|
//if( pDownFileInfo->m_strCanDownload == _T("1") ) // ¿ÉÒÔÏÂÔØ
|
if( !strFileID.IsEmpty() ) // ¿ÉÒÔÏÂÔØ
|
{
|
CAutoRefPtr<CMBDownloadFileInfo> pDownloadFileInfo(new CMBDownloadFileInfo());
|
pDownloadFileInfo->Release(); // ÊÍ·ÅÒýÓüÆÊý
|
|
pDownloadFileInfo->m_strID = CStrFileUtils::GenerateGuid(); // id
|
pDownloadFileInfo->m_strFileSvr = strFileSvr; // ·þÎñ·¾¶
|
pDownloadFileInfo->SetLocalPath(strFileName); // µ±Ç°Â·¾¶
|
pDownloadFileInfo->SetStatus(CMBDownloadFileInfo::Status_NoFinish); // ״̬
|
pDownloadFileInfo->m_nFileSize = nFileSize;; // Îļþ´óС
|
pDownloadFileInfo->m_nIsDir = nIsDir; // 1ÊÇĿ¼£¬0 ²»ÊÇ
|
pDownloadFileInfo->m_strFileID = strFileID;
|
pDownloadFileInfo->m_strServerPath = strCatalogPath;
|
pDownloadFileInfo->m_strCabinetID = strCabinetID;
|
pDownloadFileInfo->m_strCabinetName = strCabinetName;
|
pDownloadFileInfo->m_strTAddTime = CStrFileUtils::GetLocalDt(); // Ìí¼Óʱ¼ä
|
pDownloadFileInfo->m_strFrom = strFrom;
|
if(!m_pTransMgr->InsertSqliteDownloadFile( pDownloadFileInfo,strErrInfo )) // ²åÈëÊý¾Ý¿â
|
{
|
return ;
|
}
|
// Ôö¼ÓÏÂÔØ×ܼǼ
|
m_pTransMgr->AddDownloadFileSize(pDownloadFileInfo->m_nFileSize);
|
// ÉèÖÃÉÏ´«¶ÓÁÐÉÏ´«Ê¼þ
|
m_pTransMgr->GetDownloadQueue()->SetEventExeSet();
|
}
|
else // ²»ÄÜÏÂÔØµÄ·ÅÈëÔçÇëÁбí
|
{
|
// ¼ÓÈë´íÎóÁбí
|
CMBDownFileInfo *pTmpDownFileInfo = new CMBDownFileInfo(*pDownFileInfo);
|
if(!pEvt->AddDownloadFileInfo(pTmpDownFileInfo))
|
{
|
delete pTmpDownFileInfo;
|
pTmpDownFileInfo = NULL;
|
}
|
}
|
}// end for
|
|
int nCount = pEvt->GetCount();
|
if( nCount == 0 )
|
{
|
pEvt->Release();
|
return ;
|
}
|
|
// ·¢ÉäÏûÏ¢
|
SNotifyCenter::getSingleton().FireEventAsync(pEvt);
|
pEvt->Release();
|
}
|
|
// ÏÂÔØÊ±¹ØÁªÎļþ
|
void CMBThreadDownloadDB::DownloadAssFile( CMBDownloadFileMgr *pDownloadFileMgr,BOOL &bThreadStop )
|
{
|
if( NULL == pDownloadFileMgr )
|
return ;
|
|
CString strErrInfo;
|
CMBDownloadFileInfoMap::iterator it;
|
CMBDownFileInfo *pDownFileInfo = NULL;
|
CString strCatalogPath,strFileSvr,
|
strCabinetID,strFileName,strFileID,strFrom;
|
__int64 nFileSize = 0;
|
|
for( it = pDownloadFileMgr->m_mapDownloadFileInfo.begin();it != pDownloadFileMgr->m_mapDownloadFileInfo.end();it++ )
|
{
|
// ֹͣ
|
if(bThreadStop)
|
return;
|
pDownFileInfo = it->second;
|
if( !pDownFileInfo->IsGetDocInfo() ) // µ÷ÓÃGetDocInfo µÄ״̬£º 0 δµ÷Ó㬠1 ÕýÔÚµ÷Óã¬2 ÒÑÍê³É
|
{
|
m_pTaskLoopMgr->PostGetDocInfoTask( pDownFileInfo,true );
|
}
|
|
strFileName = pDownFileInfo->GetDownloadFileName();
|
strFileSvr = pDownFileInfo->m_strServerFlag;
|
nFileSize = pDownFileInfo->m_nFileSize;
|
strFileID = pDownFileInfo->m_strFileID;
|
strCatalogPath = pDownFileInfo->m_strCatalogPath;
|
strCabinetID = pDownFileInfo->m_strCabinetID;
|
strFrom = pDownFileInfo->m_strFrom;
|
|
// Èç¹ûÊÇ Èí¼þÉÕÖÆ¹¦Äܵã
|
if( strFrom == FCTN_FIRE )
|
{
|
bool bExsit = false;
|
CString strFullFileName = pDownFileInfo->GetFullFileName();
|
if(!m_pTransMgr->IsFireInSqliteDownloadFile( strFullFileName,strFileID,strFrom,bExsit,strErrInfo ))
|
{
|
return ;
|
}
|
|
if( bExsit || CStrFileUtils::IsExist(strFullFileName) )
|
{
|
continue;
|
}
|
}
|
// Ö±µ½µ÷ÓÃÍê³É
|
if( !strFileID.IsEmpty() ) // ¿ÉÒÔÏÂÔØ
|
{
|
CAutoRefPtr<CMBDownloadFileInfo> pDownloadFileInfo(new CMBDownloadFileInfo());
|
pDownloadFileInfo->Release(); // ÊÍ·ÅÒýÓüÆÊý
|
|
pDownloadFileInfo->m_strID = CStrFileUtils::GenerateGuid(); // id
|
pDownloadFileInfo->m_strFileSvr = strFileSvr; // ·þÎñ·¾¶
|
pDownloadFileInfo->SetLocalPath(strFileName); // µ±Ç°Â·¾¶
|
pDownloadFileInfo->SetStatus(CMBDownloadFileInfo::Status_NoFinish); // ״̬
|
pDownloadFileInfo->m_nFileSize = nFileSize;; // Îļþ´óС
|
pDownloadFileInfo->m_nIsDir = 0; // 1ÊÇĿ¼£¬0 ²»ÊÇ
|
pDownloadFileInfo->m_strFileID = strFileID;
|
pDownloadFileInfo->m_strServerPath = strCatalogPath;
|
pDownloadFileInfo->m_strCabinetID = strCabinetID;
|
pDownloadFileInfo->m_strTAddTime = CStrFileUtils::GetLocalDt(); // Ìí¼Óʱ¼ä
|
pDownloadFileInfo->m_strFrom = strFrom;
|
if(!m_pTransMgr->InsertSqliteDownloadFile( pDownloadFileInfo,strErrInfo )) // ²åÈëÊý¾Ý¿â
|
{
|
return ;
|
}
|
// Ôö¼ÓÏÂÔØ×ܼǼ
|
m_pTransMgr->AddDownloadFileSize(pDownloadFileInfo->m_nFileSize);
|
// ÉèÖÃÉÏ´«¶ÓÁÐÉÏ´«Ê¼þ
|
m_pTransMgr->GetDownloadQueue()->SetEventExeSet();
|
}// end if
|
}// end for( )
|
}
|
|
// ÏÂÔØÊ±£¬Ä¿Â¼¹ØÁª
|
void CMBThreadDownloadDB::DownloadAssDir( CMBDownloadFileMgr *pDownloadFileMgr,BOOL &bThreadStop )
|
{
|
CString strErrInfo;
|
CMBDownloadDirInfoMap::iterator it;
|
CMBDownDirInfo *pDownDirInfo = NULL;
|
|
for( it = pDownloadFileMgr->m_mapDownloadDirInfo.begin();it != pDownloadFileMgr->m_mapDownloadDirInfo.end();it++ )
|
{
|
// ֹͣ
|
if(bThreadStop)
|
return;
|
|
pDownDirInfo = it->second;
|
////////////////////////////Õâ±ßµÄȨÏÞÅжÏÏÈÈ¡Ïû//////////////////////////////////////
|
// ½øÐÐȨÏÞÅжϣ¬ÊÇ·ñÓÐÏÂÔØÈ¨ÏÞ
|
//if( pDownDirInfo->m_nType != CMBDownDirInfo::DOWNDIR_PSN)
|
//{
|
// CString strPermit = pDownDirInfo->m_strPermit;
|
// if( !CMBIsCabinetAce::IsCatalogAceDocDownload( strPermit ) )
|
// {
|
// // ȨÏÞΪ ²»¿ÉÒÔÏÂÔØ
|
// continue;
|
// }
|
//}
|
/////////////////////////endÕâ±ßµÄȨÏÞÅжÏÏÈÈ¡Ïû////////////////////////////////////
|
pDownDirInfo->CreateDir();
|
if( !pDownDirInfo->IsGetDocList() )
|
{
|
m_pTaskLoopMgr->PostGetDocListTask( pDownDirInfo,true );
|
}
|
|
// ¹ØÁªÏÂÔØÄ¿Â¼
|
if(!AssociateDownDir( pDownDirInfo,strErrInfo))
|
return ;
|
|
CMBDownloadFileMgr *pDownloadFileMgr = pDownDirInfo->GetChildFileMgr();
|
if( NULL == pDownloadFileMgr )
|
return ;
|
|
// ½øÐÐÏÂÔØÐÐΪ
|
//// Èç¹ûÊÇÎļþ
|
DownloadAssFile(pDownloadFileMgr,bThreadStop);
|
// Èç¹ûÊÇĿ¼
|
DownloadAssDir(pDownloadFileMgr,bThreadStop );
|
}
|
}
|
|
bool CMBThreadDownloadDB::AssociateDownDir( CMBDownDirInfo *pDownDirInfo,CString &strErrInfo )
|
{
|
CString strListXML,strCabinetID,strPath,strFrom;
|
|
CMBDownloadFileMgr *pDownloadFileMgr = pDownDirInfo->GetChildFileMgr();
|
if( NULL == pDownloadFileMgr )
|
return false;
|
|
strListXML = pDownDirInfo->m_strGetDocListRetXml;
|
strCabinetID = pDownDirInfo->m_strCabinetID;
|
strPath = pDownDirInfo->m_strCurDir;
|
strFrom = pDownDirInfo->m_strFrom;
|
if( pDownDirInfo->m_nType == CMBDownDirInfo::DOWNDIR_UNITORDEPART)
|
{
|
if(!pDownloadFileMgr->ParseUnitOrDepartXml( strListXML,strFrom,strCabinetID,strPath,strErrInfo ))
|
return false;
|
}
|
else if( pDownDirInfo->m_nType == CMBDownDirInfo::DOWNDIR_PSN )
|
{
|
if(!pDownloadFileMgr->ParsePsnXml( strListXML,strFrom,strCabinetID,strPath,strErrInfo ))
|
return false;
|
}
|
return true;
|
}
|
|
UINT CMBThreadDownloadDB::Run()
|
{
|
CMBDownloadRootMgrVector::iterator it;
|
CMBDownloadRootMgr *pDownloadRootMgr = NULL;
|
|
CMBDownloadFileMgrList::iterator it1;
|
CMBDownloadFileMgr *pDownloadFileMgr = NULL;
|
|
|
HANDLE handlesToWaitFor[2];
|
DWORD dwResult;
|
handlesToWaitFor[0] = m_pDownloadMgr->GetEventExeHandle();
|
handlesToWaitFor[1] = m_pDownloadMgr->GetEventShutdownHandle();
|
|
while ( !IsStoped() )
|
{
|
#ifdef _DEBUG
|
dwResult = ::WaitForMultipleObjects( 2, handlesToWaitFor, false, 6000 );
|
#else
|
dwResult = ::WaitForMultipleObjects( 2, handlesToWaitFor, false, 300000 );
|
#endif
|
if ( dwResult == WAIT_OBJECT_0 )
|
{
|
if( m_bStop )
|
break;
|
|
m_pDownloadMgr->ResetEventExe();
|
|
CMBDownloadRootMgrVector vectorThreadDownloadRootMgr; //Ï̶߳ÀÓÐ
|
|
m_pDownloadMgr->AttachThreadDownloadRoogMgr(vectorThreadDownloadRootMgr);
|
|
for( it = vectorThreadDownloadRootMgr.begin();it != vectorThreadDownloadRootMgr.end();it++ )
|
{
|
if(m_bStop)
|
return 0;
|
pDownloadRootMgr = *it;
|
for( it1 = pDownloadRootMgr->m_listDownloadFileMgr.begin();it1 != pDownloadRootMgr->m_listDownloadFileMgr.end();it1++ )
|
{
|
if(m_bStop)
|
return 0;
|
|
pDownloadFileMgr = *it1;
|
if( pDownloadFileMgr->GetState() == CMBDownloadFileMgr::State_NoTraversing )
|
{
|
pDownloadFileMgr->SetState(CMBDownloadFileMgr::State_Traversing);
|
// Èç¹ûÊÇÎļþ
|
DownloadAssCurSelFile(pDownloadFileMgr,m_bStop);
|
// Èç¹ûÊÇĿ¼£¬
|
DownloadAssDir(pDownloadFileMgr,m_bStop );
|
pDownloadFileMgr->SetState(CMBDownloadFileMgr::State_Traversaled);
|
}// for( it1 = pDownloadRootMgr->m_listDownloadFileMgr.begin();it1 != pDownloadRootMgr->m_listDownloadFileMgr.end();it1++ )
|
}// end
|
m_pDownloadMgr->RemoveDownloadRootMgr( pDownloadRootMgr );
|
}// end for( it = vectorThreadDownloadRootMgr.begin();it != vectorThreadDownloadRootMgr.end();it++ )
|
vectorThreadDownloadRootMgr.clear();
|
}
|
else if ( dwResult == WAIT_OBJECT_0 + 1 )
|
{
|
// Time to shutdown
|
break;
|
}
|
else if ( dwResult == WAIT_TIMEOUT )
|
{
|
m_pDownloadMgr->SetEventExe();
|
}
|
} // End of while ( true )
|
|
return 0;
|
}
|
|
|
|