Example #1
0
void CMgSingleTask::InitThread()
{
    OutMsg( -1, _("Job running..."), MSG_INFO );

    pthread_setcanceltype ( PTHREAD_CANCEL_ASYNCHRONOUS, NULL );

    CUrlParser upar;

    if ( !m_Refer.empty() )
        upar.SetRefer( m_Refer );

    int oldstate;

    vector<string> cookie; //for http

again:

    OutMsg( -1, _("Analysis URL..."), MSG_INFO );

    if ( !upar.SetUrl( m_sUrl ) )
    {
        OutMsg( -1, c_str(_("Not a support URL type.")) + m_sUrl, MSG_ERROR );
        FinishTask( _TASK_ERROR );
        pthread_exit( 0 );
    }

    OutMsg( -1, _("URL OK."), MSG_INFO );
    //任务参数
    m_Server = upar.GetServer();

    m_ServerPort = upar.GetPort();

    m_Username = upar.GetUser();

    m_Password = upar.GetPass();

    m_EscFilePathName = upar.GetEscFilePathName(); //文件名


    if ( upar.GetUrlType() == FTP_PROTOCOL )   //ftp
    {
        OutMsg( -1, _("Main URL is ftp type."), MSG_INFO );

        m_pFtpInfo = new CMgFtpInfo(
                         this,
                         m_sUrl,
                         m_nRetry,
                         m_nRetryWait,
                         -1
                     );
        //set proxy here

        if ( m_bUseProxy )
        {
            OutMsg( -1, _("Task will use socks proxy."), MSG_INFO );
            m_pFtpInfo->SetProxy( m_Proxy, m_ProxyPort, m_ProxyUser, m_ProxyPass, m_ProxyVersion );

        }

        if ( m_bUseFtpProxy )
        {
            OutMsg( -1, _("Task will use ftp proxy."), MSG_INFO );
            m_pFtpInfo->SetFtpProxy( m_FtpProxy, m_FtpProxyPort );
        }

        //go

        if ( !m_pFtpInfo->GetInfo() )
        { //error when get info. 2006/09/21

            OutMsg( -1, _("Get file info error."), MSG_ERROR );
            pthread_setcancelstate ( PTHREAD_CANCEL_DISABLE, &oldstate );
            delete m_pFtpInfo;
            m_pFtpInfo = NULL;
            pthread_setcancelstate ( oldstate, NULL );
            FinishTask( _TASK_ERROR );
            pthread_exit( 0 );
        }

        m_nFileLen = m_pFtpInfo->FileSize();
        bool bresume = m_pFtpInfo->IsResume();

        pthread_setcancelstate ( PTHREAD_CANCEL_DISABLE, &oldstate );
        delete m_pFtpInfo;
        m_pFtpInfo = NULL;
        pthread_setcancelstate ( oldstate, NULL );


        if ( m_nFileLen == -1 || !bresume )
        {
            if ( m_nFileLen == -1 )
            {
                OutMsg( -1, _("No file length got."), MSG_WARNNING );
            }

            if ( !bresume )
            {
                OutMsg( -1, _("Server cannot support resume method."), MSG_WARNNING );
            }

            m_nAnts = 1;
            //FinishTask(_TASK_ERROR);
            //InitThreadQuit();
        }


    } //main url ftp
    else if ( upar.GetUrlType() == HTTP_PROTOCOL )
    { //http
        OutMsg( -1, _("Main URL is http type."), MSG_INFO );

        m_pHttpInfo =
            new CMgHttpInfo(
                this,
                m_sUrl,
                m_nRetry,
                m_nRetryWait,
                upar.GetRefer(),
                -1
            );
        //if need proxy, make here

        if ( m_bUseProxy )
        {
            OutMsg( -1, _("Task will use socks proxy."), MSG_INFO );
            m_pHttpInfo->SetProxy( m_Proxy, m_ProxyPort, m_ProxyUser, m_ProxyPass, m_ProxyVersion );

        }

        if ( m_bUseHttpProxy )
        {
            OutMsg( -1, _("Task will use http proxy."), MSG_INFO );
            m_pHttpInfo->SetHttpProxy( m_HttpProxy, m_HttpProxyPort );
        }

        if ( !m_pHttpInfo->GetInfo() )
        { //error when get info.

            OutMsg( -1, _("Get file info error."), MSG_ERROR );
            pthread_setcancelstate ( PTHREAD_CANCEL_DISABLE, &oldstate );
            delete m_pHttpInfo;
            m_pHttpInfo = NULL;
            pthread_setcancelstate ( oldstate, NULL );
            FinishTask( _TASK_ERROR );
            pthread_exit( 0 );
        }

        if ( m_pHttpInfo->IsRedirect() )
        {
            upar.SetRefer( m_sUrl );
            m_sUrl = m_pHttpInfo->GetRedirect();

            pthread_setcancelstate ( PTHREAD_CANCEL_DISABLE, &oldstate );
            delete m_pHttpInfo;
            m_pHttpInfo = NULL;
            pthread_setcancelstate ( oldstate, NULL );
            //跳转后就不再用原来的名字了。

            if ( upar.SetUrl( m_sUrl ) )
            {
                if ( upar.GetFileName() != string( "" ) )
                    m_sFilename = upar.GetFileName();
            }

            goto again;
        }

        //		for( int coo=0; coo < m_pHttpInfo->GetCookieNum(); coo++)
        //		{
        //
        //			cookie.push_back( m_pHttpInfo->GetCookie( coo ) );
        //		}

        m_nFileLen = m_pHttpInfo->GetFileSize();

        pthread_setcancelstate ( PTHREAD_CANCEL_DISABLE, &oldstate );

        delete m_pHttpInfo;

        m_pHttpInfo = NULL;

        pthread_setcancelstate ( oldstate, NULL );

        if ( m_nFileLen == -1 )
        {
            OutMsg( -1, _("No file length got."), MSG_WARNNING );
            m_nAnts = 1;
        }


    } //http
    else
    {
        OutMsg( -1, c_str(_("Not a support URL type.")) + m_sUrl, MSG_ERROR );

        FinishTask( _TASK_ERROR );
        pthread_exit( 0 );
    }


    //create file manager
    pthread_setcancelstate ( PTHREAD_CANCEL_DISABLE, &oldstate );

    m_pFM = new CMgFileManager ( this, m_sUrl, m_sFilename, m_sSavePath, m_nFileLen, upar.GetRefer() );

    pthread_setcancelstate ( oldstate, NULL );

    //check file
    if ( !m_pFM->CheckFile() )
    {
        pthread_setcancelstate ( PTHREAD_CANCEL_DISABLE, &oldstate );
        delete m_pFM;
        m_pFM = NULL;
        pthread_setcancelstate ( oldstate, NULL );
        FinishTask( _TASK_ERROR );
        pthread_exit( 0 );
    }

    //input mirror url
    std::vector<std::string>::const_iterator it;

    for ( it = m_sMirrorUrl.begin();it != m_sMirrorUrl.end();it++ )
    {
        m_pFM->AddMirrorUrl( *it );
    }

    //start work thread
    pthread_setcancelstate ( PTHREAD_CANCEL_DISABLE, &oldstate );
	pthread_mutex_lock( &m_AntsMutex );

    for ( int i = 0;i < m_nAnts;i++ )
    {
        //not run when construct 2006/09/03
        m_pAnts[ i ] = new CProAnts(
                           this,   //用于线程消息回送
                           m_pFM,
                           i + 1,  //线程自然数编号,回送消息时带上,用于分辨消息来源
                           m_nFileLen,
                           m_nRetry,
                           m_nRetryWait
                       );


        //if need proxy, make here

        if ( m_bUseProxy )
        {
            m_pAnts[ i ] ->SetProxy( m_Proxy, m_ProxyPort, m_ProxyUser, m_ProxyPass, m_ProxyVersion );
        }

        if ( m_bUseFtpProxy )
        {
            m_pAnts[ i ] ->SetFtpProxy( m_FtpProxy, m_FtpProxyPort );
        }

        if ( m_bUseHttpProxy )
        {

            m_pAnts[ i ] ->SetHttpProxy( m_HttpProxy, m_HttpProxyPort );
        }

        m_pAnts[ i ] ->Go();

    }


    m_nRunningAnts = m_nAnts;
	m_bInitAnts = true;
	pthread_mutex_unlock( &m_AntsMutex );
    pthread_setcancelstate ( oldstate, NULL );

    pthread_exit( 0 );

}