MetalinkRequestInfo.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "MetalinkRequestInfo.h"
  36. #include "Xml2MetalinkProcessor.h"
  37. #include "prefs.h"
  38. #include "DlAbortEx.h"
  39. #include "MultiUrlRequestInfo.h"
  40. class AccumulateNonP2PUrl {
  41. private:
  42. Strings* urlsPtr;
  43. int split;
  44. public:
  45. AccumulateNonP2PUrl(Strings* urlsPtr,
  46. int split)
  47. :urlsPtr(urlsPtr),
  48. split(split) {}
  49. void operator()(const MetalinkResourceHandle& resource) {
  50. switch(resource->type) {
  51. case MetalinkResource::TYPE_HTTP:
  52. case MetalinkResource::TYPE_HTTPS:
  53. case MetalinkResource::TYPE_FTP:
  54. for(int s = 1; s <= split; s++) {
  55. urlsPtr->push_back(resource->url);
  56. }
  57. break;
  58. }
  59. }
  60. };
  61. class FindBitTorrentUrl {
  62. public:
  63. FindBitTorrentUrl() {}
  64. bool operator()(const MetalinkResourceHandle& resource) {
  65. if(resource->type == MetalinkResource::TYPE_BITTORRENT) {
  66. return true;
  67. } else {
  68. return false;
  69. }
  70. }
  71. };
  72. RequestInfos MetalinkRequestInfo::execute() {
  73. Xml2MetalinkProcessor proc;
  74. RequestInfos nextReqInfos;
  75. try {
  76. MetalinkerHandle metalinker = proc.parseFile(metalinkFile);
  77. MetalinkEntries entries =
  78. metalinker->queryEntry(op->get(PREF_METALINK_VERSION),
  79. op->get(PREF_METALINK_LANGUAGE),
  80. op->get(PREF_METALINK_OS));
  81. if(entries.size() == 0) {
  82. printf("No file matched with your preference.\n");
  83. throw new DlAbortEx("No file matched with your preference.");
  84. }
  85. RequestGroups groups;
  86. for(MetalinkEntries::iterator itr = entries.begin(); itr != entries.end();
  87. itr++) {
  88. MetalinkEntryHandle& entry = *itr;
  89. if(op->defined(PREF_METALINK_LOCATION)) {
  90. entry->setLocationPreference(op->get(PREF_METALINK_LOCATION), 100);
  91. }
  92. entry->dropUnsupportedResource();
  93. if(entry->resources.size() == 0) {
  94. continue;
  95. }
  96. logger->info("Metalink: Queueing %s for download.",
  97. entry->filename.c_str());
  98. MetalinkResources::iterator itr =
  99. find_if(entry->resources.begin(),
  100. entry->resources.end(),
  101. FindBitTorrentUrl());
  102. Strings urls;
  103. int maxConnection = 0;
  104. Checksum checksum;
  105. if(itr == entry->resources.end()) {
  106. entry->reorderResourcesByPreference();
  107. for_each(entry->resources.begin(), entry->resources.end(),
  108. AccumulateNonP2PUrl(&urls, op->getAsInt(PREF_SPLIT)));
  109. maxConnection =
  110. op->getAsInt(PREF_METALINK_SERVERS)*op->getAsInt(PREF_SPLIT);
  111. // TODO
  112. // set checksum
  113. checksum = entry->checksum;
  114. } else {
  115. // BitTorrent downloading
  116. urls.push_back((*itr)->url);
  117. }
  118. RequestGroupHandle rg = new RequestGroup(urls, op);
  119. rg->setHintFilename(entry->filename);
  120. rg->setHintTotalLength(entry->size);
  121. #ifdef ENABLE_MESSAGE_DIGEST
  122. if(entry->chunkChecksum.isNull()) {
  123. rg->setChecksum(checksum);
  124. } else {
  125. ChunkChecksumHandle cc =
  126. new ChunkChecksum(entry->chunkChecksum->digestAlgo,
  127. entry->chunkChecksum->pieceHashes,
  128. entry->chunkChecksum->pieceLength);
  129. rg->setChunkChecksum(cc);
  130. }
  131. #endif // ENABLE_MESSAGE_DIGEST
  132. groups.push_front(rg);
  133. }
  134. MultiUrlRequestInfoHandle reqInfo = new MultiUrlRequestInfo(groups, op);
  135. nextReqInfos.push_back(reqInfo);
  136. } catch(RecoverableException* ex) {
  137. logger->error("Exception caught", ex);
  138. delete ex;
  139. fail = true;
  140. }
  141. return nextReqInfos;
  142. }