MetalinkEntry.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "FileEntry.h"
  39. #include "util.h"
  40. #include "a2functional.h"
  41. #ifdef ENABLE_MESSAGE_DIGEST
  42. # include "Checksum.h"
  43. # include "ChunkChecksum.h"
  44. #endif // ENABLE_MESSAGE_DIGEST
  45. #include "Signature.h"
  46. #include "SimpleRandomizer.h"
  47. namespace aria2 {
  48. MetalinkEntry::MetalinkEntry():
  49. file(0),
  50. maxConnections(-1)
  51. {}
  52. MetalinkEntry::~MetalinkEntry() {}
  53. class AddLocationPreference {
  54. private:
  55. std::deque<std::string> _locations;
  56. int _preferenceToAdd;
  57. public:
  58. AddLocationPreference(const std::deque<std::string>& locations, int preferenceToAdd):
  59. _locations(locations), _preferenceToAdd(preferenceToAdd)
  60. {
  61. std::transform(_locations.begin(), _locations.end(), _locations.begin(), util::toUpper);
  62. std::sort(_locations.begin(), _locations.end());
  63. }
  64. void operator()(SharedHandle<MetalinkResource>& res) {
  65. if(std::binary_search(_locations.begin(), _locations.end(), res->location)) {
  66. res->preference += _preferenceToAdd;
  67. }
  68. }
  69. };
  70. MetalinkEntry& MetalinkEntry::operator=(const MetalinkEntry& metalinkEntry)
  71. {
  72. if(this != &metalinkEntry) {
  73. this->file = metalinkEntry.file;
  74. this->version = metalinkEntry.version;
  75. this->language = metalinkEntry.language;
  76. this->os = metalinkEntry.os;
  77. this->maxConnections = metalinkEntry.maxConnections;
  78. #ifdef ENABLE_MESSAGE_DIGEST
  79. this->checksum = metalinkEntry.checksum;
  80. this->chunkChecksum = metalinkEntry.chunkChecksum;
  81. #endif // ENABLE_MESSAGE_DIGEST
  82. this->_signature = metalinkEntry._signature;
  83. }
  84. return *this;
  85. }
  86. std::string MetalinkEntry::getPath() const
  87. {
  88. return file->getPath();
  89. }
  90. uint64_t MetalinkEntry::getLength() const
  91. {
  92. return file->getLength();
  93. }
  94. void MetalinkEntry::setLocationPreference(const std::deque<std::string>& locations,
  95. int preferenceToAdd)
  96. {
  97. std::for_each(resources.begin(), resources.end(),
  98. AddLocationPreference(locations, preferenceToAdd));
  99. }
  100. class AddProtocolPreference {
  101. private:
  102. std::string _protocol;
  103. int _preferenceToAdd;
  104. public:
  105. AddProtocolPreference(const std::string& protocol, int prefToAdd):
  106. _protocol(protocol), _preferenceToAdd(prefToAdd) {}
  107. void operator()(const SharedHandle<MetalinkResource>& res) const
  108. {
  109. if(_protocol == MetalinkResource::getTypeString(res->type)) {
  110. res->preference += _preferenceToAdd;
  111. }
  112. }
  113. };
  114. void MetalinkEntry::setProtocolPreference(const std::string& protocol,
  115. int preferenceToAdd)
  116. {
  117. std::for_each(resources.begin(), resources.end(),
  118. AddProtocolPreference(protocol, preferenceToAdd));
  119. }
  120. class PrefOrder {
  121. public:
  122. bool operator()(const SharedHandle<MetalinkResource>& res1,
  123. const SharedHandle<MetalinkResource>& res2) {
  124. return res1->preference > res2->preference;
  125. }
  126. };
  127. void MetalinkEntry::reorderResourcesByPreference() {
  128. std::random_shuffle(resources.begin(), resources.end(),
  129. *(SimpleRandomizer::getInstance().get()));
  130. std::sort(resources.begin(), resources.end(), PrefOrder());
  131. }
  132. class Supported:public std::unary_function<SharedHandle<MetalinkResource>, bool> {
  133. public:
  134. bool operator()(const SharedHandle<MetalinkResource>& res) const
  135. {
  136. switch(res->type) {
  137. case MetalinkResource::TYPE_FTP:
  138. case MetalinkResource::TYPE_HTTP:
  139. #ifdef ENABLE_SSL
  140. case MetalinkResource::TYPE_HTTPS:
  141. #endif // ENABLE_SSL
  142. #ifdef ENABLE_BITTORRENT
  143. case MetalinkResource::TYPE_BITTORRENT:
  144. #endif // ENABLE_BITTORRENT
  145. return true;
  146. default:
  147. return false;
  148. }
  149. }
  150. };
  151. void MetalinkEntry::dropUnsupportedResource() {
  152. resources.erase(std::remove_if(resources.begin(), resources.end(),
  153. std::not1(Supported())),
  154. resources.end());
  155. }
  156. void MetalinkEntry::toFileEntry
  157. (std::deque<SharedHandle<FileEntry> >& fileEntries,
  158. const std::deque<SharedHandle<MetalinkEntry> >& metalinkEntries)
  159. {
  160. std::transform(metalinkEntries.begin(), metalinkEntries.end(),
  161. std::back_inserter(fileEntries),
  162. mem_fun_sh(&MetalinkEntry::getFile));
  163. }
  164. void MetalinkEntry::setSignature(const SharedHandle<Signature>& signature)
  165. {
  166. _signature = signature;
  167. }
  168. } // namespace aria2