ProgressTracker.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "ProgressTracker.h"
  2. #include <stdio.h>
  3. /* Helper class for managing valid chunks in non-sequential scenarios
  4. e.g. progressive downloading */
  5. ProgressTracker::ProgressTracker()
  6. {
  7. current_position=0;
  8. current_chunk=0;
  9. chunks[0]=0;
  10. chunks[null_position]=null_position;
  11. };
  12. void ProgressTracker::Write(uint64_t bytes_written)
  13. {
  14. nu::AutoLock list_lock(list_guard);
  15. ChunkList::iterator next, itr = chunks.find(current_chunk);
  16. current_position += bytes_written;
  17. if (itr->second < current_position)
  18. itr->second = current_position;
  19. for (;;)
  20. {
  21. next = itr;
  22. next++;
  23. if (next != chunks.end() && (next->first <= itr->second))
  24. {
  25. itr->second = next->second;
  26. chunks.erase(next);
  27. }
  28. else
  29. break;
  30. }
  31. }
  32. bool ProgressTracker::Valid(uint64_t requested_position, uint64_t requested_end, uint64_t *available)
  33. {
  34. nu::AutoLock list_lock(list_guard);
  35. for (ChunkList::iterator itr=chunks.begin();itr!=chunks.end();itr++)
  36. {
  37. if (requested_position >= itr->first)
  38. {
  39. if (requested_position < itr->second)
  40. {
  41. if (available)
  42. *available = itr->second - requested_position;
  43. if (requested_end <= itr->second)
  44. return true;
  45. else
  46. return false;
  47. }
  48. }
  49. }
  50. if (available)
  51. *available = 0;
  52. return false;
  53. }
  54. bool ProgressTracker::Seek(uint64_t requested_position, uint64_t requested_end, uint64_t *new_start, uint64_t *new_end)
  55. {
  56. nu::AutoLock list_lock(list_guard);
  57. uint64_t last_good_start=0;
  58. ChunkList::iterator itr;
  59. for (itr=chunks.begin();itr!=chunks.end();itr++)
  60. {
  61. if (requested_position >= itr->first)
  62. {
  63. current_chunk = itr->first;
  64. if (requested_position <= itr->second)
  65. {
  66. ChunkList::iterator next = itr;
  67. next++;
  68. *new_end = next->first;
  69. *new_start = current_position = itr->second;
  70. if (requested_end <= itr->second)
  71. {
  72. return true;
  73. }
  74. else
  75. {
  76. return false;
  77. }
  78. }
  79. else
  80. {
  81. last_good_start = itr->second;
  82. }
  83. }
  84. }
  85. if (last_good_start > requested_position)
  86. *new_start = current_position = last_good_start;
  87. else
  88. {
  89. *new_start = current_chunk = current_position = requested_position;
  90. chunks[current_chunk] = current_chunk;
  91. }
  92. *new_end = null_position;
  93. return false;
  94. }
  95. void ProgressTracker::Dump()
  96. {
  97. ChunkList::iterator itr;
  98. for (itr=chunks.begin();itr!=chunks.end();itr++)
  99. {
  100. printf("%llu - %llu\n", itr->first, itr->second);
  101. }
  102. }
  103. const uint64_t ProgressTracker::null_position = (uint64_t)-1;