Jelajahi Sumber

Simplified TransferStat struct

Tatsuhiro Tsujikawa 13 tahun lalu
induk
melakukan
21c3903af0

+ 4 - 3
src/ActivePeerConnectionCommand.cc

@@ -81,7 +81,7 @@ bool ActivePeerConnectionCommand::execute() {
   }
   if(checkPoint_.difference(global::wallclock()) >= interval_) {
     checkPoint_ = global::wallclock();
-    TransferStat tstat = requestGroup_->calculateStat();
+    NetStat& stat = requestGroup_->getDownloadContext()->getNetStat();
     const int maxDownloadLimit = requestGroup_->getMaxDownloadSpeedLimit();
     const int maxUploadLimit = requestGroup_->getMaxUploadSpeedLimit();
     int thresholdSpeed;
@@ -97,10 +97,11 @@ bool ActivePeerConnectionCommand::execute() {
     }
     if(// for seeder state
        (pieceStorage_->downloadFinished() && btRuntime_->lessThanMaxPeers() &&
-        (maxUploadLimit == 0 || tstat.getUploadSpeed() < maxUploadLimit*0.8)) ||
+        (maxUploadLimit == 0 ||
+         stat.calculateUploadSpeed() < maxUploadLimit*0.8)) ||
        // for leecher state
        (!pieceStorage_->downloadFinished() &&
-        (tstat.getDownloadSpeed() < thresholdSpeed ||
+        (stat.calculateDownloadSpeed() < thresholdSpeed ||
          btRuntime_->lessThanMinPeers()))) {
 
       int numConnection = 0;

+ 3 - 1
src/BtStopDownloadCommand.cc

@@ -42,6 +42,7 @@
 #include "util.h"
 #include "fmt.h"
 #include "DownloadEngine.h"
+#include "DownloadContext.h"
 
 namespace aria2 {
 
@@ -72,7 +73,8 @@ void BtStopDownloadCommand::preProcess()
 
 void BtStopDownloadCommand::process()
 {
-  if(requestGroup_->calculateStat().getDownloadSpeed() > 0) {
+  NetStat& stat = requestGroup_->getDownloadContext()->getNetStat();
+  if(stat.calculateDownloadSpeed() > 0) {
     checkPoint_ = global::wallclock();
   }
 }

+ 7 - 7
src/ConsoleStatCalc.cc

@@ -102,8 +102,8 @@ void printProgress
 {
   TransferStat stat = rg->calculateStat();
   int eta = 0;
-  if(rg->getTotalLength() > 0 && stat.getDownloadSpeed() > 0) {
-    eta = (rg->getTotalLength()-rg->getCompletedLength())/stat.getDownloadSpeed();
+  if(rg->getTotalLength() > 0 && stat.downloadSpeed > 0) {
+    eta = (rg->getTotalLength()-rg->getCompletedLength())/stat.downloadSpeed;
   }
 
   o << "["
@@ -116,7 +116,7 @@ void printProgress
     o << "SEEDING" << "(" << "ratio:";
     if(rg->getCompletedLength() > 0) {
       o << std::fixed << std::setprecision(1)
-        << ((stat.getAllTimeUploadLength()*10)/rg->getCompletedLength())/10.0;
+        << ((stat.allTimeUploadLength*10)/rg->getCompletedLength())/10.0;
     } else {
       o << "--";
     }
@@ -152,13 +152,13 @@ void printProgress
   if(!rg->downloadFinished()) {
     o << " "
       << "SPD:"
-      << sizeFormatter(stat.getDownloadSpeed()) << "Bs";
+      << sizeFormatter(stat.downloadSpeed) << "Bs";
   }
-  if(stat.getSessionUploadLength() > 0) {
+  if(stat.sessionUploadLength > 0) {
     o << " "
       << "UP:"
-      << sizeFormatter(stat.getUploadSpeed()) << "Bs"
-      << "(" << sizeFormatter(stat.getAllTimeUploadLength()) << "B)";
+      << sizeFormatter(stat.uploadSpeed) << "Bs"
+      << "(" << sizeFormatter(stat.allTimeUploadLength) << "B)";
   }
   if(eta > 0) {
     o << " "

+ 2 - 3
src/PeerReceiveHandshakeCommand.cc

@@ -125,8 +125,7 @@ bool PeerReceiveHandshakeCommand::executeInternal()
                    " Dropping connection.");
       return true;
     }
-    TransferStat tstat =
-      downloadContext->getOwnerRequestGroup()->calculateStat();
+    NetStat& stat = downloadContext->getNetStat();
     const int maxDownloadLimit =
       downloadContext->getOwnerRequestGroup()->getMaxDownloadSpeedLimit();
     int thresholdSpeed =
@@ -137,7 +136,7 @@ bool PeerReceiveHandshakeCommand::executeInternal()
     }
 
     if((!pieceStorage->downloadFinished() &&
-        tstat.getDownloadSpeed() < thresholdSpeed) ||
+        stat.calculateDownloadSpeed() < thresholdSpeed) ||
        btRuntime->lessThanMaxPeers()) {
       if(peerStorage->addPeer(getPeer())) {
         getPeer()->usedBy(getCuid());

+ 8 - 8
src/RequestGroup.cc

@@ -1154,7 +1154,7 @@ SharedHandle<DownloadResult> RequestGroup::createDownloadResult() const
   res->gid = gid_;
   res->fileEntries = downloadContext_->getFileEntries();
   res->inMemoryDownload = inMemoryDownload_;
-  res->sessionDownloadLength = st.getSessionDownloadLength();
+  res->sessionDownloadLength = st.sessionDownloadLength;
   res->sessionTime = downloadContext_->calculateSessionTime();
   res->result = downloadResult();
   res->followedBy = followedByGIDs_;
@@ -1163,7 +1163,7 @@ SharedHandle<DownloadResult> RequestGroup::createDownloadResult() const
   res->metadataInfo = metadataInfo_;
   res->totalLength = getTotalLength();
   res->completedLength = getCompletedLength();
-  res->uploadLength = st.getAllTimeUploadLength();
+  res->uploadLength = st.allTimeUploadLength;
   if(pieceStorage_) {
     if(pieceStorage_->getBitfieldLength() > 0) {
       res->bitfield.assign(pieceStorage_->getBitfield(),
@@ -1193,13 +1193,13 @@ void RequestGroup::reportDownloadFinished()
     TransferStat stat = calculateStat();
     int64_t completedLength = getCompletedLength();
     double shareRatio = completedLength == 0 ? 0.0 :
-      1.0*stat.getAllTimeUploadLength()/completedLength;
+      1.0*stat.allTimeUploadLength/completedLength;
     SharedHandle<TorrentAttribute> attrs =
       bittorrent::getTorrentAttrs(downloadContext_);
     if(!attrs->metadata.empty()) {
       A2_LOG_NOTICE(fmt(MSG_SHARE_RATIO_REPORT,
                         shareRatio,
-                        util::abbrevSize(stat.getAllTimeUploadLength()).c_str(),
+                        util::abbrevSize(stat.allTimeUploadLength).c_str(),
                         util::abbrevSize(completedLength).c_str()));
     }
   }
@@ -1254,14 +1254,14 @@ void RequestGroup::setTimeout(time_t timeout)
 
 bool RequestGroup::doesDownloadSpeedExceed()
 {
-  return maxDownloadSpeedLimit_ > 0 &&
-    maxDownloadSpeedLimit_ < calculateStat().getDownloadSpeed();
+  int spd = downloadContext_->getNetStat().calculateDownloadSpeed();
+  return maxDownloadSpeedLimit_ > 0 && maxDownloadSpeedLimit_ < spd;
 }
 
 bool RequestGroup::doesUploadSpeedExceed()
 {
-  return maxUploadSpeedLimit_ > 0 &&
-    maxUploadSpeedLimit_ < calculateStat().getUploadSpeed();
+  int spd = downloadContext_->getNetStat().calculateUploadSpeed();
+  return maxUploadSpeedLimit_ > 0 && maxUploadSpeedLimit_ < spd;
 }
 
 void RequestGroup::saveControlFile() const

+ 3 - 3
src/RpcMethodImpl.cc

@@ -657,14 +657,14 @@ void gatherProgressCommon
   }
   TransferStat stat = group->calculateStat();
   if(requested_key(keys, KEY_DOWNLOAD_SPEED)) {
-    entryDict->put(KEY_DOWNLOAD_SPEED, util::itos(stat.getDownloadSpeed()));
+    entryDict->put(KEY_DOWNLOAD_SPEED, util::itos(stat.downloadSpeed));
   }
   if(requested_key(keys, KEY_UPLOAD_SPEED)) {
-    entryDict->put(KEY_UPLOAD_SPEED, util::itos(stat.getUploadSpeed()));
+    entryDict->put(KEY_UPLOAD_SPEED, util::itos(stat.uploadSpeed));
   }
   if(requested_key(keys, KEY_UPLOAD_LENGTH)) {
     entryDict->put
-      (KEY_UPLOAD_LENGTH, util::itos(stat.getAllTimeUploadLength()));
+      (KEY_UPLOAD_LENGTH, util::itos(stat.allTimeUploadLength));
   }
   if(requested_key(keys, KEY_CONNECTIONS)) {
     entryDict->put(KEY_CONNECTIONS, util::itos(group->getNumConnection()));

+ 24 - 45
src/TransferStat.cc

@@ -2,7 +2,7 @@
 /*
  * aria2 - The high speed download utility
  *
- * Copyright (C) 2006 Tatsuhiro Tsujikawa
+ * Copyright (C) 2012 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
@@ -34,67 +34,46 @@
 /* copyright --> */
 #include "TransferStat.h"
 
+#include <algorithm>
+
 namespace aria2 {
 
 TransferStat operator+(const TransferStat& a, const TransferStat& b)
 {
-  TransferStat c;
-  c.downloadSpeed = a.downloadSpeed+b.downloadSpeed;
-  c.uploadSpeed = a.uploadSpeed+b.uploadSpeed;
-  c.sessionDownloadLength = a.sessionDownloadLength+b.sessionDownloadLength;
-  c.sessionUploadLength = a.sessionUploadLength+b.sessionUploadLength;
+  TransferStat c(a);
+  c += b;
   return c;
 }
 
 TransferStat operator-(const TransferStat& a, const TransferStat& b)
 {
-  TransferStat c;
-  if(a.downloadSpeed > b.downloadSpeed) {
-    c.downloadSpeed = a.downloadSpeed-b.downloadSpeed;
-  }
-  if(a.uploadSpeed > b.uploadSpeed) {
-    c.uploadSpeed = a.uploadSpeed-b.uploadSpeed;
-  }
-  if(a.sessionDownloadLength > b.sessionDownloadLength) {
-    c.sessionDownloadLength = a.sessionDownloadLength-b.sessionDownloadLength;
-  }
-  if(a.sessionUploadLength > b.sessionUploadLength) {
-    c.sessionUploadLength = a.sessionUploadLength-b.sessionUploadLength;
-  }
+  TransferStat c(a);
+  c -= b;
   return c;
 }
 
-TransferStat& TransferStat::operator+=(const TransferStat& stat)
+TransferStat& TransferStat::operator+=(const TransferStat& b)
 {
-  downloadSpeed += stat.downloadSpeed;
-  uploadSpeed += stat.uploadSpeed;
-  sessionDownloadLength += stat.sessionDownloadLength;
-  sessionUploadLength += stat.sessionUploadLength;
+  downloadSpeed += b.downloadSpeed;
+  uploadSpeed += b.uploadSpeed;
+  sessionDownloadLength += b.sessionDownloadLength;
+  sessionUploadLength += b.sessionUploadLength;
   return *this;
 }
 
-TransferStat& TransferStat::operator-=(const TransferStat& stat)
+TransferStat& TransferStat::operator-=(const TransferStat& b)
 {
-  if(downloadSpeed > stat.downloadSpeed) {
-    downloadSpeed -= stat.downloadSpeed;
-  } else {
-    downloadSpeed = 0;
-  }
-  if(uploadSpeed > stat.uploadSpeed) {
-    uploadSpeed -= stat.uploadSpeed;
-  } else {
-    uploadSpeed = 0;
-  }
-  if(sessionDownloadLength > stat.sessionDownloadLength) {
-    sessionDownloadLength -= stat.sessionDownloadLength;
-  } else {
-    sessionDownloadLength = 0;
-  }
-  if(sessionUploadLength > stat.sessionUploadLength) {
-    sessionUploadLength -= stat.sessionUploadLength;
-  } else {
-    sessionUploadLength = 0;
-  }
+  downloadSpeed -= b.downloadSpeed;
+  uploadSpeed -= b.uploadSpeed;
+  sessionDownloadLength -= b.sessionDownloadLength;
+  sessionUploadLength -= b.sessionUploadLength;
+
+  downloadSpeed = std::max(0, downloadSpeed);
+  uploadSpeed = std::max(0, uploadSpeed);
+  sessionDownloadLength = std::max(static_cast<int64_t>(0),
+                                   sessionDownloadLength);
+  sessionUploadLength = std::max(static_cast<int64_t>(0),
+                                 sessionUploadLength);
   return *this;
 }
 

+ 7 - 70
src/TransferStat.h

@@ -2,7 +2,7 @@
 /*
  * aria2 - The high speed download utility
  *
- * Copyright (C) 2006 Tatsuhiro Tsujikawa
+ * Copyright (C) 2012 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
@@ -36,93 +36,30 @@
 #define D_TRANSFER_STAT_H
 
 #include "common.h"
-#include <stdint.h>
 
 namespace aria2 {
 
-class TransferStat {
-public:
-  int downloadSpeed;
-  int uploadSpeed;
-  int64_t sessionDownloadLength;
-  int64_t sessionUploadLength;
-  int64_t allTimeUploadLength;
-
-  void copy(const TransferStat& stat)
-  {
-    downloadSpeed = stat.downloadSpeed;
-    uploadSpeed = stat.uploadSpeed;
-    sessionDownloadLength = stat.sessionDownloadLength;
-    sessionUploadLength = stat.sessionUploadLength;
-    allTimeUploadLength = stat.allTimeUploadLength;
-  }
-public:
+struct TransferStat {
   TransferStat():downloadSpeed(0), uploadSpeed(0),
                  sessionDownloadLength(0), sessionUploadLength(0),
                  allTimeUploadLength(0) {}
 
-  TransferStat(const TransferStat& stat)
-  {
-    copy(stat);
-  }
-
-  TransferStat& operator=(const TransferStat& stat)
-  {
-    if(this != &stat) {
-      copy(stat);
-    }
-    return *this;
-  }
-
-  friend TransferStat operator+(const TransferStat& a, const TransferStat& b);
-
-  friend TransferStat operator-(const TransferStat& a, const TransferStat& b);
-
   TransferStat& operator+=(const TransferStat& stat);
-
   TransferStat& operator-=(const TransferStat& stat);
 
-  int getDownloadSpeed() const {
-    return downloadSpeed;
-  }
-
-  void setDownloadSpeed(int s) { downloadSpeed = s; }
-
-  int getUploadSpeed() const {
-    return uploadSpeed;
-  }
-
-  void setUploadSpeed(int s) { uploadSpeed = s; }
-
+  int downloadSpeed;
+  int uploadSpeed;
   /**
    * Returns the number of bytes downloaded since the program started.
    * This is not the total number of bytes downloaded.
    */
-  int64_t getSessionDownloadLength() const {
-    return sessionDownloadLength;
-  }
-
-  void setSessionDownloadLength(int64_t s) { sessionDownloadLength = s; }
-
+  int64_t sessionDownloadLength;
   /**
    * Returns the number of bytes uploaded since the program started.
    * This is not the total number of bytes uploaded.
    */
-  int64_t getSessionUploadLength() const {
-    return sessionUploadLength;
-  }
-
-  void setSessionUploadLength(int64_t s) { sessionUploadLength = s; }
-
-  void setAllTimeUploadLength(int64_t s)
-  {
-    allTimeUploadLength = s;
-  }
-
-  int64_t getAllTimeUploadLength() const
-  {
-    return allTimeUploadLength;
-  }
+  int64_t sessionUploadLength;
+  int64_t allTimeUploadLength;
 };
 
 TransferStat operator+(const TransferStat& a, const TransferStat& b);