示例#1
0
 public FileDownloader(ISegmentProcessor segmentProcessor, IProtocolDownloader protocolDownloader, IInitializeDonwload initializeDonwload)
 {
     _segments           = new List <Segment>();
     _tasks              = new List <Task>();
     _segmentProcessor   = segmentProcessor;
     _protocolDownloader = protocolDownloader;
     _initializeDonwload = initializeDonwload;
 }
示例#2
0
        public IProtocolDownloader BindProtocolProviderInstance()
        {
            if (_protocolType == null)
            {
                BindProtocolProviderType();
            }

            if (_provider == null)
            {
                _provider = ProtocolProviderFactory.CreateProvider(_protocolType);
            }

            return(_provider);
        }
示例#3
0
        public List <Task> Download()
        {
            //Each URL will be downloaded on it's own thread
            List <Task> tasks = new List <Task>();

            foreach (var resourceDetail in _resourceDetails)
            {
                if (resourceDetail.ProtocolType == null)
                {
                    Log.Fatal("protocol not supported for url" + resourceDetail.Url);
                    continue;
                }
                //Resolve the Download provider
                _protocolDownloader = ProtocolProviderFactory.ResolveProvider(resourceDetail.ProtocolType);

                var fileDownloader = new FileDownloader(_segmentProcessor, _protocolDownloader, _initializeDonwload);
                tasks.Add(fileDownloader.StartAsynch(resourceDetail, _configurationSetting));
            }
            return(tasks);
        }
示例#4
0
        public static IProtocolDownloader CreateProvider(Type providerType)
        {
            IProtocolDownloader provider = (IProtocolDownloader)Activator.CreateInstance(providerType);

            return(provider);
        }
示例#5
0
        public ResponseBase ProcessSegment(ResourceDetail rl, IProtocolDownloader protocolDownloader, Segment segment)
        {
            var response = new ResponseBase();

            segment.LastError = null;
            try
            {
                if (segment.EndPosition > 0 && segment.StartPosition >= segment.EndPosition)
                {
                    segment.State = SegmentState.Finished;
                    return(new ResponseBase <CalculatedSegment[]>()
                    {
                        Denied = false
                    });
                }

                int    buffSize = 8192;
                byte[] buffer   = new byte[buffSize];

                segment.State = SegmentState.Connecting;
                var responseDownloadSegment = protocolDownloader.CreateStream(rl, segment.StartPosition,
                                                                              segment.EndPosition);
                if (responseDownloadSegment.Denied || responseDownloadSegment.ReturnedValue == null)
                {
                    Serilog.Log.Error("Task #" + Task.CurrentId + " File : " + segment.CurrentURL + " Current segment " +
                                      segment.Index + "Current Try: " + segment.CurrentTry +
                                      " An exception was raised in processSegment.cs");
                    segment.State     = SegmentState.Error;
                    segment.LastError = string.Join(",", responseDownloadSegment.Messages);
                    segment.CurrentTry++;
                    var responseFail = new ResponseBase()
                    {
                        Denied = true
                    };
                    responseFail.AddListMessage(responseDownloadSegment.Messages);
                    return(responseFail);
                }

                segment.InputStream = responseDownloadSegment.ReturnedValue;

                using (segment.InputStream)
                {
                    segment.State = SegmentState.Downloading;

                    long readSize;
                    do
                    {
                        readSize = segment.InputStream.Read(buffer, 0, buffSize);

                        if (segment.EndPosition > 0 && segment.StartPosition + readSize > segment.EndPosition)
                        {
                            readSize = (segment.EndPosition - segment.StartPosition);
                            if (readSize <= 0)
                            {
                                segment.StartPosition = segment.EndPosition;
                                break;
                            }
                        }

                        lock (segment.OutputStream)
                        {
                            segment.OutputStream.Position = segment.StartPosition;
                            segment.OutputStream.Write(buffer, 0, (int)readSize);
                        }

                        segment.IncreaseStartPosition(readSize);

                        if (segment.EndPosition > 0 && segment.StartPosition >= segment.EndPosition)
                        {
                            segment.StartPosition = segment.EndPosition;
                            break;
                        }
                    } while (readSize > 0);

                    if (segment.State == SegmentState.Downloading)
                    {
                        Serilog.Log.Information("Task #" + Task.CurrentId + " File : " + segment.CurrentURL +
                                                " Current segment " + segment.Index + "Current Try: " +
                                                segment.CurrentTry + " finished successfully");
                        segment.State   = SegmentState.Finished;
                        response.Denied = false;
                    }
                }
            }
            catch (Exception ex)
            {
                Serilog.Log.Error(ex,
                                  "Task #" + Task.CurrentId + " File : " + segment.CurrentURL + " Current segment " + segment.Index +
                                  "Current Try: " + segment.CurrentTry + " An exception was raised");
                segment.CurrentTry++;
                while (ex.InnerException != null)
                {
                    ex = ex.InnerException;
                }
                segment.State     = SegmentState.Error;
                segment.LastError = ex.Message;
                response.Denied   = true;
                response.Messages.Add(ex.Message);
            }
            finally
            {
                segment.InputStream = null;
            }
            return(response);
        }