public void TestSerializeDeserializeInProgressDownloadEntryOk()
        {
            var file   = Guid.NewGuid().ToString();
            var folder = Path.GetTempPath();
            var list   = new List <InProgressDownloadEntry>();

            list.Add(new InProgressDownloadEntry
            {
                Id                = Guid.NewGuid().ToString(),
                Name              = "Sample entry 1",
                DateAdded         = DateTime.Now,
                DownloadType      = "Http",
                FileNameFetchMode = FileNameFetchMode.FileNameAndExtension,
                Size              = 12345,
                Progress          = 10,
                TargetDir         = "abc"
            });
            list.Add(new InProgressDownloadEntry
            {
                Id                = Guid.NewGuid().ToString(),
                Name              = "Sample entry 2",
                DateAdded         = DateTime.Now,
                DownloadType      = "Http",
                FileNameFetchMode = FileNameFetchMode.FileNameAndExtension,
                Size              = 1234567,
                Progress          = 20,
                TargetDir         = "abcd"
            });
            TransactedIO.WriteInProgressList(list, file, folder);
            Console.WriteLine(JsonConvert.SerializeObject(TransactedIO.ReadInProgressList(file, folder), Formatting.Indented));
        }
示例#2
0
        protected override void RestoreState()
        {
            var state = DownloadStateIO.LoadMultiSourceHLSDownloadState(Id !);

            this._state = state;

            try
            {
                Log.Debug("Restoring chunks from: " + Path.Combine(state.TempDirectory, "chunks.db"));

                if (!TransactedIO.ReadStream("chunks.db", state.TempDirectory, s =>
                {
                    _chunks = ChunkStateFromBytes(s);
                }))
                {
                    throw new FileNotFoundException(Path.Combine(state.TempDirectory, "chunks.db"));
                }

                var hlsDir = state.TempDirectory;

                var streamMap = _chunks.Select(c => new
                {
                    c.Id,
                    TempFilePath = Path.Combine(hlsDir, (c.StreamIndex == 0 ? "1_" : "2_") + c.Id + FileHelper.GetFileName(c.Uri))
                }).ToDictionary(e => e.Id, e => e.TempFilePath);
                _chunkStreamMap = new SimpleStreamMap {
                    StreamMap = streamMap
                };

                var count = 0;
                totalDownloadedBytes = 0;
                _chunks.ForEach(c =>
                {
                    if (c.ChunkState == ChunkState.Finished)
                    {
                        count++;
                    }
                    if (c.Downloaded > 0)
                    {
                        totalDownloadedBytes += c.Downloaded;
                    }
                });
                ticksAtDownloadStartOrResume = Helpers.TickCount();
                this.lastProgress            = (count * 100) / _chunks.Count;
                Log.Debug("Already downloaded: " + count + " Total: " + _chunks.Count);
            }
            catch
            {
                // ignored
                Log.Debug("Chunk restore failed");
            }
        }
示例#3
0
 protected override void SaveChunkState()
 {
     try
     {
         rwLock.EnterWriteLock();
         if (pieces.Count == 0)
         {
             return;
         }
         TransactedIO.WriteStream("chunks.db", state !.TempDir !, base.ChunkStateToBytes);
     }
     finally
     {
         rwLock.ExitWriteLock();
     }
 }
示例#4
0
        public override void RestoreState()
        {
            state = DownloadStateIO.LoadSingleSourceHTTPDownloaderState(Id !);
            //if (!TransactedIO.ReadStream(Id + ".state", Config.DataDir, s =>
            //{
            //    state = DownloadStateStore.SingleSourceHTTPDownloaderStateFromBytes(s);
            //}))
            //{
            //    throw new FileNotFoundException(Path.Combine(Config.DataDir, Id + ".state"));
            //}

            //var bytes = TransactedIO.ReadBytes(Id + ".state", Config.DataDir);
            //if (bytes == null)
            //{
            //    throw new FileNotFoundException(Path.Combine(Config.DataDir, Id + ".state"));
            //}

            //state = DownloadStateStore.SingleSourceHTTPDownloaderStateFromBytes(bytes);
            try
            {
                //var chunkBytes = TransactedIO.ReadBytes("chunks.db", state.TempDir);
                //if (chunkBytes == null)
                //{
                //    throw new FileNotFoundException(Path.Combine(state.TempDir, "chunks.json"));
                //}
                //pieces = ChunkStateFromBytes(chunkBytes);
                if (!TransactedIO.ReadStream("chunks.db", state !.TempDir !, s =>
                {
                    pieces = ChunkStateFromBytes(s);
                }))
                {
                    throw new FileNotFoundException(Path.Combine(state.TempDir, "chunks.db"));
                }
                Log.Debug("Total size: " + state.FileSize);
                //foreach (var item in pieces.Keys)
                //{
                //    Log.Debug("Chunk id: " + item + " offset: " + pieces[item].Offset + " downloaded: " +
                //        pieces[item].Downloaded + " length: " + pieces[item].Length + " state: " + pieces[item].State);
                //}
            }
            catch
            {
                // ignored
                Console.WriteLine("Chunk restore failed");
            }
            TicksAndSizeAtResume();
        }
