rtorrent: Polling http and torrent. Improved the download list.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@247 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2005-01-30 17:14:53 +00:00
parent b17dc37a3b
commit 3357b5caab
15 changed files with 212 additions and 116 deletions
+2 -2
View File
@@ -7,7 +7,7 @@ libsub_display_a_SOURCES = \
manager.h \
window.cc \
window.h \
window_downloads.cc \
window_downloads.h
window_download_list.cc \
window_download_list.h
INCLUDES = -I$(srcdir) -I$(srcdir)/.. -I$(top_srcdir)
+8
View File
@@ -35,6 +35,14 @@ public:
template <typename A1, typename A2>
void print(int x, int y, const char* str, A1 a1, A2 a2) { mvwprintw(m_window, y, x, str, a1, a2); }
template <typename A1, typename A2, typename A3>
void print(int x, int y, const char* str,
A1 a1, A2 a2, A3 a3) { mvwprintw(m_window, y, x, str, a1, a2, a3); }
template <typename A1, typename A2, typename A3, typename A4>
void print(int x, int y, const char* str,
A1 a1, A2 a2, A3 a3, A4 a4) { mvwprintw(m_window, y, x, str, a1, a2, a3, a4); }
void print_border(chtype ls, chtype rs,
chtype ts, chtype bs,
chtype tl, chtype tr,
+30
View File
@@ -0,0 +1,30 @@
#include "config.h"
#include "canvas.h"
#include "window_download_list.h"
#include "engine/download_list.h"
namespace display {
WindowDownloadList::WindowDownloadList(engine::DownloadList* d) :
Window(new Canvas, true),
m_downloads(d) {
}
void
WindowDownloadList::redraw() {
m_canvas->erase();
m_canvas->print_border(' ', ' ', '-', '-', ' ', ' ', ' ', ' ');
int pos = 1;
for (engine::DownloadList::iterator itr = m_downloads->begin(); itr != m_downloads->end(); ++itr, ++pos)
m_canvas->print(1, pos, "Download: %s Open: %c Tracker: %c Active: %c",
itr->get_download().get_name().c_str(),
itr->get_download().is_open() ? 'Y' : 'N',
itr->get_download().is_tracker_busy() ? 'Y' : 'N',
itr->get_download().is_active() ? 'Y' : 'N');
}
}
+24
View File
@@ -0,0 +1,24 @@
#ifndef RTORRENT_DISPLAY_WINDOW_DOWNLOAD_LIST_H
#define RTORRENT_DISPLAY_WINDOW_DOWNLOAD_LIST_H
#include "window.h"
namespace engine {
class DownloadList;
}
namespace display {
class WindowDownloadList : public Window {
public:
WindowDownloadList(engine::DownloadList* d);
virtual void redraw();
private:
engine::DownloadList* m_downloads;
};
}
#endif
-24
View File
@@ -1,24 +0,0 @@
#include "config.h"
#include "canvas.h"
#include "window_downloads.h"
namespace display {
WindowDownloads::WindowDownloads(engine::Downloads* d) :
Window(new Canvas, true),
m_downloads(d) {
}
void
WindowDownloads::redraw() {
m_canvas->erase();
m_canvas->print_border(' ', ' ', '-', '-', ' ', ' ', ' ', ' ');
int pos = 1;
for (engine::Downloads::iterator itr = m_downloads->begin(); itr != m_downloads->end(); ++itr, ++pos)
m_canvas->print(1, pos, "Download: %s", itr->get_name().c_str());
}
}
-21
View File
@@ -1,21 +0,0 @@
#ifndef RTORRENT_DISPLAY_WINDOW_DOWNLOADS_H
#define RTORRENT_DISPLAY_WINDOW_DOWNLOADS_H
#include "window.h"
#include "engine/downloads.h"
namespace display {
class WindowDownloads : public Window {
public:
WindowDownloads(engine::Downloads* d);
virtual void redraw();
private:
engine::Downloads* m_downloads;
};
}
#endif
+3 -2
View File
@@ -5,8 +5,9 @@ libsub_engine_a_SOURCES = \
curl_get.h \
curl_stack.cc \
curl_stack.h \
downloads.cc \
downloads.h \
download.h \
download_list.cc \
download_list.h \
poll.cc \
poll.h
+29
View File
@@ -0,0 +1,29 @@
#ifndef RTORRENT_ENGINE_DOWNLOAD_H
#define RTORRENT_ENGINE_DOWNLOAD_H
#include <sigc++/connection.h>
#include <torrent/download.h>
namespace engine {
class Download {
public:
Download(torrent::Download d) : m_download(d) {}
std::string get_hash() { return m_download.get_hash(); }
torrent::Download& get_download() { return m_download; }
void open() { m_download.open(); }
void close() { m_download.close(); }
void hash_check(bool resume = false) { m_download.hash_check(resume); }
bool operator == (const std::string& str) { return str == m_download.get_hash(); }
private:
torrent::Download m_download;
};
}
#endif
+39
View File
@@ -0,0 +1,39 @@
#include "config.h"
#include <algorithm>
#include <sigc++/bind.h>
#include <torrent/exceptions.h>
#include <torrent/torrent.h>
#include "download_list.h"
namespace engine {
DownloadList::iterator
DownloadList::create(std::istream& str) {
iterator itr = Base::insert(end(), torrent::download_create(str));
itr->get_download().signal_hash_done(sigc::bind(sigc::mem_fun(*this, &DownloadList::receive_hash_done),
itr->get_hash()));
return itr;
}
void
DownloadList::erase(iterator itr) {
torrent::download_remove(itr->get_hash());
Base::erase(itr);
}
void
DownloadList::receive_hash_done(const std::string& str) {
iterator itr = std::find(begin(), end(), str);
if (itr == end())
throw torrent::client_error("DownloadList received hash check done, but couldn't find the download");
itr->get_download().start();
}
}
+33
View File
@@ -0,0 +1,33 @@
#ifndef RTORRENT_ENGINE_DOWNLOAD_LIST_H
#define RTORRENT_ENGINE_DOWNLOAD_LIST_H
#include <iosfwd>
#include "download.h"
namespace engine {
class DownloadList : private std::list<Download> {
public:
typedef std::list<Download> Base;
using Base::iterator;
using Base::const_iterator;
using Base::reverse_iterator;
using Base::const_reverse_iterator;
using Base::begin;
using Base::end;
using Base::rbegin;
using Base::rend;
iterator create(std::istream& str);
void erase(iterator itr);
private:
void receive_hash_done(const std::string& str);
};
}
#endif
-23
View File
@@ -1,23 +0,0 @@
#include "config.h"
#include <torrent/torrent.h>
#include "downloads.h"
namespace engine {
void
Downloads::create(std::istream& str) {
torrent::Download d = torrent::download_create(str);
Base::push_back(d);
}
void
Downloads::erase(iterator itr) {
torrent::download_remove(itr->get_hash());
Base::erase(itr);
}
}
-29
View File
@@ -1,29 +0,0 @@
#ifndef RTORRENT_ENGINE_DOWNLOADS_H
#define RTORRENT_ENGINE_DOWNLOADS_H
#include <iosfwd>
#include <torrent/download.h>
namespace engine {
class Downloads : private std::list<torrent::Download> {
public:
typedef std::list<torrent::Download> Base;
using Base::iterator;
using Base::const_iterator;
using Base::reverse_iterator;
using Base::const_reverse_iterator;
using Base::begin;
using Base::end;
using Base::rbegin;
using Base::rend;
void create(std::istream& str);
void erase(iterator itr);
};
}
#endif
+25 -2
View File
@@ -2,8 +2,11 @@
#include <stdexcept>
#include <ncurses.h>
#include <sigc++/bind.h>
#include <torrent/torrent.h>
#include "poll.h"
#include "curl_get.h"
namespace engine {
@@ -13,12 +16,22 @@ Poll::poll() {
FD_ZERO(&m_writeSet);
FD_ZERO(&m_exceptSet);
m_maxFd = 1;
torrent::mark(&m_readSet, &m_writeSet, &m_exceptSet, &m_maxFd);
m_maxFd = std::max(m_maxFd, 1);
if (m_readStdin)
FD_SET(0, &m_readSet);
timeval timeout = {60, 0};
if (m_curlStack.is_busy())
m_curlStack.fdset(&m_readSet, &m_writeSet, &m_exceptSet, &m_maxFd);
uint64_t t = torrent::get(torrent::TIME_SELECT);
if (t > 10000000)
t = 10000000;
timeval timeout = {t / 1000000, t % 1000000};
m_maxFd = select(m_maxFd, &m_readSet, &m_writeSet, &m_exceptSet, &timeout);
@@ -34,6 +47,16 @@ Poll::work() {
while ((key = getch()) >= 0)
m_readStdin(key);
}
if (m_curlStack.is_busy())
m_curlStack.perform();
torrent::work(&m_readSet, &m_writeSet, &m_exceptSet, m_maxFd);
}
void
Poll::register_http() {
torrent::Http::set_factory(sigc::bind(sigc::ptr_fun(&engine::CurlGet::new_object), &m_curlStack));
}
}
+6
View File
@@ -4,6 +4,8 @@
#include <sys/select.h>
#include <sigc++/slot.h>
#include "curl_stack.h"
namespace engine {
class Poll {
@@ -17,6 +19,8 @@ public:
void poll();
void work();
void register_http();
void slot_read_stdin(SlotInt s) { m_readStdin = s; }
private:
@@ -27,6 +31,8 @@ private:
fd_set m_readSet;
fd_set m_writeSet;
fd_set m_exceptSet;
CurlStack m_curlStack;
};
}
+13 -13
View File
@@ -3,17 +3,14 @@
#include <iostream>
#include <fstream>
#include <torrent/torrent.h>
#include <torrent/http.h>
#include <sigc++/bind.h>
#include "display/canvas.h"
#include "display/manager.h"
#include "display/window_downloads.h"
#include "display/window_download_list.h"
#include "engine/poll.h"
#include "engine/curl_stack.h"
#include "engine/curl_get.h"
#include "engine/downloads.h"
#include "engine/download_list.h"
#include "input/bindings.h"
#include "input/manager.h"
@@ -21,29 +18,32 @@
int main(int argc, char** argv) {
try {
engine::Poll poll;
engine::Downloads downloads;
engine::CurlStack curlStack;
display::Canvas::init();
engine::Poll poll;
engine::DownloadList downloads;
display::Manager display;
input::Manager inputManager;
inputManager.push_back(new input::Bindings);
poll.slot_read_stdin(sigc::mem_fun(inputManager, &input::Manager::pressed));
poll.register_http();
display.push_back(new display::WindowDownloads(&downloads));
display.push_back(new display::WindowDownloadList(&downloads));
torrent::initialize();
torrent::listen_open(6880, 6999);
torrent::Http::set_factory(sigc::bind(sigc::ptr_fun(&engine::CurlGet::new_object), &curlStack));
for (int i = 1; i < argc; ++i) {
std::fstream f(argv[i], std::ios::in);
downloads.create(f);
engine::DownloadList::iterator itr = downloads.create(f);
itr->open();
itr->hash_check();
}
while (poll.is_running()) {