예제 #1
0
        private void EndDownloadFilePart(int downloadId)
        {
            try
            {
                DownloadLinkRef      downloadLinkRef   = _currentDownloadFiles[downloadId];
                QueueDownloadFile    queueDownloadFile = GetQueueDownloadFile(downloadLinkRef.QueueDownloadFileId);
                DownloadFilePartLink filePartLink      = GetDownloadFilePartLink(downloadLinkRef);

                if (_trace)
                {
                    pb.Trace.WriteLine("DownloadManager.ManageEndDownloadFiles() 02                   : end download file : state {0} file \"{1}\"", filePartLink.State, filePartLink.File);
                }

                filePartLink.DownloadedPath = _downloadClient.GetDownloadLocalFileById(downloadLinkRef.DownloadId);
                string file = filePartLink.DownloadedPath;
                if (queueDownloadFile.Directory != null)
                {
                    file = zpath.PathSetDirectory(file, queueDownloadFile.Directory);
                }
                filePartLink.DownloadedFile        = file;
                filePartLink.EndDownloadTime       = DateTime.Now;
                filePartLink.DownloadDuration      = filePartLink.EndDownloadTime - filePartLink.StartDownloadTime;
                queueDownloadFile.EndDownloadTime  = filePartLink.EndDownloadTime;
                queueDownloadFile.DownloadDuration = queueDownloadFile.EndDownloadTime - queueDownloadFile.StartDownloadTime;

                if (_trace)
                {
                    pb.Trace.WriteLine("DownloadManager.ManageEndDownloadFiles() 03                   : _currentDownloadFiles.Remove({0})", downloadId);
                }
                _currentDownloadFiles.Remove(downloadId);

                if (_trace)
                {
                    pb.Trace.WriteLine("DownloadManager.ManageEndDownloadFiles() 04                   : _mongoCurrentDownloadFileManager.Remove({0})", downloadLinkRef.DownloadId);
                }
                _mongoCurrentDownloadFileManager.Remove(downloadLinkRef.DownloadId);

                queueDownloadFile.DownloadNbInProgress--;

                _downloadClient.RemoveDownloadById(downloadLinkRef.DownloadId);

                if (_trace)
                {
                    pb.Trace.WriteLine("DownloadManager.ManageEndDownloadFiles() 05                   : downloadFile.Id {0} downloadFile.DownloadNbInProgress {1} downloadFile.AllDownloadLinkTreated {2} directory \"{3}\" filename \"{4}\"", queueDownloadFile.Id, queueDownloadFile.DownloadNbInProgress, queueDownloadFile.AllDownloadLinkTreated, queueDownloadFile.Directory, queueDownloadFile.Filename);
                }
                if (queueDownloadFile.DownloadNbInProgress == 0 && queueDownloadFile.AllDownloadLinkTreated)
                {
                    EndDownload(queueDownloadFile);
                }
                else
                {
                    SaveQueueDownloadFile(queueDownloadFile);
                }
            }
            catch (Exception exception)
            {
                pb.Trace.WriteLine("error in DownloadManager.EndDownloadFilePart() : {0}", exception.Message);
                pb.Trace.WriteLine(exception.StackTrace);
            }
        }
예제 #2
0
        private void ManageEndDownloadFiles()
        {
            if (_trace)
            {
                pb.Trace.WriteLine("DownloadManager.ManageEndDownloadFiles() 01 begin");
            }
            foreach (int downloadId in _currentDownloadFiles.Keys.ToArray())
            {
                DownloadLinkRef downloadLinkRef = _currentDownloadFiles[downloadId];
                DownloaderState state           = _downloadClient.GetDownloadStateById(downloadLinkRef.DownloadId);
                if (state == DownloaderState.Ended || state == DownloaderState.EndedWithError || state == DownloaderState.UnknowDownload)
                {
                    QueueDownloadFile downloadFile = GetQueueDownloadFile(downloadLinkRef.QueueDownloadFileId);
                    downloadFile.Modified = true;
                    DownloadFilePartLink filePartLink = GetDownloadFilePartLink(downloadLinkRef);
                    if (state == DownloaderState.Ended)
                    {
                        filePartLink.State = DownloadState.DownloadCompleted;
                    }
                    else
                    {
                        downloadFile.UncompleteDownload = true;
                        DownloadItemLink itemLink = GetDownloadItemLink(downloadLinkRef);
                        itemLink.UncompleteDownload = true;
                        filePartLink.State          = DownloadState.DownloadFailed;
                    }

                    EndDownloadFilePart(downloadId);
                }
            }
            if (_trace)
            {
                pb.Trace.WriteLine("DownloadManager.ManageEndDownloadFiles() 06 end");
            }
        }
