From adcad4d282a35eaa6fcbebd92550e3e4f925422a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 1 May 2017 21:52:15 +0200 Subject: [PATCH] extract torrent to own file --- toREST/include/torrents.hpp | 40 ++++++++++++++++ toREST/src/main.cxx | 83 +++++++++++----------------------- toREST/tests/torrents_test.hpp | 4 +- 3 files changed, 69 insertions(+), 58 deletions(-) create mode 100644 toREST/include/torrents.hpp diff --git a/toREST/include/torrents.hpp b/toREST/include/torrents.hpp new file mode 100644 index 0000000..af9e85a --- /dev/null +++ b/toREST/include/torrents.hpp @@ -0,0 +1,40 @@ +#ifndef _TR_TORRENTS_HPP_ +#define _TR_TORRENTS_HPP_ +#include + +namespace tr { +namespace session { +namespace torrents { +template +void get(torrent_session &session, response resp, request req) { + auto http_response = http::response(); + if (session.is_valid()) { + auto torrents = session.get_torrents(); + auto response_object = nlohmann::json::object(); + auto torrents_json = nlohmann::json::array(); + for (auto &torrent : torrents) { + if (torrent.is_valid()) { + const auto hash = torrent.info_hash().to_string(); + const auto status = torrent.status( + torrent.query_name | + torrent.query_save_path); + torrents_json.push_back({{"hash", hash}, + {"paused", status.paused}, + {"seeding", status.is_seeding}, + {"state", status.state}, + {"priority", status.priority}, + {"name", status.name}}); + } + } + response_object["torrents"] = torrents_json; + http_response.set_status(http::ok); + http_response.set_body(response_object); + } else { + auto response_code = http::code(http::service_unavailable); + http_response.set_body({{response_code.first, response_code.second}}); + http_response.set_status(response_code.first); + } + *resp << http_response; +} +}}} +#endif diff --git a/toREST/src/main.cxx b/toREST/src/main.cxx index 08db99a..52ebbe6 100644 --- a/toREST/src/main.cxx +++ b/toREST/src/main.cxx @@ -1,88 +1,59 @@ -#include +#include #include -#include #include #include #include -#include +#include +#include +#include typedef SimpleWeb::Server HttpServer; using namespace std; int main(int argc, char *argv[]) { - int http_port=8080, http_threads=4; - const boost::filesystem::path root_dir="/home/zalox/downloads"; - const boost::filesystem::path default_download_dir=root_dir/"toREST"; - if(!boost::filesystem::exists(default_download_dir)){ + int http_port = 8080, http_threads = 4; + const boost::filesystem::path root_dir = "/home/zalox/downloads"; + const boost::filesystem::path default_download_dir = root_dir / "toREST"; + if (!boost::filesystem::exists(default_download_dir)) { boost::system::error_code ec; - boost::filesystem::create_directories(default_download_dir,ec); - if(ec){ + boost::filesystem::create_directories(default_download_dir, ec); + if (ec) { std::cout << ec.message() << "\n"; return 1; } } - HttpServer http_server(http_port,http_threads); + HttpServer http_server(http_port, http_threads); libtorrent::session session; const auto stats_metrics = libtorrent::session_stats_metrics(); - http_server.default_resource["GET"]=[](std::shared_ptr resp, std::shared_ptr req) { + http_server.default_resource["GET"] = [](std::shared_ptr resp, std::shared_ptr req) { auto response = http::response(); response.set_status(http::not_found); *resp << response; }; - - http_server.resource["^/session(\\?.*)?\\/?$"]["GET"]=[&](std::shared_ptr resp, std::shared_ptr req) { - tr::session::get(session,resp,req); + + http_server.resource["^/session(\\?.*)?\\/?$"]["GET"] = [&](std::shared_ptr resp, std::shared_ptr req) { + tr::session::get(session, resp, req); }; - - http_server.resource["^/session(\\?.*)?\\/?$"]["PATCH"]=[&](std::shared_ptr resp, std::shared_ptr req) { - tr::session::patch(session,resp,req); + + http_server.resource["^/session(\\?.*)?\\/?$"]["PATCH"] = [&](std::shared_ptr resp, std::shared_ptr req) { + tr::session::patch(session, resp, req); }; - - http_server.resource["^/session/torrents(\\?.*)?\\/?$"]["GET"]=[&session](std::shared_ptr resp, std::shared_ptr req) { - auto response = http::response(); - if(session.is_valid()) { - auto torrents = session.get_torrents(); - auto response_object = nlohmann::json::object(); - auto torrents_json = nlohmann::json::array(); - for(auto &torrent:torrents){ - if(torrent.is_valid()){ - const auto hash=torrent.info_hash().to_string(); - const auto status=torrent.status( - libtorrent::torrent_handle::query_name | - libtorrent::torrent_handle::query_save_path - ); - torrents_json.push_back({ - { "hash", hash }, - { "paused", status.paused }, - { "seeding", status.is_seeding }, - { "state", status.state }, - { "priority", status.priority }, - { "name",status.name } - }); - } - } - response_object["torrents"] = torrents_json; - response.set_status(http::ok); - response.set_body(response_object); - } else { - auto response_code = http::code(http::service_unavailable); - response.set_body({{response_code.first, response_code.second}}); - response.set_status(response_code.first); - } - *resp << response; + + http_server.resource["^/session/torrents(\\?.*)?\\/?$"]["GET"] = [&session](std::shared_ptr resp, std::shared_ptr req) { + tr::session::torrents::get(session, resp, req); }; - - std::thread server_thread([&http_server](){ + + std::thread server_thread([&http_server]() { http_server.start(); }); - + std::cout << "HttpServer listening on port " << 8080 << std::endl; - + std::string input; - while(input != "q") { + while (input != "q") { std::getline(std::cin, input); - if(input == "q") { + if (input == "q") { std::cout << "Stopping server..." << std::endl; http_server.stop(); // TODO check if throws std::cout << "Server stopped" << std::endl; diff --git a/toREST/tests/torrents_test.hpp b/toREST/tests/torrents_test.hpp index 296b9fd..e95957d 100644 --- a/toREST/tests/torrents_test.hpp +++ b/toREST/tests/torrents_test.hpp @@ -10,9 +10,9 @@ SCENARIO("We are running a GET /session resource") { auto request = std::make_shared(); GIVEN("the server is not working properly") { AND_WHEN("we recive a request") { - tr::session::get(torrent_session, response, request); + tr::session::torrents::get(torrent_session, response, request); THEN("the server should reply with service unavailable") { - reply_is_service_unavailable(response); + CommonResponse::service_unavailable(response); } } }