UrlRequestInfo.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "UrlRequestInfo.h"
  23. #include "TorrentRequestInfo.h"
  24. #include "MetalinkRequestInfo.h"
  25. #include "prefs.h"
  26. #include "DownloadEngineFactory.h"
  27. extern RequestInfo* requestInfo;
  28. extern void setSignalHander(int signal, void (*handler)(int), int flags);
  29. void UrlRequestInfo::adjustRequestSize(Requests& requests,
  30. Requests& reserved,
  31. int maxConnections) const
  32. {
  33. if(maxConnections > 0 && (int)requests.size() > maxConnections) {
  34. copy(requests.begin()+maxConnections, requests.end(),
  35. back_inserter(reserved));
  36. //insert_iterator<Requests>(reserved, reserved.end()));
  37. requests.erase(requests.begin()+maxConnections, requests.end());
  38. }
  39. }
  40. RequestInfo* UrlRequestInfo::createNextRequestInfo() const
  41. {
  42. #ifdef ENABLE_BITTORRENT
  43. if(op->getAsBool(PREF_FOLLOW_TORRENT) &&
  44. Util::endsWith(fileInfo.filename, ".torrent")) {
  45. return new TorrentRequestInfo(fileInfo.filename, op);
  46. } else
  47. #endif // ENABLE_BITTORRENT
  48. #ifdef ENABLE_METALINK
  49. if(op->getAsBool(PREF_FOLLOW_METALINK) &&
  50. Util::endsWith(fileInfo.filename, ".metalink")) {
  51. return new MetalinkRequestInfo(fileInfo.filename, op);
  52. } else
  53. #endif // ENABLE_METALINK
  54. {
  55. return 0;
  56. }
  57. }
  58. void handler(int signal) {
  59. printf(_("\nstopping application...\n"));
  60. fflush(stdout);
  61. requestInfo->getDownloadEngine()->segmentMan->save();
  62. requestInfo->getDownloadEngine()->segmentMan->diskWriter->closeFile();
  63. delete requestInfo->getDownloadEngine();
  64. printf(_("done\n"));
  65. exit(EXIT_SUCCESS);
  66. }
  67. class CreateRequest {
  68. private:
  69. Requests* requestsPtr;
  70. string referer;
  71. int split;
  72. public:
  73. CreateRequest(Requests* requestsPtr,
  74. const string& referer,
  75. int split)
  76. :requestsPtr(requestsPtr),
  77. referer(referer),
  78. split(split) {}
  79. void operator()(const string& url) {
  80. for(int s = 1; s <= split; s++) {
  81. Request* req = new Request();
  82. req->setReferer(referer);
  83. if(req->setUrl(url)) {
  84. requestsPtr->push_back(req);
  85. } else {
  86. fprintf(stderr, _("Unrecognized URL or unsupported protocol: %s\n"),
  87. req->getUrl().c_str());
  88. delete req;
  89. }
  90. }
  91. }
  92. };
  93. RequestInfo* UrlRequestInfo::execute() {
  94. Requests requests;
  95. Requests reserved;
  96. for_each(urls.begin(), urls.end(),
  97. CreateRequest(&requests,
  98. op->get(PREF_REFERER),
  99. op->getAsInt(PREF_SPLIT)));
  100. adjustRequestSize(requests, reserved, maxConnections);
  101. e = DownloadEngineFactory::newConsoleEngine(op, requests, reserved);
  102. setSignalHander(SIGINT, handler, 0);
  103. setSignalHander(SIGTERM, handler, 0);
  104. RequestInfo* next = 0;
  105. try {
  106. e->run();
  107. if(e->segmentMan->finished()) {
  108. printDownloadCompeleteMessage(e->segmentMan->getFilePath());
  109. fileInfo.filename = e->segmentMan->getFilePath();
  110. fileInfo.length = e->segmentMan->totalSize;
  111. fileInfo.checksum = checksum;
  112. next = createNextRequestInfo();
  113. } else {
  114. e->segmentMan->save();
  115. e->segmentMan->diskWriter->closeFile();
  116. printDownloadAbortMessage();
  117. }
  118. } catch(Exception *e) {
  119. logger->error("Exception caught", e);
  120. delete e;
  121. fail = true;
  122. }
  123. for_each(requests.begin(), requests.end(), Deleter());
  124. for_each(reserved.begin(), reserved.end(), Deleter());
  125. setSignalHander(SIGINT, SIG_DFL, 0);
  126. setSignalHander(SIGTERM, SIG_DFL, 0);
  127. delete e;
  128. e = 0;
  129. return next;
  130. }