DownloadEngine.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "DownloadEngine.h"
  23. #include "Util.h"
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <algorithm>
  29. using namespace std;
  30. DownloadEngine::DownloadEngine():noWait(false), segmentMan(NULL) {}
  31. DownloadEngine::~DownloadEngine() {
  32. assert(rsockets.empty());
  33. assert(wsockets.empty());
  34. }
  35. void DownloadEngine::run() {
  36. initStatistics();
  37. while(!commands.empty()) {
  38. int max = commands.size();
  39. for(int i = 0; i < max; i++) {
  40. Command* com = commands.front();
  41. commands.pop();
  42. if(com->execute()) {
  43. delete(com);
  44. }
  45. }
  46. afterEachIteration();
  47. shortSleep();
  48. if(!noWait && !commands.empty()) {
  49. waitData();
  50. }
  51. noWait = false;
  52. calculateStatistics();
  53. }
  54. onEndOfRun();
  55. }
  56. void DownloadEngine::shortSleep() const {
  57. struct timeval tv;
  58. tv.tv_sec = 0;
  59. tv.tv_usec = 1000;
  60. fd_set rfds;
  61. FD_ZERO(&rfds);
  62. select(0, &rfds, NULL, NULL, &tv);
  63. }
  64. void DownloadEngine::waitData() {
  65. fd_set rfds;
  66. fd_set wfds;
  67. struct timeval tv;
  68. int retval;
  69. FD_ZERO(&rfds);
  70. FD_ZERO(&wfds);
  71. int max = 0;
  72. for(Sockets::iterator itr = rsockets.begin(); itr != rsockets.end(); itr++) {
  73. FD_SET((*itr)->getSockfd(), &rfds);
  74. if(max < (*itr)->getSockfd()) {
  75. max = (*itr)->getSockfd();
  76. }
  77. }
  78. for(Sockets::iterator itr = wsockets.begin(); itr != wsockets.end(); itr++) {
  79. FD_SET((*itr)->getSockfd(), &wfds);
  80. if(max < (*itr)->getSockfd()) {
  81. max = (*itr)->getSockfd();
  82. }
  83. }
  84. tv.tv_sec = 1;
  85. tv.tv_usec = 0;
  86. retval = select(max+1, &rfds, /*&wfds*/NULL, NULL, &tv);
  87. }
  88. bool DownloadEngine::addSocket(Sockets& sockets, Socket* socket) {
  89. Sockets::iterator itr = find(sockets.begin(),
  90. sockets.end(),
  91. socket);
  92. if(itr == sockets.end()) {
  93. sockets.push_back(socket);
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. }
  99. bool DownloadEngine::deleteSocket(Sockets& sockets, Socket* socket) {
  100. Sockets::iterator itr = find(sockets.begin(),
  101. sockets.end(),
  102. socket);
  103. if(itr != sockets.end()) {
  104. sockets.erase(itr);
  105. return true;
  106. } else {
  107. return false;
  108. }
  109. }
  110. bool DownloadEngine::addSocketForReadCheck(Socket* socket) {
  111. return addSocket(rsockets, socket);
  112. }
  113. bool DownloadEngine::deleteSocketForReadCheck(Socket* socket) {
  114. return deleteSocket(rsockets , socket);
  115. }
  116. bool DownloadEngine::addSocketForWriteCheck(Socket* socket) {
  117. return addSocket(wsockets, socket);
  118. }
  119. bool DownloadEngine::deleteSocketForWriteCheck(Socket* socket) {
  120. return deleteSocket(wsockets, socket);
  121. }