DownloadEngine.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <unistd.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #include "DlAbortEx.h"
  28. #include "Util.h"
  29. using namespace std;
  30. DownloadEngine::DownloadEngine():noWait(false) {}
  31. DownloadEngine::~DownloadEngine() {
  32. assert(rsockets.empty());
  33. assert(wsockets.empty());
  34. }
  35. void DownloadEngine::run() {
  36. struct timeval cp = { 0, 0 };
  37. int speed = 0;
  38. long long int psize = 0;
  39. while(!commands.empty()) {
  40. int max = commands.size();
  41. for(int i = 0; i < max; i++) {
  42. Command* com = commands.front();
  43. commands.pop();
  44. if(com->execute()) {
  45. delete(com);
  46. }
  47. }
  48. if(!noWait && !commands.empty()) {
  49. waitData();
  50. }
  51. noWait = false;
  52. long long int dlSize = segmentMan->getDownloadedSize();
  53. struct timeval now;
  54. gettimeofday(&now, NULL);
  55. if(cp.tv_sec == 0 && cp.tv_usec == 0) {
  56. cp = now;
  57. psize = dlSize;
  58. } else {
  59. long long int elapsed = Util::difftv(now, cp);
  60. if(elapsed >= 500000) {
  61. int nspeed = (int)((dlSize-psize)/(elapsed/1000000.0));
  62. speed = (nspeed+speed)/2;
  63. cp = now;
  64. psize = dlSize;
  65. cout << "\r ";
  66. cout << "\rProgress " << Util::llitos(dlSize, true) << " Bytes/" <<
  67. Util::llitos(segmentMan->totalSize, true) << " Bytes " <<
  68. (segmentMan->totalSize == 0 ? 0 : (dlSize*100)/segmentMan->totalSize) << "% " <<
  69. speed/1000.0 << "KB/s " <<
  70. "(" << commands.size() << " connections)" << flush;
  71. }
  72. }
  73. }
  74. segmentMan->removeIfFinished();
  75. diskWriter->closeFile();
  76. if(segmentMan->finished()) {
  77. cout << "\nThe download was complete. <" << segmentMan->getFilePath() << ">" << endl;
  78. } else {
  79. cout << "\nThe download was not complete because of errors. Check the log." << endl;
  80. }
  81. }
  82. // void DownloadEngine::shortSleep() {
  83. // int wait = rpm == 0 ? 0 : 4096*1000/rpm;
  84. // struct timeval tv;
  85. // int retval;
  86. // tv.tv_sec = 0;
  87. // tv.tv_usec = wait*1000;
  88. // retval = select(0, NULL, NULL, NULL, &tv);
  89. // }
  90. void DownloadEngine::waitData() {
  91. fd_set rfds;
  92. fd_set wfds;
  93. struct timeval tv;
  94. int retval;
  95. FD_ZERO(&rfds);
  96. FD_ZERO(&wfds);
  97. int max = 0;
  98. for(vector<Socket*>::iterator itr = rsockets.begin(); itr != rsockets.end(); itr++) {
  99. FD_SET((*itr)->getSockfd(), &rfds);
  100. if(max < (*itr)->getSockfd()) {
  101. max = (*itr)->getSockfd();
  102. }
  103. }
  104. for(vector<Socket*>::iterator itr = wsockets.begin(); itr != wsockets.end(); itr++) {
  105. FD_SET((*itr)->getSockfd(), &wfds);
  106. if(max < (*itr)->getSockfd()) {
  107. max = (*itr)->getSockfd();
  108. }
  109. }
  110. tv.tv_sec = 1;
  111. tv.tv_usec = 0;
  112. retval = select(max+1, &rfds, /*&wfds*/NULL, NULL, &tv);
  113. }
  114. bool DownloadEngine::addSocket(vector<Socket*>& sockets, Socket* socket) {
  115. vector<Socket*>::iterator itr = find(sockets.begin(),
  116. sockets.end(),
  117. socket);
  118. if(itr == sockets.end()) {
  119. sockets.push_back(socket);
  120. return true;
  121. } else {
  122. return false;
  123. }
  124. }
  125. bool DownloadEngine::deleteSocket(vector<Socket*>& sockets, Socket* socket) {
  126. vector<Socket*>::iterator itr = find(sockets.begin(),
  127. sockets.end(),
  128. socket);
  129. if(itr != sockets.end()) {
  130. sockets.erase(itr);
  131. return true;
  132. } else {
  133. return false;
  134. }
  135. }
  136. bool DownloadEngine::addSocketForReadCheck(Socket* socket) {
  137. return addSocket(rsockets, socket);
  138. }
  139. bool DownloadEngine::deleteSocketForReadCheck(Socket* socket) {
  140. return deleteSocket(rsockets , socket);
  141. }
  142. bool DownloadEngine::addSocketForWriteCheck(Socket* socket) {
  143. return addSocket(wsockets, socket);
  144. }
  145. bool DownloadEngine::deleteSocketForWriteCheck(Socket* socket) {
  146. return deleteSocket(wsockets, socket);
  147. }