From 534facda79eda35bcd8bc0c30985b3b2166c8e0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Tue, 16 May 2017 23:52:29 +0200 Subject: [PATCH] Test open connection --- toREST/include/config.hpp | 3 +++ toREST/include/ws.hpp | 26 ++++++++++++------ toREST/src/main.cxx | 31 +++++++++++++++------- toREST/tests/config_test.cpp | 3 +++ toREST/tests/include/SessionContext.hpp | 2 ++ toREST/tests/stubs/SessionContext.cpp | 13 ++++----- toREST/tests/ws_test.cpp | 35 +++++++++++++++++++++++++ 7 files changed, 87 insertions(+), 26 deletions(-) diff --git a/toREST/include/config.hpp b/toREST/include/config.hpp index e43f40b..d6e520e 100644 --- a/toREST/include/config.hpp +++ b/toREST/include/config.hpp @@ -6,6 +6,9 @@ struct Config { const boost::filesystem::path root_dir = "/"; const boost::filesystem::path default_download_dir = "toREST"; + const int http_port = 8080; + const int ws_port = 3000; + const int http_threads = 1; }; #endif \ No newline at end of file diff --git a/toREST/include/ws.hpp b/toREST/include/ws.hpp index bc00345..75a5fcb 100644 --- a/toREST/include/ws.hpp +++ b/toREST/include/ws.hpp @@ -2,20 +2,30 @@ #define _TR_TORRENT_HPP_ #include +#include namespace tr { namespace ws { -template -void on_error(Server server, Connection con, const boost::system::error_code) { +enum status { + normal = 1000, +}; +template +void on_error(Session session, Connection con, const boost::system::error_code &ec) { + std::cerr << ec.message() << std::endl; } -template -void on_close(Server server, Connection con, int status, const std::string &reason) { +template +void on_close(Session session, Connection con, status sta, const std::string &reason) { } -template -void on_open(Server server, Connection con) { +template +void on_open(Server &server, Session &session, Connection con) { + if (session.is_valid()) { + session.post_torrent_updates(); + } else { + server.send_close(con, status::normal, "Session not valid"); + } } -template -void on_message(Server server, Connection con, Message message) { +template +void on_message(Session session, Connection con, Message message) { } } } diff --git a/toREST/src/main.cxx b/toREST/src/main.cxx index f88f41e..4c3114b 100644 --- a/toREST/src/main.cxx +++ b/toREST/src/main.cxx @@ -15,20 +15,23 @@ typedef SimpleWeb::Server HttpServer; typedef SimpleWeb::SocketServer WsServer; int main(int argc, char *argv[]) { - int http_port = 8080, http_threads = 4; + Config config; - Config options; - - if (!boost::filesystem::exists(options.root_dir)) { + if (!boost::filesystem::exists(config.root_dir)) { boost::system::error_code ec; - boost::filesystem::create_directories(options.default_download_dir, ec); + boost::filesystem::create_directories(config.default_download_dir, ec); if (ec) { std::cout << ec.message() << "\n"; return 1; } } - HttpServer http_server(http_port, http_threads); + HttpServer http_server(config.http_port, config.http_threads); + WsServer ws_server; + + ws_server.config.port = config.ws_port; + ws_server.config.thread_pool_size = config.http_threads; + libtorrent::session session; http_server.default_resource["GET"] = [](std::shared_ptr resp, std::shared_ptr req) { @@ -50,7 +53,7 @@ int main(int argc, char *argv[]) { }; http_server.resource["^/session/torrents(\\?.*)?\\/?$"]["POST"] = [&](std::shared_ptr resp, std::shared_ptr req) { - tr::session::torrents::post(options, session, resp, req); + tr::session::torrents::post(config, session, resp, req); }; http_server.resource["^/session/torrents(\\?.*)?\\/?$"]["DELETE"] = [&](std::shared_ptr resp, std::shared_ptr req) { @@ -65,11 +68,17 @@ int main(int argc, char *argv[]) { tr::session::torrents::id::patch(session, resp, req); }; - std::thread server_thread([&http_server]() { + std::thread http_thread([&http_server]() { http_server.start(); }); - std::cout << "HttpServer listening on port " << 8080 << std::endl; + std::cout << "HTTP/1.1 server listening on port " << config.http_port << std::endl; + + std::thread ws_thread([&ws_server]() { + ws_server.start(); + }); + + std::cout << "HTTP/1.1 websocket server listening on port " << config.ws_port << std::endl; std::string input; while (input != "q") { @@ -77,12 +86,14 @@ int main(int argc, char *argv[]) { if (input == "q") { std::cout << "Stopping server..." << std::endl; http_server.stop(); // TODO check if throws + ws_server.stop(); std::cout << "Server stopped" << std::endl; break; } } - server_thread.join(); + http_thread.join(); + ws_thread.join(); return 0; }; diff --git a/toREST/tests/config_test.cpp b/toREST/tests/config_test.cpp index 77e5072..8b9e6ed 100644 --- a/toREST/tests/config_test.cpp +++ b/toREST/tests/config_test.cpp @@ -6,4 +6,7 @@ SCENARIO("The configuration have default properties") { REQUIRE_FALSE(config.default_download_dir.empty()); REQUIRE_FALSE(config.root_dir.empty()); REQUIRE(config.default_download_dir == "toREST"); + REQUIRE(config.http_port == 8080); + REQUIRE(config.http_threads == 1); + REQUIRE(config.ws_port == 3000); } \ No newline at end of file diff --git a/toREST/tests/include/SessionContext.hpp b/toREST/tests/include/SessionContext.hpp index 2ec5620..ddd59ea 100644 --- a/toREST/tests/include/SessionContext.hpp +++ b/toREST/tests/include/SessionContext.hpp @@ -45,6 +45,8 @@ public: void remove_torrent(const TestTorrent &torrent, int options); void apply_settings(TestSessionSettings settings); void async_add_torrent(const libtorrent::add_torrent_params ¶ms); + void post_torrent_updates(int flags = 0); + int post_torrent_updates_calls_ = 0; void theTorrentExists(); TestTorrent invalid_; }; diff --git a/toREST/tests/stubs/SessionContext.cpp b/toREST/tests/stubs/SessionContext.cpp index 93ad9dc..eaef222 100644 --- a/toREST/tests/stubs/SessionContext.cpp +++ b/toREST/tests/stubs/SessionContext.cpp @@ -90,14 +90,7 @@ TestTorrent &TestSession::find_torrent(const libtorrent::sha1_hash &hash) { void TestSession::remove_torrent(const TestTorrent &torrent, int options = -1) { if (options > -1) { - // auto itr = torrents_.end(); - // for (itr = torrents_.begin(); itr != torrents_.end(); itr++) { - // if (itr->info_hash() == torrent.info_hash()) { - // break; - // } - // } - // if (itr != torrents_.end()) - torrents_.erase(std::find_if(torrents_.begin(), torrents_.end(), [&](TestTorrent a) { + torrents_.erase(std::find_if(torrents_.begin(), torrents_.end(), [&](TestTorrent a) { return a.info_hash() == torrent.info_hash(); })); } @@ -110,3 +103,7 @@ void TestSession::apply_settings(TestSessionSettings settings) { void TestSession::async_add_torrent(const libtorrent::add_torrent_params &settings) { torrents_.emplace_back(settings); }; + +void TestSession::post_torrent_updates(int flags) { + post_torrent_updates_calls_++; +} diff --git a/toREST/tests/ws_test.cpp b/toREST/tests/ws_test.cpp index 10c7f3e..21db7cf 100644 --- a/toREST/tests/ws_test.cpp +++ b/toREST/tests/ws_test.cpp @@ -1,7 +1,42 @@ #include +#include #include SCENARIO("the test suite is working") { REQUIRE(true); REQUIRE_FALSE(false); } + +struct TestConnection { + +}; + +struct TestWsServer { + void send_close(TestConnection &con,tr::ws::status status, std::string reason) { + send_close_calls_++; + } + int send_close_calls_ = 0; +}; + +SCENARIO("we have a websocket resource") { + TestConnection connection; + TestSession session; + TestWsServer server; + GIVEN("the session is valid") { + session.valid = true; + WHEN("we open the connection") { + tr::ws::on_open(server, session, connection); + THEN("the session should post torrent torrent updates") { + REQUIRE(session.post_torrent_updates_calls_ == 1); + } + } + } + GIVEN("the session is invalid") { + WHEN("we open the connection") { + tr::ws::on_open(server, session, connection); + THEN("the server should close the connection") { + REQUIRE(server.send_close_calls_ == 1); + } + } + } +} \ No newline at end of file