| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 | 
							- /* <!-- copyright */
 
- /*
 
-  * aria2 - a simple utility for downloading files faster
 
-  *
 
-  * Copyright (C) 2006 Tatsuhiro Tsujikawa
 
-  *
 
-  * This program is free software; you can redistribute it and/or modify
 
-  * it under the terms of the GNU General Public License as published by
 
-  * the Free Software Foundation; either version 2 of the License, or
 
-  * (at your option) any later version.
 
-  *
 
-  * This program is distributed in the hope that it will be useful,
 
-  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
-  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
-  * GNU General Public License for more details.
 
-  *
 
-  * You should have received a copy of the GNU General Public License
 
-  * along with this program; if not, write to the Free Software
 
-  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-  */
 
- /* copyright --> */
 
- #include "DownloadEngine.h"
 
- #include "Util.h"
 
- #include "LogFactory.h"
 
- #include "TimeA2.h"
 
- #include <unistd.h>
 
- #include <sys/types.h>
 
- #include <sys/stat.h>
 
- #include <fcntl.h>
 
- #include <errno.h>
 
- #include <algorithm>
 
- using namespace std;
 
- DownloadEngine::DownloadEngine():noWait(false), segmentMan(0) {
 
-   logger = LogFactory::getInstance();
 
- }
 
- DownloadEngine::~DownloadEngine() {
 
-   delete segmentMan;
 
-   cleanQueue();
 
- }
 
- void DownloadEngine::cleanQueue() {
 
-   for_each(commands.begin(), commands.end(), Deleter());
 
-   commands.clear();
 
- }
 
- void DownloadEngine::run() {
 
-   initStatistics();
 
-   Time cp;
 
-   cp.setTimeInSec(0);
 
-   Commands activeCommands;
 
-   while(!commands.empty()) {
 
-     if(cp.elapsed(1)) {
 
-       cp.reset();
 
-       int max = commands.size();
 
-       for(int i = 0; i < max; i++) {
 
- 	Command* com = commands.front();
 
- 	commands.pop_front();
 
- 	if(com->execute()) {
 
- 	  delete com;
 
- 	}
 
-       }
 
-     } else {
 
-       for(Commands::iterator itr = activeCommands.begin();
 
- 	  itr != activeCommands.end(); itr++) {
 
- 	Commands::iterator comItr = find(commands.begin(), commands.end(),
 
- 					 *itr);
 
- 	assert(comItr != commands.end());
 
- 	Command* command = *itr;
 
- 	commands.erase(comItr);
 
- 	if(command->execute()) {
 
- 	  delete command;
 
- 	}
 
-       }
 
-     }
 
-     afterEachIteration();
 
-     activeCommands.clear();
 
-     if(!noWait && !commands.empty()) {
 
-       waitData(activeCommands);
 
-     }
 
-     noWait = false;
 
-     calculateStatistics();
 
-   }
 
-   onEndOfRun();
 
- }
 
- void DownloadEngine::shortSleep() const {
 
-   struct timeval tv;
 
-   tv.tv_sec = 0;
 
-   tv.tv_usec = 1000;
 
-   fd_set rfds;
 
-   FD_ZERO(&rfds);
 
-   select(0, &rfds, NULL, NULL, &tv);
 
- }
 
