|
From: Yoda-JM <yo...@us...> - 2012-09-16 03:14:20
|
Author: Vincent Le Ligeour <yo...@us...>
Date: Sun Sep 16 05:12:07 2012 +0200
new: add new torrent information
Add the upload and download rate for the session and per torrent. Also
add the torrent size.
---
game/downloader.cc | 25 ++++++++++++++++++++++---
game/downloader.hh | 6 ++++++
game/screen_downloads.cc | 26 ++++++++++++++++++++++++--
3 files changed, 52 insertions(+), 5 deletions(-)
diff --git a/game/downloader.cc b/game/downloader.cc
index 1b5adb4..cc377a4 100644
--- a/game/downloader.cc
+++ b/game/downloader.cc
@@ -36,6 +36,8 @@ class Downloader::Impl {
std::vector<Torrent> m_torrents;
public:
+ int m_uploadRate;
+ int m_downloadRate;
Impl() try {
m_quit = false;
session_settings settings;
@@ -58,10 +60,14 @@ class Downloader::Impl {
std::deque<alert*> alerts;
s.pop_alerts(&alerts);
for(std::deque<alert*>::const_iterator ita = alerts.begin() ; ita != alerts.end(); ++ita) {
- std::clog << "torrent/error: " << (*ita)->what() << std::endl;
+ std::clog << "torrent/error: " << (*ita)->what() << ": " << (*ita)->message() << std::endl;
}
- // update torrent informations
+ // update session information
+ session_status ss = s.status();
+ m_uploadRate = ss.upload_rate;
+ m_downloadRate = ss.download_rate;
+ // update torrent information
std::vector<Torrent> torrents;
std::vector<torrent_handle> torrent_handles = s.get_torrents();
for(std::vector<torrent_handle>::const_iterator it = torrent_handles.begin() ; it != torrent_handles.end() ; ++it) {
@@ -75,11 +81,20 @@ class Downloader::Impl {
t.state = state_str[status.state];
}
t.progress = status.progress;
+ t.uploadRate = status.upload_rate;
+ t.downloadRate = status.download_rate;
t.sha1 = h.info_hash().to_string();
+ try {
+ torrent_info const& info = h.get_torrent_info();
+ t.size = info.total_size();
+ } catch(libtorrent_exception &ex) {
+ // do nothing, info are just not available
+ t.size = 0;
+ }
+ torrents.push_back(t);
std::string percent = boost::lexical_cast<std::string>(int(round(t.progress*100)));
std::clog << "torrent/debug: " << t.name << " (" << t.state << ":" << percent << "%)" << std::endl;
- torrents.push_back(t);
/*
// display files when metadata are here
try {
@@ -193,3 +208,7 @@ void Downloader::addTorrent(std::string url) { self->addTorrent(url); }
void Downloader::removeTorrent(std::string sha1) { self->removeTorrent(sha1); }
std::vector<Torrent> Downloader::getTorrents() const { return self->getTorrents(); };
+
+int Downloader::getUploadRate() const { return self->m_uploadRate; };
+
+int Downloader::getDownloadRate() const { return self->m_downloadRate; };
diff --git a/game/downloader.hh b/game/downloader.hh
index 1b0a0d0..ad301bc 100644
--- a/game/downloader.hh
+++ b/game/downloader.hh
@@ -1,5 +1,6 @@
#pragma once
+#include <boost/cstdint.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/noncopyable.hpp>
#include <vector>
@@ -10,6 +11,9 @@ struct Torrent {
std::string state;
std::string sha1;
float progress;
+ int uploadRate;
+ int downloadRate;
+ boost::int64_t size;
};
class Downloader : boost::noncopyable {
@@ -21,6 +25,8 @@ class Downloader : boost::noncopyable {
void addTorrent(std::string url);
void removeTorrent(std::string sha1);
std::vector<Torrent> getTorrents() const;
+ int getUploadRate() const;
+ int getDownloadRate() const;
private:
class Impl;
boost::scoped_ptr<Impl> self;
diff --git a/game/screen_downloads.cc b/game/screen_downloads.cc
index 93f5e87..53852af 100644
--- a/game/screen_downloads.cc
+++ b/game/screen_downloads.cc
@@ -6,6 +6,7 @@
#include "joystick.hh"
#include "theme.hh"
#include "i18n.hh"
+#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <boost/format.hpp>
@@ -44,6 +45,25 @@ void ScreenDownloads::manageEvent(SDL_Event event) {
}
}
+namespace {
+ std::string addSuffix(boost::int64_t val, std::string suffix = "") {
+ std::string unit = "b";
+ std::vector<std::string> modifier = boost::assign::list_of("")("k")("M")("H")("T")("P");
+ std::string chosenModifier = modifier.back();
+ for(std::vector<std::string>::const_iterator it = modifier.begin() ; it != modifier.end() ; ++it) {
+ if(val < 1024) {
+ chosenModifier = *it;
+ break;
+ } else {
+ val /= 1024;
+ }
+ }
+ std::ostringstream ret;
+ ret << boost::lexical_cast<std::string>(val) << " " << chosenModifier << unit << suffix;
+ return ret.str();
+ }
+}
+
void ScreenDownloads::draw() {
m_theme->bg.draw();
std::vector<Torrent> torrents = m_downloader.getTorrents();
@@ -59,7 +79,9 @@ void ScreenDownloads::draw() {
<< "Torrent " << (m_selectedTorrent+1) << "/" << torrents.size() << ": "
<< torrent.name
<< ", state=" << torrent.state
- << ", " << boost::lexical_cast<std::string>(int(round(torrent.progress*100))) << "%";
+ << ", " << boost::lexical_cast<std::string>(int(round(torrent.progress*100))) << "%"
+ << ", size: " << addSuffix(torrent.size)
+ << " (down: " << addSuffix(torrent.downloadRate,"/s") << ", up: " << addSuffix(torrent.uploadRate,"/s") << ")";
} else {
info << _("No torrent available");
}
@@ -69,7 +91,7 @@ void ScreenDownloads::draw() {
m_theme->comment.draw(info.str());
// Additional info
std::ostringstream message;
- message << boost::format(_("Use left/right keys to scoll accross the %1% torrent(s)")) % torrents.size();
+ message << boost::format(_("Use left/right keys to scoll accross the %1% torrent(s) (down: %2%, up: %3%)")) % torrents.size() % addSuffix(m_downloader.getDownloadRate(),"/s") % addSuffix(m_downloader.getUploadRate(),"/s");
m_theme->comment_bg.dimensions.middle().screenBottom(-0.01);
m_theme->comment_bg.draw();
m_theme->comment.dimensions.left(-0.48).screenBottom(-0.023);
|