MetalinkEntry.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "MetalinkEntry.h"
  36. #include <algorithm>
  37. #include "MetalinkResource.h"
  38. #include "MetalinkMetaurl.h"
  39. #include "FileEntry.h"
  40. #include "util.h"
  41. #include "a2functional.h"
  42. #include "Checksum.h"
  43. #include "ChunkChecksum.h"
  44. #include "Signature.h"
  45. #include "SimpleRandomizer.h"
  46. namespace aria2 {
  47. MetalinkEntry::MetalinkEntry() : sizeKnown(false), maxConnections(-1) {}
  48. MetalinkEntry::~MetalinkEntry() {}
  49. const std::string& MetalinkEntry::getPath() const { return file->getPath(); }
  50. int64_t MetalinkEntry::getLength() const { return file->getLength(); }
  51. void MetalinkEntry::setLocationPriority(
  52. const std::vector<std::string>& locations, int priorityToAdd)
  53. {
  54. for (auto& res : resources) {
  55. if (std::find(std::begin(locations), std::end(locations), res->location) !=
  56. std::end(locations)) {
  57. res->priority += priorityToAdd;
  58. }
  59. }
  60. }
  61. void MetalinkEntry::setProtocolPriority(const std::string& protocol,
  62. int priorityToAdd)
  63. {
  64. for (auto& res : resources) {
  65. if (protocol == MetalinkResource::getTypeString(res->type)) {
  66. res->priority += priorityToAdd;
  67. }
  68. }
  69. }
  70. namespace {
  71. template <typename T> class PriorityHigher {
  72. public:
  73. bool operator()(const T& res1, const T& res2)
  74. {
  75. return res1->priority < res2->priority;
  76. }
  77. };
  78. } // namespace
  79. void MetalinkEntry::reorderResourcesByPriority()
  80. {
  81. std::shuffle(std::begin(resources), std::end(resources),
  82. *SimpleRandomizer::getInstance());
  83. std::sort(std::begin(resources), std::end(resources),
  84. PriorityHigher<std::unique_ptr<MetalinkResource>>{});
  85. }
  86. void MetalinkEntry::reorderMetaurlsByPriority()
  87. {
  88. std::sort(std::begin(metaurls), std::end(metaurls),
  89. PriorityHigher<std::unique_ptr<MetalinkMetaurl>>{});
  90. }
  91. namespace {
  92. class Supported
  93. : public std::unary_function<std::shared_ptr<MetalinkResource>, bool> {
  94. public:
  95. bool operator()(const std::shared_ptr<MetalinkResource>& res) const
  96. {
  97. switch (res->type) {
  98. case MetalinkResource::TYPE_FTP:
  99. case MetalinkResource::TYPE_HTTP:
  100. #ifdef ENABLE_SSL
  101. case MetalinkResource::TYPE_HTTPS:
  102. #endif // ENABLE_SSL
  103. #ifdef ENABLE_BITTORRENT
  104. case MetalinkResource::TYPE_BITTORRENT:
  105. #endif // ENABLE_BITTORRENT
  106. return true;
  107. default:
  108. return false;
  109. }
  110. }
  111. };
  112. } // namespace
  113. void MetalinkEntry::dropUnsupportedResource()
  114. {
  115. resources.erase(
  116. std::remove_if(std::begin(resources), std::end(resources),
  117. [](const std::unique_ptr<MetalinkResource>& res) {
  118. switch (res->type) {
  119. case MetalinkResource::TYPE_FTP:
  120. case MetalinkResource::TYPE_HTTP:
  121. #ifdef ENABLE_SSL
  122. case MetalinkResource::TYPE_HTTPS:
  123. #endif // ENABLE_SSL
  124. #ifdef ENABLE_BITTORRENT
  125. case MetalinkResource::TYPE_BITTORRENT:
  126. #endif // ENABLE_BITTORRENT
  127. return false;
  128. default:
  129. return true;
  130. }
  131. }),
  132. std::end(resources));
  133. }
  134. std::vector<std::unique_ptr<FileEntry>> MetalinkEntry::toFileEntry(
  135. std::vector<std::unique_ptr<MetalinkEntry>> metalinkEntries)
  136. {
  137. std::vector<std::unique_ptr<FileEntry>> res;
  138. res.reserve(metalinkEntries.size());
  139. for (auto& entry : metalinkEntries) {
  140. res.push_back(entry->popFile());
  141. }
  142. return res;
  143. }
  144. void MetalinkEntry::setSignature(std::unique_ptr<Signature> signature)
  145. {
  146. signature_ = std::move(signature);
  147. }
  148. bool MetalinkEntry::containsLanguage(const std::string& lang) const
  149. {
  150. return std::find(languages.begin(), languages.end(), lang) != languages.end();
  151. }
  152. bool MetalinkEntry::containsOS(const std::string& os) const
  153. {
  154. return std::find(oses.begin(), oses.end(), os) != oses.end();
  155. }
  156. std::unique_ptr<Signature> MetalinkEntry::popSignature()
  157. {
  158. return std::move(signature_);
  159. }
  160. std::unique_ptr<FileEntry> MetalinkEntry::popFile() { return std::move(file); }
  161. } // namespace aria2