Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/wasm remote #1287

Open
wants to merge 9 commits into
base: feature/wasm-remote
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@
[submodule "3rdparty/hap"]
path = 3rdparty/hap
url = https://github.com/Vidvox/hap
[submodule "3rdparty/qml-remote"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I don't think we want the qml remote repository like that

path = 3rdparty/qml-remote
url = https://github.com/ossia/qml-remote.git
1 change: 1 addition & 0 deletions 3rdparty/qml-remote
Submodule qml-remote added at e4427b
13 changes: 13 additions & 0 deletions src/lib/score/widgets/QuantificationWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
#include <score/widgets/QuantificationWidget.hpp>

#include <QAbstractItemModel>
#include <QAction>
#include <QIcon>
#include <QMenu>
#include <QMouseEvent>
#include <QString>
#include <QVariant>
#include <qnamespace.h>

#include <wobjectimpl.h>

Q_DECLARE_METATYPE(QModelIndex)

W_OBJECT_IMPL(score::QuantificationWidget)

namespace score
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/score-plugin-remotecontrol/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ void ApplicationPlugin::on_createdDocument(score::Document& doc)
{
doc.model().addPluginModel(new DocumentPlugin{
doc.context(), &doc.model()});

m_server.start_thread();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,16 @@
// Official repository: https://github.com/boostorg/beast
//

//------------------------------------------------------------------------------
//
// Example: HTTP server, synchronous
//
//------------------------------------------------------------------------------

#include <RemoteControl/Http_server.hpp>

//------------------------------------------------------------------------------
#include <QApplication>

namespace RemoteControl
{

Http_server::Http_server()
{
m_docRoot = "/tmp";
m_serverThread = std::thread{[this] { open_server(); }};
// m_docRoot = "/tmp";
}

Http_server::~Http_server()
Expand Down Expand Up @@ -130,7 +123,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.<br> Go to the following address : http://ip_address:port/remote.html.";
res.body() = "The resource '" + std::string(target) + "' was not found.y<br> Go to the following address : http://ip_address:port/remote.html.";
res.prepare_payload();
return res;
};
Expand Down Expand Up @@ -187,7 +180,7 @@ 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));
}

Expand All @@ -199,7 +192,7 @@ 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));
}

Expand All @@ -215,7 +208,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<std::string const> const& doc_root)
{
bool close = false;
beast::error_code ec;
Expand All @@ -237,7 +231,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)
Expand All @@ -256,39 +250,51 @@ Http_server::do_session(

//------------------------------------------------------------------------------

std::string
Http_server::get_ip_address()
// Set the IP address in the remote.html file
void
Http_server::set_ip_address(std::string address)
{
std::string ip_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~");

QList<QHostAddress> list = QNetworkInterface::allAddresses();
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");

for(int nIter=0; nIter<list.count(); nIter++)
{
if(!list[nIter].isLoopback())
{
if (list[nIter].protocol() == QAbstractSocket::IPv4Protocol ){
qDebug() << list[nIter].toString();
ip_address = list[nIter].toString().toUtf8().constData();
}
}
std::string addr = "\"" + 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);
new_file << contents_of_file << '\n';
}
}

//------------------------------------------------------------------------------

return ip_address;
// Launch the open_server function in a thread
void
Http_server::start_thread()
{
m_serverThread = std::thread{[this] { open_server(); }};
}

//------------------------------------------------------------------------------

// Open a server using sockets
int
Http_server::open_server()
{
try
{
auto const address = net::ip::make_address("0.0.0.0");
auto const address2 = net::ip::make_address("0.0.0.0");
auto const port = static_cast<unsigned short>(std::atoi("8080"));
auto const m_docRoot = std::make_shared<std::string>("./src/plugins/score-plugin-remotecontrol/CMakeFiles/score_plugin_remotecontrol.dir/RemoteControl/build-wasm/");

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(;;)
{
Expand All @@ -298,8 +304,15 @@ 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);
do_session(socket, m_docRoot);
}
}
catch (const std::exception& e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
// Official repository: https://github.com/boostorg/beast
//

//------------------------------------------------------------------------------
//
// Example: HTTP server, synchronous
//
//------------------------------------------------------------------------------
#pragma once

#define BOOST_DATE_TIME_NO_LIB 1
Expand All @@ -26,10 +21,17 @@
#include <memory>
#include <string>
#include <thread>
#include <fstream>

#ifdef _WIN32
#define SHUT_RDWR 2
#endif

#include <QDir>
#include <QHostAddress>
#include <QNetworkInterface>
#include <QtDebug>
#include <QApplication>

namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
Expand Down Expand Up @@ -121,20 +123,30 @@ class Http_server
// Handles an HTTP server connection
void
do_session(
tcp::socket& socket);
tcp::socket& socket,
std::shared_ptr<std::string const> const& doc_root);

//------------------------------------------------------------------------------

std::string get_ip_address();
// Set the IP address in the remote.html file
void
set_ip_address(std::string address);

//------------------------------------------------------------------------------

int open_server();
// Launch the open_server function in a thread
void
start_thread();

//void open_server_thread();
std::thread m_serverThread;
std::string m_docRoot;
//------------------------------------------------------------------------------

// Open a server using sockets
int
open_server();

//------------------------------------------------------------------------------

std::thread m_serverThread;
int m_listenSocket{};
};
}