Add total bytes downloaded tracking.

This commit is contained in:
James Turner 2013-10-24 23:35:34 +01:00
parent ca79d99ec4
commit d896e71ae9
3 changed files with 26 additions and 0 deletions

View File

@ -87,6 +87,7 @@ public:
SGTimeStamp timeTransferSample; SGTimeStamp timeTransferSample;
unsigned int bytesTransferred; unsigned int bytesTransferred;
unsigned int lastTransferRate; unsigned int lastTransferRate;
uint64_t totalBytesDownloaded;
}; };
class Connection : public NetChat class Connection : public NetChat
@ -603,6 +604,7 @@ Client::Client() :
d->bytesTransferred = 0; d->bytesTransferred = 0;
d->lastTransferRate = 0; d->lastTransferRate = 0;
d->timeTransferSample.stamp(); d->timeTransferSample.stamp();
d->totalBytesDownloaded = 0;
setUserAgent("SimGear-" SG_STRINGIZE(SIMGEAR_VERSION)); setUserAgent("SimGear-" SG_STRINGIZE(SIMGEAR_VERSION));
} }
@ -784,6 +786,7 @@ bool Client::hasActiveRequests() const
void Client::receivedBytes(unsigned int count) void Client::receivedBytes(unsigned int count)
{ {
d->bytesTransferred += count; d->bytesTransferred += count;
d->totalBytesDownloaded += count;
} }
unsigned int Client::transferRateBytesPerSec() const unsigned int Client::transferRateBytesPerSec() const
@ -812,6 +815,11 @@ unsigned int Client::transferRateBytesPerSec() const
return smoothed; return smoothed;
} }
uint64_t Client::totalBytesDownloaded() const
{
return d->totalBytesDownloaded;
}
} // of namespace HTTP } // of namespace HTTP
} // of namespace simgear } // of namespace simgear

View File

@ -25,6 +25,7 @@
#define SG_HTTP_CLIENT_HXX #define SG_HTTP_CLIENT_HXX
#include <memory> // for std::auto_ptr #include <memory> // for std::auto_ptr
#include <stdint.h> // for uint_64t
#include <simgear/io/HTTPRequest.hxx> #include <simgear/io/HTTPRequest.hxx>
@ -73,6 +74,12 @@ public:
* suitable for user feedback and rough profiling, nothing more. * suitable for user feedback and rough profiling, nothing more.
*/ */
unsigned int transferRateBytesPerSec() const; unsigned int transferRateBytesPerSec() const;
/**
* total bytes downloaded by this HTTP client, for bandwidth usage
* monitoring
*/
uint64_t totalBytesDownloaded() const;
private: private:
void requestFinished(Connection* con); void requestFinished(Connection* con);

View File

@ -243,6 +243,9 @@ public:
volatile int _allowed_errors; volatile int _allowed_errors;
volatile int _cache_hits; volatile int _cache_hits;
volatile int _transfer_rate; volatile int _transfer_rate;
// kbytes, not bytes, because bytes might overflow 2^31
volatile int _total_kb_downloaded;
private: private:
virtual void run(); virtual void run();
@ -293,6 +296,7 @@ SGTerraSync::SvnThread::SvnThread() :
_allowed_errors(6), _allowed_errors(6),
_cache_hits(0), _cache_hits(0),
_transfer_rate(0), _transfer_rate(0),
_total_kb_downloaded(0),
_use_built_in(true), _use_built_in(true),
_is_dirty(false), _is_dirty(false),
_stop(false), _stop(false),
@ -609,6 +613,9 @@ void SGTerraSync::SvnThread::runInternal()
while (!_stop) { while (!_stop) {
_http.update(100); _http.update(100);
_transfer_rate = _http.transferRateBytesPerSec(); _transfer_rate = _http.transferRateBytesPerSec();
// convert from bytes to kbytes
_total_kb_downloaded = static_cast<int>(_http.totalBytesDownloaded() / 1024);
if (_stop) if (_stop)
break; break;
@ -835,6 +842,10 @@ void SGTerraSync::bind()
_tiedProperties.Tie( _terraRoot->getNode("cache-hits", true), (int*) &_svnThread->_cache_hits ); _tiedProperties.Tie( _terraRoot->getNode("cache-hits", true), (int*) &_svnThread->_cache_hits );
_tiedProperties.Tie( _terraRoot->getNode("transfer-rate-bytes-sec", true), (int*) &_svnThread->_transfer_rate ); _tiedProperties.Tie( _terraRoot->getNode("transfer-rate-bytes-sec", true), (int*) &_svnThread->_transfer_rate );
// use kbytes here because propety doesn't support 64-bit and we might conceivably
// download more than 2G in a single session
_tiedProperties.Tie( _terraRoot->getNode("downloaded-kbytes", true), (int*) &_svnThread->_total_kb_downloaded );
_terraRoot->getNode("busy", true)->setAttribute(SGPropertyNode::WRITE,false); _terraRoot->getNode("busy", true)->setAttribute(SGPropertyNode::WRITE,false);
_terraRoot->getNode("active", true)->setAttribute(SGPropertyNode::WRITE,false); _terraRoot->getNode("active", true)->setAttribute(SGPropertyNode::WRITE,false);
_terraRoot->getNode("update-count", true)->setAttribute(SGPropertyNode::WRITE,false); _terraRoot->getNode("update-count", true)->setAttribute(SGPropertyNode::WRITE,false);