Metalink2RequestGroup.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "Util.h"
  41. #include "message.h"
  42. #include "SingleFileDownloadContext.h"
  43. #include "MetalinkHelper.h"
  44. #include "BinaryStream.h"
  45. #include "MemoryBufferPreDownloadHandler.h"
  46. #include "TrueRequestGroupCriteria.h"
  47. #include "MetalinkEntry.h"
  48. #ifdef ENABLE_BITTORRENT
  49. # include "BtDependency.h"
  50. #endif // ENABLE_BITTORRENT
  51. Metalink2RequestGroup::Metalink2RequestGroup(const Option* option):_option(option), _logger(LogFactory::getInstance()) {}
  52. Metalink2RequestGroup::~Metalink2RequestGroup() {}
  53. class AccumulateNonP2PUrl {
  54. private:
  55. Strings* urlsPtr;
  56. int32_t split;
  57. public:
  58. AccumulateNonP2PUrl(Strings* urlsPtr, int32_t split)
  59. :urlsPtr(urlsPtr),
  60. split(split) {}
  61. void operator()(const MetalinkResourceHandle& resource) {
  62. int32_t maxConnections;
  63. if(resource->maxConnections < 0) {
  64. maxConnections = split;
  65. } else {
  66. maxConnections = min<int32_t>(resource->maxConnections, split);
  67. }
  68. switch(resource->type) {
  69. case MetalinkResource::TYPE_HTTP:
  70. case MetalinkResource::TYPE_HTTPS:
  71. case MetalinkResource::TYPE_FTP:
  72. for(int32_t s = 1; s <= maxConnections; s++) {
  73. urlsPtr->push_back(resource->url);
  74. }
  75. break;
  76. default:
  77. break;
  78. }
  79. }
  80. };
  81. class FindBitTorrentUrl {
  82. public:
  83. FindBitTorrentUrl() {}
  84. bool operator()(const MetalinkResourceHandle& resource) {
  85. if(resource->type == MetalinkResource::TYPE_BITTORRENT) {
  86. return true;
  87. } else {
  88. return false;
  89. }
  90. }
  91. };
  92. RequestGroups Metalink2RequestGroup::generate(const string& metalinkFile)
  93. {
  94. MetalinkEntries entries = MetalinkHelper::parseAndQuery(metalinkFile,
  95. _option);
  96. return createRequestGroup(entries);
  97. }
  98. RequestGroups Metalink2RequestGroup::generate(const BinaryStreamHandle& binaryStream)
  99. {
  100. MetalinkEntries entries = MetalinkHelper::parseAndQuery(binaryStream,
  101. _option);
  102. return createRequestGroup(entries);
  103. }
  104. RequestGroups Metalink2RequestGroup::createRequestGroup(MetalinkEntries entries)
  105. {
  106. if(entries.size() == 0) {
  107. _logger->notice(EX_NO_RESULT_WITH_YOUR_PREFS);
  108. return RequestGroups();
  109. }
  110. Integers selectIndexes = Util::parseIntRange(_option->get(PREF_SELECT_FILE)).flush();
  111. bool useIndex;
  112. if(selectIndexes.size()) {
  113. useIndex = true;
  114. } else {
  115. useIndex = false;
  116. }
  117. RequestGroups groups;
  118. int32_t count = 0;
  119. for(MetalinkEntries::iterator itr = entries.begin(); itr != entries.end();
  120. itr++, ++count) {
  121. MetalinkEntryHandle& entry = *itr;
  122. if(_option->defined(PREF_METALINK_LOCATION)) {
  123. Strings locations;
  124. Util::slice(locations, _option->get(PREF_METALINK_LOCATION), ',', true);
  125. entry->setLocationPreference(locations, 100);
  126. }
  127. if(useIndex) {
  128. if(find(selectIndexes.begin(), selectIndexes.end(), count+1) == selectIndexes.end()) {
  129. continue;
  130. }
  131. }
  132. entry->dropUnsupportedResource();
  133. if(entry->resources.size() == 0) {
  134. continue;
  135. }
  136. _logger->info(MSG_METALINK_QUEUEING, entry->getPath().c_str());
  137. MetalinkResources::iterator itr = find_if(entry->resources.begin(),
  138. entry->resources.end(),
  139. FindBitTorrentUrl());
  140. #ifdef ENABLE_BITTORRENT
  141. RequestGroupHandle torrentRg = 0;
  142. // there is torrent entry
  143. if(itr != entry->resources.end()) {
  144. Strings uris;
  145. uris.push_back((*itr)->url);
  146. torrentRg = new RequestGroup(_option, uris);
  147. SingleFileDownloadContextHandle dctx =
  148. new SingleFileDownloadContext(_option->getAsInt(PREF_SEGMENT_SIZE),
  149. 0,
  150. "");
  151. //dctx->setDir(_option->get(PREF_DIR));
  152. torrentRg->setDownloadContext(dctx);
  153. torrentRg->clearPreDowloadHandler();
  154. torrentRg->clearPostDowloadHandler();
  155. // make it in-memory download
  156. PreDownloadHandlerHandle preh = new MemoryBufferPreDownloadHandler();
  157. preh->setCriteria(new TrueRequestGroupCriteria());
  158. torrentRg->addPreDownloadHandler(preh);
  159. groups.push_back(torrentRg);
  160. }
  161. #endif // ENABLE_BITTORRENT
  162. entry->reorderResourcesByPreference();
  163. Strings uris;
  164. for_each(entry->resources.begin(), entry->resources.end(),
  165. AccumulateNonP2PUrl(&uris, _option->getAsInt(PREF_SPLIT)));
  166. RequestGroupHandle rg = new RequestGroup(_option, uris);
  167. // If piece hash is specified in the metalink,
  168. // make segment size equal to piece hash size.
  169. int32_t pieceLength;
  170. #ifdef ENABLE_MESSAGE_DIGEST
  171. if(entry->chunkChecksum.isNull()) {
  172. pieceLength = _option->getAsInt(PREF_SEGMENT_SIZE);
  173. } else {
  174. pieceLength = entry->chunkChecksum->getChecksumLength();
  175. }
  176. #else
  177. pieceLength = _option->getAsInt(PREF_SEGMENT_SIZE);
  178. #endif // ENABLE_MESSAGE_DIGEST
  179. SingleFileDownloadContextHandle dctx =
  180. new SingleFileDownloadContext(pieceLength,
  181. entry->getLength(),
  182. "",
  183. entry->file->getPath());
  184. dctx->setDir(_option->get(PREF_DIR));
  185. #ifdef ENABLE_MESSAGE_DIGEST
  186. if(entry->chunkChecksum.isNull()) {
  187. if(!entry->checksum.isNull()) {
  188. dctx->setChecksum(entry->checksum->getMessageDigest());
  189. dctx->setChecksumHashAlgo(entry->checksum->getAlgo());
  190. }
  191. } else {
  192. dctx->setPieceHashes(entry->chunkChecksum->getChecksums());
  193. dctx->setPieceHashAlgo(entry->chunkChecksum->getAlgo());
  194. }
  195. #endif // ENABLE_MESSAGE_DIGEST
  196. rg->setDownloadContext(dctx);
  197. rg->setNumConcurrentCommand(entry->maxConnections < 0 ?
  198. _option->getAsInt(PREF_METALINK_SERVERS) :
  199. min<int32_t>(_option->getAsInt(PREF_METALINK_SERVERS), entry->maxConnections));
  200. #ifdef ENABLE_BITTORRENT
  201. // Inject depenency between rg and torrentRg here if torrentRg.isNull() == false
  202. if(!torrentRg.isNull()) {
  203. rg->dependsOn(new BtDependency(rg, torrentRg, _option));
  204. }
  205. #endif // ENABLE_BITTORRENT
  206. groups.push_back(rg);
  207. }
  208. return groups;
  209. }