rtorrent: Moving towards working torrents.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@246 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2005-01-29 22:42:52 +00:00
parent 53f32e74a1
commit b17dc37a3b
16 changed files with 182 additions and 100 deletions
+5 -4
View File
@@ -35,18 +35,19 @@ AC_ARG_ENABLE(werror,
AC_SEARCH_LIBS(wbkgdset, ncurses curses,,echo "*** The ncurses library is required!";exit 1) AC_SEARCH_LIBS(wbkgdset, ncurses curses,,echo "*** The ncurses library is required!";exit 1)
PKG_CHECK_MODULES(STUFF, sigc++-2.0 libtorrent >= 0.4.8,
CXXFLAGS="$CXXFLAGS $STUFF_CFLAGS";
LIBS="$LIBS $STUFF_LIBS")
TORRENT_CHECK_CURL() TORRENT_CHECK_CURL()
TORRENT_OTFD() TORRENT_OTFD()
PKG_CHECK_MODULES(STUFF, sigc++-2.0 libtorrent >= 0.4.8,
CXXFLAGS="$CXXFLAGS $STUFF_CFLAGS $CURL_CFLAGS";
LIBS="$LIBS $STUFF_LIBS $CURL_LIBS")
AC_DEFINE(HAVE_CONFIG_H, 1, true if config.h was included) AC_DEFINE(HAVE_CONFIG_H, 1, true if config.h was included)
AC_OUTPUT([ AC_OUTPUT([
Makefile Makefile
src/Makefile src/Makefile
src/display/Makefile src/display/Makefile
src/engine/Makefile
src/input/Makefile src/input/Makefile
]) ])
+3
View File
@@ -17,6 +17,9 @@ AC_DEFUN([TORRENT_CHECK_CURL], [
if test x$ok != x0; then if test x$ok != x0; then
my_cv_curl_vers="$ver" my_cv_curl_vers="$ver"
AC_MSG_RESULT([$my_cv_curl_vers]) AC_MSG_RESULT([$my_cv_curl_vers])
CURL_CFLAGS=`curl-config --cflags`
CURL_LIBS=`curl-config --libs`
else else
AC_MSG_RESULT(FAILED) AC_MSG_RESULT(FAILED)
AC_MSG_ERROR([$ver is too old. Need version $check or higher.]) AC_MSG_ERROR([$ver is too old. Need version $check or higher.])
+3 -5
View File
@@ -1,16 +1,14 @@
SUBDIRS = \ SUBDIRS = \
display \ display \
engine \
input input
bin_PROGRAMS = rtorrent bin_PROGRAMS = rtorrent
rtorrent_LDADD = \ rtorrent_LDADD = \
$(top_srcdir)/src/display/libsub_display.a \ $(top_srcdir)/src/display/libsub_display.a \
$(top_srcdir)/src/engine/libsub_engine.a \
$(top_srcdir)/src/input/libsub_input.a $(top_srcdir)/src/input/libsub_input.a
rtorrent_SOURCES = \ rtorrent_SOURCES = \
downloads.cc \ main.cc
downloads.h \
main.cc \
poll.cc \
poll.h
-37
View File
@@ -1,37 +0,0 @@
#ifndef LIBTORRENT_CURL_STACK_H
#define LIBTORRENT_CURL_STACK_H
#include <list>
class CurlStack {
friend class CurlGet;
public:
typedef std::list<CurlGet*> CurlGetList;
CurlStack();
~CurlStack();
int get_size() const { return m_size; }
bool is_busy() const { return !m_getList.empty(); }
void perform();
void fdset(fd_set* readfds, fd_set* writefds, fd_set* exceptfds, int& maxFd);
static void global_init();
static void global_cleanup();
protected:
void add_get(CurlGet* get);
void remove_get(CurlGet* get);
private:
void* m_handle;
int m_size;
CurlGetList m_getList;
};
#endif
+3 -3
View File
@@ -1,11 +1,11 @@
#include "config.h" #include "config.h"
#include "window_downloads.h"
#include "canvas.h" #include "canvas.h"
#include "window_downloads.h"
namespace display { namespace display {
WindowDownloads::WindowDownloads(Downloads* d) : WindowDownloads::WindowDownloads(engine::Downloads* d) :
Window(new Canvas, true), Window(new Canvas, true),
m_downloads(d) { m_downloads(d) {
} }
@@ -17,7 +17,7 @@ WindowDownloads::redraw() {
int pos = 1; int pos = 1;
for (Downloads::iterator itr = m_downloads->begin(); itr != m_downloads->end(); ++itr, ++pos) 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()); m_canvas->print(1, pos, "Download: %s", itr->get_name().c_str());
} }
+3 -3
View File
@@ -1,19 +1,19 @@
#ifndef RTORRENT_DISPLAY_WINDOW_DOWNLOADS_H #ifndef RTORRENT_DISPLAY_WINDOW_DOWNLOADS_H
#define RTORRENT_DISPLAY_WINDOW_DOWNLOADS_H #define RTORRENT_DISPLAY_WINDOW_DOWNLOADS_H
#include "downloads.h"
#include "window.h" #include "window.h"
#include "engine/downloads.h"
namespace display { namespace display {
class WindowDownloads : public Window { class WindowDownloads : public Window {
public: public:
WindowDownloads(Downloads* d); WindowDownloads(engine::Downloads* d);
virtual void redraw(); virtual void redraw();
private: private:
Downloads* m_downloads; engine::Downloads* m_downloads;
}; };
} }
+14
View File
@@ -0,0 +1,14 @@
noinst_LIBRARIES = libsub_engine.a
libsub_engine_a_SOURCES = \
curl_get.cc \
curl_get.h \
curl_stack.cc \
curl_stack.h \
downloads.cc \
downloads.h \
poll.cc \
poll.h
INCLUDES = -I$(srcdir) -I$(srcdir)/.. -I$(top_srcdir)
+19 -10
View File
@@ -1,12 +1,14 @@
#include "config.h" #include "config.h"
#include "curl_get.h"
#include "curl_stack.h"
#include <torrent/exceptions.h>
#include <ostream> #include <ostream>
#include <curl/curl.h> #include <curl/curl.h>
#include <curl/easy.h> #include <curl/easy.h>
#include <torrent/exceptions.h>
#include "curl_get.h"
#include "curl_stack.h"
namespace engine {
CurlGet::~CurlGet() { CurlGet::~CurlGet() {
close(); close();
@@ -27,7 +29,8 @@ CurlGet::new_object(CurlStack* s) {
return new CurlGet(s); return new CurlGet(s);
} }
void CurlGet::set_url(const std::string& url) { void
CurlGet::set_url(const std::string& url) {
if (is_busy()) if (is_busy())
throw torrent::local_error("Tried to call CurlGet::set_url on a busy object"); throw torrent::local_error("Tried to call CurlGet::set_url on a busy object");
@@ -39,7 +42,8 @@ CurlGet::get_url() const {
return m_url; return m_url;
} }
void CurlGet::set_out(std::ostream* out) { void
CurlGet::set_out(std::ostream* out) {
if (is_busy()) if (is_busy())
throw torrent::local_error("Tried to call CurlGet::set_url on a busy object"); throw torrent::local_error("Tried to call CurlGet::set_url on a busy object");
@@ -63,7 +67,8 @@ CurlGet::get_user_agent() {
return m_useragent; return m_useragent;
} }
void CurlGet::start() { void
CurlGet::start() {
if (is_busy()) if (is_busy())
throw torrent::local_error("Tried to call CurlGet::start on a busy object"); throw torrent::local_error("Tried to call CurlGet::start on a busy object");
@@ -80,7 +85,8 @@ void CurlGet::start() {
m_stack->add_get(this); m_stack->add_get(this);
} }
void CurlGet::close() { void
CurlGet::close() {
if (!is_busy()) if (!is_busy())
return; return;
@@ -91,7 +97,8 @@ void CurlGet::close() {
m_handle = NULL; m_handle = NULL;
} }
void CurlGet::perform(CURLMsg* msg) { void
CurlGet::perform(CURLMsg* msg) {
if (msg->msg != CURLMSG_DONE) if (msg->msg != CURLMSG_DONE)
throw torrent::client_error("CurlGet::process got CURLMSG that isn't done"); throw torrent::client_error("CurlGet::process got CURLMSG that isn't done");
@@ -103,7 +110,8 @@ void CurlGet::perform(CURLMsg* msg) {
} }
} }
size_t curl_get_receive_write(void* data, size_t size, size_t nmemb, void* handle) { size_t
curl_get_receive_write(void* data, size_t size, size_t nmemb, void* handle) {
return ((CurlGet*)handle)->m_out->write((char*)data, size * nmemb).fail() ? 0 : size * nmemb; return ((CurlGet*)handle)->m_out->write((char*)data, size * nmemb).fail() ? 0 : size * nmemb;
} }
@@ -117,3 +125,4 @@ CurlGet::signal_failed() {
return m_failed; return m_failed;
} }
}
+14 -10
View File
@@ -1,5 +1,5 @@
#ifndef LIBTORRENT_CURL_GET_H #ifndef RTORRENT_ENGINE_CURL_GET_H
#define LIBTORRENT_CURL_GET_H #define RTORRENT_ENGINE_CURL_GET_H
#include <iosfwd> #include <iosfwd>
#include <string> #include <string>
@@ -9,6 +9,8 @@
struct CURLMsg; struct CURLMsg;
namespace engine {
class CurlGet : public torrent::Http { class CurlGet : public torrent::Http {
public: public:
friend class CurlStack; friend class CurlStack;
@@ -36,22 +38,24 @@ class CurlGet : public torrent::Http {
SignalFailed& signal_failed(); SignalFailed& signal_failed();
protected: protected:
CURL* handle() { return m_handle; } CURL* handle() { return m_handle; }
void perform(CURLMsg* msg); void perform(CURLMsg* msg);
private: private:
friend size_t curl_get_receive_write(void* data, size_t size, size_t nmemb, void* handle); friend size_t curl_get_receive_write(void* data, size_t size, size_t nmemb, void* handle);
std::string m_url; std::string m_url;
std::string m_useragent; std::string m_useragent;
std::ostream* m_out; std::ostream* m_out;
CURL* m_handle; CURL* m_handle;
CurlStack* m_stack; CurlStack* m_stack;
sigc::signal0<void> m_done; sigc::signal0<void> m_done;
sigc::signal1<void, std::string> m_failed; sigc::signal1<void, std::string> m_failed;
}; };
}
#endif #endif
+43 -18
View File
@@ -1,13 +1,32 @@
#include "curl_get.h" #include "config.h"
#include "curl_stack.h"
#include <algorithm>
#include <functional>
#include <curl/multi.h>
#include <torrent/exceptions.h> #include <torrent/exceptions.h>
#include <algo/algo.h> #include "curl_get.h"
#include <curl/multi.h> #include "curl_stack.h"
using namespace algo; namespace engine {
namespace torrent { template <typename Type, typename Ftor>
struct _equal {
_equal(Type t, Ftor f) : m_t(t), m_f(f) {}
template <typename Arg>
bool operator () (Arg& a) {
return m_t == m_f(a);
}
Type m_t;
Ftor m_f;
};
template <typename Type, typename Ftor>
_equal<Type, Ftor> equal(Type t, Ftor f) {
return _equal<Type, Ftor>(t, f);
}
CurlStack::CurlStack() : CurlStack::CurlStack() :
m_handle((void*)curl_multi_init()), m_handle((void*)curl_multi_init()),
@@ -21,7 +40,8 @@ CurlStack::~CurlStack() {
curl_multi_cleanup((CURLM*)m_handle); curl_multi_cleanup((CURLM*)m_handle);
} }
void CurlStack::perform() { void
CurlStack::perform() {
int s; int s;
CURLMcode code; CURLMcode code;
@@ -39,8 +59,10 @@ void CurlStack::perform() {
CURLMsg* msg = curl_multi_info_read((CURLM*)m_handle, &t); CURLMsg* msg = curl_multi_info_read((CURLM*)m_handle, &t);
CurlGetList::iterator itr = std::find_if(m_getList.begin(), m_getList.end(), CurlGetList::iterator itr = std::find_if(m_getList.begin(), m_getList.end(),
eq(call_member(&CurlGet::handle), equal(msg->easy_handle, std::mem_fun(&CurlGet::handle)));
value(msg->easy_handle)));
// eq(call_member(&CurlGet::handle),
// value(msg->easy_handle)));
if (itr == m_getList.end()) if (itr == m_getList.end())
throw torrent::client_error("Could not find CurlGet with the right easy_handle"); throw torrent::client_error("Could not find CurlGet with the right easy_handle");
@@ -52,27 +74,29 @@ void CurlStack::perform() {
} while (code == CURLM_CALL_MULTI_PERFORM); } while (code == CURLM_CALL_MULTI_PERFORM);
} }
void CurlStack::fdset(fd_set* readfds, fd_set* writefds, fd_set* exceptfds, int& maxFd) { void
CurlStack::fdset(fd_set* readfds, fd_set* writefds, fd_set* exceptfds, int* maxFd) {
int f; int f;
if (curl_multi_fdset((CURLM*)m_handle, readfds, writefds, exceptfds, &f) > 0) if (curl_multi_fdset((CURLM*)m_handle, readfds, writefds, exceptfds, &f) > 0)
throw torrent::local_error("Error calling curl_multi_fdset"); throw torrent::local_error("Error calling curl_multi_fdset");
maxFd = std::max(f, maxFd); *maxFd = std::max(f, *maxFd);
} }
void CurlStack::add_get(CurlGet* get) { void
CurlStack::add_get(CurlGet* get) {
CURLMcode code; CURLMcode code;
if ((code = curl_multi_add_handle((CURLM*)m_handle, get->handle())) > 0) if ((code = curl_multi_add_handle((CURLM*)m_handle, get->handle())) > 0)
throw torrent::local_error("curl_multi_add_handle \"" + std::string(curl_multi_strerror(code))); throw torrent::local_error("curl_multi_add_handle \"" + std::string(curl_multi_strerror(code)));
m_size++; m_size++;
m_getList.push_back(get); m_getList.push_back(get);
} }
void CurlStack::remove_get(CurlGet* get) { void
CurlStack::remove_get(CurlGet* get) {
if (curl_multi_remove_handle((CURLM*)m_handle, get->handle()) > 0) if (curl_multi_remove_handle((CURLM*)m_handle, get->handle()) > 0)
throw torrent::local_error("Error calling curl_multi_remove_handle"); throw torrent::local_error("Error calling curl_multi_remove_handle");
@@ -81,16 +105,17 @@ void CurlStack::remove_get(CurlGet* get) {
if (itr == m_getList.end()) if (itr == m_getList.end())
throw torrent::client_error("Could not find CurlGet when calling CurlStack::remove"); throw torrent::client_error("Could not find CurlGet when calling CurlStack::remove");
m_getList.erase(itr);
m_size--; m_size--;
m_getList.erase(itr);
} }
void CurlStack::global_init() { void
CurlStack::global_init() {
curl_global_init(CURL_GLOBAL_ALL); curl_global_init(CURL_GLOBAL_ALL);
} }
void CurlStack::global_cleanup() { void
CurlStack::global_cleanup() {
curl_global_cleanup(); curl_global_cleanup();
} }
+41
View File
@@ -0,0 +1,41 @@
#ifndef RTORRENT_ENGINE_CURL_STACK_H
#define RTORRENT_ENGINE_CURL_STACK_H
#include <list>
namespace engine {
class CurlStack {
public:
friend class CurlGet;
typedef std::list<CurlGet*> CurlGetList;
CurlStack();
~CurlStack();
int get_size() const { return m_size; }
bool is_busy() const { return !m_getList.empty(); }
void perform();
// 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();
protected:
void add_get(CurlGet* get);
void remove_get(CurlGet* get);
private:
void* m_handle;
int m_size;
CurlGetList m_getList;
};
}
#endif
@@ -4,6 +4,8 @@
#include "downloads.h" #include "downloads.h"
namespace engine {
void void
Downloads::create(std::istream& str) { Downloads::create(std::istream& str) {
torrent::Download d = torrent::download_create(str); torrent::Download d = torrent::download_create(str);
@@ -17,3 +19,5 @@ Downloads::erase(iterator itr) {
Base::erase(itr); Base::erase(itr);
} }
}
+6 -2
View File
@@ -1,9 +1,11 @@
#ifndef RTORRENT_DOWNLOADS_H #ifndef RTORRENT_ENGINE_DOWNLOADS_H
#define RTORRENT_DOWNLOADS_H #define RTORRENT_ENGINE_DOWNLOADS_H
#include <iosfwd> #include <iosfwd>
#include <torrent/download.h> #include <torrent/download.h>
namespace engine {
class Downloads : private std::list<torrent::Download> { class Downloads : private std::list<torrent::Download> {
public: public:
typedef std::list<torrent::Download> Base; typedef std::list<torrent::Download> Base;
@@ -22,4 +24,6 @@ public:
void erase(iterator itr); void erase(iterator itr);
}; };
}
#endif #endif
+4
View File
@@ -5,6 +5,8 @@
#include "poll.h" #include "poll.h"
namespace engine {
void void
Poll::poll() { Poll::poll() {
FD_ZERO(&m_readSet); FD_ZERO(&m_readSet);
@@ -33,3 +35,5 @@ Poll::work() {
m_readStdin(key); m_readStdin(key);
} }
} }
}
+7 -3
View File
@@ -1,16 +1,18 @@
#ifndef RTORRENT_POLL_H #ifndef RTORRENT_ENGINE_POLL_H
#define RTORRENT_POLL_H #define RTORRENT_ENGINE_POLL_H
#include <sys/select.h> #include <sys/select.h>
#include <sigc++/slot.h> #include <sigc++/slot.h>
namespace engine {
class Poll { class Poll {
public: public:
typedef sigc::slot1<void, int> SlotInt; typedef sigc::slot1<void, int> SlotInt;
Poll() : m_running(true) {} Poll() : m_running(true) {}
bool is_running() { return m_running; } bool is_running() { return m_running; }
void poll(); void poll();
void work(); void work();
@@ -27,4 +29,6 @@ private:
fd_set m_exceptSet; fd_set m_exceptSet;
}; };
}
#endif #endif
+13 -5
View File
@@ -3,21 +3,27 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <torrent/torrent.h> #include <torrent/torrent.h>
#include <torrent/http.h>
#include <sigc++/bind.h>
#include "display/canvas.h" #include "display/canvas.h"
#include "display/manager.h" #include "display/manager.h"
#include "display/window_downloads.h" #include "display/window_downloads.h"
#include "engine/poll.h"
#include "engine/curl_stack.h"
#include "engine/curl_get.h"
#include "engine/downloads.h"
#include "input/bindings.h" #include "input/bindings.h"
#include "input/manager.h" #include "input/manager.h"
#include "poll.h"
#include "downloads.h"
int main(int argc, char** argv) { int main(int argc, char** argv) {
try { try {
Poll poll; engine::Poll poll;
Downloads downloads; engine::Downloads downloads;
engine::CurlStack curlStack;
display::Canvas::init(); display::Canvas::init();
display::Manager display; display::Manager display;
@@ -32,6 +38,8 @@ int main(int argc, char** argv) {
torrent::initialize(); torrent::initialize();
torrent::listen_open(6880, 6999); 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) { for (int i = 1; i < argc; ++i) {
std::fstream f(argv[i], std::ios::in); std::fstream f(argv[i], std::ios::in);