MetalinkEntry.cc 5.6 KB

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