Metalink2RequestGroup.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "Metalink2RequestGroup.h"
  36. #include "RequestGroup.h"
  37. #include "Option.h"
  38. #include "LogFactory.h"
  39. #include "prefs.h"
  40. #include "Xml2MetalinkProcessor.h"
  41. #include "Util.h"
  42. #include "message.h"
  43. #include "BtDependency.h"
  44. #include "SingleFileDownloadContext.h"
  45. Metalink2RequestGroup::Metalink2RequestGroup(const Option* option):_option(option), _logger(LogFactory::getInstance()) {}
  46. Metalink2RequestGroup::~Metalink2RequestGroup() {}
  47. class AccumulateNonP2PUrl {
  48. private:
  49. Strings* urlsPtr;
  50. int32_t split;
  51. public:
  52. AccumulateNonP2PUrl(Strings* urlsPtr, int32_t split)
  53. :urlsPtr(urlsPtr),
  54. split(split) {}
  55. void operator()(const MetalinkResourceHandle& resource) {
  56. int32_t maxConnections;
  57. if(resource->maxConnections < 0) {
  58. maxConnections = split;
  59. } else {
  60. maxConnections = min<int32_t>(resource->maxConnections, split);
  61. }
  62. switch(resource->type) {
  63. case MetalinkResource::TYPE_HTTP:
  64. case MetalinkResource::TYPE_HTTPS:
  65. case MetalinkResource::TYPE_FTP:
  66. for(int32_t s = 1; s <= maxConnections; s++) {
  67. urlsPtr->push_back(resource->url);
  68. }
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. };
  75. class FindBitTorrentUrl {
  76. public:
  77. FindBitTorrentUrl() {}
  78. bool operator()(const MetalinkResourceHandle& resource) {
  79. if(resource->type == MetalinkResource::TYPE_BITTORRENT) {
  80. return true;
  81. } else {
  82. return false;
  83. }
  84. }
  85. };
  86. RequestGroups Metalink2RequestGroup::generate(const string& metalinkFile)
  87. {
  88. Xml2MetalinkProcessor proc;
  89. MetalinkerHandle metalinker = proc.parseFile(metalinkFile);
  90. if(metalinker->entries.empty()) {
  91. throw new DlAbortEx("No file entry found. Probably, the metalink file is not configured properly or broken.");
  92. }
  93. MetalinkEntries entries =
  94. metalinker->queryEntry(_option->get(PREF_METALINK_VERSION),
  95. _option->get(PREF_METALINK_LANGUAGE),
  96. _option->get(PREF_METALINK_OS));
  97. if(entries.size() == 0) {
  98. _logger->notice(EX_NO_RESULT_WITH_YOUR_PREFS);
  99. return RequestGroups();
  100. }
  101. bool useIndex;
  102. Integers selectIndexes;
  103. Util::unfoldRange(_option->get(PREF_SELECT_FILE), selectIndexes);
  104. if(selectIndexes.size()) {
  105. useIndex = true;
  106. } else {
  107. useIndex = false;
  108. }
  109. RequestGroups groups;
  110. int32_t count = 0;
  111. for(MetalinkEntries::iterator itr = entries.begin(); itr != entries.end();
  112. itr++, ++count) {
  113. MetalinkEntryHandle& entry = *itr;
  114. if(_option->defined(PREF_METALINK_LOCATION)) {
  115. entry->setLocationPreference(_option->get(PREF_METALINK_LOCATION), 100);
  116. }
  117. if(useIndex) {
  118. if(find(selectIndexes.begin(), selectIndexes.end(), count+1) == selectIndexes.end()) {
  119. continue;
  120. }
  121. }
  122. entry->dropUnsupportedResource();
  123. if(entry->resources.size() == 0) {
  124. continue;
  125. }
  126. _logger->info(MSG_METALINK_QUEUEING, entry->getPath().c_str());
  127. MetalinkResources::iterator itr = find_if(entry->resources.begin(),
  128. entry->resources.end(),
  129. FindBitTorrentUrl());
  130. RequestGroupHandle torrentRg = 0;
  131. // there is torrent entry
  132. if(itr != entry->resources.end()) {
  133. Strings uris;
  134. uris.push_back((*itr)->url);
  135. torrentRg = new RequestGroup(_option, uris);
  136. SingleFileDownloadContextHandle dctx =
  137. new SingleFileDownloadContext(_option->getAsInt(PREF_SEGMENT_SIZE),
  138. 0,
  139. "");
  140. dctx->setDir(_option->get(PREF_DIR));
  141. torrentRg->setDownloadContext(dctx);
  142. torrentRg->clearPostDowloadHandler();
  143. groups.push_back(torrentRg);
  144. }
  145. entry->reorderResourcesByPreference();
  146. Strings uris;
  147. for_each(entry->resources.begin(), entry->resources.end(),
  148. AccumulateNonP2PUrl(&uris, _option->getAsInt(PREF_SPLIT)));
  149. RequestGroupHandle rg = new RequestGroup(_option, uris);
  150. // If piece hash is specified in the metalink,
  151. // make segment size equal to piece hash size.
  152. int32_t pieceLength;
  153. if(entry->chunkChecksum.isNull()) {
  154. pieceLength = _option->getAsInt(PREF_SEGMENT_SIZE);
  155. } else {
  156. pieceLength = entry->chunkChecksum->getChecksumLength();
  157. }
  158. SingleFileDownloadContextHandle dctx =
  159. new SingleFileDownloadContext(pieceLength,
  160. 0,
  161. "");
  162. dctx->setDir(_option->get(PREF_DIR));
  163. if(!entry->chunkChecksum.isNull()) {
  164. dctx->setPieceHashes(entry->chunkChecksum->getChecksums());
  165. dctx->setPieceHashAlgo(entry->chunkChecksum->getAlgo());
  166. }
  167. // TODO set checksum here to DownloadContext
  168. // * hash and hash algorithm
  169. rg->setDownloadContext(dctx);
  170. rg->setHintFilename(entry->file->getBasename());
  171. rg->setHintTotalLength(entry->getLength());
  172. rg->setNumConcurrentCommand(entry->maxConnections < 0 ?
  173. _option->getAsInt(PREF_METALINK_SERVERS) :
  174. min<int32_t>(_option->getAsInt(PREF_METALINK_SERVERS), entry->maxConnections));
  175. // Inject depenency between rg and torrentRg here if torrentRg.isNull() == false
  176. if(!torrentRg.isNull()) {
  177. rg->dependsOn(new BtDependency(rg, torrentRg, _option));
  178. }
  179. groups.push_back(rg);
  180. }
  181. return groups;
  182. }