MetalinkEntry.cc 5.8 KB

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