Renamed engine to core.

Added focus to WindowDownloadList.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@249 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2005-01-31 17:59:16 +00:00
parent c3a4a7d554
commit cd9b014eb6
17 changed files with 107 additions and 47 deletions
+15
View File
@@ -0,0 +1,15 @@
noinst_LIBRARIES = libsub_core.a
libsub_core_a_SOURCES = \
curl_get.cc \
curl_get.h \
curl_stack.cc \
curl_stack.h \
download.h \
download_list.cc \
download_list.h \
poll.cc \
poll.h
INCLUDES = -I$(srcdir) -I$(srcdir)/.. -I$(top_srcdir)
+128
View File
@@ -0,0 +1,128 @@
#include "config.h"
#include <ostream>
#include <curl/curl.h>
#include <curl/easy.h>
#include <torrent/exceptions.h>
#include "curl_get.h"
#include "curl_stack.h"
namespace core {
CurlGet::~CurlGet() {
close();
}
CurlGet::CurlGet(CurlStack* s) :
m_useragent("rtorrent_unknown"),
m_out(NULL),
m_handle(NULL),
m_stack(s) {
if (m_stack == NULL)
throw torrent::client_error("Tried to create CurlGet without a valid CurlStack");
}
CurlGet*
CurlGet::new_object(CurlStack* s) {
return new CurlGet(s);
}
void
CurlGet::set_url(const std::string& url) {
if (is_busy())
throw torrent::local_error("Tried to call CurlGet::set_url on a busy object");
m_url = url;
}
const std::string&
CurlGet::get_url() const {
return m_url;
}
void
CurlGet::set_out(std::ostream* out) {
if (is_busy())
throw torrent::local_error("Tried to call CurlGet::set_url on a busy object");
m_out = out;
}
std::ostream*
CurlGet::get_out() {
return m_out;
}
void
CurlGet::set_user_agent(const std::string& s) {
curl_easy_setopt(m_handle, CURLOPT_USERAGENT, s.c_str());
m_useragent = s;
}
const std::string&
CurlGet::get_user_agent() {
return m_useragent;
}
void
CurlGet::start() {
if (is_busy())
throw torrent::local_error("Tried to call CurlGet::start on a busy object");
if (m_out == NULL)
throw torrent::local_error("Tried to call CurlGet::start without a valid output stream");
m_handle = curl_easy_init();
curl_easy_setopt(m_handle, CURLOPT_URL, m_url.c_str());
curl_easy_setopt(m_handle, CURLOPT_WRITEFUNCTION, &curl_get_receive_write);
curl_easy_setopt(m_handle, CURLOPT_WRITEDATA, this);
curl_easy_setopt(m_handle, CURLOPT_FORBID_REUSE, 1);
m_stack->add_get(this);
}
void
CurlGet::close() {
if (!is_busy())
return;
m_stack->remove_get(this);
curl_easy_cleanup(m_handle);
m_handle = NULL;
}
void
CurlGet::perform(CURLMsg* msg) {
if (msg->msg != CURLMSG_DONE)
throw torrent::client_error("CurlGet::process got CURLMSG that isn't done");
if (msg->data.result == CURLE_OK) {
m_done.emit();
} else {
m_failed.emit(curl_easy_strerror(msg->data.result));
}
}
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;
}
CurlGet::SignalDone&
CurlGet::signal_done() {
return m_done;
}
CurlGet::SignalFailed&
CurlGet::signal_failed() {
return m_failed;
}
}
+61
View File
@@ -0,0 +1,61 @@
#ifndef RTORRENT_CORE_CURL_GET_H
#define RTORRENT_CORE_CURL_GET_H
#include <iosfwd>
#include <string>
#include <curl/curl.h>
#include <torrent/http.h>
#include <sigc++/signal.h>
struct CURLMsg;
namespace core {
class CurlGet : public torrent::Http {
public:
friend class CurlStack;
CurlGet(CurlStack* s);
virtual ~CurlGet();
static CurlGet* new_object(CurlStack* s);
void start();
void close();
void set_url(const std::string& url);
const std::string& get_url() const;
void set_out(std::ostream* out);
std::ostream* get_out();
void set_user_agent(const std::string& s);
const std::string& get_user_agent();
bool is_busy() { return m_handle; }
SignalDone& signal_done();
SignalFailed& signal_failed();
protected:
CURL* handle() { return m_handle; }
void perform(CURLMsg* msg);
private:
friend size_t curl_get_receive_write(void* data, size_t size, size_t nmemb, void* handle);
std::string m_url;
std::string m_useragent;
std::ostream* m_out;
CURL* m_handle;
CurlStack* m_stack;
sigc::signal0<void> m_done;
sigc::signal1<void, std::string> m_failed;
};
}
#endif
+122
View File
@@ -0,0 +1,122 @@
#include "config.h"
#include <algorithm>
#include <functional>
#include <curl/multi.h>
#include <torrent/exceptions.h>
#include "curl_get.h"
#include "curl_stack.h"
namespace core {
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() :
m_handle((void*)curl_multi_init()),
m_size(0) {
}
CurlStack::~CurlStack() {
while (!m_getList.empty())
m_getList.front()->close();
curl_multi_cleanup((CURLM*)m_handle);
}
void
CurlStack::perform() {
int s;
CURLMcode code;
do {
code = curl_multi_perform((CURLM*)m_handle, &s);
if (code > 0)
throw torrent::local_error("Error calling curl_multi_perform");
if (s != m_size) {
// Done with some handles.
int t;
do {
CURLMsg* msg = curl_multi_info_read((CURLM*)m_handle, &t);
CurlGetList::iterator itr = std::find_if(m_getList.begin(), m_getList.end(),
equal(msg->easy_handle, std::mem_fun(&CurlGet::handle)));
// eq(call_member(&CurlGet::handle),
// value(msg->easy_handle)));
if (itr == m_getList.end())
throw torrent::client_error("Could not find CurlGet with the right easy_handle");
(*itr)->perform(msg);
} while (t);
}
} while (code == CURLM_CALL_MULTI_PERFORM);
}
void
CurlStack::fdset(fd_set* readfds, fd_set* writefds, fd_set* exceptfds, int* maxFd) {
int f;
if (curl_multi_fdset((CURLM*)m_handle, readfds, writefds, exceptfds, &f) > 0)
throw torrent::local_error("Error calling curl_multi_fdset");
*maxFd = std::max(f, *maxFd);
}
void
CurlStack::add_get(CurlGet* get) {
CURLMcode code;
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)));
m_size++;
m_getList.push_back(get);
}
void
CurlStack::remove_get(CurlGet* get) {
if (curl_multi_remove_handle((CURLM*)m_handle, get->handle()) > 0)
throw torrent::local_error("Error calling curl_multi_remove_handle");
CurlGetList::iterator itr = std::find(m_getList.begin(), m_getList.end(), get);
if (itr == m_getList.end())
throw torrent::client_error("Could not find CurlGet when calling CurlStack::remove");
m_size--;
m_getList.erase(itr);
}
void
CurlStack::global_init() {
curl_global_init(CURL_GLOBAL_ALL);
}
void
CurlStack::global_cleanup() {
curl_global_cleanup();
}
}
+41
View File
@@ -0,0 +1,41 @@
#ifndef RTORRENT_CORE_CURL_STACK_H
#define RTORRENT_CORE_CURL_STACK_H
#include <list>
namespace core {
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
+31
View File
@@ -0,0 +1,31 @@
#ifndef RTORRENT_CORE_DOWNLOAD_H
#define RTORRENT_CORE_DOWNLOAD_H
#include <sigc++/connection.h>
#include <torrent/download.h>
namespace core {
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 stop() { m_download.stop(); }
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 core {
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_CORE_DOWNLOAD_LIST_H
#define RTORRENT_CORE_DOWNLOAD_LIST_H
#include <iosfwd>
#include "download.h"
namespace core {
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
@@ -0,0 +1,23 @@
#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
@@ -0,0 +1,29 @@
#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
+63
View File
@@ -0,0 +1,63 @@
#include "config.h"
#include <stdexcept>
#include <ncurses.h>
#include <sigc++/bind.h>
#include <torrent/torrent.h>
#include "poll.h"
#include "curl_get.h"
namespace core {
void
Poll::poll() {
FD_ZERO(&m_readSet);
FD_ZERO(&m_writeSet);
FD_ZERO(&m_exceptSet);
torrent::mark(&m_readSet, &m_writeSet, &m_exceptSet, &m_maxFd);
m_maxFd = std::max(m_maxFd, 1);
FD_SET(0, &m_readSet);
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);
if (m_maxFd == EINTR)
m_slotSelectInterrupted();
else if (m_maxFd < 0)
throw std::runtime_error("Poll::work(): select error");
}
void
Poll::work() {
if (FD_ISSET(0, &m_readSet)) {
int key;
while ((key = getch()) >= 0)
m_slotReadStdin(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(&core::CurlGet::new_object), &m_curlStack));
}
}
+38
View File
@@ -0,0 +1,38 @@
#ifndef RTORRENT_CORE_POLL_H
#define RTORRENT_CORE_POLL_H
#include <sys/select.h>
#include <sigc++/slot.h>
#include "curl_stack.h"
namespace core {
class Poll {
public:
typedef sigc::slot0<void> Slot;
typedef sigc::slot1<void, int> SlotInt;
void poll();
void work();
void register_http();
void slot_read_stdin(SlotInt s) { m_slotReadStdin = s; }
void slot_select_interrupted(Slot s) { m_slotSelectInterrupted = s; }
private:
SlotInt m_slotReadStdin;
Slot m_slotSelectInterrupted;
int m_maxFd;
fd_set m_readSet;
fd_set m_writeSet;
fd_set m_exceptSet;
CurlStack m_curlStack;
};
}
#endif