public int StartDownload(string address)
        {
            // put a record in the database.
            int id;

            using (IDownloadItemRepository repository = createRepository())
            {
                var item = new DownloadItem()
                {
                    Address = address,
                    Status  = DownloadStatus.Requested
                };
                repository.Add(item);
                repository.SaveChanges();

                // get the database generated id.
                id = item.Id;
            }

            eventBus.Notify(this, EventBusToken.DownlaodRequested);

            var channelFactory  = new ChannelFactory <IDownloadService>("DLS");
            var downloadService = channelFactory.CreateChannel();

            using (downloadService as IDisposable)
                downloadService.Download(id);

            return(id);
        }
Ejemplo n.º 2
0
        public void Download(int id)
        {
            using (IDownloadItemRepository repository = createRepository())
            {
                var item = repository.Get(id);
                item.Status = DownloadStatus.Delegated;
                repository.SaveChanges();
            }
            eventBus.Notify(this, EventBusToken.DownloadedDelegated);

            ThreadPool.QueueUserWorkItem(ProcessQueueItem, id);
        }
Ejemplo n.º 3
0
        private async void ProcessQueueItem(object status)
        {
            var    id = (int)status;
            string address;

            using (IDownloadItemRepository repository = createRepository())
            {
                var item = repository.Get(id);
                item.Status = DownloadStatus.Downloading;
                address     = item.Address;
                repository.SaveChanges();
            }

            eventBus.Notify(this, EventBusToken.DownloadBegin);

            try
            {
                string value = await DownloadFile(address);

                using (IDownloadItemRepository repository = createRepository())
                {
                    var item = repository.Get(id);
                    item.Html   = value;
                    item.Status = DownloadStatus.Downloaded;
                    repository.SaveChanges();
                }

                eventBus.Notify(this, EventBusToken.DownloadComplete);
            }
            catch (Exception e)
            {
                using (IDownloadItemRepository repository = createRepository())
                {
                    var item = repository.Get(id);
                    item.Status = DownloadStatus.Errored;
                    repository.SaveChanges();
                }
                eventBus.Notify(this, EventBusToken.DownloadErrored);
            }
        }
Ejemplo n.º 4
0
        public void Download(int id)
        {
            string address;

            using (IDownloadItemRepository repository = createRepository())
            {
                var item = repository.Get(id);
                item.Status = DownloadStatus.Downloading;
                address     = item.Address;
                repository.SaveChanges();
            }

            eventBus.Notify(this, EventBusToken.DownloadBegin);

            string value;

            if (DownloadFile(address, out value))
            {
                using (IDownloadItemRepository repository = createRepository())
                {
                    var item = repository.Get(id);
                    item.Html   = value;
                    item.Status = DownloadStatus.Downloaded;
                    repository.SaveChanges();
                }

                eventBus.Notify(this, EventBusToken.DownloadComplete);
            }
            else
            {
                using (IDownloadItemRepository repository = createRepository())
                {
                    var item = repository.Get(id);
                    item.Status = DownloadStatus.Errored;
                    repository.SaveChanges();
                }
                eventBus.Notify(this, EventBusToken.DownloadErrored);
            }
        }