public FileDownloader(ISegmentProcessor segmentProcessor, IProtocolDownloader protocolDownloader, IInitializeDonwload initializeDonwload) { _segments = new List <Segment>(); _tasks = new List <Task>(); _segmentProcessor = segmentProcessor; _protocolDownloader = protocolDownloader; _initializeDonwload = initializeDonwload; }
public IProtocolDownloader BindProtocolProviderInstance() { if (_protocolType == null) { BindProtocolProviderType(); } if (_provider == null) { _provider = ProtocolProviderFactory.CreateProvider(_protocolType); } return(_provider); }
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); }
public static IProtocolDownloader CreateProvider(Type providerType) { IProtocolDownloader provider = (IProtocolDownloader)Activator.CreateInstance(providerType); return(provider); }
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); }