- class SetDescriptor {
 
- private:
 
-   int* max_ptr;
 
-   fd_set* rfds_ptr;
 
-   fd_set* wfds_ptr;
 
- public:
 
-   SetDescriptor(int* max_ptr, fd_set* rfds_ptr, fd_set* wfds_ptr):
 
-     max_ptr(max_ptr),
 
-     rfds_ptr(rfds_ptr),
 
-     wfds_ptr(wfds_ptr) {}
 
-   void operator()(const SocketEntry& entry) {
 
-     int fd = entry.socket->getSockfd();
 
-     switch(entry.type) {
 
-     case SocketEntry::TYPE_RD:
 
-       FD_SET(fd, rfds_ptr);
 
-       break;
 
-     case SocketEntry::TYPE_WR:
 
-       FD_SET(fd, wfds_ptr);
 
-       break;
 
-     }
 
-     if(*max_ptr < fd) {
 
-       *max_ptr = fd;
 
-     }
 
-   }
 
- #ifdef HAVE_LIBARES
 
-   void operator()(const NameResolverEntry& entry) {
 
-     int tempFd = entry.nameResolver->getFds(rfds_ptr, wfds_ptr);
 
-     if(*max_ptr < tempFd) {
 
-       *max_ptr = tempFd;
 
-     }
 
-   }
 
- #endif // HAVE_LIBARES
 
- };
 
- class AccumulateActiveCommand {
 
- private:
 
-   Commands* activeCommands_ptr;
 
-   fd_set* rfds_ptr;
 
-   fd_set* wfds_ptr;
 
- public:
 
-   AccumulateActiveCommand(Commands* activeCommands_ptr,
 
- 		       fd_set* rfds_ptr,
 
- 		       fd_set* wfds_ptr):
 
-     activeCommands_ptr(activeCommands_ptr),
 
-     rfds_ptr(rfds_ptr),
 
-     wfds_ptr(wfds_ptr) {}
 
-   void operator()(const SocketEntry& entry) {
 
-     if(FD_ISSET(entry.socket->getSockfd(), rfds_ptr) ||
 
-        FD_ISSET(entry.socket->getSockfd(), wfds_ptr)) {
 
-       activeCommands_ptr->push_back(entry.command);
 
-     }
 
-     /*
 
-     switch(entry.type) {
 
-     case SocketEntry::TYPE_RD:
 
-       if(FD_ISSET(entry.socket->getSockfd(), rfds_ptr)) {
 
-       activeCommands_ptr->push_back(entry.command);
 
-       }
 
-       break;
 
-     case SocketEntry::TYPE_WR:
 
-       if(FD_ISSET(entry.socket->getSockfd(), wfds_ptr)) {
 
- 	activeCommands_ptr->push_back(entry.command);
 
-       }
 
-       break;
 
-     }
 
-     */
 
-   }
 
- #ifdef HAVE_LIBARES
 
-   void operator()(const NameResolverEntry& entry) {
 
-     entry.nameResolver->process(rfds_ptr, wfds_ptr);
 
-     switch(entry.nameResolver->getStatus()) {
 
-     case NameResolver::STATUS_SUCCESS:
 
-     case NameResolver::STATUS_ERROR:
 
-       activeCommands_ptr->push_back(entry.command);
 
-       break;
 
-     default:
 
-       break;
 
-     }
 
-   }
 
- #endif // HAVE_LIBARES
 
- };
 
- void DownloadEngine::waitData(Commands& activeCommands) {
 
-   fd_set rfds;
 
-   fd_set wfds;
 
-   int retval = 0;
 
-   struct timeval tv;
 
-   
 
-   memcpy(&rfds, &rfdset, sizeof(fd_set));
 
-   memcpy(&wfds, &wfdset, sizeof(fd_set));
 
-   
 
-   tv.tv_sec = 1;
 
-   tv.tv_usec = 0;
 
-   retval = select(fdmax+1, &rfds, &wfds, NULL, &tv);
 
-   if(retval > 0) {
 
-     for_each(socketEntries.begin(), socketEntries.end(),
 
- 	     AccumulateActiveCommand(&activeCommands, &rfds, &wfds));
 
- #ifdef HAVE_LIBARES
 
-     for_each(nameResolverEntries.begin(), nameResolverEntries.end(),
 
- 	     AccumulateActiveCommand(&activeCommands, &rfds, &wfds));
 
- #endif // HAVE_LIBARES
 
-     sort(activeCommands.begin(), activeCommands.end());
 
-     activeCommands.erase(unique(activeCommands.begin(),
 
- 				activeCommands.end()),
 
- 			 activeCommands.end());
 
-   }
 
- }
 
