MetalinkRequestInfo.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "UrlRequestInfo.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. for(MetalinkEntries::iterator itr = entries.begin(); itr != entries.end();
  86. itr++) {
  87. MetalinkEntryHandle& entry = *itr;
  88. if(op->defined(PREF_METALINK_LOCATION)) {
  89. entry->setLocationPreference(op->get(PREF_METALINK_LOCATION), 100);
  90. }
  91. entry->dropUnsupportedResource();
  92. if(entry->resources.size() == 0) {
  93. continue;
  94. }
  95. logger->notice("Metalink: Queueing %s for download.",
  96. entry->filename.c_str());
  97. MetalinkResources::iterator itr =
  98. find_if(entry->resources.begin(),
  99. entry->resources.end(),
  100. FindBitTorrentUrl());
  101. Strings urls;
  102. int maxConnection = 0;
  103. Checksum checksum;
  104. if(itr == entry->resources.end()) {
  105. entry->reorderResourcesByPreference();
  106. for_each(entry->resources.begin(), entry->resources.end(),
  107. AccumulateNonP2PUrl(&urls, op->getAsInt(PREF_SPLIT)));
  108. maxConnection =
  109. op->getAsInt(PREF_METALINK_SERVERS)*op->getAsInt(PREF_SPLIT);
  110. // TODO
  111. // set checksum
  112. checksum = entry->checksum;
  113. } else {
  114. // BitTorrent downloading
  115. urls.push_back((*itr)->url);
  116. }
  117. RequestInfoHandle reqInfo(new UrlRequestInfo(urls, maxConnection, op));
  118. reqInfo->setChecksum(checksum);
  119. nextReqInfos.push_front(reqInfo);
  120. }
  121. } catch(Exception* ex) {
  122. logger->error("Exception caught", ex);
  123. delete ex;
  124. fail = true;
  125. }
  126. return nextReqInfos;
  127. }