예제 #3
0
        private DownloadFilePartLink GetDownloadFilePartLink(DownloadLinkRef downloadLinkRef)
        {
            QueueDownloadFile downloadFile = GetQueueDownloadFile(downloadLinkRef.QueueDownloadFileId);

            if (downloadFile != null)
            {
                return(downloadFile.DownloadItemLinks[downloadLinkRef.ItemIndex].ServerLinks[downloadLinkRef.ServerIndex].FilePartLinks[downloadLinkRef.FilePartIndex]);
            }
            else
            {
                return(null);
            }
        }
예제 #4
0
        private void ControlDownloadFiles()
        {
            foreach (int downloadId in _currentDownloadFiles.Keys.ToArray())
            {
                DownloadLinkRef downloadLinkRef = _currentDownloadFiles[downloadId];
                DownloaderState state           = _downloadClient.GetDownloadStateById(downloadLinkRef.DownloadId);
                if (state == DownloaderState.NeedToPrepare)
                {
                    if (_downloadClient.GetDownloadRetryCountById(downloadLinkRef.DownloadId) == _downloadClient.GetMaxRetryCount())
                    {
                        QueueDownloadFile downloadFile = GetQueueDownloadFile(downloadLinkRef.QueueDownloadFileId);
                        downloadFile.Modified = true;
                        DownloadFilePartLink filePartLink = GetDownloadFilePartLink(downloadLinkRef);

                        downloadFile.UncompleteDownload = true;
                        DownloadItemLink itemLink = GetDownloadItemLink(downloadLinkRef);
                        itemLink.UncompleteDownload = true;
                        filePartLink.State          = DownloadState.DownloadFailed;

                        EndDownloadFilePart(downloadId);
                    }
                }
            }
        }
