Exemple #1
0
void QueueCoordinator::ArticleCompleted(ArticleDownloader* pArticleDownloader)
{
    debug("Article downloaded");

    FileInfo* pFileInfo = pArticleDownloader->GetFileInfo();
    NZBInfo* pNZBInfo = pFileInfo->GetNZBInfo();
    ArticleInfo* pArticleInfo = pArticleDownloader->GetArticleInfo();
    bool bRetry = false;
    bool fileCompleted = false;

    DownloadQueue* pDownloadQueue = DownloadQueue::Lock();

    if (pArticleDownloader->GetStatus() == ArticleDownloader::adFinished)
    {
        pArticleInfo->SetStatus(ArticleInfo::aiFinished);
        pFileInfo->SetSuccessSize(pFileInfo->GetSuccessSize() + pArticleInfo->GetSize());
        pNZBInfo->SetCurrentSuccessSize(pNZBInfo->GetCurrentSuccessSize() + pArticleInfo->GetSize());
        pNZBInfo->SetParCurrentSuccessSize(pNZBInfo->GetParCurrentSuccessSize() + (pFileInfo->GetParFile() ? pArticleInfo->GetSize() : 0));
        pFileInfo->SetSuccessArticles(pFileInfo->GetSuccessArticles() + 1);
        pNZBInfo->SetCurrentSuccessArticles(pNZBInfo->GetCurrentSuccessArticles() + 1);
    }
    else if (pArticleDownloader->GetStatus() == ArticleDownloader::adFailed)
    {
        pArticleInfo->SetStatus(ArticleInfo::aiFailed);
        pFileInfo->SetFailedSize(pFileInfo->GetFailedSize() + pArticleInfo->GetSize());
        pNZBInfo->SetCurrentFailedSize(pNZBInfo->GetCurrentFailedSize() + pArticleInfo->GetSize());
        pNZBInfo->SetParCurrentFailedSize(pNZBInfo->GetParCurrentFailedSize() + (pFileInfo->GetParFile() ? pArticleInfo->GetSize() : 0));
        pFileInfo->SetFailedArticles(pFileInfo->GetFailedArticles() + 1);
        pNZBInfo->SetCurrentFailedArticles(pNZBInfo->GetCurrentFailedArticles() + 1);
    }
    else if (pArticleDownloader->GetStatus() == ArticleDownloader::adRetry)
    {
        pArticleInfo->SetStatus(ArticleInfo::aiUndefined);
        bRetry = true;
    }

    if (!bRetry)
    {
        pFileInfo->SetRemainingSize(pFileInfo->GetRemainingSize() - pArticleInfo->GetSize());
        pNZBInfo->SetRemainingSize(pNZBInfo->GetRemainingSize() - pArticleInfo->GetSize());
        if (pFileInfo->GetPaused())
        {
            pNZBInfo->SetPausedSize(pNZBInfo->GetPausedSize() - pArticleInfo->GetSize());
        }
        pFileInfo->SetCompletedArticles(pFileInfo->GetCompletedArticles() + 1);
        fileCompleted = (int)pFileInfo->GetArticles()->size() == pFileInfo->GetCompletedArticles();
        pFileInfo->GetServerStats()->ListOp(pArticleDownloader->GetServerStats(), ServerStatList::soAdd);
        pNZBInfo->GetCurrentServerStats()->ListOp(pArticleDownloader->GetServerStats(), ServerStatList::soAdd);
        pFileInfo->SetPartialChanged(true);
    }

    if (!pFileInfo->GetFilenameConfirmed() &&
            pArticleDownloader->GetStatus() == ArticleDownloader::adFinished &&
            pArticleDownloader->GetArticleFilename())
    {
        pFileInfo->SetFilename(pArticleDownloader->GetArticleFilename());
        pFileInfo->SetFilenameConfirmed(true);
        if (g_pOptions->GetDupeCheck() &&
                pNZBInfo->GetDupeMode() != dmForce &&
                !pNZBInfo->GetManyDupeFiles() &&
                Util::FileExists(pNZBInfo->GetDestDir(), pFileInfo->GetFilename()))
        {
            warn("File \"%s\" seems to be duplicate, cancelling download and deleting file from queue", pFileInfo->GetFilename());
            fileCompleted = false;
            pFileInfo->SetAutoDeleted(true);
            DeleteQueueEntry(pDownloadQueue, pFileInfo);
        }
    }

    pNZBInfo->SetDownloadedSize(pNZBInfo->GetDownloadedSize() + pArticleDownloader->GetDownloadedSize());

    bool deleteFileObj = false;

    if (fileCompleted && !pFileInfo->GetDeleted())
    {
        // all jobs done
        DownloadQueue::Unlock();
        pArticleDownloader->CompleteFileParts();
        pDownloadQueue = DownloadQueue::Lock();
        deleteFileObj = true;
    }

    CheckHealth(pDownloadQueue, pFileInfo);

    bool hasOtherDownloaders = false;
    for (ActiveDownloads::iterator it = m_ActiveDownloads.begin(); it != m_ActiveDownloads.end(); it++)
    {
        ArticleDownloader* pDownloader = *it;
        if (pDownloader != pArticleDownloader && pDownloader->GetFileInfo() == pFileInfo)
        {
            hasOtherDownloaders = true;
            break;
        }
    }
    deleteFileObj |= pFileInfo->GetDeleted() && !hasOtherDownloaders;

    // remove downloader from downloader list
    m_ActiveDownloads.erase(std::find(m_ActiveDownloads.begin(), m_ActiveDownloads.end(), pArticleDownloader));

    pFileInfo->SetActiveDownloads(pFileInfo->GetActiveDownloads() - 1);
    pNZBInfo->SetActiveDownloads(pNZBInfo->GetActiveDownloads() - 1);

    if (deleteFileObj)
    {
        DeleteFileInfo(pDownloadQueue, pFileInfo, fileCompleted);
        pDownloadQueue->Save();
    }

    DownloadQueue::Unlock();
}
Exemple #2
0
void QueueCoordinator::AddNZBFileToQueue(NZBFile* pNZBFile, NZBInfo* pUrlInfo, bool bAddFirst)
{
    debug("Adding NZBFile to queue");

    NZBInfo* pNZBInfo = pNZBFile->GetNZBInfo();

    DownloadQueue* pDownloadQueue = DownloadQueue::Lock();

    DownloadQueue::Aspect foundAspect = { DownloadQueue::eaNzbFound, pDownloadQueue, pNZBInfo, NULL };
    pDownloadQueue->Notify(&foundAspect);

    NZBInfo::EDeleteStatus eDeleteStatus = pNZBInfo->GetDeleteStatus();

    if (eDeleteStatus != NZBInfo::dsNone)
    {
        bool bAllPaused = !pNZBInfo->GetFileList()->empty();
        for (FileList::iterator it = pNZBInfo->GetFileList()->begin(); it != pNZBInfo->GetFileList()->end(); it++)
        {
            FileInfo* pFileInfo = *it;
            bAllPaused &= pFileInfo->GetPaused();
            if (g_pOptions->GetSaveQueue() && g_pOptions->GetServerMode())
            {
                g_pDiskState->DiscardFile(pFileInfo, true, false, false);
            }
        }
        pNZBInfo->SetDeletePaused(bAllPaused);
    }

    if (eDeleteStatus != NZBInfo::dsManual)
    {
        // NZBInfo will be added either to queue or to history as duplicate
        // and therefore can be detached from NZBFile.
        pNZBFile->DetachNZBInfo();
    }

    if (eDeleteStatus == NZBInfo::dsNone)
    {
        if (g_pOptions->GetDupeCheck() && pNZBInfo->GetDupeMode() != dmForce)
        {
            CheckDupeFileInfos(pNZBInfo);
        }

        if (pUrlInfo)
        {
            // insert at the URL position
            for (NZBList::iterator it = pDownloadQueue->GetQueue()->begin(); it != pDownloadQueue->GetQueue()->end(); it++)
            {
                NZBInfo* pPosNzbInfo = *it;
                if (pPosNzbInfo == pUrlInfo)
                {
                    pDownloadQueue->GetQueue()->insert(it, pNZBInfo);
                    break;
                }
            }
        }
        else if (bAddFirst)
        {
            pDownloadQueue->GetQueue()->push_front(pNZBInfo);
        }
        else
        {
            pDownloadQueue->GetQueue()->push_back(pNZBInfo);
        }
    }

    if (pUrlInfo)
    {
        pNZBInfo->SetID(pUrlInfo->GetID());
        pDownloadQueue->GetQueue()->Remove(pUrlInfo);
        delete pUrlInfo;
    }

    if (eDeleteStatus == NZBInfo::dsNone)
    {
        pNZBInfo->PrintMessage(Message::mkInfo, "Collection %s added to queue", pNZBInfo->GetName());
    }

    if (eDeleteStatus != NZBInfo::dsManual)
    {
        DownloadQueue::Aspect addedAspect = { DownloadQueue::eaNzbAdded, pDownloadQueue, pNZBInfo, NULL };
        pDownloadQueue->Notify(&addedAspect);
    }

    pDownloadQueue->Save();

    DownloadQueue::Unlock();
}