RequestGroupMan.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "RequestGroupMan.h"
  36. #include <iomanip>
  37. #include <sstream>
  38. #include <ostream>
  39. #include <fstream>
  40. #include <numeric>
  41. #include <algorithm>
  42. #include "BtProgressInfoFile.h"
  43. #include "RecoverableException.h"
  44. #include "RequestGroup.h"
  45. #include "LogFactory.h"
  46. #include "Logger.h"
  47. #include "DownloadEngine.h"
  48. #include "message.h"
  49. #include "a2functional.h"
  50. #include "DownloadResult.h"
  51. #include "DownloadContext.h"
  52. #include "ServerStatMan.h"
  53. #include "ServerStat.h"
  54. #include "PeerStat.h"
  55. #include "SegmentMan.h"
  56. #include "FeedbackURISelector.h"
  57. #include "InOrderURISelector.h"
  58. #include "AdaptiveURISelector.h"
  59. #include "Option.h"
  60. #include "prefs.h"
  61. #include "File.h"
  62. #include "Util.h"
  63. #include "Command.h"
  64. #include "FileEntry.h"
  65. namespace aria2 {
  66. RequestGroupMan::RequestGroupMan(const RequestGroups& requestGroups,
  67. unsigned int maxSimultaneousDownloads,
  68. const Option* option):
  69. _requestGroups(requestGroups),
  70. _logger(LogFactory::getInstance()),
  71. _maxSimultaneousDownloads(maxSimultaneousDownloads),
  72. _gidCounter(0),
  73. _option(option),
  74. _serverStatMan(new ServerStatMan()),
  75. _maxOverallDownloadSpeedLimit
  76. (option->getAsInt(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)),
  77. _maxOverallUploadSpeedLimit(option->getAsInt(PREF_MAX_OVERALL_UPLOAD_LIMIT))
  78. {}
  79. bool RequestGroupMan::downloadFinished()
  80. {
  81. if(_option->getAsBool(PREF_ENABLE_HTTP_SERVER)) {
  82. return false;
  83. }
  84. if(!_reservedGroups.empty()) {
  85. return false;
  86. }
  87. for(RequestGroups::iterator itr = _requestGroups.begin();
  88. itr != _requestGroups.end(); ++itr) {
  89. if((*itr)->getNumCommand() > 0 || !(*itr)->downloadFinished()) {
  90. return false;
  91. }
  92. }
  93. return true;
  94. }
  95. void RequestGroupMan::addRequestGroup(const RequestGroupHandle& group)
  96. {
  97. _requestGroups.push_back(group);
  98. }
  99. void RequestGroupMan::addReservedGroup(const RequestGroups& groups)
  100. {
  101. _reservedGroups.insert(_reservedGroups.end(), groups.begin(), groups.end());
  102. }
  103. void RequestGroupMan::addReservedGroup(const RequestGroupHandle& group)
  104. {
  105. _reservedGroups.push_back(group);
  106. }
  107. size_t RequestGroupMan::countRequestGroup() const
  108. {
  109. return _requestGroups.size();
  110. }
  111. RequestGroupHandle RequestGroupMan::getRequestGroup(size_t index) const
  112. {
  113. if(index < _requestGroups.size()) {
  114. return _requestGroups[index];
  115. } else {
  116. return SharedHandle<RequestGroup>();
  117. }
  118. }
  119. const std::deque<SharedHandle<RequestGroup> >&
  120. RequestGroupMan::getRequestGroups() const
  121. {
  122. return _requestGroups;
  123. }
  124. const std::deque<SharedHandle<RequestGroup> >&
  125. RequestGroupMan::getReservedGroups() const
  126. {
  127. return _reservedGroups;
  128. }
  129. class ProcessStoppedRequestGroup {
  130. private:
  131. DownloadEngine* _e;
  132. std::deque<SharedHandle<RequestGroup> >& _reservedGroups;
  133. std::deque<SharedHandle<DownloadResult> >& _downloadResults;
  134. Logger* _logger;
  135. void saveSignature(const SharedHandle<RequestGroup>& group)
  136. {
  137. SharedHandle<Signature> sig =
  138. group->getDownloadContext()->getSignature();
  139. if(!sig.isNull() && !sig->getBody().empty()) {
  140. // filename of signature file is the path to download file followed by
  141. // ".sig".
  142. std::string signatureFile = group->getFilePath()+".sig";
  143. if(sig->save(signatureFile)) {
  144. _logger->notice(MSG_SIGNATURE_SAVED, signatureFile.c_str());
  145. } else {
  146. _logger->notice(MSG_SIGNATURE_NOT_SAVED, signatureFile.c_str());
  147. }
  148. }
  149. }
  150. public:
  151. ProcessStoppedRequestGroup
  152. (DownloadEngine* e,
  153. std::deque<SharedHandle<RequestGroup> >& reservedGroups,
  154. std::deque<SharedHandle<DownloadResult> >& downloadResults):
  155. _e(e),
  156. _reservedGroups(reservedGroups),
  157. _downloadResults(downloadResults),
  158. _logger(LogFactory::getInstance()) {}
  159. void operator()(const SharedHandle<RequestGroup>& group)
  160. {
  161. if(group->getNumCommand() == 0) {
  162. try {
  163. group->closeFile();
  164. if(group->downloadFinished()) {
  165. group->applyLastModifiedTimeToLocalFiles();
  166. group->reportDownloadFinished();
  167. if(group->allDownloadFinished()) {
  168. group->getProgressInfoFile()->removeFile();
  169. saveSignature(group);
  170. } else {
  171. group->getProgressInfoFile()->save();
  172. }
  173. RequestGroups nextGroups;
  174. group->postDownloadProcessing(nextGroups);
  175. if(!nextGroups.empty()) {
  176. _logger->debug
  177. ("Adding %lu RequestGroups as a result of PostDownloadHandler.",
  178. static_cast<unsigned long>(nextGroups.size()));
  179. _reservedGroups.insert(_reservedGroups.begin(),
  180. nextGroups.begin(), nextGroups.end());
  181. }
  182. } else {
  183. group->getProgressInfoFile()->save();
  184. }
  185. } catch(RecoverableException& ex) {
  186. _logger->error(EX_EXCEPTION_CAUGHT, ex);
  187. }
  188. group->releaseRuntimeResource(_e);
  189. _downloadResults.push_back(group->createDownloadResult());
  190. }
  191. }
  192. };
  193. class CollectServerStat {
  194. private:
  195. RequestGroupMan* _requestGroupMan;
  196. public:
  197. CollectServerStat(RequestGroupMan* requestGroupMan):
  198. _requestGroupMan(requestGroupMan) {}
  199. void operator()(const SharedHandle<RequestGroup>& group)
  200. {
  201. if(group->getNumCommand() == 0) {
  202. // Collect statistics during download in PeerStats and update/register
  203. // ServerStatMan
  204. if(!group->getSegmentMan().isNull()) {
  205. const std::deque<SharedHandle<PeerStat> >& peerStats =
  206. group->getSegmentMan()->getPeerStats();
  207. for(std::deque<SharedHandle<PeerStat> >::const_iterator i =
  208. peerStats.begin(); i != peerStats.end(); ++i) {
  209. if((*i)->getHostname().empty() || (*i)->getProtocol().empty()) {
  210. continue;
  211. }
  212. int speed = (*i)->getAvgDownloadSpeed();
  213. if (speed == 0) continue;
  214. SharedHandle<ServerStat> ss =
  215. _requestGroupMan->getOrCreateServerStat((*i)->getHostname(),
  216. (*i)->getProtocol());
  217. ss->increaseCounter();
  218. ss->updateDownloadSpeed(speed);
  219. if(peerStats.size() == 1) {
  220. ss->updateSingleConnectionAvgSpeed(speed);
  221. }
  222. else {
  223. ss->updateMultiConnectionAvgSpeed(speed);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. };
  230. class FindStoppedRequestGroup {
  231. public:
  232. bool operator()(const SharedHandle<RequestGroup>& group) {
  233. return group->getNumCommand() == 0;
  234. }
  235. };
  236. void RequestGroupMan::updateServerStat()
  237. {
  238. std::for_each(_requestGroups.begin(), _requestGroups.end(),
  239. CollectServerStat(this));
  240. }
  241. void RequestGroupMan::removeStoppedGroup(DownloadEngine* e)
  242. {
  243. size_t numPrev = _requestGroups.size();
  244. updateServerStat();
  245. std::for_each(_requestGroups.begin(), _requestGroups.end(),
  246. ProcessStoppedRequestGroup(e, _reservedGroups,
  247. _downloadResults));
  248. _requestGroups.erase(std::remove_if(_requestGroups.begin(),
  249. _requestGroups.end(),
  250. FindStoppedRequestGroup()),
  251. _requestGroups.end());
  252. size_t numRemoved = numPrev-_requestGroups.size();
  253. if(numRemoved > 0) {
  254. _logger->debug("%lu RequestGroup(s) deleted.",
  255. static_cast<unsigned long>(numRemoved));
  256. }
  257. }
  258. void RequestGroupMan::configureRequestGroup
  259. (const SharedHandle<RequestGroup>& requestGroup) const
  260. {
  261. const std::string& uriSelectorValue = _option->get(PREF_URI_SELECTOR);
  262. if(uriSelectorValue == V_FEEDBACK) {
  263. requestGroup->setURISelector
  264. (SharedHandle<URISelector>(new FeedbackURISelector(_serverStatMan)));
  265. } else if(uriSelectorValue == V_INORDER) {
  266. requestGroup->setURISelector
  267. (SharedHandle<URISelector>(new InOrderURISelector()));
  268. } else if(uriSelectorValue == V_ADAPTIVE) {
  269. requestGroup->setURISelector
  270. (SharedHandle<URISelector>(new AdaptiveURISelector(_serverStatMan,
  271. requestGroup.get())));
  272. }
  273. }
  274. static void createInitialCommand(const SharedHandle<RequestGroup>& requestGroup,
  275. std::deque<Command*>& commands,
  276. DownloadEngine* e,
  277. bool useHead)
  278. {
  279. requestGroup->createInitialCommand(commands, e,
  280. useHead ?
  281. Request::METHOD_HEAD :
  282. Request::METHOD_GET);
  283. }
  284. void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e)
  285. {
  286. RequestGroups temp;
  287. removeStoppedGroup(e);
  288. unsigned int count = 0;
  289. for(int num = _maxSimultaneousDownloads-_requestGroups.size();
  290. num > 0 && !_reservedGroups.empty(); --num) {
  291. RequestGroupHandle groupToAdd = _reservedGroups.front();
  292. _reservedGroups.pop_front();
  293. try {
  294. if(!groupToAdd->isDependencyResolved()) {
  295. temp.push_back(groupToAdd);
  296. continue;
  297. }
  298. configureRequestGroup(groupToAdd);
  299. Commands commands;
  300. createInitialCommand(groupToAdd, commands, e,
  301. _option->getAsBool(PREF_USE_HEAD)||
  302. _option->getAsBool(PREF_DRY_RUN));
  303. _requestGroups.push_back(groupToAdd);
  304. ++count;
  305. e->addCommand(commands);
  306. } catch(RecoverableException& ex) {
  307. _logger->error(EX_EXCEPTION_CAUGHT, ex);
  308. _downloadResults.push_back(groupToAdd->createDownloadResult());
  309. }
  310. }
  311. _reservedGroups.insert(_reservedGroups.begin(), temp.begin(), temp.end());
  312. if(count > 0) {
  313. e->setNoWait(true);
  314. _logger->debug("%d RequestGroup(s) added.", count);
  315. }
  316. }
  317. void RequestGroupMan::getInitialCommands(std::deque<Command*>& commands,
  318. DownloadEngine* e)
  319. {
  320. for(RequestGroups::iterator itr = _requestGroups.begin();
  321. itr != _requestGroups.end();) {
  322. try {
  323. if((*itr)->isDependencyResolved()) {
  324. configureRequestGroup(*itr);
  325. createInitialCommand(*itr, commands, e,
  326. _option->getAsBool(PREF_USE_HEAD));
  327. ++itr;
  328. } else {
  329. _reservedGroups.push_front((*itr));
  330. itr = _requestGroups.erase(itr);
  331. }
  332. } catch(RecoverableException& e) {
  333. _logger->error(EX_EXCEPTION_CAUGHT, e);
  334. _downloadResults.push_back((*itr)->createDownloadResult());
  335. itr = _requestGroups.erase(itr);
  336. }
  337. }
  338. }
  339. void RequestGroupMan::save()
  340. {
  341. for(RequestGroups::iterator itr = _requestGroups.begin();
  342. itr != _requestGroups.end(); ++itr) {
  343. if((*itr)->allDownloadFinished()) {
  344. (*itr)->getProgressInfoFile()->removeFile();
  345. } else {
  346. try {
  347. (*itr)->getProgressInfoFile()->save();
  348. } catch(RecoverableException& e) {
  349. _logger->error(EX_EXCEPTION_CAUGHT, e);
  350. }
  351. }
  352. }
  353. }
  354. void RequestGroupMan::closeFile()
  355. {
  356. for(RequestGroups::iterator itr = _requestGroups.begin();
  357. itr != _requestGroups.end(); ++itr) {
  358. (*itr)->closeFile();
  359. }
  360. }
  361. RequestGroupMan::DownloadStat RequestGroupMan::getDownloadStat() const
  362. {
  363. size_t finished = 0;
  364. size_t error = 0;
  365. size_t inprogress = 0;
  366. DownloadResult::RESULT lastError = DownloadResult::FINISHED;
  367. for(std::deque<SharedHandle<DownloadResult> >::const_iterator itr = _downloadResults.begin();
  368. itr != _downloadResults.end(); ++itr) {
  369. if((*itr)->result == DownloadResult::FINISHED) {
  370. ++finished;
  371. } else {
  372. ++error;
  373. lastError = (*itr)->result;
  374. }
  375. }
  376. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  377. itr != _requestGroups.end(); ++itr) {
  378. DownloadResultHandle result = (*itr)->createDownloadResult();
  379. if(result->result == DownloadResult::FINISHED) {
  380. ++finished;
  381. } else {
  382. ++inprogress;
  383. }
  384. }
  385. return DownloadStat(finished, error, inprogress, _reservedGroups.size(),
  386. lastError);
  387. }
  388. void RequestGroupMan::showDownloadResults(std::ostream& o) const
  389. {
  390. static const std::string MARK_OK("OK");
  391. static const std::string MARK_ERR("ERR");
  392. static const std::string MARK_INPR("INPR");
  393. // Download Results:
  394. // idx|stat|path/length
  395. // ===+====+=======================================================================
  396. o << "\n"
  397. <<_("Download Results:") << "\n"
  398. << "gid|stat|avg speed |path/URI" << "\n"
  399. << "===+====+===========+==========================================================" << "\n";
  400. int ok = 0;
  401. int err = 0;
  402. int inpr = 0;
  403. for(std::deque<SharedHandle<DownloadResult> >::const_iterator itr = _downloadResults.begin();
  404. itr != _downloadResults.end(); ++itr) {
  405. std::string status;
  406. if((*itr)->result == DownloadResult::FINISHED) {
  407. status = MARK_OK;
  408. ++ok;
  409. } else {
  410. status = MARK_ERR;
  411. ++err;
  412. }
  413. o << formatDownloadResult(status, *itr) << "\n";
  414. }
  415. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  416. itr != _requestGroups.end(); ++itr) {
  417. DownloadResultHandle result = (*itr)->createDownloadResult();
  418. std::string status;
  419. if(result->result == DownloadResult::FINISHED) {
  420. status = MARK_OK;
  421. ++ok;
  422. } else {
  423. // Since this RequestGroup is not processed by ProcessStoppedRequestGroup,
  424. // its download stop time is not reseted.
  425. // Reset download stop time and assign sessionTime here.
  426. (*itr)->getDownloadContext()->resetDownloadStopTime();
  427. result->sessionTime =
  428. (*itr)->getDownloadContext()->calculateSessionTime();
  429. status = MARK_INPR;
  430. ++inpr;
  431. }
  432. o << formatDownloadResult(status, result) << "\n";
  433. }
  434. if(ok > 0 || err > 0 || inpr > 0) {
  435. o << "\n"
  436. << _("Status Legend:") << "\n";
  437. if(ok > 0) {
  438. o << " (OK):download completed.";
  439. }
  440. if(err > 0) {
  441. o << "(ERR):error occurred.";
  442. }
  443. if(inpr > 0) {
  444. o << "(INPR):download in-progress.";
  445. }
  446. o << "\n";
  447. }
  448. }
  449. std::string RequestGroupMan::formatDownloadResult(const std::string& status, const DownloadResultHandle& downloadResult) const
  450. {
  451. std::stringstream o;
  452. o << std::setw(3) << downloadResult->gid << "|"
  453. << std::setw(4) << status << "|"
  454. << std::setw(11);
  455. if(downloadResult->sessionTime > 0) {
  456. o << Util::abbrevSize
  457. (downloadResult->sessionDownloadLength*1000/downloadResult->sessionTime)+
  458. "B/s";
  459. } else {
  460. o << "n/a";
  461. }
  462. o << "|";
  463. if(downloadResult->result == DownloadResult::FINISHED) {
  464. o << downloadResult->filePath;
  465. } else {
  466. if(downloadResult->numUri == 0) {
  467. if(downloadResult->filePath.empty()) {
  468. o << "n/a";
  469. } else {
  470. o << downloadResult->filePath;
  471. }
  472. } else {
  473. o << downloadResult->uri;
  474. if(downloadResult->numUri > 1) {
  475. o << " (" << downloadResult->numUri-1 << "more)";
  476. }
  477. }
  478. }
  479. return o.str();
  480. }
  481. template<typename StringInputIterator, typename FileEntryInputIterator>
  482. static bool sameFilePathExists(StringInputIterator sfirst,
  483. StringInputIterator slast,
  484. FileEntryInputIterator ffirst,
  485. FileEntryInputIterator flast)
  486. {
  487. for(; ffirst != flast; ++ffirst) {
  488. if(std::binary_search(sfirst, slast, (*ffirst)->getPath())) {
  489. return true;
  490. }
  491. }
  492. return false;
  493. }
  494. bool RequestGroupMan::isSameFileBeingDownloaded(RequestGroup* requestGroup) const
  495. {
  496. // TODO it may be good to use dedicated method rather than use
  497. // isPreLocalFileCheckEnabled
  498. if(!requestGroup->isPreLocalFileCheckEnabled()) {
  499. return false;
  500. }
  501. std::deque<std::string> files;
  502. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  503. itr != _requestGroups.end(); ++itr) {
  504. if((*itr).get() != requestGroup) {
  505. std::deque<SharedHandle<FileEntry> > entries =
  506. (*itr)->getDownloadContext()->getFileEntries();
  507. std::transform(entries.begin(), entries.end(),
  508. std::back_inserter(files),
  509. mem_fun_sh(&FileEntry::getPath));
  510. }
  511. }
  512. std::sort(files.begin(), files.end());
  513. std::deque<SharedHandle<FileEntry> > entries =
  514. requestGroup->getDownloadContext()->getFileEntries();
  515. return sameFilePathExists(files.begin(), files.end(),
  516. entries.begin(), entries.end());
  517. }
  518. void RequestGroupMan::halt()
  519. {
  520. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  521. itr != _requestGroups.end(); ++itr) {
  522. (*itr)->setHaltRequested(true);
  523. }
  524. }
  525. void RequestGroupMan::forceHalt()
  526. {
  527. for(RequestGroups::const_iterator itr = _requestGroups.begin();
  528. itr != _requestGroups.end(); ++itr) {
  529. (*itr)->setForceHaltRequested(true);
  530. }
  531. }
  532. TransferStat RequestGroupMan::calculateStat()
  533. {
  534. TransferStat s;
  535. for(std::deque<SharedHandle<RequestGroup> >::const_iterator i =
  536. _requestGroups.begin(); i != _requestGroups.end(); ++i) {
  537. s += (*i)->calculateStat();
  538. }
  539. return s;
  540. }
  541. const std::deque<SharedHandle<DownloadResult> >&
  542. RequestGroupMan::getDownloadResults() const
  543. {
  544. return _downloadResults;
  545. }
  546. SharedHandle<ServerStat>
  547. RequestGroupMan::findServerStat(const std::string& hostname,
  548. const std::string& protocol) const
  549. {
  550. return _serverStatMan->find(hostname, protocol);
  551. }
  552. SharedHandle<ServerStat>
  553. RequestGroupMan::getOrCreateServerStat(const std::string& hostname,
  554. const std::string& protocol)
  555. {
  556. SharedHandle<ServerStat> ss = findServerStat(hostname, protocol);
  557. if(ss.isNull()) {
  558. ss.reset(new ServerStat(hostname, protocol));
  559. addServerStat(ss);
  560. }
  561. return ss;
  562. }
  563. bool RequestGroupMan::addServerStat(const SharedHandle<ServerStat>& serverStat)
  564. {
  565. return _serverStatMan->add(serverStat);
  566. }
  567. bool RequestGroupMan::loadServerStat(const std::string& filename)
  568. {
  569. std::ifstream in(filename.c_str(), std::ios::binary);
  570. if(!in) {
  571. _logger->error(MSG_OPENING_READABLE_SERVER_STAT_FILE_FAILED, filename.c_str());
  572. return false;
  573. }
  574. if(_serverStatMan->load(in)) {
  575. _logger->notice(MSG_SERVER_STAT_LOADED, filename.c_str());
  576. return true;
  577. } else {
  578. _logger->error(MSG_READING_SERVER_STAT_FILE_FAILED, filename.c_str());
  579. return false;
  580. }
  581. }
  582. bool RequestGroupMan::saveServerStat(const std::string& filename) const
  583. {
  584. std::string tempfile = filename+"__temp";
  585. std::ofstream out(tempfile.c_str(), std::ios::binary);
  586. if(!out) {
  587. _logger->error(MSG_OPENING_WRITABLE_SERVER_STAT_FILE_FAILED,
  588. tempfile.c_str());
  589. return false;
  590. }
  591. if (_serverStatMan->save(out)) {
  592. out.close();
  593. if (File(tempfile).renameTo(filename)) {
  594. _logger->notice(MSG_SERVER_STAT_SAVED, filename.c_str());
  595. return true;
  596. }
  597. }
  598. _logger->error(MSG_WRITING_SERVER_STAT_FILE_FAILED, filename.c_str());
  599. return false;
  600. }
  601. void RequestGroupMan::removeStaleServerStat(time_t timeout)
  602. {
  603. _serverStatMan->removeStaleServerStat(timeout);
  604. }
  605. bool RequestGroupMan::doesOverallDownloadSpeedExceed()
  606. {
  607. return _maxOverallDownloadSpeedLimit > 0 &&
  608. _maxOverallDownloadSpeedLimit < calculateStat().getDownloadSpeed();
  609. }
  610. bool RequestGroupMan::doesOverallUploadSpeedExceed()
  611. {
  612. return _maxOverallUploadSpeedLimit > 0 &&
  613. _maxOverallUploadSpeedLimit < calculateStat().getUploadSpeed();
  614. }
  615. } // namespace aria2