MetalinkEntry.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #ifdef ENABLE_MESSAGE_DIGEST
  43. # include "Checksum.h"
  44. # include "ChunkChecksum.h"
  45. #endif // ENABLE_MESSAGE_DIGEST
  46. #include "Signature.h"
  47. #include "SimpleRandomizer.h"
  48. namespace aria2 {
  49. MetalinkEntry::MetalinkEntry():
  50. sizeKnown(false),
  51. maxConnections(-1)
  52. {}
  53. MetalinkEntry::~MetalinkEntry() {}
  54. namespace {
  55. class AddLocationPriority {
  56. private:
  57. std::vector<std::string> locations_;
  58. int priorityToAdd_;
  59. public:
  60. AddLocationPriority
  61. (const std::vector<std::string>& locations, int priorityToAdd):
  62. locations_(locations), priorityToAdd_(priorityToAdd)
  63. {
  64. std::sort(locations_.begin(), locations_.end());
  65. }
  66. void operator()(SharedHandle<MetalinkResource>& res) {
  67. if(std::binary_search
  68. (locations_.begin(), locations_.end(), res->location)) {
  69. res->priority += priorityToAdd_;
  70. }
  71. }
  72. };
  73. } // namespace
  74. MetalinkEntry& MetalinkEntry::operator=(const MetalinkEntry& metalinkEntry)
  75. {
  76. if(this != &metalinkEntry) {
  77. this->file = metalinkEntry.file;
  78. this->version = metalinkEntry.version;
  79. this->languages = metalinkEntry.languages;
  80. this->oses = metalinkEntry.oses;
  81. this->maxConnections = metalinkEntry.maxConnections;
  82. #ifdef ENABLE_MESSAGE_DIGEST
  83. this->checksum = metalinkEntry.checksum;
  84. this->chunkChecksum = metalinkEntry.chunkChecksum;
  85. #endif // ENABLE_MESSAGE_DIGEST
  86. this->signature_ = metalinkEntry.signature_;
  87. }
  88. return *this;
  89. }
  90. const std::string& MetalinkEntry::getPath() const
  91. {
  92. return file->getPath();
  93. }
  94. uint64_t MetalinkEntry::getLength() const
  95. {
  96. return file->getLength();
  97. }
  98. void MetalinkEntry::setLocationPriority
  99. (const std::vector<std::string>& locations, int priorityToAdd)
  100. {
  101. std::for_each(resources.begin(), resources.end(),
  102. AddLocationPriority(locations, priorityToAdd));
  103. }
  104. namespace {
  105. class AddProtocolPriority {
  106. private:
  107. std::string protocol_;
  108. int priorityToAdd_;
  109. public:
  110. AddProtocolPriority(const std::string& protocol, int prefToAdd):
  111. protocol_(protocol), priorityToAdd_(prefToAdd) {}
  112. void operator()(const SharedHandle<MetalinkResource>& res) const
  113. {
  114. if(protocol_ == MetalinkResource::getTypeString(res->type)) {
  115. res->priority += priorityToAdd_;
  116. }
  117. }
  118. };
  119. } // namespace
  120. void MetalinkEntry::setProtocolPriority(const std::string& protocol,
  121. int priorityToAdd)
  122. {
  123. std::for_each(resources.begin(), resources.end(),
  124. AddProtocolPriority(protocol, priorityToAdd));
  125. }
  126. namespace {
  127. template<typename T>
  128. class PriorityHigher {
  129. public:
  130. bool operator()(const SharedHandle<T>& res1,
  131. const SharedHandle<T>& res2)
  132. {
  133. return res1->priority < res2->priority;
  134. }
  135. };
  136. } // namespace
  137. void MetalinkEntry::reorderResourcesByPriority() {
  138. std::random_shuffle(resources.begin(), resources.end(),
  139. *(SimpleRandomizer::getInstance().get()));
  140. std::sort(resources.begin(), resources.end(),
  141. PriorityHigher<MetalinkResource>());
  142. }
  143. void MetalinkEntry::reorderMetaurlsByPriority()
  144. {
  145. std::sort(metaurls.begin(), metaurls.end(),PriorityHigher<MetalinkMetaurl>());
  146. }
  147. namespace {
  148. class Supported:public std::unary_function<SharedHandle<MetalinkResource>, bool> {
  149. public:
  150. bool operator()(const SharedHandle<MetalinkResource>& res) const
  151. {
  152. switch(res->type) {
  153. case MetalinkResource::TYPE_FTP:
  154. case MetalinkResource::TYPE_HTTP:
  155. #ifdef ENABLE_SSL
  156. case MetalinkResource::TYPE_HTTPS:
  157. #endif // ENABLE_SSL
  158. #ifdef ENABLE_BITTORRENT
  159. case MetalinkResource::TYPE_BITTORRENT:
  160. #endif // ENABLE_BITTORRENT
  161. return true;
  162. default:
  163. return false;
  164. }
  165. }
  166. };
  167. } // namespace
  168. void MetalinkEntry::dropUnsupportedResource() {
  169. resources.erase(std::remove_if(resources.begin(), resources.end(),
  170. std::not1(Supported())),
  171. resources.end());
  172. }
  173. void MetalinkEntry::toFileEntry
  174. (std::vector<SharedHandle<FileEntry> >& fileEntries,
  175. const std::vector<SharedHandle<MetalinkEntry> >& metalinkEntries)
  176. {
  177. std::transform(metalinkEntries.begin(), metalinkEntries.end(),
  178. std::back_inserter(fileEntries),
  179. mem_fun_sh(&MetalinkEntry::getFile));
  180. }
  181. void MetalinkEntry::setSignature(const SharedHandle<Signature>& signature)
  182. {
  183. signature_ = signature;
  184. }
  185. bool MetalinkEntry::containsLanguage(const std::string& lang) const
  186. {
  187. return std::find(languages.begin(), languages.end(), lang) != languages.end();
  188. }
  189. bool MetalinkEntry::containsOS(const std::string& os) const
  190. {
  191. return std::find(oses.begin(), oses.end(), os) != oses.end();
  192. }
  193. } // namespace aria2