Metalink2RequestGroup.cc 8.0 KB

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