From 74023d86948b61b9105f5711365caf7c5b3cf611 Mon Sep 17 00:00:00 2001 From: thibaudk Date: Mon, 9 Aug 2021 10:19:15 +0200 Subject: [PATCH 01/20] add httpserver to the remote cotrol --- .../score-plugin-remotecontrol/CMakeLists.txt | 7 +- .../RemoteControl/ApplicationPlugin.cpp | 7 + .../RemoteControl/Http_server.cpp | 326 ++++++++++++++++++ .../RemoteControl/Http_server.hpp | 138 ++++++++ 4 files changed, 477 insertions(+), 1 deletion(-) create mode 100644 src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp create mode 100644 src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp diff --git a/src/plugins/score-plugin-remotecontrol/CMakeLists.txt b/src/plugins/score-plugin-remotecontrol/CMakeLists.txt index d0ec3ed4f3..5be4da36fc 100644 --- a/src/plugins/score-plugin-remotecontrol/CMakeLists.txt +++ b/src/plugins/score-plugin-remotecontrol/CMakeLists.txt @@ -25,6 +25,7 @@ set(HDRS "RemoteControl/DocumentPlugin.hpp" "i-score-remote/RemoteApplication.hpp" "score_plugin_remotecontrol.hpp" + "RemoteControl/Http_server.hpp" ) set(SRCS "${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/Settings/Model.cpp" @@ -41,12 +42,16 @@ set(SRCS "${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/ApplicationPlugin.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/DocumentPlugin.cpp" +"${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/Http_server.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/score_plugin_remotecontrol.cpp" ) +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/build-wasm/ DESTINATION ${CMAKE_BINARY_DIR}/src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/ ) + add_library(${PROJECT_NAME} ${SRCS} ${HDRS} ${QRCS}) -target_link_libraries(${PROJECT_NAME} PUBLIC score_plugin_scenario score_plugin_js ${QT_PREFIX}::WebSockets) +target_link_libraries(${PROJECT_NAME} PUBLIC score_plugin_scenario score_plugin_js ${QT_PREFIX}::WebSockets ossia) setup_score_plugin(${PROJECT_NAME}) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp index 7bcbdcdc58..42eb50ba7e 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp @@ -5,6 +5,9 @@ #include #include + +#include + namespace RemoteControl { ApplicationPlugin::ApplicationPlugin(const score::GUIApplicationContext& app) @@ -16,6 +19,10 @@ void ApplicationPlugin::on_createdDocument(score::Document& doc) { doc.model().addPluginModel(new DocumentPlugin{ doc.context(), &doc.model()}); + + Http_server Http_server; + + Http_server.open_server(); } } diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp new file mode 100644 index 0000000000..7c6ab7636e --- /dev/null +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp @@ -0,0 +1,326 @@ +// +// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// Official repository: https://github.com/boostorg/beast +// + +//------------------------------------------------------------------------------ +// +// Example: HTTP server, synchronous +// +//------------------------------------------------------------------------------ + +#include + +//------------------------------------------------------------------------------ + +namespace RemoteControl +{ + +Http_server::Http_server() + : ioc{1} +{ + // auto th = [this] { this->ioc.run(); }; + + //th(&Http_server::open_server, this); + + open_server(); + + //th = [this] { this->ioc.run(); }; + +} + +Http_server::~Http_server() +{ + //ioc.stop(); + th.join(); +} + +// Return a reasonable mime type based on the extension of a file. +beast::string_view +Http_server::mime_type(beast::string_view path) +{ + using beast::iequals; + auto const ext = [&path] + { + auto const pos = path.rfind("."); + if(pos == beast::string_view::npos) + return beast::string_view{}; + return path.substr(pos); + }(); + if(iequals(ext, ".htm")) return "text/html"; + if(iequals(ext, ".html")) return "text/html"; + if(iequals(ext, ".php")) return "text/html"; + if(iequals(ext, ".css")) return "text/css"; + if(iequals(ext, ".txt")) return "text/plain"; + if(iequals(ext, ".js")) return "application/javascript"; + if(iequals(ext, ".json")) return "application/json"; + if(iequals(ext, ".xml")) return "application/xml"; + if(iequals(ext, ".swf")) return "application/x-shockwave-flash"; + if(iequals(ext, ".flv")) return "video/x-flv"; + if(iequals(ext, ".png")) return "image/png"; + if(iequals(ext, ".jpe")) return "image/jpeg"; + if(iequals(ext, ".jpeg")) return "image/jpeg"; + if(iequals(ext, ".jpg")) return "image/jpeg"; + if(iequals(ext, ".gif")) return "image/gif"; + if(iequals(ext, ".bmp")) return "image/bmp"; + if(iequals(ext, ".ico")) return "image/vnd.microsoft.icon"; + if(iequals(ext, ".tiff")) return "image/tiff"; + if(iequals(ext, ".tif")) return "image/tiff"; + if(iequals(ext, ".svg")) return "image/svg+xml"; + if(iequals(ext, ".svgz")) return "image/svg+xml"; + return "application/text"; +} + +// Append an HTTP rel-path to a local filesystem path. +// The returned path is normalized for the platform. +std::string +Http_server::path_cat( + beast::string_view base, + beast::string_view path) +{ + if (base.empty()) + return std::string(path); + std::string result(base); +#ifdef BOOST_MSVC + char constexpr path_separator = '\\'; + if(result.back() == path_separator) + result.resize(result.size() - 1); + result.append(path.data(), path.size()); + for(auto& c : result) + if(c == '/') + c = path_separator; +#else + char constexpr path_separator = '/'; + if(result.back() == path_separator) + result.resize(result.size() - 1); + result.append(path.data(), path.size()); +#endif + return result; +} + +// This function produces an HTTP response for the given +// request. The type of the response object depends on the +// contents of the request, so the interface requires the +// caller to pass a generic lambda for receiving the response. +template< + class Body, class Allocator, + class Send> +void +Http_server::handle_request( + beast::string_view doc_root, + http::request>&& req, + Send&& send) +{ + // Returns a bad request response + auto const bad_request = + [&req](beast::string_view why) + { + http::response res{http::status::bad_request, req.version()}; + res.set(http::field::server, BOOST_BEAST_VERSION_STRING); + res.set(http::field::content_type, "text/html"); + res.keep_alive(req.keep_alive()); + res.body() = std::string(why); + res.prepare_payload(); + return res; + }; + + // Returns a not found response + auto const not_found = + [&req](beast::string_view target) + { + http::response res{http::status::not_found, req.version()}; + res.set(http::field::server, BOOST_BEAST_VERSION_STRING); + res.set(http::field::content_type, "text/html"); + res.keep_alive(req.keep_alive()); + res.body() = "The resource '" + std::string(target) + "' was not found.
Go to the following address : http://ip_address:port/remote.html."; + res.prepare_payload(); + return res; + }; + + // Returns a server error response + auto const server_error = + [&req](beast::string_view what) + { + http::response res{http::status::internal_server_error, req.version()}; + res.set(http::field::server, BOOST_BEAST_VERSION_STRING); + res.set(http::field::content_type, "text/html"); + res.keep_alive(req.keep_alive()); + res.body() = "An error occurred: '" + std::string(what) + "'"; + res.prepare_payload(); + return res; + }; + + // Make sure we can handle the method + if( req.method() != http::verb::get && + req.method() != http::verb::head) + return send(bad_request("Unknown HTTP-method")); + + // Request path must be absolute and not contain "..". + if( req.target().empty() || + req.target()[0] != '/' || + req.target().find("..") != beast::string_view::npos) + return send(bad_request("Illegal request-target")); + + // Build the path to the requested file + std::string path = path_cat(doc_root, req.target()); + if(req.target().back() == '/') + path.append("index.html"); + + // Attempt to open the file + beast::error_code ec; + http::file_body::value_type body; + body.open(path.c_str(), beast::file_mode::scan, ec); + + // Handle the case where the file doesn't exist + if(ec == beast::errc::no_such_file_or_directory) + return send(not_found(req.target())); + + // Handle an unknown error + if(ec) + return send(server_error(ec.message())); + + // Cache the size since we need it after the move + auto const size = body.size(); + + // Respond to HEAD request + if(req.method() == http::verb::head) + { + http::response res{http::status::ok, req.version()}; + res.set(http::field::server, BOOST_BEAST_VERSION_STRING); + res.set(http::field::content_type, mime_type(path)); + res.content_length(size); + res.keep_alive(req.keep_alive()); + return send(std::move(res)); + } + + // Respond to GET request + http::response res{ + std::piecewise_construct, + std::make_tuple(std::move(body)), + std::make_tuple(http::status::ok, req.version())}; + res.set(http::field::server, BOOST_BEAST_VERSION_STRING); + res.set(http::field::content_type, mime_type(path)); + res.content_length(size); + res.keep_alive(req.keep_alive()); + return send(std::move(res)); +} + +//------------------------------------------------------------------------------ + +// Report a failure +void +Http_server::fail(beast::error_code ec, char const* what) +{ + std::cerr << what << ": " << ec.message() << "\n"; +} + +// Handles an HTTP server connection +void +Http_server::do_session( + tcp::socket& socket, + std::shared_ptr const& doc_root) +{ + bool close = false; + beast::error_code ec; + + // This buffer is required to persist across reads + beast::flat_buffer buffer; + + // This lambda is used to send messages + send_lambda lambda{socket, close, ec}; + + for(;;) + { + // Read a request + http::request req; + http::read(socket, buffer, req, ec); + if(ec == http::error::end_of_stream) + break; + if(ec) + return Http_server::fail(ec, "read"); + + // Send the response + Http_server::handle_request(*doc_root, std::move(req), lambda); + if(ec) + return Http_server::fail(ec, "write"); + if(close) + { + // This means we should close the connection, usually because + // the response indicated the "Connection: close" semantic. + break; + } + } + + // Send a TCP shutdown + socket.shutdown(tcp::socket::shutdown_send, ec); + + // At this point the connection is closed gracefully +} + +//------------------------------------------------------------------------------ + +std::string +Http_server::get_ip_address() +{ + std::string ip_address; + + QList list = QNetworkInterface::allAddresses(); + + for(int nIter=0; nIter(std::atoi("8080")); + auto const doc_root = std::make_shared("./build-wasm/"); + + // The acceptor receives incoming connections + tcp::acceptor acceptor{ioc, {address, port}}; + for(;;) + { + // This will receive the new connection + tcp::socket socket{ioc}; + + // Block until we get a connection + acceptor.accept(socket); + + // Launch the session, transferring ownership of the socket + std::thread{std::bind( + &Http_server::do_session, + this, + std::move(socket), + doc_root)}.detach(); + } + } + catch (const std::exception& e) + { + std::cerr << "Error: " << e.what() << std::endl; + return EXIT_FAILURE; + } +} + +} diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp new file mode 100644 index 0000000000..3ac3da6a2f --- /dev/null +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp @@ -0,0 +1,138 @@ +// +// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// Official repository: https://github.com/boostorg/beast +// + +//------------------------------------------------------------------------------ +// +// Example: HTTP server, synchronous +// +//------------------------------------------------------------------------------ +#pragma once + +#define BOOST_DATE_TIME_NO_LIB 1 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace beast = boost::beast; // from +namespace http = beast::http; // from +namespace net = boost::asio; // from +using tcp = boost::asio::ip::tcp; // from + +//------------------------------------------------------------------------------ + +namespace RemoteControl +{ +class Http_server +{ +public: + std::thread th; + net::io_context ioc; + + // std::thread th1; + + Http_server(); + + ~Http_server(); + + //------------------------------------------------------------------------------ + + // Return a reasonable mime type based on the extension of a file. + beast::string_view + mime_type(beast::string_view path); + + // Append an HTTP rel-path to a local filesystem path. + // The returned path is normalized for the platform. + std::string + path_cat( + beast::string_view base, + beast::string_view path); + + // This function produces an HTTP response for the given + // request. The type of the response object depends on the + // contents of the request, so the interface requires the + // caller to pass a generic lambda for receiving the response. + template< + class Body, class Allocator, + class Send> + void + handle_request( + beast::string_view doc_root, + http::request>&& req, + Send&& send); + + //------------------------------------------------------------------------------ + + // Report a failure + void + fail(beast::error_code ec, char const* what); + + // This is the C++11 equivalent of a generic lambda. + // The function object is used to send an HTTP message. + template + struct send_lambda + { + Stream& stream_; + bool& close_; + beast::error_code& ec_; + + explicit + send_lambda( + Stream& stream, + bool& close, + beast::error_code& ec) + : stream_(stream) + , close_(close) + , ec_(ec) + { + } + + template + void + operator()(http::message&& msg) const + { + // Determine if we should close the connection after + close_ = msg.need_eof(); + + // We need the serializer here because the serializer requires + // a non-const file_body, and the message oriented version of + // http::write only works with const messages. + http::serializer sr{msg}; + http::write(stream_, sr, ec_); + } + }; + + // Handles an HTTP server connection + void + do_session( + tcp::socket& socket, + std::shared_ptr const& doc_root); + + //------------------------------------------------------------------------------ + + std::string get_ip_address(); + + //------------------------------------------------------------------------------ + + int open_server(); + + //void open_server_thread(); +}; +} From c035986ebbecefa3c772d44aa7e8657e7ee8ff2a Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Mon, 9 Aug 2021 15:07:19 +0200 Subject: [PATCH 02/20] try to add a thread for the web socket connection --- .../RemoteControl/ApplicationPlugin.cpp | 2 +- .../RemoteControl/Http_server.cpp | 9 +++++++-- .../RemoteControl/Http_server.hpp | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp index 42eb50ba7e..38825b2b86 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp @@ -22,7 +22,7 @@ void ApplicationPlugin::on_createdDocument(score::Document& doc) Http_server Http_server; - Http_server.open_server(); + // Http_server.open_server(); } } diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp index 7c6ab7636e..5c2f77ccc0 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp @@ -35,7 +35,7 @@ Http_server::Http_server() Http_server::~Http_server() { - //ioc.stop(); + ioc.stop(); th.join(); } @@ -313,8 +313,13 @@ Http_server::open_server() &Http_server::do_session, this, std::move(socket), - doc_root)}.detach(); + doc_root)}.join(); + + // auto th1 = [this] { this->do_session(std::move(socket), doc_root); }; + } + + auto th = [this] { this->ioc.run(); }; } catch (const std::exception& e) { diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp index 3ac3da6a2f..6790b1bf94 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp @@ -46,7 +46,7 @@ class Http_server std::thread th; net::io_context ioc; - // std::thread th1; + //std::thread th1; Http_server(); From e2d4b35ca3a32dc80503fa926e46702724d0059f Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Mon, 9 Aug 2021 16:52:05 +0200 Subject: [PATCH 03/20] Update .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index dd0b7e80b6..93ddf650bb 100755 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ *.zip -build-* AppRun* *.AppImage *.txz From e6028a2337d9c9f7417253b7d59fe807546ef94f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Wed, 11 Aug 2021 15:05:56 +0200 Subject: [PATCH 04/20] Do not block score with the http server for the remotecontrol --- .../RemoteControl/ApplicationPlugin.cpp | 5 --- .../RemoteControl/ApplicationPlugin.hpp | 2 + .../RemoteControl/Http_server.cpp | 39 +++++-------------- .../RemoteControl/Http_server.hpp | 8 ++-- 4 files changed, 17 insertions(+), 37 deletions(-) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp index 38825b2b86..fb6fc6b9c6 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp @@ -6,7 +6,6 @@ #include #include -#include namespace RemoteControl { @@ -19,10 +18,6 @@ void ApplicationPlugin::on_createdDocument(score::Document& doc) { doc.model().addPluginModel(new DocumentPlugin{ doc.context(), &doc.model()}); - - Http_server Http_server; - - // Http_server.open_server(); } } diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp index 1e562545ea..9e119eff29 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include namespace RemoteControl { class ApplicationPlugin final : public score::GUIApplicationPlugin @@ -10,5 +11,6 @@ class ApplicationPlugin final : public score::GUIApplicationPlugin protected: void on_createdDocument(score::Document& doc) override; + Http_server m_server; }; } diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp index 5c2f77ccc0..75ce90112a 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp @@ -16,27 +16,21 @@ #include //------------------------------------------------------------------------------ - +#include namespace RemoteControl { Http_server::Http_server() - : ioc{1} { - // auto th = [this] { this->ioc.run(); }; - - //th(&Http_server::open_server, this); - - open_server(); - - //th = [this] { this->ioc.run(); }; - + m_docRoot = "/tmp"; + m_serverThread = std::thread{[this] { open_server(); }}; } Http_server::~Http_server() { + shutdown(m_listenSocket, SHUT_RDWR); ioc.stop(); - th.join(); + m_serverThread.join(); } // Return a reasonable mime type based on the extension of a file. @@ -221,8 +215,7 @@ Http_server::fail(beast::error_code ec, char const* what) // Handles an HTTP server connection void Http_server::do_session( - tcp::socket& socket, - std::shared_ptr const& doc_root) + tcp::socket& socket) { bool close = false; beast::error_code ec; @@ -244,7 +237,7 @@ Http_server::do_session( return Http_server::fail(ec, "read"); // Send the response - Http_server::handle_request(*doc_root, std::move(req), lambda); + Http_server::handle_request(m_docRoot, std::move(req), lambda); if(ec) return Http_server::fail(ec, "write"); if(close) @@ -291,15 +284,12 @@ Http_server::open_server() { try { - //std::string ip_address = get_ip_address(); - - // auto const address = net::ip::make_address(ip_address); - auto const address = net::ip::make_address("127.0.0.1"); + auto const address = net::ip::make_address("0.0.0.0"); auto const port = static_cast(std::atoi("8080")); - auto const doc_root = std::make_shared("./build-wasm/"); // The acceptor receives incoming connections tcp::acceptor acceptor{ioc, {address, port}}; + m_listenSocket = acceptor.native_handle(); for(;;) { // This will receive the new connection @@ -309,17 +299,8 @@ Http_server::open_server() acceptor.accept(socket); // Launch the session, transferring ownership of the socket - std::thread{std::bind( - &Http_server::do_session, - this, - std::move(socket), - doc_root)}.join(); - - // auto th1 = [this] { this->do_session(std::move(socket), doc_root); }; - + do_session(socket); } - - auto th = [this] { this->ioc.run(); }; } catch (const std::exception& e) { diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp index 6790b1bf94..a52762cc3d 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp @@ -43,7 +43,6 @@ namespace RemoteControl class Http_server { public: - std::thread th; net::io_context ioc; //std::thread th1; @@ -122,8 +121,7 @@ class Http_server // Handles an HTTP server connection void do_session( - tcp::socket& socket, - std::shared_ptr const& doc_root); + tcp::socket& socket); //------------------------------------------------------------------------------ @@ -134,5 +132,9 @@ class Http_server int open_server(); //void open_server_thread(); + std::thread m_serverThread; + std::string m_docRoot; + + int m_listenSocket{}; }; } From b9f71f6fdc8ee7609c1ee44a08de738e5891f91a Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Thu, 12 Aug 2021 09:49:36 +0200 Subject: [PATCH 05/20] rm some useless include --- .../score-plugin-remotecontrol/RemoteControl/Http_server.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp index a52762cc3d..3407c539df 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp @@ -27,6 +27,10 @@ #include #include +#ifdef _WIN32 +#define SHUT_RDWR 2 +#endif + #include #include #include From de1831f66b7d0952bdbbc52fe9b90cd418ac2d27 Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Thu, 12 Aug 2021 15:14:49 +0200 Subject: [PATCH 06/20] modify some variable and path --- .../score-plugin-remotecontrol/CMakeLists.txt | 2 +- .../RemoteControl/ApplicationPlugin.cpp | 2 ++ .../RemoteControl/Http_server.cpp | 26 ++++++++++++++----- .../RemoteControl/Http_server.hpp | 16 +++++++++--- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/plugins/score-plugin-remotecontrol/CMakeLists.txt b/src/plugins/score-plugin-remotecontrol/CMakeLists.txt index 5be4da36fc..b7278ea621 100644 --- a/src/plugins/score-plugin-remotecontrol/CMakeLists.txt +++ b/src/plugins/score-plugin-remotecontrol/CMakeLists.txt @@ -47,7 +47,7 @@ set(SRCS "${CMAKE_CURRENT_SOURCE_DIR}/score_plugin_remotecontrol.cpp" ) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/build-wasm/ DESTINATION ${CMAKE_BINARY_DIR}/src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/ ) +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/build-wasm/ DESTINATION ${CMAKE_BINARY_DIR}/src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/) add_library(${PROJECT_NAME} ${SRCS} ${HDRS} ${QRCS}) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp index fb6fc6b9c6..7614296562 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp @@ -18,6 +18,8 @@ void ApplicationPlugin::on_createdDocument(score::Document& doc) { doc.model().addPluginModel(new DocumentPlugin{ doc.context(), &doc.model()}); + + m_server.start_thread(); } } diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp index 75ce90112a..39f75b0278 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp @@ -22,8 +22,7 @@ namespace RemoteControl Http_server::Http_server() { - m_docRoot = "/tmp"; - m_serverThread = std::thread{[this] { open_server(); }}; + // m_docRoot = "/tmp"; } Http_server::~Http_server() @@ -122,6 +121,8 @@ Http_server::handle_request( return res; }; + QDir::currentPath(); + // Returns a not found response auto const not_found = [&req](beast::string_view target) @@ -130,7 +131,7 @@ Http_server::handle_request( res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, "text/html"); res.keep_alive(req.keep_alive()); - res.body() = "The resource '" + std::string(target) + "' was not found.
Go to the following address : http://ip_address:port/remote.html."; + res.body() = "The resource '" + std::string(target) + "' was not found.
Current repository :" + QDir::currentPath().toUtf8().constData() +".
Go to the following address : http://ip_address:port/remote.html."; res.prepare_payload(); return res; }; @@ -215,7 +216,8 @@ Http_server::fail(beast::error_code ec, char const* what) // Handles an HTTP server connection void Http_server::do_session( - tcp::socket& socket) + tcp::socket& socket, + std::shared_ptr const& doc_root) { bool close = false; beast::error_code ec; @@ -237,7 +239,7 @@ Http_server::do_session( return Http_server::fail(ec, "read"); // Send the response - Http_server::handle_request(m_docRoot, std::move(req), lambda); + Http_server::handle_request(*doc_root, std::move(req), lambda); if(ec) return Http_server::fail(ec, "write"); if(close) @@ -279,13 +281,23 @@ Http_server::get_ip_address() //------------------------------------------------------------------------------ +void +Http_server::start_thread() +{ + m_serverThread = std::thread{[this] { open_server(); }}; +} + +//------------------------------------------------------------------------------ + int Http_server::open_server() { try { - auto const address = net::ip::make_address("0.0.0.0"); + // auto const address = net::ip::make_address("0.0.0.0"); + auto const address = net::ip::make_address("127.0.0.1"); auto const port = static_cast(std::atoi("8080")); + auto const m_docRoot = std::make_shared("./src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/"); // The acceptor receives incoming connections tcp::acceptor acceptor{ioc, {address, port}}; @@ -299,7 +311,7 @@ Http_server::open_server() acceptor.accept(socket); // Launch the session, transferring ownership of the socket - do_session(socket); + do_session(socket, m_docRoot); } } catch (const std::exception& e) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp index 3407c539df..f689728b3b 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp @@ -125,7 +125,8 @@ class Http_server // Handles an HTTP server connection void do_session( - tcp::socket& socket); + tcp::socket& socket, + std::shared_ptr const& doc_root); //------------------------------------------------------------------------------ @@ -133,11 +134,20 @@ class Http_server //------------------------------------------------------------------------------ - int open_server(); + void + start_thread(); + + //------------------------------------------------------------------------------ + + int + open_server(); + + //------------------------------------------------------------------------------ //void open_server_thread(); std::thread m_serverThread; - std::string m_docRoot; + //std::string m_docRoot; + //auto const m_docRoot = std::make_shared("./build-wasm/"); int m_listenSocket{}; }; From c43b2aa48d7234db8fa8ade4e91fa4244ce9bddf Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Thu, 12 Aug 2021 16:18:45 +0200 Subject: [PATCH 07/20] clean code --- .../RemoteControl/Http_server.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp index 39f75b0278..ece25170a7 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp @@ -121,8 +121,6 @@ Http_server::handle_request( return res; }; - QDir::currentPath(); - // Returns a not found response auto const not_found = [&req](beast::string_view target) @@ -131,7 +129,7 @@ Http_server::handle_request( res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, "text/html"); res.keep_alive(req.keep_alive()); - res.body() = "The resource '" + std::string(target) + "' was not found.
Current repository :" + QDir::currentPath().toUtf8().constData() +".
Go to the following address : http://ip_address:port/remote.html."; + res.body() = "The resource '" + std::string(target) + "' was not found.
Go to the following address : http://ip_address:port/remote.html."; res.prepare_payload(); return res; }; @@ -270,12 +268,10 @@ Http_server::get_ip_address() if(!list[nIter].isLoopback()) { if (list[nIter].protocol() == QAbstractSocket::IPv4Protocol ){ - qDebug() << list[nIter].toString(); ip_address = list[nIter].toString().toUtf8().constData(); } } } - return ip_address; } @@ -294,8 +290,8 @@ Http_server::open_server() { try { - // auto const address = net::ip::make_address("0.0.0.0"); - auto const address = net::ip::make_address("127.0.0.1"); + auto const address = net::ip::make_address("0.0.0.0"); + // auto const address = net::ip::make_address("127.0.0.1"); auto const port = static_cast(std::atoi("8080")); auto const m_docRoot = std::make_shared("./src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/"); From 2304b1aa0986b741f35cd5f3729454dd14d8ab5f Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Mon, 16 Aug 2021 10:30:41 +0200 Subject: [PATCH 08/20] get ip address and set in remote.html --- .../RemoteControl/Http_server.cpp | 54 ++++++++++++++----- .../RemoteControl/Http_server.hpp | 7 +++ 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp index ece25170a7..21c688b64a 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp @@ -129,7 +129,7 @@ Http_server::handle_request( res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, "text/html"); res.keep_alive(req.keep_alive()); - res.body() = "The resource '" + std::string(target) + "' was not found.
Go to the following address : http://ip_address:port/remote.html."; + res.body() = "The resource '" + std::string(target) + "' was not found.y
Go to the following address : http://ip_address:port/remote.html."; res.prepare_payload(); return res; }; @@ -259,20 +259,42 @@ Http_server::do_session( std::string Http_server::get_ip_address() { - std::string ip_address; + std::string ip_address = "127.0.0.1"; + std::string tmp_ip_address; + const QHostAddress &localhost = QHostAddress(QHostAddress::LocalHost); + for (const QHostAddress &address: QNetworkInterface::allAddresses()) { + if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost) { + tmp_ip_address = address.toString().toUtf8().constData(); + qDebug() << "Address : " << address.toString(); + if((tmp_ip_address.find("192.168")) == 0 || (tmp_ip_address.find("172.16")) == 0 ) { + ip_address = tmp_ip_address; + qDebug() << "Address : " << address.toString(); + } + } + } - QList list = QNetworkInterface::allAddresses(); + return ip_address; +} - for(int nIter=0; nIter(std::atoi("8080")); auto const m_docRoot = std::make_shared("./src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/"); + set_ip_address(string_address); + // The acceptor receives incoming connections tcp::acceptor acceptor{ioc, {address, port}}; m_listenSocket = acceptor.native_handle(); diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp index f689728b3b..aa81f7adfd 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp @@ -26,6 +26,7 @@ #include #include #include +#include #ifdef _WIN32 #define SHUT_RDWR 2 @@ -34,6 +35,7 @@ #include #include #include +#include namespace beast = boost::beast; // from namespace http = beast::http; // from @@ -144,6 +146,11 @@ class Http_server //------------------------------------------------------------------------------ + void + set_ip_address(std::string address); + + //------------------------------------------------------------------------------ + //void open_server_thread(); std::thread m_serverThread; //std::string m_docRoot; From 703d9b78847a1ecb58bc73929ca33ab29b1127dd Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Mon, 16 Aug 2021 11:13:44 +0200 Subject: [PATCH 09/20] add comments and clean code --- .../RemoteControl/Http_server.cpp | 14 ++++------- .../RemoteControl/Http_server.hpp | 24 ++++++++----------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp index 21c688b64a..6840917cc3 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp @@ -7,16 +7,10 @@ // Official repository: https://github.com/boostorg/beast // -//------------------------------------------------------------------------------ -// -// Example: HTTP server, synchronous -// -//------------------------------------------------------------------------------ - #include //------------------------------------------------------------------------------ -#include + namespace RemoteControl { @@ -256,6 +250,7 @@ Http_server::do_session( //------------------------------------------------------------------------------ +// Get the IP address. It is an heuristic function std::string Http_server::get_ip_address() { @@ -278,6 +273,7 @@ Http_server::get_ip_address() //------------------------------------------------------------------------------ +// Set the IP address in the remote.html file void Http_server::set_ip_address(std::string address) { @@ -299,6 +295,7 @@ Http_server::set_ip_address(std::string address) //------------------------------------------------------------------------------ +// Launch the open_server function in a thread void Http_server::start_thread() { @@ -307,6 +304,7 @@ Http_server::start_thread() //------------------------------------------------------------------------------ +// Open a server using sockets int Http_server::open_server() { @@ -314,8 +312,6 @@ Http_server::open_server() { std::string string_address = get_ip_address(); auto const address = net::ip::make_address(string_address); - //auto const address = net::ip::make_address("192.168.0.40"); - //auto const address = net::ip::make_address("127.0.0.1"); auto const port = static_cast(std::atoi("8080")); auto const m_docRoot = std::make_shared("./src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/"); diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp index aa81f7adfd..317b4a8f56 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp @@ -7,11 +7,6 @@ // Official repository: https://github.com/boostorg/beast // -//------------------------------------------------------------------------------ -// -// Example: HTTP server, synchronous -// -//------------------------------------------------------------------------------ #pragma once #define BOOST_DATE_TIME_NO_LIB 1 @@ -36,6 +31,7 @@ #include #include #include +#include namespace beast = boost::beast; // from namespace http = beast::http; // from @@ -132,30 +128,30 @@ class Http_server //------------------------------------------------------------------------------ + // Get the IP address. It is an heuristic function std::string get_ip_address(); //------------------------------------------------------------------------------ + // Set the IP address in the remote.html file void - start_thread(); + set_ip_address(std::string address); //------------------------------------------------------------------------------ - int - open_server(); + // Launch the open_server function in a thread + void + start_thread(); //------------------------------------------------------------------------------ - void - set_ip_address(std::string address); + // Open a server using sockets + int + open_server(); //------------------------------------------------------------------------------ - //void open_server_thread(); std::thread m_serverThread; - //std::string m_docRoot; - //auto const m_docRoot = std::make_shared("./build-wasm/"); - int m_listenSocket{}; }; } From ed8d1aa32a27accc365ad989086ed7524beb25b1 Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Mon, 16 Aug 2021 11:22:07 +0200 Subject: [PATCH 10/20] detect ip address which start with 172. --- .../score-plugin-remotecontrol/RemoteControl/Http_server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp index 6840917cc3..603214aad5 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp @@ -261,7 +261,7 @@ Http_server::get_ip_address() if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost) { tmp_ip_address = address.toString().toUtf8().constData(); qDebug() << "Address : " << address.toString(); - if((tmp_ip_address.find("192.168")) == 0 || (tmp_ip_address.find("172.16")) == 0 ) { + if((tmp_ip_address.find("192.168")) == 0 || (tmp_ip_address.find("172.")) == 0 ) { ip_address = tmp_ip_address; qDebug() << "Address : " << address.toString(); } From 0d54c99e07b59e7cc1bab3809740da9cb9a24fee Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Wed, 18 Aug 2021 12:35:34 +0200 Subject: [PATCH 11/20] detect ip address with socket / set keep_alive = false --- .../RemoteControl/Http_server.cpp | 43 ++++++------------- .../RemoteControl/Http_server.hpp | 5 --- 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp index 603214aad5..dd4d053728 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp @@ -180,7 +180,8 @@ Http_server::handle_request( res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); res.content_length(size); - res.keep_alive(req.keep_alive()); + //res.keep_alive(req.keep_alive()); + res.keep_alive(false); return send(std::move(res)); } @@ -192,7 +193,8 @@ Http_server::handle_request( res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); res.content_length(size); - res.keep_alive(req.keep_alive()); + //res.keep_alive(req.keep_alive()); + res.keep_alive(false); return send(std::move(res)); } @@ -250,29 +252,6 @@ Http_server::do_session( //------------------------------------------------------------------------------ -// Get the IP address. It is an heuristic function -std::string -Http_server::get_ip_address() -{ - std::string ip_address = "127.0.0.1"; - std::string tmp_ip_address; - const QHostAddress &localhost = QHostAddress(QHostAddress::LocalHost); - for (const QHostAddress &address: QNetworkInterface::allAddresses()) { - if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost) { - tmp_ip_address = address.toString().toUtf8().constData(); - qDebug() << "Address : " << address.toString(); - if((tmp_ip_address.find("192.168")) == 0 || (tmp_ip_address.find("172.")) == 0 ) { - ip_address = tmp_ip_address; - qDebug() << "Address : " << address.toString(); - } - } - } - - return ip_address; -} - -//------------------------------------------------------------------------------ - // Set the IP address in the remote.html file void Http_server::set_ip_address(std::string address) @@ -310,15 +289,14 @@ Http_server::open_server() { try { - std::string string_address = get_ip_address(); - auto const address = net::ip::make_address(string_address); + auto const address2 = net::ip::make_address("0.0.0.0"); auto const port = static_cast(std::atoi("8080")); auto const m_docRoot = std::make_shared("./src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/"); - set_ip_address(string_address); + bool is_ip_address_set = false; // The acceptor receives incoming connections - tcp::acceptor acceptor{ioc, {address, port}}; + tcp::acceptor acceptor{ioc, {address2, port}}; m_listenSocket = acceptor.native_handle(); for(;;) { @@ -328,6 +306,13 @@ Http_server::open_server() // Block until we get a connection acceptor.accept(socket); + // Set ip address + if(!is_ip_address_set) + { + set_ip_address(socket.local_endpoint().address().to_string()); + is_ip_address_set = true; + } + // Launch the session, transferring ownership of the socket do_session(socket, m_docRoot); } diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp index 317b4a8f56..adee0e6a66 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp @@ -128,11 +128,6 @@ class Http_server //------------------------------------------------------------------------------ - // Get the IP address. It is an heuristic function - std::string get_ip_address(); - - //------------------------------------------------------------------------------ - // Set the IP address in the remote.html file void set_ip_address(std::string address); From 49bb13857aa0b6c959666925c3547f5a7c26c8b2 Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Wed, 18 Aug 2021 14:21:57 +0200 Subject: [PATCH 12/20] clean code --- .../score-plugin-remotecontrol/RemoteControl/Http_server.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp index dd4d053728..a24838cf3f 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp @@ -180,7 +180,6 @@ Http_server::handle_request( res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); res.content_length(size); - //res.keep_alive(req.keep_alive()); res.keep_alive(false); return send(std::move(res)); } @@ -193,7 +192,6 @@ Http_server::handle_request( res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); res.content_length(size); - //res.keep_alive(req.keep_alive()); res.keep_alive(false); return send(std::move(res)); } From c231ef0882d8d3346b8e390effbc54aba8f27df4 Mon Sep 17 00:00:00 2001 From: thibaudk Date: Mon, 9 Aug 2021 10:21:05 +0200 Subject: [PATCH 13/20] add qml-remote submodule --- .gitmodules | 3 +++ 3rdparty/qml-remote | 1 + 2 files changed, 4 insertions(+) create mode 160000 3rdparty/qml-remote diff --git a/.gitmodules b/.gitmodules index fa90567081..36acbcc1ab 100755 --- a/.gitmodules +++ b/.gitmodules @@ -44,3 +44,6 @@ [submodule "3rdparty/hap"] path = 3rdparty/hap url = https://github.com/Vidvox/hap +[submodule "3rdparty/qml-remote"] + path = 3rdparty/qml-remote + url = https://github.com/ossia/qml-remote.git diff --git a/3rdparty/qml-remote b/3rdparty/qml-remote new file mode 160000 index 0000000000..e4427ba798 --- /dev/null +++ b/3rdparty/qml-remote @@ -0,0 +1 @@ +Subproject commit e4427ba7989a8a63273e173d9a1f6f2ee368899d From 0fd3ef53b34a2a74374ccc25d8aeb6a84801b84d Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Thu, 19 Aug 2021 11:47:26 +0200 Subject: [PATCH 14/20] changes from the review --- .gitmodules | 5 +-- .../score-plugin-remotecontrol/CMakeLists.txt | 4 +- .../RemoteControl/ApplicationPlugin.hpp | 4 +- .../{Http_server.cpp => HttpServer.cpp} | 42 ++++++++++--------- .../{Http_server.hpp => HttpServer.hpp} | 9 ++-- 5 files changed, 34 insertions(+), 30 deletions(-) rename src/plugins/score-plugin-remotecontrol/RemoteControl/{Http_server.cpp => HttpServer.cpp} (87%) rename src/plugins/score-plugin-remotecontrol/RemoteControl/{Http_server.hpp => HttpServer.hpp} (95%) diff --git a/.gitmodules b/.gitmodules index 36acbcc1ab..85a8f80583 100755 --- a/.gitmodules +++ b/.gitmodules @@ -43,7 +43,4 @@ url = https://github.com/jcelerier/snappy [submodule "3rdparty/hap"] path = 3rdparty/hap - url = https://github.com/Vidvox/hap -[submodule "3rdparty/qml-remote"] - path = 3rdparty/qml-remote - url = https://github.com/ossia/qml-remote.git + url = https://github.com/Vidvox/hap \ No newline at end of file diff --git a/src/plugins/score-plugin-remotecontrol/CMakeLists.txt b/src/plugins/score-plugin-remotecontrol/CMakeLists.txt index b7278ea621..b010ab5024 100644 --- a/src/plugins/score-plugin-remotecontrol/CMakeLists.txt +++ b/src/plugins/score-plugin-remotecontrol/CMakeLists.txt @@ -25,7 +25,7 @@ set(HDRS "RemoteControl/DocumentPlugin.hpp" "i-score-remote/RemoteApplication.hpp" "score_plugin_remotecontrol.hpp" - "RemoteControl/Http_server.hpp" + "RemoteControl/HttpServer.hpp" ) set(SRCS "${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/Settings/Model.cpp" @@ -42,7 +42,7 @@ set(SRCS "${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/ApplicationPlugin.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/DocumentPlugin.cpp" -"${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/Http_server.cpp" +"${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/HttpServer.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/score_plugin_remotecontrol.cpp" ) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp index 9e119eff29..014b6d3c9f 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include namespace RemoteControl { class ApplicationPlugin final : public score::GUIApplicationPlugin @@ -11,6 +11,6 @@ class ApplicationPlugin final : public score::GUIApplicationPlugin protected: void on_createdDocument(score::Document& doc) override; - Http_server m_server; + HttpServer m_server; }; } diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp similarity index 87% rename from src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp rename to src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp index a24838cf3f..bf40088d59 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp @@ -7,19 +7,19 @@ // Official repository: https://github.com/boostorg/beast // -#include +#include //------------------------------------------------------------------------------ namespace RemoteControl { -Http_server::Http_server() +HttpServer::HttpServer() { // m_docRoot = "/tmp"; } -Http_server::~Http_server() +HttpServer::~HttpServer() { shutdown(m_listenSocket, SHUT_RDWR); ioc.stop(); @@ -28,7 +28,7 @@ Http_server::~Http_server() // Return a reasonable mime type based on the extension of a file. beast::string_view -Http_server::mime_type(beast::string_view path) +HttpServer::mime_type(beast::string_view path) { using beast::iequals; auto const ext = [&path] @@ -65,7 +65,7 @@ Http_server::mime_type(beast::string_view path) // Append an HTTP rel-path to a local filesystem path. // The returned path is normalized for the platform. std::string -Http_server::path_cat( +HttpServer::path_cat( beast::string_view base, beast::string_view path) { @@ -97,7 +97,7 @@ template< class Body, class Allocator, class Send> void -Http_server::handle_request( +HttpServer::handle_request( beast::string_view doc_root, http::request>&& req, Send&& send) @@ -200,14 +200,14 @@ Http_server::handle_request( // Report a failure void -Http_server::fail(beast::error_code ec, char const* what) +HttpServer::fail(beast::error_code ec, char const* what) { std::cerr << what << ": " << ec.message() << "\n"; } // Handles an HTTP server connection void -Http_server::do_session( +HttpServer::do_session( tcp::socket& socket, std::shared_ptr const& doc_root) { @@ -228,12 +228,12 @@ Http_server::do_session( if(ec == http::error::end_of_stream) break; if(ec) - return Http_server::fail(ec, "read"); + return HttpServer::fail(ec, "read"); // Send the response - Http_server::handle_request(*doc_root, std::move(req), lambda); + HttpServer::handle_request(*doc_root, std::move(req), lambda); if(ec) - return Http_server::fail(ec, "write"); + return HttpServer::fail(ec, "write"); if(close) { // This means we should close the connection, usually because @@ -252,13 +252,13 @@ Http_server::do_session( // Set the IP address in the remote.html file void -Http_server::set_ip_address(std::string address) +HttpServer::set_ip_address(std::string address) { - std::rename("./src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/remote.html", - "./src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/remote.html~"); + QDebug << "buildWasmPath :" << buildWasmPath; + std::rename(buildWasmPath + "remote.html", buildWasmPath + "remote.html~"); - std::ifstream old_file("./src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/remote.html~"); - std::ofstream new_file("./src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/remote.html"); + std::ifstream old_file(buildWasmPath + "remote.html~"); + std::ofstream new_file(buildWasmPath + "remote.html"); std::string addr = "\"" + address + "\""; @@ -274,7 +274,7 @@ Http_server::set_ip_address(std::string address) // Launch the open_server function in a thread void -Http_server::start_thread() +HttpServer::start_thread() { m_serverThread = std::thread{[this] { open_server(); }}; } @@ -283,13 +283,17 @@ Http_server::start_thread() // Open a server using sockets int -Http_server::open_server() +HttpServer::open_server() { try { auto const address2 = net::ip::make_address("0.0.0.0"); auto const port = static_cast(std::atoi("8080")); - auto const m_docRoot = std::make_shared("./src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/"); + // auto const m_docRoot = std::make_shared("./src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/"); + std::string packagesPath = score::AppContext().settings().getPackagesPath().toUtf8().constData(); + QDebug << "packagesPath :" << packagesPath; + buildWasmPath = packagesPath + "/build-wasm/"; + auto const m_docRoot = std::make_shared(buildWasmPath); bool is_ip_address_set = false; diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp similarity index 95% rename from src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp rename to src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp index adee0e6a66..cfa3ae28c8 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/Http_server.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp @@ -22,6 +22,8 @@ #include #include #include +#include +#include #ifdef _WIN32 #define SHUT_RDWR 2 @@ -42,16 +44,16 @@ using tcp = boost::asio::ip::tcp; // from namespace RemoteControl { -class Http_server +class HttpServer { public: net::io_context ioc; //std::thread th1; - Http_server(); + HttpServer(); - ~Http_server(); + ~HttpServer(); //------------------------------------------------------------------------------ @@ -148,5 +150,6 @@ class Http_server std::thread m_serverThread; int m_listenSocket{}; + std::string buildWasmPath; }; } From c8b914cd2da223f1dbac0c2d7576619b2eba3d8e Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Thu, 19 Aug 2021 16:41:58 +0200 Subject: [PATCH 15/20] changes from the review --- .../score-plugin-remotecontrol/CMakeLists.txt | 2 +- .../RemoteControl/ApplicationPlugin.cpp | 6 ++++++ .../RemoteControl/ApplicationPlugin.hpp | 1 + .../RemoteControl/HttpServer.cpp | 17 ++++++++--------- .../RemoteControl/HttpServer.hpp | 2 +- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/plugins/score-plugin-remotecontrol/CMakeLists.txt b/src/plugins/score-plugin-remotecontrol/CMakeLists.txt index b010ab5024..229538d28a 100644 --- a/src/plugins/score-plugin-remotecontrol/CMakeLists.txt +++ b/src/plugins/score-plugin-remotecontrol/CMakeLists.txt @@ -47,7 +47,7 @@ set(SRCS "${CMAKE_CURRENT_SOURCE_DIR}/score_plugin_remotecontrol.cpp" ) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/build-wasm/ DESTINATION ${CMAKE_BINARY_DIR}/src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/) +#file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/build-wasm/ DESTINATION ${CMAKE_BINARY_DIR}/src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/) add_library(${PROJECT_NAME} ${SRCS} ${HDRS} ${QRCS}) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp index 7614296562..2ed6df7599 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp @@ -14,6 +14,12 @@ ApplicationPlugin::ApplicationPlugin(const score::GUIApplicationContext& app) { } +ApplicationPlugin::~ApplicationPlugin() +{ + std::remove((m_server.m_buildWasmPath + "remote.html").c_str()); + std::rename((m_server.m_buildWasmPath + "remote.html~").c_str(), (m_server.m_buildWasmPath + "remote.html").c_str()); +} + void ApplicationPlugin::on_createdDocument(score::Document& doc) { doc.model().addPluginModel(new DocumentPlugin{ diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp index 014b6d3c9f..c4b8b60509 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp @@ -8,6 +8,7 @@ class ApplicationPlugin final : public score::GUIApplicationPlugin { public: ApplicationPlugin(const score::GUIApplicationContext& app); + ~ApplicationPlugin(); protected: void on_createdDocument(score::Document& doc) override; diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp index bf40088d59..36492e6009 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp @@ -254,11 +254,11 @@ HttpServer::do_session( void HttpServer::set_ip_address(std::string address) { - QDebug << "buildWasmPath :" << buildWasmPath; - std::rename(buildWasmPath + "remote.html", buildWasmPath + "remote.html~"); + qDebug() << "buildWasmPath :" << QString::fromStdString(m_buildWasmPath); + std::rename((m_buildWasmPath + "remote.html").c_str(), (m_buildWasmPath + "remote.html~").c_str()); - std::ifstream old_file(buildWasmPath + "remote.html~"); - std::ofstream new_file(buildWasmPath + "remote.html"); + std::ifstream old_file(m_buildWasmPath + "remote.html~"); + std::ofstream new_file(m_buildWasmPath + "remote.html"); std::string addr = "\"" + address + "\""; @@ -289,11 +289,10 @@ HttpServer::open_server() { auto const address2 = net::ip::make_address("0.0.0.0"); auto const port = static_cast(std::atoi("8080")); - // auto const m_docRoot = std::make_shared("./src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/"); - std::string packagesPath = score::AppContext().settings().getPackagesPath().toUtf8().constData(); - QDebug << "packagesPath :" << packagesPath; - buildWasmPath = packagesPath + "/build-wasm/"; - auto const m_docRoot = std::make_shared(buildWasmPath); + std::string packagesPath = score::AppContext().settings().getPackagesPath().toStdString(); + qDebug() << "packagesPath :" << QString::fromStdString(packagesPath); + m_buildWasmPath = packagesPath + "/build-wasm/"; + auto const m_docRoot = std::make_shared(m_buildWasmPath); bool is_ip_address_set = false; diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp index cfa3ae28c8..d4b63682b5 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp @@ -150,6 +150,6 @@ class HttpServer std::thread m_serverThread; int m_listenSocket{}; - std::string buildWasmPath; + std::string m_buildWasmPath; }; } From 819619eddaaf77c6dfe1db9021d46526d29b2f2d Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Fri, 20 Aug 2021 22:09:11 +0200 Subject: [PATCH 16/20] load remote.html, read and write ip address --- .../RemoteControl/ApplicationPlugin.cpp | 4 ++-- .../RemoteControl/HttpServer.cpp | 21 ++++++++++++++++++- .../RemoteControl/HttpServer.hpp | 1 + 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp index 2ed6df7599..8a02148ff4 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp @@ -16,8 +16,8 @@ ApplicationPlugin::ApplicationPlugin(const score::GUIApplicationContext& app) ApplicationPlugin::~ApplicationPlugin() { - std::remove((m_server.m_buildWasmPath + "remote.html").c_str()); - std::rename((m_server.m_buildWasmPath + "remote.html~").c_str(), (m_server.m_buildWasmPath + "remote.html").c_str()); + //std::remove((m_server.m_buildWasmPath + "remote.html").c_str()); + //std::rename((m_server.m_buildWasmPath + "remote.html~").c_str(), (m_server.m_buildWasmPath + "remote.html").c_str()); } void ApplicationPlugin::on_createdDocument(score::Document& doc) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp index 36492e6009..cdba4c9d14 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp @@ -160,6 +160,7 @@ HttpServer::handle_request( // Attempt to open the file beast::error_code ec; http::file_body::value_type body; + body.open(path.c_str(), beast::file_mode::scan, ec); // Handle the case where the file doesn't exist @@ -173,6 +174,21 @@ HttpServer::handle_request( // Cache the size since we need it after the move auto const size = body.size(); + if ( req.target().find("remote.html") != std::string::npos ) + { + qDebug() << "Path name:" << req.target().data(); + QFile f(path.c_str()); + f.open(QIODevice::ReadOnly); + QByteArray remote = f.readAll(); + std::string string_remote = remote.toStdString(); + qDebug() << QString::fromStdString(string_remote); + std::string::size_type position = string_remote.find("%SCORE_IP_ADDRESS%"); + std::string address = "192.168.0.40"; + std::string addr = "\"" + address + "\""; + string_remote = string_remote.replace(position, 18, addr); + qDebug() << QString::fromStdString(string_remote); + } + // Respond to HEAD request if(req.method() == http::verb::head) { @@ -265,7 +281,10 @@ HttpServer::set_ip_address(std::string address) for( std::string contents_of_file; std::getline(old_file, contents_of_file); ) { std::string::size_type position = contents_of_file.find("%SCORE_IP_ADDRESS%"); if( position != std::string::npos ) - contents_of_file = contents_of_file.replace(position, 18, addr); + { + //contents_of_file = contents_of_file.replace(position, 18, addr); + contents_of_file = contents_of_file.replace(position, 18, "%SCORE_IP_ADDRESS%"); + } new_file << contents_of_file << '\n'; } } diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp index d4b63682b5..8e12871cee 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp @@ -34,6 +34,7 @@ #include #include #include +#include namespace beast = boost::beast; // from namespace http = beast::http; // from From 48f003688cda2a6f1301f0a2af28b44e3d880a21 Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Fri, 20 Aug 2021 22:12:27 +0200 Subject: [PATCH 17/20] Update CMakeLists.txt --- src/plugins/score-plugin-remotecontrol/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/plugins/score-plugin-remotecontrol/CMakeLists.txt b/src/plugins/score-plugin-remotecontrol/CMakeLists.txt index 229538d28a..62538b9f77 100644 --- a/src/plugins/score-plugin-remotecontrol/CMakeLists.txt +++ b/src/plugins/score-plugin-remotecontrol/CMakeLists.txt @@ -47,8 +47,6 @@ set(SRCS "${CMAKE_CURRENT_SOURCE_DIR}/score_plugin_remotecontrol.cpp" ) -#file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/RemoteControl/build-wasm/ DESTINATION ${CMAKE_BINARY_DIR}/src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/) - add_library(${PROJECT_NAME} ${SRCS} ${HDRS} ${QRCS}) target_link_libraries(${PROJECT_NAME} PUBLIC score_plugin_scenario score_plugin_js ${QT_PREFIX}::WebSockets ossia) From be79bdbfbc33cd831765956ab3a156fc56cf1f2f Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Fri, 20 Aug 2021 22:12:55 +0200 Subject: [PATCH 18/20] Update ApplicationPlugin.cpp --- .../RemoteControl/ApplicationPlugin.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp index 8a02148ff4..7614296562 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.cpp @@ -14,12 +14,6 @@ ApplicationPlugin::ApplicationPlugin(const score::GUIApplicationContext& app) { } -ApplicationPlugin::~ApplicationPlugin() -{ - //std::remove((m_server.m_buildWasmPath + "remote.html").c_str()); - //std::rename((m_server.m_buildWasmPath + "remote.html~").c_str(), (m_server.m_buildWasmPath + "remote.html").c_str()); -} - void ApplicationPlugin::on_createdDocument(score::Document& doc) { doc.model().addPluginModel(new DocumentPlugin{ From b1914f33cd5b69dd8b786997992097244d3a2af9 Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Fri, 20 Aug 2021 22:13:25 +0200 Subject: [PATCH 19/20] Update ApplicationPlugin.hpp --- .../RemoteControl/ApplicationPlugin.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp index c4b8b60509..014b6d3c9f 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/ApplicationPlugin.hpp @@ -8,7 +8,6 @@ class ApplicationPlugin final : public score::GUIApplicationPlugin { public: ApplicationPlugin(const score::GUIApplicationContext& app); - ~ApplicationPlugin(); protected: void on_createdDocument(score::Document& doc) override; From ab4893e951d9933e0f7d6eb2d244f40a4b2ed113 Mon Sep 17 00:00:00 2001 From: Toine-ddt <65868237+Toine-ddt@users.noreply.github.com> Date: Fri, 20 Aug 2021 22:22:20 +0200 Subject: [PATCH 20/20] clean code --- .../RemoteControl/HttpServer.cpp | 14 ++++++-------- .../RemoteControl/HttpServer.hpp | 1 + 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp index cdba4c9d14..0d534cb0ef 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.cpp @@ -176,17 +176,16 @@ HttpServer::handle_request( if ( req.target().find("remote.html") != std::string::npos ) { - qDebug() << "Path name:" << req.target().data(); + // Load file QFile f(path.c_str()); f.open(QIODevice::ReadOnly); QByteArray remote = f.readAll(); + + // Write ip address in the std::string std::string string_remote = remote.toStdString(); - qDebug() << QString::fromStdString(string_remote); std::string::size_type position = string_remote.find("%SCORE_IP_ADDRESS%"); - std::string address = "192.168.0.40"; - std::string addr = "\"" + address + "\""; + std::string addr = "\"" + m_ipAddress + "\""; string_remote = string_remote.replace(position, 18, addr); - qDebug() << QString::fromStdString(string_remote); } // Respond to HEAD request @@ -276,7 +275,7 @@ HttpServer::set_ip_address(std::string address) std::ifstream old_file(m_buildWasmPath + "remote.html~"); std::ofstream new_file(m_buildWasmPath + "remote.html"); - std::string addr = "\"" + address + "\""; + std::string addr = "\"" + m_ipAddress + "\""; for( std::string contents_of_file; std::getline(old_file, contents_of_file); ) { std::string::size_type position = contents_of_file.find("%SCORE_IP_ADDRESS%"); @@ -309,7 +308,6 @@ HttpServer::open_server() auto const address2 = net::ip::make_address("0.0.0.0"); auto const port = static_cast(std::atoi("8080")); std::string packagesPath = score::AppContext().settings().getPackagesPath().toStdString(); - qDebug() << "packagesPath :" << QString::fromStdString(packagesPath); m_buildWasmPath = packagesPath + "/build-wasm/"; auto const m_docRoot = std::make_shared(m_buildWasmPath); @@ -329,7 +327,7 @@ HttpServer::open_server() // Set ip address if(!is_ip_address_set) { - set_ip_address(socket.local_endpoint().address().to_string()); + m_ipAddress = socket.local_endpoint().address().to_string(); is_ip_address_set = true; } diff --git a/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp index 8e12871cee..c8497bb80a 100644 --- a/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp +++ b/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer.hpp @@ -152,5 +152,6 @@ class HttpServer std::thread m_serverThread; int m_listenSocket{}; std::string m_buildWasmPath; + std::string m_ipAddress; }; }