From bd7849199c45be2207f9145574a8586db36c893d Mon Sep 17 00:00:00 2001 From: rakshasa Date: Wed, 2 Feb 2005 21:51:40 +0000 Subject: [PATCH] Listing peers. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@252 e378c898-3ddf-0310-93e7-cc216c733640 --- src/core/curl_get.h | 3 + src/core/curl_stack.cc | 4 +- src/core/curl_stack.h | 7 +- src/core/poll.cc | 4 +- src/core/poll.h | 3 +- src/display/Makefile.am | 4 +- src/display/window_download.cc | 20 ----- src/display/window_download.h | 25 ------ src/display/window_download_list.cc | 4 +- src/display/window_download_list.h | 16 ++-- src/display/window_peer_list.cc | 116 ++++++++++++++++++++++++++++ src/display/window_peer_list.h | 37 +++++++++ src/main.cc | 4 +- 13 files changed, 182 insertions(+), 65 deletions(-) delete mode 100644 src/display/window_download.cc delete mode 100644 src/display/window_download.h create mode 100644 src/display/window_peer_list.cc create mode 100644 src/display/window_peer_list.h diff --git a/src/core/curl_get.h b/src/core/curl_get.h index 03736d5e..ac8b810b 100644 --- a/src/core/curl_get.h +++ b/src/core/curl_get.h @@ -43,6 +43,9 @@ class CurlGet : public torrent::Http { void perform(CURLMsg* msg); private: + CurlGet(const CurlGet&); + void operator = (const CurlGet&); + friend size_t curl_get_receive_write(void* data, size_t size, size_t nmemb, void* handle); std::string m_url; diff --git a/src/core/curl_stack.cc b/src/core/curl_stack.cc index 3f6f5c51..9eb2bb28 100644 --- a/src/core/curl_stack.cc +++ b/src/core/curl_stack.cc @@ -110,12 +110,12 @@ CurlStack::remove_get(CurlGet* get) { } void -CurlStack::global_init() { +CurlStack::init() { curl_global_init(CURL_GLOBAL_ALL); } void -CurlStack::global_cleanup() { +CurlStack::cleanup() { curl_global_cleanup(); } diff --git a/src/core/curl_stack.h b/src/core/curl_stack.h index 640edf0e..91459543 100644 --- a/src/core/curl_stack.h +++ b/src/core/curl_stack.h @@ -22,14 +22,17 @@ class CurlStack { // TODO: Set fd_set's only once? void fdset(fd_set* readfds, fd_set* writefds, fd_set* exceptfds, int* maxFd); - static void global_init(); - static void global_cleanup(); + static void init(); + static void cleanup(); protected: void add_get(CurlGet* get); void remove_get(CurlGet* get); private: + CurlStack(const CurlStack&); + void operator = (const CurlStack&); + void* m_handle; int m_size; diff --git a/src/core/poll.cc b/src/core/poll.cc index 2f7fd96a..7b38b941 100644 --- a/src/core/poll.cc +++ b/src/core/poll.cc @@ -33,10 +33,10 @@ Poll::poll() { timeval timeout = {t / 1000000, t % 1000000}; - m_maxFd = select(m_maxFd, &m_readSet, &m_writeSet, &m_exceptSet, &timeout); + m_maxFd = select(m_maxFd + 1, &m_readSet, &m_writeSet, &m_exceptSet, &timeout); if (m_maxFd >= 0) - return; + work(); else if (errno == EINTR) m_slotSelectInterrupted(); diff --git a/src/core/poll.h b/src/core/poll.h index 383f1a33..8a760eb6 100644 --- a/src/core/poll.h +++ b/src/core/poll.h @@ -14,7 +14,6 @@ public: typedef sigc::slot1 SlotInt; void poll(); - void work(); void register_http(); @@ -22,6 +21,8 @@ public: void slot_select_interrupted(Slot s) { m_slotSelectInterrupted = s; } private: + void work(); + SlotInt m_slotReadStdin; Slot m_slotSelectInterrupted; diff --git a/src/display/Makefile.am b/src/display/Makefile.am index 1f12c7a5..217518e8 100644 --- a/src/display/Makefile.am +++ b/src/display/Makefile.am @@ -7,10 +7,10 @@ libsub_display_a_SOURCES = \ manager.h \ window.cc \ window.h \ - window_download.cc \ - window_download.h \ window_download_list.cc \ window_download_list.h \ + window_peer_list.cc \ + window_peer_list.h \ window_statusbar.cc \ window_statusbar.h \ window_title.cc \ diff --git a/src/display/window_download.cc b/src/display/window_download.cc deleted file mode 100644 index 7b873757..00000000 --- a/src/display/window_download.cc +++ /dev/null @@ -1,20 +0,0 @@ -#include "config.h" - -#include "canvas.h" -#include "window_download.h" - -namespace display { - -WindowDownload::WindowDownload(DPtr d) : - Window(new Canvas, true), - m_download(d) { -} - -void -WindowDownload::redraw() { - m_canvas->erase(); - - m_canvas->print(0, 0, "Download: %s", m_download->get_download().get_name().c_str()); -} - -} diff --git a/src/display/window_download.h b/src/display/window_download.h deleted file mode 100644 index b6085831..00000000 --- a/src/display/window_download.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef RTORRENT_DISPLAY_WINDOW_DOWNLOAD_H -#define RTORRENT_DISPLAY_WINDOW_DOWNLOAD_H - -#include "window.h" -#include "core/download_list.h" - -namespace display { - -class WindowDownload : public Window { -public: - typedef core::DownloadList::iterator DPtr; - - WindowDownload(DPtr d); - - DPtr get_download() { return m_download; } - - virtual void redraw(); - -private: - DPtr m_download; -}; - -} - -#endif diff --git a/src/display/window_download_list.cc b/src/display/window_download_list.cc index 82276c91..ed89cecc 100644 --- a/src/display/window_download_list.cc +++ b/src/display/window_download_list.cc @@ -5,7 +5,7 @@ namespace display { -WindowDownloadList::WindowDownloadList(List* d) : +WindowDownloadList::WindowDownloadList(DList* d) : Window(new Canvas, true), m_downloads(d) { @@ -22,7 +22,7 @@ WindowDownloadList::redraw() { // Remember to check for end of screen too. - for (List::iterator itr = m_downloads->begin(); itr != m_downloads->end(); ++itr) { + for (DList::iterator itr = m_downloads->begin(); itr != m_downloads->end(); ++itr) { m_canvas->print(0, pos++, "%c %s", itr == m_focus ? '*' : ' ', itr->get_download().get_name().c_str()); diff --git a/src/display/window_download_list.h b/src/display/window_download_list.h index 7137a530..f65f8a06 100644 --- a/src/display/window_download_list.h +++ b/src/display/window_download_list.h @@ -8,20 +8,20 @@ namespace display { class WindowDownloadList : public Window { public: - typedef core::DownloadList List; + typedef core::DownloadList DList; - WindowDownloadList(List* d); + WindowDownloadList(DList* d); - List* get_list() { return m_downloads; } + DList& get_list() { return *m_downloads; } - List::iterator get_focus() { return m_focus; } - void set_focus(core::DownloadList::iterator itr) { m_focus = itr; } + DList::iterator get_focus() { return m_focus; } + void set_focus(DList::iterator itr) { m_focus = itr; } - virtual void redraw(); + virtual void redraw(); private: - List* m_downloads; - List::iterator m_focus; + DList* m_downloads; + DList::iterator m_focus; }; } diff --git a/src/display/window_peer_list.cc b/src/display/window_peer_list.cc new file mode 100644 index 00000000..093c623e --- /dev/null +++ b/src/display/window_peer_list.cc @@ -0,0 +1,116 @@ +#include "config.h" + +#include + +#include "canvas.h" +#include "window_peer_list.h" + +namespace display { + +WindowPeerList::WindowPeerList(PList* l) : + Window(new Canvas, true), + m_list(l), + m_focus(l->end()) { +} + +void +WindowPeerList::redraw() { + m_canvas->erase(); + + int x = 2; + int y = 0; + + m_canvas->print(x, y, "DNS"); x += 16; + m_canvas->print(x, y, "UP"); x += 7; + m_canvas->print(x, y, "DOWN"); x += 7; + m_canvas->print(x, y, "PEER"); x += 7; + m_canvas->print(x, y, "RE/LO"); x += 7; + m_canvas->print(x, y, "QS"); x += 6; + m_canvas->print(x, y, "DONE"); x += 6; + m_canvas->print(x, y, "REQ"); x += 6; + m_canvas->print(x, y, "SNUB"); + + ++y; + + if (m_list->empty()) + return; + + PList::iterator itr, last; + + 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 (itr != m_list->begin()) { + --itr; + ++i; + } + + if (last != m_list->end() && i < m_canvas->get_height() - y) { + ++last; + ++i; + } + } + + } else { + itr = m_list->begin(); + last = m_list->end(); + } + + uint32_t chunksTotal = m_slotChunksTotal(); + + if (chunksTotal <= 0) + throw std::logic_error("WindowPeerList::redraw() m_slotChunksTotal() returned invalid value"); + + while (itr != m_list->end() && y < m_canvas->get_height()) { + x = 0; + + m_canvas->print(x, y, "%c %s", + itr == m_focus ? '*' : ' ', + itr->get_dns().c_str()); + x += 18; + + m_canvas->print(x, y, "%.1f", + (double)itr->get_rate_up() / 1024); + x += 7; + + m_canvas->print(x, y, "%.1f", + (double)itr->get_rate_down() / 1024); + x += 7; + + m_canvas->print(x, y, "%.1f", + (double)itr->get_rate_peer() / 1024); + x += 7; + + m_canvas->print(x, y, "%c%c/%c%c%c", + itr->get_remote_choked() ? 'c' : 'u', + itr->get_remote_interested() ? 'i' : 'n', + itr->get_local_choked() ? 'c' : 'u', + itr->get_local_interested() ? 'i' : 'n', + itr->get_choke_delayed() ? 'd' : ' '); + x += 7; + + m_canvas->print(x, y, "%i/%i", + itr->get_outgoing_queue_size(), + itr->get_incoming_queue_size()); + x += 6; + + m_canvas->print(x, y, "%3i", (itr->get_chunks_done() * 100) / chunksTotal); + x += 6; + + if (itr->get_incoming_queue_size()) + m_canvas->print(x, y, "%i", + itr->get_incoming_index(0)); + + x += 6; + + if (itr->get_snubbed()) + m_canvas->print(x, y, "*"); + + ++y; + ++itr; + } +} + +} diff --git a/src/display/window_peer_list.h b/src/display/window_peer_list.h new file mode 100644 index 00000000..e33fcfa7 --- /dev/null +++ b/src/display/window_peer_list.h @@ -0,0 +1,37 @@ +#ifndef RTORRENT_DISPLAY_PEER_LIST_H +#define RTORRENT_DISPLAY_PEER_LIST_H + +#include +#include +#include + +#include "window.h" + +namespace display { + +class WindowPeerList : public Window { +public: + typedef std::list PList; + typedef sigc::slot0 SlotChunksTotal; + + WindowPeerList(PList* l); + + PList& get_list() { return *m_list; } + + PList::iterator get_focus() { return m_focus; } + void set_focus(PList::iterator itr) { m_focus = itr; } + + void slot_chunks_total(SlotChunksTotal s) { m_slotChunksTotal = s; } + + virtual void redraw(); + +private: + PList* m_list; + PList::iterator m_focus; + + SlotChunksTotal m_slotChunksTotal; +}; + +} + +#endif diff --git a/src/main.cc b/src/main.cc index 4519d26d..30ad366a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -9,6 +9,7 @@ #include "display/window_statusbar.h" #include "core/poll.h" +#include "core/curl_stack.h" #include "core/download_list.h" #include "ui/control.h" @@ -45,6 +46,7 @@ main(int argc, char** argv) { try { display::Canvas::init(); + core::CurlStack::init(); ui::Control uiControl; ui::DownloadList uiDownloadList(&downloads, &uiControl); @@ -78,7 +80,6 @@ main(int argc, char** argv) { uiControl.get_display().do_update(); poll.poll(); - poll.work(); } display::Canvas::cleanup(); @@ -90,6 +91,7 @@ main(int argc, char** argv) { } torrent::cleanup(); + core::CurlStack::cleanup(); return 0; }