diff --git a/toREST/include/config.hpp b/toREST/include/config.hpp index d6e520e..d19ef43 100644 --- a/toREST/include/config.hpp +++ b/toREST/include/config.hpp @@ -2,13 +2,14 @@ #define _TR_CONFIG_HPP_ #include - -struct Config { +namespace tr { +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 75a5fcb..ada7b1f 100644 --- a/toREST/include/ws.hpp +++ b/toREST/include/ws.hpp @@ -1,31 +1,32 @@ -#ifndef _TR_TORRENT_HPP_ -#define _TR_TORRENT_HPP_ +#ifndef _TR_WS_HPP_ +#define _TR_WS_HPP_ #include #include namespace tr { namespace ws { -enum status { - normal = 1000, +namespace status { +const int normal = 1000; +const int unexpected = 1011; }; template -void on_error(Session session, Connection con, const boost::system::error_code &ec) { +void on_error(Session &session, Connection con, const boost::system::error_code &ec) { std::cerr << ec.message() << std::endl; } template -void on_close(Session session, Connection con, status sta, const std::string &reason) { +void on_close(Session &session, Connection con, int status, const std::string &reason) { } 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"); + server.send_close(con, status::unexpected, "Session not valid"); } } template -void on_message(Session session, Connection con, Message message) { +void on_message(Session &session, Connection con, Message message) { } } } diff --git a/toREST/src/main.cxx b/toREST/src/main.cxx index 4c3114b..489ae14 100644 --- a/toREST/src/main.cxx +++ b/toREST/src/main.cxx @@ -10,12 +10,13 @@ #include #include #include +#include typedef SimpleWeb::Server HttpServer; typedef SimpleWeb::SocketServer WsServer; int main(int argc, char *argv[]) { - Config config; + tr::config config; if (!boost::filesystem::exists(config.root_dir)) { boost::system::error_code ec; @@ -68,6 +69,25 @@ int main(int argc, char *argv[]) { tr::session::torrents::id::patch(session, resp, req); }; + auto &end_point = ws_server.endpoint["^/session"]; + + end_point.on_open = [&](std::shared_ptr connection) { + tr::ws::on_open(ws_server, session, connection); + }; + + end_point.on_message = [&](std::shared_ptr connection, std::shared_ptr message) { + tr::ws::on_message(session, connection, message); + }; + + end_point.on_error = [&](std::shared_ptr connection, const boost::system::error_code &code) { + tr::ws::on_error(session, connection, code); + }; + + end_point.on_close = [&](std::shared_ptr connection, int status, const std::string &reason) { + tr::ws::on_close(session, connection, status, reason); + }; + + std::thread http_thread([&http_server]() { http_server.start(); }); diff --git a/toREST/tests/config_test.cpp b/toREST/tests/config_test.cpp index 8b9e6ed..c5ecf79 100644 --- a/toREST/tests/config_test.cpp +++ b/toREST/tests/config_test.cpp @@ -2,7 +2,7 @@ #include SCENARIO("The configuration have default properties") { - Config config; + tr::config config; REQUIRE_FALSE(config.default_download_dir.empty()); REQUIRE_FALSE(config.root_dir.empty()); REQUIRE(config.default_download_dir == "toREST"); diff --git a/toREST/tests/torrents_test.cpp b/toREST/tests/torrents_test.cpp index dc39252..be394cc 100644 --- a/toREST/tests/torrents_test.cpp +++ b/toREST/tests/torrents_test.cpp @@ -38,7 +38,7 @@ SCENARIO("We are running a GET /session/torrents resource") { SCENARIO("We are running a POST /session/torrents resource") { auto torrent_session = TestSession(); - auto settings = Config(); + auto settings = tr::config(); auto response = std::make_shared(); auto request = std::make_shared(); GIVEN("the server is not working properly") { diff --git a/toREST/tests/ws_test.cpp b/toREST/tests/ws_test.cpp index 21db7cf..422c2fc 100644 --- a/toREST/tests/ws_test.cpp +++ b/toREST/tests/ws_test.cpp @@ -8,20 +8,23 @@ SCENARIO("the test suite is working") { } struct TestConnection { - + std::string reason_ = ""; + int status_ = 0; }; struct TestWsServer { - void send_close(TestConnection &con,tr::ws::status status, std::string reason) { + void send_close(std::shared_ptr con, int status, const std::string &reason) { send_close_calls_++; + con->reason_ = reason; + con->status_ = status; } int send_close_calls_ = 0; }; SCENARIO("we have a websocket resource") { - TestConnection connection; TestSession session; TestWsServer server; + auto connection = std::make_shared(); GIVEN("the session is valid") { session.valid = true; WHEN("we open the connection") { @@ -36,6 +39,8 @@ SCENARIO("we have a websocket resource") { tr::ws::on_open(server, session, connection); THEN("the server should close the connection") { REQUIRE(server.send_close_calls_ == 1); + REQUIRE(connection->status_ == 1011); + REQUIRE(connection->reason_ == "Session not valid"); } } }