From f7144a333fe8ce0d2b6a3bba1a5a29b6cb9f7a0a Mon Sep 17 00:00:00 2001 From: rakshasa Date: Tue, 8 Feb 2005 22:06:57 +0000 Subject: [PATCH] Adding some tracker msg related stuff, download status bar. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@262 e378c898-3ddf-0310-93e7-cc216c733640 --- src/core/Makefile.am | 1 + src/core/download.cc | 29 +++++++++++++++ src/core/download.h | 16 +++++++-- src/core/download_list.cc | 7 +++- src/core/download_list.h | 3 ++ src/display/Makefile.am | 2 ++ src/display/window_download_list.cc | 38 ++++++++++++++++---- src/display/window_download_statusbar.cc | 45 ++++++++++++++++++++++++ src/display/window_download_statusbar.h | 24 +++++++++++++ src/display/window_peer_info.cc | 5 ++- src/ui/download.cc | 36 ++++++++++++------- src/ui/download.h | 29 ++++++++------- 12 files changed, 197 insertions(+), 38 deletions(-) create mode 100644 src/core/download.cc create mode 100644 src/display/window_download_statusbar.cc create mode 100644 src/display/window_download_statusbar.h diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 6475469a..f45cd69c 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -5,6 +5,7 @@ libsub_core_a_SOURCES = \ curl_get.h \ curl_stack.cc \ curl_stack.h \ + download.cc \ download.h \ download_list.cc \ download_list.h \ diff --git a/src/core/download.cc b/src/core/download.cc new file mode 100644 index 00000000..b56d4319 --- /dev/null +++ b/src/core/download.cc @@ -0,0 +1,29 @@ +#include "config.h" + +#include "download.h" + +#include +#include + +namespace core { + +void +Download::set_download(torrent::Download d) { + m_download = d; + + m_connTrackerSucceded = m_download.signal_tracker_succeded(sigc::bind(sigc::mem_fun(*this, &Download::receive_tracker_msg), "^_^")); + m_connTrackerFailed = m_download.signal_tracker_failed(sigc::mem_fun(*this, &Download::receive_tracker_msg)); +} + +void +Download::release_download() { + m_connTrackerSucceded.disconnect(); + m_connTrackerFailed.disconnect(); +} + +void +Download::receive_tracker_msg(std::string msg) { + m_trackerMsg = msg; +} + +} diff --git a/src/core/download.h b/src/core/download.h index e9d80bff..560afef0 100644 --- a/src/core/download.h +++ b/src/core/download.h @@ -8,10 +8,13 @@ namespace core { class Download { public: - Download(torrent::Download d) : m_download(d) {} - - std::string get_hash() { return m_download.get_hash(); } + void set_download(torrent::Download d); + void release_download(); + torrent::Download& get_download() { return m_download; } + std::string get_hash() { return m_download.get_hash(); } + + const std::string& get_tracker_msg() { return m_trackerMsg; } void open() { m_download.open(); } void close() { m_download.close(); } @@ -23,7 +26,14 @@ public: bool operator == (const std::string& str) { return str == m_download.get_hash(); } private: + void receive_tracker_msg(std::string msg); + torrent::Download m_download; + + std::string m_trackerMsg; + + sigc::connection m_connTrackerSucceded; + sigc::connection m_connTrackerFailed; }; } diff --git a/src/core/download_list.cc b/src/core/download_list.cc index fb288b91..af0cd84a 100644 --- a/src/core/download_list.cc +++ b/src/core/download_list.cc @@ -11,8 +11,11 @@ namespace core { DownloadList::iterator DownloadList::create(std::istream& str) { - iterator itr = Base::insert(end(), torrent::download_create(str)); + torrent::Download d = torrent::download_create(str); + iterator itr = Base::insert(end(), Download()); + + itr->set_download(d); itr->get_download().signal_hash_done(sigc::bind(sigc::mem_fun(*this, &DownloadList::receive_hash_done), itr->get_hash())); @@ -21,6 +24,8 @@ DownloadList::create(std::istream& str) { void DownloadList::erase(iterator itr) { + itr->release_download(); + torrent::download_remove(itr->get_hash()); Base::erase(itr); diff --git a/src/core/download_list.h b/src/core/download_list.h index c44e778b..505c68d9 100644 --- a/src/core/download_list.h +++ b/src/core/download_list.h @@ -21,6 +21,9 @@ public: using Base::rbegin; using Base::rend; + using Base::empty; + using Base::size; + iterator create(std::istream& str); void erase(iterator itr); diff --git a/src/display/Makefile.am b/src/display/Makefile.am index bdd3ba03..319f6f71 100644 --- a/src/display/Makefile.am +++ b/src/display/Makefile.am @@ -7,6 +7,8 @@ libsub_display_a_SOURCES = \ manager.h \ window.cc \ window.h \ + window_download_statusbar.cc \ + window_download_statusbar.h \ window_download_list.cc \ window_download_list.h \ window_peer_info.cc \ diff --git a/src/display/window_download_list.cc b/src/display/window_download_list.cc index f44ca295..b2896eab 100644 --- a/src/display/window_download_list.cc +++ b/src/display/window_download_list.cc @@ -20,11 +20,35 @@ WindowDownloadList::redraw() { m_canvas->erase(); - int pos = 0; + if (m_list->empty()) + return; - // Remember to check for end of screen too. + DList::iterator itr, last; + + int pos = 0, y = 0; - for (DList::iterator itr = m_list->begin(); itr != m_list->end(); ++itr) { + if (*m_focus != m_list->end()) { + int i = 0; + itr = last = *m_focus; + + while (i < m_canvas->get_height() - y && !(itr == m_list->begin() && last == m_list->end())) { + if (last != m_list->end()) { + ++last; + i += 3; + } + + if (itr != m_list->begin() && i < m_canvas->get_height() - y) { + --itr; + i += 3; + } + } + + } else { + itr = m_list->begin(); + last = m_list->end(); + } + + while (itr != m_list->end() && y < m_canvas->get_height()) { m_canvas->print(0, pos++, "%c %s", itr == *m_focus ? '*' : ' ', itr->get_download().get_name().c_str()); @@ -46,11 +70,11 @@ WindowDownloadList::redraw() { (double)itr->get_download().get_rate_down() / 1024.0, (double)itr->get_download().get_bytes_up() / (double)(1 << 20)); - m_canvas->print(0, pos++, "%c Tracker: [%c:%i] %s", + m_canvas->print(0, pos++, "%c Tracker: %s", itr == *m_focus ? '*' : ' ', - itr->get_download().is_tracker_busy() ? 'C' : ' ', - (int)(itr->get_download().get_tracker_timeout() / 1000000), - ""); + itr->get_download().is_tracker_busy() ? "Connecting" : itr->get_tracker_msg().c_str()); + + ++itr; } } diff --git a/src/display/window_download_statusbar.cc b/src/display/window_download_statusbar.cc new file mode 100644 index 00000000..66a99e7d --- /dev/null +++ b/src/display/window_download_statusbar.cc @@ -0,0 +1,45 @@ +#include "config.h" + +#include "canvas.h" +#include "window_download_statusbar.h" + +#include "core/download.h" + +namespace display { + +WindowDownloadStatusbar::WindowDownloadStatusbar(core::Download* d) : + Window(new Canvas, false, 2), + m_download(d) { +} + +void +WindowDownloadStatusbar::redraw() { + if (Timer::cache() - m_lastDraw < 1000000) + return; + + m_lastDraw = Timer::cache(); + + m_canvas->erase(); + + if (m_download->get_download().get_chunks_done() != m_download->get_download().get_chunks_total() || !m_download->get_download().is_open()) + m_canvas->print(0, 0, "Torrent: %.1f / %.1f MiB Rate:%5.1f /%5.1f KiB Uploaded: %.1f MiB", + (double)m_download->get_download().get_bytes_done() / (double)(1 << 20), + (double)m_download->get_download().get_bytes_total() / (double)(1 << 20), + (double)m_download->get_download().get_rate_up() / 1024.0, + (double)m_download->get_download().get_rate_down() / 1024.0, + (double)m_download->get_download().get_bytes_up() / (double)(1 << 20)); + + else + m_canvas->print(0, 0, "Torrent: Done %.1f MiB Rate:%5.1f /%5.1f KiB Uploaded: %.1f MiB", + (double)m_download->get_download().get_bytes_total() / (double)(1 << 20), + (double)m_download->get_download().get_rate_up() / 1024.0, + (double)m_download->get_download().get_rate_down() / 1024.0, + (double)m_download->get_download().get_bytes_up() / (double)(1 << 20)); + + m_canvas->print(0, 1, "Tracker: [%c:%i] %s", + m_download->get_download().is_tracker_busy() ? 'C' : ' ', + (int)(m_download->get_download().get_tracker_timeout() / 1000000), + m_download->get_tracker_msg().c_str()); +} + +} diff --git a/src/display/window_download_statusbar.h b/src/display/window_download_statusbar.h new file mode 100644 index 00000000..9db6f4fe --- /dev/null +++ b/src/display/window_download_statusbar.h @@ -0,0 +1,24 @@ +#ifndef RTORRENT_WINDOW_DOWNLOAD_STATUSBAR_H +#define RTORRENT_WINDOW_DOWNLOAD_STATUSBAR_H + +#include "window.h" + +namespace core { + class Download; +} + +namespace display { + +class WindowDownloadStatusbar : public Window { +public: + WindowDownloadStatusbar(core::Download* d); + + virtual void redraw(); + +private: + core::Download* m_download; +}; + +} + +#endif diff --git a/src/display/window_peer_info.cc b/src/display/window_peer_info.cc index 428176fe..6c3dd13b 100644 --- a/src/display/window_peer_info.cc +++ b/src/display/window_peer_info.cc @@ -33,8 +33,11 @@ WindowPeerInfo::redraw() { m_download->get_download().get_chunks_total(), m_download->get_download().get_chunks_size()); - if (*m_focus == m_list->end()) + if (*m_focus == m_list->end()) { + m_canvas->print(0, y++, "No peer in focus"); + return; + } y++; diff --git a/src/ui/download.cc b/src/ui/download.cc index f4a2e001..d1d0f088 100644 --- a/src/ui/download.cc +++ b/src/ui/download.cc @@ -9,6 +9,7 @@ #include "display/window_title.h" #include "display/window_peer_list.h" #include "display/window_peer_info.h" +#include "display/window_download_statusbar.h" #include "display/window_statusbar.h" #include "control.h" @@ -19,9 +20,12 @@ namespace ui { Download::Download(DPtr d, Control* c) : m_download(d), m_state(DISPLAY_NONE), + + m_title(c->get_display().end()), m_window(c->get_display().end()), - m_title(new WTitle(d->get_download().get_name())), - m_status(new WStatus), + m_downloadStatus(c->get_display().end()), + m_status(c->get_display().end()), + m_control(c), m_bindings(new input::Bindings) { @@ -36,12 +40,11 @@ Download::Download(DPtr d, Control* c) : } Download::~Download() { + if (m_window != m_control->get_display().end()) + throw std::logic_error("ui::Download::~Download() called on an active object"); + m_connPeerConnected.disconnect(); m_connPeerDisconnected.disconnect(); - - delete m_title; - delete m_status; - delete m_bindings; } void @@ -49,10 +52,10 @@ Download::activate() { if (m_window != m_control->get_display().end()) throw std::logic_error("ui::Download::activate() called on an already activated object"); - m_window = m_control->get_display().insert(m_control->get_display().begin(), NULL); - - m_control->get_display().push_front(m_title); - m_control->get_display().push_back(m_status); + m_window = m_control->get_display().insert(m_control->get_display().begin(), NULL); + m_title = m_control->get_display().insert(m_control->get_display().begin(), new WTitle(m_download->get_download().get_name())); + m_downloadStatus = m_control->get_display().insert(m_control->get_display().end(), new WDownloadStatus(m_download)); + m_status = m_control->get_display().insert(m_control->get_display().end(), new WStatus); m_control->get_input().push_front(m_bindings); @@ -66,13 +69,18 @@ Download::disable() { disable_display(); + delete *m_title; + delete *m_downloadStatus; + delete *m_status; + m_control->get_display().erase(m_window); m_control->get_display().erase(m_title); + m_control->get_display().erase(m_downloadStatus); m_control->get_display().erase(m_status); - m_control->get_input().erase(m_bindings); + m_window = m_title = m_downloadStatus = m_status = m_control->get_display().end(); - m_window = m_control->get_display().end(); + m_control->get_input().erase(m_bindings); } void @@ -172,7 +180,7 @@ Download::receive_peer_disconnected(torrent::Peer p) { void Download::receive_throttle(int t) { - m_status->mark_dirty(); + (*m_status)->mark_dirty(); torrent::set(torrent::THROTTLE_ROOT_CONST_RATE, torrent::get(torrent::THROTTLE_ROOT_CONST_RATE) + t * 1024); } @@ -196,6 +204,8 @@ Download::bind_keys(input::Bindings* b) { (*b)['d'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_throttle), 50); (*b)['c'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_throttle), -50); + (*b)['t'] = sigc::bind(sigc::mem_fun(m_download->get_download(), &torrent::Download::set_tracker_timeout), 2 * 1000000); + (*b)[KEY_UP] = sigc::mem_fun(*this, &Download::receive_prev); (*b)[KEY_DOWN] = sigc::mem_fun(*this, &Download::receive_next); } diff --git a/src/ui/download.h b/src/ui/download.h index 3b3e982e..24c6f01c 100644 --- a/src/ui/download.h +++ b/src/ui/download.h @@ -10,6 +10,7 @@ namespace display { class WindowPeerInfo; class WindowPeerList; class WindowStatusbar; + class WindowDownloadStatusbar; } namespace core { @@ -22,22 +23,23 @@ class Control; class Download { public: + typedef display::Window Window; + typedef display::WindowPeerInfo WPeerInfo; + typedef display::WindowPeerList WPeerList; + typedef display::WindowTitle WTitle; + typedef display::WindowStatusbar WStatus; + typedef display::WindowDownloadStatusbar WDownloadStatus; + + typedef core::Download* DPtr; + typedef std::list PList; + typedef display::Manager::iterator MItr; + typedef enum { DISPLAY_NONE, DISPLAY_MAIN, DISPLAY_PEER } Display; - typedef display::Window Window; - typedef display::WindowPeerInfo WPeerInfo; - typedef display::WindowPeerList WPeerList; - typedef display::WindowTitle WTitle; - typedef display::WindowStatusbar WStatus; - - typedef core::Download* DPtr; - typedef std::list PList; - typedef display::Manager::iterator MItr; - // We own 'window'. Download(DPtr d, Control* c); ~Download(); @@ -73,10 +75,11 @@ private: PList::iterator m_focus; Display m_state; - MItr m_window; - WTitle* m_title; - WStatus* m_status; + MItr m_title; + MItr m_window; + MItr m_downloadStatus; + MItr m_status; Control* m_control; input::Bindings* m_bindings;