bep_download_progress.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package protocol
  7. import "github.com/syncthing/syncthing/internal/gen/bep"
  8. type FileDownloadProgressUpdateType = bep.FileDownloadProgressUpdateType
  9. const (
  10. FileDownloadProgressUpdateTypeAppend = bep.FileDownloadProgressUpdateType_FILE_DOWNLOAD_PROGRESS_UPDATE_TYPE_APPEND
  11. FileDownloadProgressUpdateTypeForget = bep.FileDownloadProgressUpdateType_FILE_DOWNLOAD_PROGRESS_UPDATE_TYPE_FORGET
  12. )
  13. type DownloadProgress struct {
  14. Folder string
  15. Updates []FileDownloadProgressUpdate
  16. }
  17. func (d *DownloadProgress) toWire() *bep.DownloadProgress {
  18. updates := make([]*bep.FileDownloadProgressUpdate, len(d.Updates))
  19. for i, u := range d.Updates {
  20. updates[i] = u.toWire()
  21. }
  22. return &bep.DownloadProgress{
  23. Folder: d.Folder,
  24. Updates: updates,
  25. }
  26. }
  27. func downloadProgressFromWire(w *bep.DownloadProgress) *DownloadProgress {
  28. dp := &DownloadProgress{
  29. Folder: w.Folder,
  30. Updates: make([]FileDownloadProgressUpdate, len(w.Updates)),
  31. }
  32. for i, u := range w.Updates {
  33. dp.Updates[i] = fileDownloadProgressUpdateFromWire(u)
  34. }
  35. return dp
  36. }
  37. type FileDownloadProgressUpdate struct {
  38. UpdateType FileDownloadProgressUpdateType
  39. Name string
  40. Version Vector
  41. BlockIndexes []int
  42. BlockSize int
  43. }
  44. func (f *FileDownloadProgressUpdate) toWire() *bep.FileDownloadProgressUpdate {
  45. bidxs := make([]int32, len(f.BlockIndexes))
  46. for i, b := range f.BlockIndexes {
  47. bidxs[i] = int32(b)
  48. }
  49. return &bep.FileDownloadProgressUpdate{
  50. UpdateType: f.UpdateType,
  51. Name: f.Name,
  52. Version: f.Version.ToWire(),
  53. BlockIndexes: bidxs,
  54. BlockSize: int32(f.BlockSize),
  55. }
  56. }
  57. func fileDownloadProgressUpdateFromWire(w *bep.FileDownloadProgressUpdate) FileDownloadProgressUpdate {
  58. bidxs := make([]int, len(w.BlockIndexes))
  59. for i, b := range w.BlockIndexes {
  60. bidxs[i] = int(b)
  61. }
  62. return FileDownloadProgressUpdate{
  63. UpdateType: w.UpdateType,
  64. Name: w.Name,
  65. Version: VectorFromWire(w.Version),
  66. BlockIndexes: bidxs,
  67. BlockSize: int(w.BlockSize),
  68. }
  69. }