예제 #5
0
        private void ManageNewDownloadFiles()
        {
            if (_trace)
            {
                pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 01 begin");
            }

            int nb = _downloadClient.GetDownloadCount();

            if (_trace)
            {
                pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 02                   : search new download link : current download count {0}", nb);
            }

            if (nb >= _maxSimultaneousDownload)
            {
                if (_trace)
                {
                    pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 09 end");
                }
                return;
            }

            foreach (QueueDownloadFile queueDownloadFile in _mongoQueueDownloadFileManager.Find("{}", sort: "{ _id: 1 }"))
            {
                QueueDownloadFile queueDownloadFile2 = queueDownloadFile;
                if (_queueDownloadFiles.ContainsKey(queueDownloadFile2.Id))
                {
                    queueDownloadFile2 = GetQueueDownloadFile(queueDownloadFile2.Id);
                }

                while (true)
                {
                    if (!_queueDownloadFiles.ContainsKey(queueDownloadFile2.Id))
                    {
                        if (!_queueDownloadFiles.TryAdd(queueDownloadFile2.Id, queueDownloadFile2))
                        {
                            pb.Trace.WriteLine("error adding QueueDownloadFile id {0} to ConcurrentDictionary _queueDownloadFiles", queueDownloadFile2.Id);
                        }
                        if (_trace)
                        {
                            //pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 05                   : add to _queueDownloadFiles, queueDownloadFile.Id {0} queueDownloadFile.Key {1} queueDownloadFile.File \"{2}\"", queueDownloadFile2.Id, queueDownloadFile2.Key, queueDownloadFile2.File);
                            pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 05                   : add to _queueDownloadFiles, queueDownloadFile.Id {0} queueDownloadFile.Key {1} queueDownloadFile.Directory \"{2}\" queueDownloadFile.Filename \"{3}\"", queueDownloadFile2.Id, queueDownloadFile2.Key, queueDownloadFile2.Directory, queueDownloadFile2.Filename);
                        }
                    }

                    DownloadLinkRef downloadLinkRef = GetNextDownloadLink(queueDownloadFile2);
                    if (downloadLinkRef == null)
                    {
                        if (_trace)
                        {
                            //pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 03                   : no more link to download for \"{0}\"", queueDownloadFile2.File);
                            pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 03                   : no more link to download for \"{0}\" \"{1}\"", queueDownloadFile2.Directory, queueDownloadFile2.Filename);
                            //pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 04                   : downloadFile.Id {0} downloadFile.DownloadNbInProgress {1} downloadFile.AllDownloadLinkTreated {2} file \"{3}\"", queueDownloadFile2.Id, queueDownloadFile2.DownloadNbInProgress, queueDownloadFile2.AllDownloadLinkTreated, queueDownloadFile2.File);
                            pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 04                   : downloadFile.Id {0} downloadFile.DownloadNbInProgress {1} downloadFile.AllDownloadLinkTreated {2} directory \"{3}\" filename \"{4}\"", queueDownloadFile2.Id, queueDownloadFile2.DownloadNbInProgress, queueDownloadFile2.AllDownloadLinkTreated, queueDownloadFile2.Directory, queueDownloadFile2.Filename);
                        }

                        // sauver dans mongo Downloaded _mongoDownloadedFileManager
                        if (queueDownloadFile2.DownloadNbInProgress == 0)
                        {
                            EndDownload(queueDownloadFile2);
                        }
                        else
                        {
                            SaveQueueDownloadFile(queueDownloadFile2);
                        }
                        break;
                    }
                    else
                    {
                        downloadLinkRef.DownloadId  = _downloadClient.AddDownload(downloadLinkRef.DebridedDownloadLink, downloadLinkRef.File, startNow: true);
                        queueDownloadFile2.Modified = true;

                        if (_trace)
                        {
                            pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 06                   : start download DownloadId {0} file \"{1}\"", downloadLinkRef.DownloadId, downloadLinkRef.File);
                        }

                        DownloadFilePartLink filePartLink = GetDownloadFilePartLink(downloadLinkRef);
                        if (filePartLink != null)
                        {
                            filePartLink.State             = DownloadState.DownloadStarted;
                            filePartLink.StartDownloadTime = DateTime.Now;
                        }
                        if (queueDownloadFile2.StartDownloadTime == null)
                        {
                            queueDownloadFile2.StartDownloadTime = DateTime.Now;
                        }
                        queueDownloadFile2.DownloadNbInProgress++;
                        SaveQueueDownloadFile(queueDownloadFile2);

                        if (_trace)
                        {
                            //pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 07                   : downloadFile.Id {0} downloadFile.DownloadNbInProgress {1} downloadFile.AllDownloadLinkTreated {2} file \"{3}\"", queueDownloadFile2.Id, queueDownloadFile2.DownloadNbInProgress, queueDownloadFile2.AllDownloadLinkTreated, queueDownloadFile2.File);
                            pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 07                   : downloadFile.Id {0} downloadFile.DownloadNbInProgress {1} downloadFile.AllDownloadLinkTreated {2} directory \"{3}\" filename \"{4}\"", queueDownloadFile2.Id, queueDownloadFile2.DownloadNbInProgress, queueDownloadFile2.AllDownloadLinkTreated, queueDownloadFile2.Directory, queueDownloadFile2.Filename);
                            pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 08                   : _currentDownloadFiles.Add({0})", downloadLinkRef.DownloadId);
                        }

                        _currentDownloadFiles.Add(downloadLinkRef.DownloadId, downloadLinkRef);
                        _mongoCurrentDownloadFileManager.Save(BsonValue.Create(downloadLinkRef.DownloadId), downloadLinkRef);

                        if (++nb >= _maxSimultaneousDownload)
                        {
                            break;
                        }
                    }
                }
                if (nb >= _maxSimultaneousDownload)
                {
                    break;
                }
            }
            if (_trace)
            {
                pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 09 end");
            }
        }
예제 #6
0
 private DownloadFilePartLink GetDownloadFilePartLink(DownloadLinkRef downloadLinkRef)
 {
     QueueDownloadFile downloadFile = GetQueueDownloadFile(downloadLinkRef.QueueDownloadFileId);
     if (downloadFile != null)
         return downloadFile.DownloadItemLinks[downloadLinkRef.ItemIndex].ServerLinks[downloadLinkRef.ServerIndex].FilePartLinks[downloadLinkRef.FilePartIndex];
     else
         return null;
 }