Ejemplo n.º 1
0
        public void nextFile()
        {
            Logger.log(Logger.TYPE.DEBUG, "Try next file from download queue...");
            if (queueFile.Any())
            {
                QueueBlock <Boolean> block = queueFile.Dequeue();
                if (block != null)
                {
                    downloadFileAsync(block.getUrl(), Path.Combine(block.getDestination(),
                                                                   block.getFileName()),
                                      (object sender, AsyncCompletedEventArgs e) => {
                        block.invokeCallback(e.Cancelled);
                        nextFile();
                    }, block);
                }
            }
            else
            {
                totalDownloaded   = 0;
                queueDownloadSize = 0;

                if (queueFileCallback != null)
                {
                    queueFileCallback.onFinished();
                    queueFileCallback = null;
                }
            }
        }
Ejemplo n.º 2
0
        public void nextString()
        {
            Logger.log(Logger.TYPE.DEBUG, "Try next string from download queue...");
            if (queueString.Any())
            {
                QueueBlock <String> block = queueString.Dequeue();
                if (block != null)
                {
                    downloadStringAsync(block.getUrl(), (object sender, DownloadStringCompletedEventArgs e) => {
                        block.invokeCallback(e.Result);
                        nextString();
                    }, block);
                }
            }
            else
            {
                totalDownloaded   = 0;
                queueDownloadSize = 0;

                if (queueStringCallback != null)
                {
                    queueStringCallback.onFinished();
                    queueStringCallback = null;
                }
            }
        }
Ejemplo n.º 3
0
 public FileProvider(params string[] directories)
 {
     Directories            = directories;
     _searchOption          = IncludeSubpath ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
     _producerTokenSource   = new CancellationTokenSource();
     _consumerTokenSource   = new CancellationTokenSource();
     _createdQueue          = new QueueBlock <FileMessage>();
     _changedQueue          = new QueueBlock <FileMessage>();
     _deletedQueue          = new QueueBlock <FileMessage>();
     _createdQueue.OnTaked += (_, e) => OnFileCreated(this, e);
     _changedQueue.OnTaked += (_, e) => OnFileChanged(this, e);
     _deletedQueue.OnTaked += (_, e) => OnFileDeleted(this, e);
 }
Ejemplo n.º 4
0
        private void downloadCompleted <T>(QueueBlock <T> block)
        {
            busy             = false;
            downloadComplete = true;

            if (block != null)
            {
                totalDownloaded += block.getSize();
            }

            if (statusLabel != null)
            {
                statusLabel.Text += " - Finished!";
            }
        }
Ejemplo n.º 5
0
        private async Task <int> SubmitNextRequests()
        {
            var emptySlots = MaxRequestsInParallel - QueueBlock.Count;

            Console.WriteLine($"Empty slots: {emptySlots}, left = {batches - currentBatch}");
            if (emptySlots > 0)
            {
                var dataRequests = await GetNextRequests(emptySlots);

                foreach (var data in dataRequests)
                {
                    await QueueBlock.SendAsync(data);
                }
            }
            return(emptySlots);
        }
Ejemplo n.º 6
0
        public void downloadFileAsync(Uri url, String dest, EventHandler <AsyncCompletedEventArgs> completed,
                                      QueueBlock <Boolean> block)
        {
            Logger.log(Logger.TYPE.DEBUG, "Downloading file (async) from " + url);
            if (!isBusy())
            {
                prepare(url);

                if (block == null && progressBar != null)
                {
                    progressBar.Value = 0;
                }

                description = "Status: Downloading " + getCurrentFileName();
                if (statusLabel != null)
                {
                    statusLabel.Text = description;
                }

                WebClient webClient = new WebClient();
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(
                    (object sender, AsyncCompletedEventArgs e) => {
                    try {
                        Logger.log(Logger.TYPE.DEBUG, "Finished downloading file.");
                        if (e.Error != null && e.Error.InnerException != null)
                        {
                            throw e.Error.InnerException;
                        }

                        downloadCompleted(block);
                        completed(sender, e);
                    } catch (Exception ex) {
                        Logger.log(Logger.TYPE.ERROR, "There was a problem downloading from " + url + ", error: " + e.Error);
                        statusLabel.Text = "File download failed: " + url + "\n" + e.Error.Message;
                    }
                });

                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(
                    (object sender, DownloadProgressChangedEventArgs e) => {
                    Logger.log(Logger.TYPE.DEBUG, "Download progress (async) " + url + ": " + e.ProgressPercentage);
                    downloadProgressChanged(e, block);
                });

                webClient.DownloadFileAsync(currentUrl, dest);
                fileDownloadElapsed.Start();
            }
        }
Ejemplo n.º 7
0
        private void downloadProgressChanged <T>(DownloadProgressChangedEventArgs e,
                                                 QueueBlock <T> block)
        {
            fileSize = e.TotalBytesToReceive / 1024;

            fileBytesDownloaded = e.BytesReceived / 1024;
            if (fileBytesDownloaded > 0 && fileDownloadElapsed.ElapsedMilliseconds > 1000)
            {
                KBps = (int)(fileBytesDownloaded / (fileDownloadElapsed.ElapsedMilliseconds / 1000));
            }

            if (statusLabel != null)
            {
                statusLabel.Text = description + "\n" + fileBytesDownloaded +
                                   " KB / " + fileSize + " KB - " + KBps + " KB/s";
            }

            float percentage = 0;

            if (fileBytesDownloaded > 0)
            {
                percentage = (fileBytesDownloaded / (float)fileSize) * 100f;
            }

            if (block != null)
            {
                long totalSize = queueDownloadSize;
                if (totalSize > 0)
                {
                    percentage = ((fileBytesDownloaded + totalDownloaded) /
                                  (float)totalSize) * 100f;
                }
            }

            if (progressBar != null)
            {
                progressBar.Value = (int)percentage;
            }
        }
Ejemplo n.º 8
0
        public void Enqueue(int priority, int item)
        {
            var block = new QueueBlock()
            {
                Priority = priority, Entity = item
            };

            for (int i = _index; i >= 0; i--)
            {
                if (i - 1 < 0 || _buffer[i - 1].Priority <= priority)
                {
                    _buffer[i] = block;
                    break;
                }
                else
                {
                    _buffer[i] = _buffer[i - 1];
                }
            }

            _index++;
        }