MetalinkEntry.cc 5.4 KB

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