示例#5
0
 public override void RestoreState()
 {
     state = DownloadStateIO.LoadDualSourceHTTPDownloaderState(Id !);
     try
     {
         if (!TransactedIO.ReadStream("chunks.db", state !.TempDir !, s =>
         {
             pieces = ChunkStateFromBytes(s);
         }))
         {
             throw new FileNotFoundException(Path.Combine(state.TempDir, "chunks.db"));
         }
     }
     catch
     {
         // ignored
         Log.Debug("Chunk restore failed");
     }
     TicksAndSizeAtResume();
 }
示例#6
0
        protected override void RestoreState()
        {
            var state = DownloadStateIO.LoadMultiSourceDASHDownloadState(Id !);

            this._state = state;

            //var bytes = TransactedIO.ReadBytes(Id + ".state", Config.DataDir);
            //if (bytes == null)
            //{
            //    throw new FileNotFoundException(Path.Combine(Config.DataDir, Id + ".state"));
            //}

            //var state = DownloadStateStore.MultiSourceDASHDownloadStateFromBytes(bytes);
            //this._state = state;

            //var text = TransactedIO.Read(Id + ".state", Config.DataDir);
            //if (text == null)
            //{
            //    throw new FileNotFoundException(Path.Combine(Config.DataDir, Id + ".state"));
            //}
            ////since all information is available in constructor we assume chunk restore can not fail
            //var state = JsonConvert.DeserializeObject<MultiSourceDASHDownloadState>(
            //                     text);
            //this._state = state;

            try
            {
                Log.Debug("Restoring chunks from: " + Path.Combine(_state.TempDirectory, "chunks.db"));

                if (!TransactedIO.ReadStream("chunks.db", state.TempDirectory, s =>
                {
                    _chunks = ChunkStateFromBytes(s);// pieces = ChunkStateFromBytes(s);
                }))
                {
                    throw new FileNotFoundException(Path.Combine(state.TempDirectory, "chunks.db"));
                }

                //var bytes2 = TransactedIO.ReadBytes("chunks.db", _state.TempDirectory);
                //if (bytes2 == null)
                //{
                //    throw new FileNotFoundException(Path.Combine(_state.TempDirectory, "chunks.json"));
                //}

                //_chunks = ChunkStateFromBytes(bytes2);

                var dashDir   = _state.TempDirectory;
                var streamMap = _chunks.Select(c => new
                {
                    c.Id,
                    TempFilePath = Path.Combine(dashDir, (c.StreamIndex == 0 ? "1_" : "2_") + c.Id + FileHelper.GetFileName(c.Uri))
                }).ToDictionary(e => e.Id, e =>
                                e.TempFilePath);
                _chunkStreamMap = new SimpleStreamMap {
                    StreamMap = streamMap
                };
            }
            catch (Exception ex)
            {
                Log.Debug(ex, "Error loading chunks");

                if (state.Demuxed)
                {
                    CreateChunks2(state, _chunks, _chunkStreamMap);
                }
                else
                {
                    CreateChunks1(state, _chunks, _chunkStreamMap);
                }
            }


            var count = 0;

            totalDownloadedBytes = 0;
            _chunks.ForEach(c =>
            {
                if (c.ChunkState == ChunkState.Finished)
                {
                    count++;
                }
                if (c.Downloaded > 0)
                {
                    totalDownloadedBytes += c.Downloaded;
                }
            });
            this.lastProgress            = (count * 100) / _chunks.Count;
            ticksAtDownloadStartOrResume = Helpers.TickCount();
            Log.Debug("Already downloaded: " + count);
        }