MetalinkRequestInfo.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "MetalinkRequestInfo.h"
  23. #include "Xml2MetalinkProcessor.h"
  24. #include "prefs.h"
  25. #include "DlAbortEx.h"
  26. #include "UrlRequestInfo.h"
  27. class AccumulateNonP2PUrl {
  28. private:
  29. Strings* urlsPtr;
  30. int split;
  31. public:
  32. AccumulateNonP2PUrl(Strings* urlsPtr,
  33. int split)
  34. :urlsPtr(urlsPtr),
  35. split(split) {}
  36. void operator()(const MetalinkResource* resource) {
  37. switch(resource->type) {
  38. case MetalinkResource::TYPE_HTTP:
  39. case MetalinkResource::TYPE_HTTPS:
  40. case MetalinkResource::TYPE_FTP:
  41. for(int s = 1; s <= split; s++) {
  42. urlsPtr->push_back(resource->url);
  43. }
  44. break;
  45. }
  46. }
  47. };
  48. class FindBitTorrentUrl {
  49. public:
  50. FindBitTorrentUrl() {}
  51. bool operator()(const MetalinkResource* resource) {
  52. if(resource->type == MetalinkResource::TYPE_BITTORRENT) {
  53. return true;
  54. } else {
  55. return false;
  56. }
  57. }
  58. };
  59. RequestInfo* MetalinkRequestInfo::execute() {
  60. RequestInfo* next = 0;
  61. Xml2MetalinkProcessor proc;
  62. Metalinker* metalinker = 0;
  63. try {
  64. metalinker = proc.parseFile(metalinkFile);
  65. MetalinkEntry* entry =
  66. metalinker->queryEntry(op->get(PREF_METALINK_VERSION),
  67. op->get(PREF_METALINK_LANGUAGE),
  68. op->get(PREF_METALINK_OS));
  69. if(entry == 0) {
  70. printf("No file matched with your preference.\n");
  71. throw new DlAbortEx("No file matched with your preference.");
  72. }
  73. entry->dropUnsupportedResource();
  74. MetalinkResources::iterator itr =
  75. find_if(entry->resources.begin(),
  76. entry->resources.end(),
  77. FindBitTorrentUrl());
  78. Strings urls;
  79. int maxConnection = 0;
  80. Checksum checksum;
  81. if(itr == entry->resources.end()) {
  82. entry->reorderResourcesByPreference();
  83. for_each(entry->resources.begin(), entry->resources.end(),
  84. AccumulateNonP2PUrl(&urls, op->getAsInt(PREF_SPLIT)));
  85. maxConnection =
  86. op->getAsInt(PREF_METALINK_SERVERS)*op->getAsInt(PREF_SPLIT);
  87. // TODO
  88. // set checksum
  89. checksum = entry->checksum;
  90. } else {
  91. // BitTorrent downloading
  92. urls.push_back((*itr)->url);
  93. }
  94. next = new UrlRequestInfo(urls, maxConnection, op);
  95. next->setChecksum(checksum);
  96. } catch(Exception* e) {
  97. logger->error("Exception caught", e);
  98. delete e;
  99. fail = true;
  100. }
  101. delete metalinker;
  102. return next;
  103. }