- void DownloadEngine::updateFdSet() {
 
-   fdmax = 0;
 
-   FD_ZERO(&rfdset);
 
-   FD_ZERO(&wfdset);
 
- #ifdef HAVE_LIBARES
 
-   for_each(nameResolverEntries.begin(), nameResolverEntries.end(),
 
- 	   SetDescriptor(&fdmax, &rfdset, &wfdset));
 
- #endif // HAVE_LIBARES
 
-   for_each(socketEntries.begin(), socketEntries.end(),
 
- 	   SetDescriptor(&fdmax, &rfdset, &wfdset));
 
- }
 
- bool DownloadEngine::addSocket(const SocketEntry& entry) {
 
-   SocketEntries::iterator itr =
 
-     find(socketEntries.begin(), socketEntries.end(), entry);
 
-   if(itr == socketEntries.end()) {
 
-     socketEntries.push_back(entry);
 
-     updateFdSet();
 
-     return true;
 
-   } else {
 
-     return false;
 
-   }
 
- }
 
- bool DownloadEngine::deleteSocket(const SocketEntry& entry) {
 
-   SocketEntries::iterator itr =
 
-     find(socketEntries.begin(), socketEntries.end(), entry);
 
-   if(itr == socketEntries.end()) {
 
-     return false;
 
-   } else {
 
-     socketEntries.erase(itr);
 
-     updateFdSet();
 
-     return true;
 
-   }
 
- }
 
- bool DownloadEngine::addSocketForReadCheck(const SocketHandle& socket,
 
- 					   Command* command) {
 
-   SocketEntry entry(socket, command, SocketEntry::TYPE_RD);
 
-   return addSocket(entry);
 
- }
 
- bool DownloadEngine::deleteSocketForReadCheck(const SocketHandle& socket,
 
- 					      Command* command) {
 
-   SocketEntry entry(socket, command, SocketEntry::TYPE_RD);
 
-   return deleteSocket(entry);
 
- }
 
- bool DownloadEngine::addSocketForWriteCheck(const SocketHandle& socket,
 
- 					    Command* command) {
 
-   SocketEntry entry(socket, command, SocketEntry::TYPE_WR);
 
-   return addSocket(entry);
 
- }
 
- bool DownloadEngine::deleteSocketForWriteCheck(const SocketHandle& socket,
 
- 					       Command* command) {
 
-   SocketEntry entry(socket, command, SocketEntry::TYPE_WR);
 
-   return deleteSocket(entry);
 
- }
 
- #ifdef HAVE_LIBARES
 
- bool DownloadEngine::addNameResolverCheck(const NameResolverHandle& resolver,
 
- 					  Command* command) {
 
-   NameResolverEntry entry(resolver, command);
 
-   NameResolverEntries::iterator itr = find(nameResolverEntries.begin(),
 
- 					   nameResolverEntries.end(),
 
- 					   entry);
 
-   if(itr == nameResolverEntries.end()) {
 
-     nameResolverEntries.push_back(entry);
 
-     updateFdSet();
 
-     return true;
 
-   } else {
 
-     return false;
 
-   }
 
- }
 
- bool DownloadEngine::deleteNameResolverCheck(const NameResolverHandle& resolver,
 
- 					     Command* command) {
 
-   NameResolverEntry entry(resolver, command);
 
-   NameResolverEntries::iterator itr = find(nameResolverEntries.begin(),
 
- 					   nameResolverEntries.end(),
 
- 					   entry);
 
-   if(itr == nameResolverEntries.end()) {
 
-     return false;
 
-   } else {
 
-     nameResolverEntries.erase(itr);
 
-     updateFdSet();
 
-     return true;
 
-   }
 
- }
 
- #endif // HAVE_LIBARES
 
 
  |