Client work.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@244 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2005-01-29 21:06:53 +00:00
parent 96a28a78bb
commit 53f32e74a1
15 changed files with 498 additions and 64 deletions
+5 -1
View File
@@ -9,4 +9,8 @@ rtorrent_LDADD = \
$(top_srcdir)/src/input/libsub_input.a
rtorrent_SOURCES = \
main.cc
downloads.cc \
downloads.h \
main.cc \
poll.cc \
poll.h
+119
View File
@@ -0,0 +1,119 @@
#include "config.h"
#include "curl_get.h"
#include "curl_stack.h"
#include <torrent/exceptions.h>
#include <ostream>
#include <curl/curl.h>
#include <curl/easy.h>
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;
}
+57
View File
@@ -0,0 +1,57 @@
#ifndef LIBTORRENT_CURL_GET_H
#define LIBTORRENT_CURL_GET_H
#include <iosfwd>
#include <string>
#include <curl/curl.h>
#include <torrent/http.h>
#include <sigc++/signal.h>
struct CURLMsg;
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
+97
View File
@@ -0,0 +1,97 @@
#include "curl_get.h"
#include "curl_stack.h"
#include <torrent/exceptions.h>
#include <algo/algo.h>
#include <curl/multi.h>
using namespace algo;
namespace torrent {
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(),
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_getList.erase(itr);
m_size--;
}
void CurlStack::global_init() {
curl_global_init(CURL_GLOBAL_ALL);
}
void CurlStack::global_cleanup() {
curl_global_cleanup();
}
}
+37
View File
@@ -0,0 +1,37 @@
#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 -2
View File
@@ -5,8 +5,9 @@ libsub_display_a_SOURCES = \
canvas.h \
manager.cc \
manager.h \
manager_element.h \
window.cc \
window.h \
window.cc
window_downloads.cc \
window_downloads.h
INCLUDES = -I$(srcdir) -I$(srcdir)/.. -I$(top_srcdir)
+24
View File
@@ -0,0 +1,24 @@
#include "config.h"
#include "window_downloads.h"
#include "canvas.h"
namespace display {
WindowDownloads::WindowDownloads(Downloads* d) :
Window(new Canvas, true),
m_downloads(d) {
}
void
WindowDownloads::redraw() {
m_canvas->erase();
m_canvas->print_border(' ', ' ', '-', '-', ' ', ' ', ' ', ' ');
int pos = 1;
for (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
@@ -0,0 +1,21 @@
#ifndef RTORRENT_DISPLAY_WINDOW_DOWNLOADS_H
#define RTORRENT_DISPLAY_WINDOW_DOWNLOADS_H
#include "downloads.h"
#include "window.h"
namespace display {
class WindowDownloads : public Window {
public:
WindowDownloads(Downloads* d);
virtual void redraw();
private:
Downloads* m_downloads;
};
}
#endif
+19
View File
@@ -0,0 +1,19 @@
#include "config.h"
#include <torrent/torrent.h>
#include "downloads.h"
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);
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef RTORRENT_DOWNLOADS_H
#define RTORRENT_DOWNLOADS_H
#include <iosfwd>
#include <torrent/download.h>
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
+2 -2
View File
@@ -8,9 +8,9 @@
namespace input {
bool
void
Manager::pressed(int key) {
return std::find_if(begin(), end(), std::bind2nd(std::mem_fun(&Bindings::pressed), key)) != end();
std::find_if(begin(), end(), std::bind2nd(std::mem_fun(&Bindings::pressed), key));
}
}
+3 -1
View File
@@ -24,7 +24,9 @@ public:
using Base::push_back;
using Base::push_front;
bool pressed(int key);
void pressed(int key);
// Slot for unreacted keys.
};
}
+21 -58
View File
@@ -1,86 +1,49 @@
#include <string>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <torrent/torrent.h>
#include "display/window.h"
#include "display/canvas.h"
#include "display/manager.h"
#include "display/window_downloads.h"
#include "input/bindings.h"
#include "input/manager.h"
class WindowTest : public display::Window {
public:
WindowTest(const std::string& str, bool reverse = false) :
Window(new display::Canvas, true, 3), m_str(str), m_reverse(reverse) {}
virtual void redraw() {
if (m_canvas == NULL)
return;
if (m_reverse)
m_canvas->set_background(A_REVERSE);
else
m_canvas->set_background(0);
m_canvas->erase();
m_canvas->print_border('|', '|', '-', '-', '+', '+', '+', '+');
m_canvas->print(1, 1, "%s minHeight %i", m_str.c_str(), m_minHeight);
}
void reverse() {
m_reverse = !m_reverse;
}
void inc_min() {
m_minHeight++;
}
void dec_min() {
if (m_minHeight)
m_minHeight--;
}
private:
std::string m_str;
bool m_reverse;
};
#include "poll.h"
#include "downloads.h"
int main(int argc, char** argv) {
try {
Poll poll;
Downloads downloads;
display::Canvas::init();
display::Manager display;
input::Manager inputManager;
inputManager.push_back(new input::Bindings);
WindowTest window1("This is window 1");
WindowTest window2("This is window 2");
poll.slot_read_stdin(sigc::mem_fun(inputManager, &input::Manager::pressed));
input::Bindings bindings1;
input::Bindings bindings2;
display.push_back(new display::WindowDownloads(&downloads));
inputManager.push_back(&bindings1);
inputManager.push_back(&bindings2);
torrent::initialize();
torrent::listen_open(6880, 6999);
bindings1[KEY_LEFT] = sigc::mem_fun(window1, &WindowTest::reverse);
bindings1[KEY_UP] = sigc::mem_fun(window1, &WindowTest::inc_min);
bindings1[KEY_DOWN] = sigc::mem_fun(window1, &WindowTest::dec_min);
bindings1[KEY_LEFT] = sigc::mem_fun(window1, &WindowTest::reverse);
bindings2[KEY_LEFT] = sigc::mem_fun(window2, &WindowTest::reverse);
bindings2[KEY_RIGHT] = sigc::mem_fun(window2, &WindowTest::reverse);
for (int i = 1; i < argc; ++i) {
std::fstream f(argv[i], std::ios::in);
display.push_back(&window1);
display.push_back(&window2);
downloads.create(f);
}
display.adjust_layout();
while (true) {
while (poll.is_running()) {
display.adjust_layout();
display.do_update();
inputManager.pressed(getch());
sleep(0);
poll.poll();
poll.work();
}
display::Canvas::cleanup();
+35
View File
@@ -0,0 +1,35 @@
#include "config.h"
#include <stdexcept>
#include <ncurses.h>
#include "poll.h"
void
Poll::poll() {
FD_ZERO(&m_readSet);
FD_ZERO(&m_writeSet);
FD_ZERO(&m_exceptSet);
m_maxFd = 1;
if (m_readStdin)
FD_SET(0, &m_readSet);
timeval timeout = {60, 0};
m_maxFd = select(m_maxFd, &m_readSet, &m_writeSet, &m_exceptSet, &timeout);
if (m_maxFd < 0)
throw std::runtime_error("Poll::work(): select error");
}
void
Poll::work() {
if (m_readStdin && FD_ISSET(0, &m_readSet)) {
int key;
while ((key = getch()) >= 0)
m_readStdin(key);
}
}
+30
View File
@@ -0,0 +1,30 @@
#ifndef RTORRENT_POLL_H
#define RTORRENT_POLL_H
#include <sys/select.h>
#include <sigc++/slot.h>
class Poll {
public:
typedef sigc::slot1<void, int> SlotInt;
Poll() : m_running(true) {}
bool is_running() { return m_running; }
void poll();
void work();
void slot_read_stdin(SlotInt s) { m_readStdin = s; }
private:
bool m_running;
SlotInt m_readStdin;
int m_maxFd;
fd_set m_readSet;
fd_set m_writeSet;
fd_set m_exceptSet;
};
#endif