MetalinkEntry.cc 5.7 KB

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