Skip to content

Commit

Permalink
[oscquery] Implement support for dense packing of values
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Nov 9, 2024
1 parent 9044dfa commit cfff7f7
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ namespace Protocols
QThread DNSSDEnumerator::g_dnssd_worker_thread;
DNSSDEnumerator::DNSSDEnumerator(const std::string& service)
{
g_dnssd_worker_thread.setObjectName("Foo");
g_dnssd_worker_thread.start();
m_worker = new DNSSDWorker{service};
}
Expand Down
38 changes: 36 additions & 2 deletions src/plugins/score-plugin-protocols/Protocols/OSC/OSCDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,39 @@ struct osc_protocols
ossia::net::rate_limiting_protocol* ratelimit{};
};

struct convert_osc_transport_to_server
{
using ret = std::optional<ossia::net::osc_server_configuration>;
ret operator()(const ossia::net::udp_configuration& conf)
{
if(conf.local)
return ossia::net::udp_server_configuration{*conf.local};
return std::nullopt;
}
ret operator()(const ossia::net::unix_dgram_configuration& conf)
{
if(conf.local)
return ossia::net::unix_dgram_server_configuration{*conf.local};
return std::nullopt;
}
ret operator()(const ossia::net::unix_stream_configuration& conf)
{
return std::nullopt;
}
ret operator()(const ossia::net::serial_configuration& conf) { return conf; }
ret operator()(const ossia::net::tcp_server_configuration& conf) { return conf; }
ret operator()(const ossia::net::ws_server_configuration& conf) { return conf; }
ret operator()(const ossia::net::tcp_client_configuration& conf)
{
return std::nullopt;
}
ret operator()(const ossia::net::ws_client_configuration& conf)
{
return std::nullopt;
}

ret operator()(const auto& conf) { return conf; }
};
osc_protocols make_osc_protocol(
const ossia::net::network_context_ptr& m_ctx, const OSCSpecificSettings& stgs)
{
Expand All @@ -68,9 +101,10 @@ osc_protocols make_osc_protocol(
return {};

std::vector<ossia::net::osc_server_configuration> conf;

ossia::visit([&](const auto& elt) {
if constexpr(requires { conf.push_back(elt); })
conf.push_back(elt);
if(auto t = convert_osc_transport_to_server{}(elt))
conf.push_back(std::move(*t));
}, stgs.configuration.transport);

if(auto proto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <ossia/network/oscquery/oscquery_mirror.hpp>
#include <ossia/network/rate_limiting_protocol.hpp>
#include <ossia/protocols/oscquery/oscquery_mirror_asio.hpp>
#include <ossia/protocols/oscquery/oscquery_mirror_asio_dense.hpp>

#include <boost/algorithm/string.hpp>
#include <boost/asio/io_service.hpp>
Expand All @@ -31,29 +32,29 @@
#include <memory>
W_OBJECT_IMPL(Protocols::OSCQueryDevice)

bool resolve_ip(std::string host)
static bool resolve_ip(const std::string& host)
{
try
{
std::string m_queryPort;
auto m_queryHost = host;
auto port_idx = m_queryHost.find_last_of(':');
std::string queryPort;
auto queryHost = host;
auto port_idx = queryHost.find_last_of(':');
if(port_idx != std::string::npos)
{
m_queryPort = m_queryHost.substr(port_idx + 1);
m_queryHost = m_queryHost.substr(0, port_idx);
queryPort = queryHost.substr(port_idx + 1);
queryHost = queryHost.substr(0, port_idx);
}
else
m_queryPort = "80";
queryPort = "80";

if(boost::starts_with(m_queryHost, "http://"))
m_queryHost.erase(m_queryHost.begin(), m_queryHost.begin() + 7);
else if(boost::starts_with(m_queryHost, "ws://"))
m_queryHost.erase(m_queryHost.begin(), m_queryHost.begin() + 5);
if(boost::starts_with(queryHost, "http://"))
queryHost.erase(queryHost.begin(), queryHost.begin() + 7);
else if(boost::starts_with(queryHost, "ws://"))
queryHost.erase(queryHost.begin(), queryHost.begin() + 5);

boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(m_queryHost, m_queryPort);
boost::asio::ip::tcp::resolver::query query(queryHost, queryPort);
boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query);
return true;
}
Expand All @@ -65,6 +66,7 @@ bool resolve_ip(std::string host)
}
return false;
}

namespace Protocols
{
OSCQueryDevice::OSCQueryDevice(
Expand Down Expand Up @@ -201,11 +203,21 @@ void OSCQueryDevice::slot_createDevice()

try
{
std::unique_ptr<ossia::net::protocol_base> ossia_settings
= std::make_unique<mirror_proto>(m_ctx, stgs.host.toStdString(), stgs.localPort);
std::unique_ptr<ossia::net::protocol_base> ossia_settings;
if(stgs.dense)
{
ossia_settings
= std::make_unique<ossia::oscquery_asio::oscquery_mirror_asio_protocol_dense>(
m_ctx, stgs.host.toStdString(), stgs.localPort);
}
else
{
ossia_settings
= std::make_unique<ossia::oscquery_asio::oscquery_mirror_asio_protocol>(
m_ctx, stgs.host.toStdString(), stgs.localPort);
}

auto& p = static_cast<mirror_proto&>(*ossia_settings);
m_mirror = &p;
m_mirror = ossia_settings.get();

if(stgs.rate)
{
Expand All @@ -222,9 +234,9 @@ void OSCQueryDevice::slot_createDevice()

deviceChanged(nullptr, m_dev.get());

//p.set_command_callback([this] { sig_command(); });
p.on_connection_closed.connect<&OSCQueryDevice::sig_disconnect>(*this);
p.on_connection_failure.connect<&OSCQueryDevice::sig_disconnect>(*this);
//m_mirror->set_command_callback([this] { sig_command(); });
m_mirror->on_connection_closed.connect<&OSCQueryDevice::sig_disconnect>(*this);
m_mirror->on_connection_failure.connect<&OSCQueryDevice::sig_disconnect>(*this);

setLogging_impl(Device::get_cur_logging(isLogging()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class OSCQueryDevice final : public Device::OwningDeviceInterface
void slot_createDevice();
W_SLOT(slot_createDevice);

using mirror_proto = ossia::oscquery_asio::oscquery_mirror_asio_protocol;
mirror_proto* m_mirror{};
ossia::net::protocol_base* m_mirror{};
bool m_connected{};
Device::DeviceSettings m_oldSettings;
const ossia::net::network_context_ptr& m_ctx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class OSCQueryEnumerator final : public DNSSDEnumerator
.arg(websockets ? "ws" : "http")
.arg(ws_ip.isEmpty() ? ip : ws_ip)
.arg(ws_port.isEmpty() ? port : ws_port);

sub.dense = doc.object()["EXTENSIONS"].toObject()["DENSE"].toBool();
set.deviceSpecificSettings = QVariant::fromValue(std::move(sub));
deviceAdded(set.name, set);
ret->deleteLater();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,21 @@ OSCQueryProtocolSettingsWidget::OSCQueryProtocolSettingsWidget(QWidget* parent)
"feedback from an external software. If 0, a random port will be chosen."));
checkForChanges(m_localPort);

m_dense = new QCheckBox("Dense message packing", this);
m_dense->setChecked(false);
m_dense->setEnabled(false);
m_localPort->setWhatsThis(
tr("Indicated whether the remote API supports dense packing of values ; useful "
"mainly for low-power embedded devices."));

QFormLayout* layout = new QFormLayout;

layout->addRow(tr("Name"), m_deviceNameEdit);
layout->addRow(tr("Host"), m_localHostEdit);
layout->addRow(tr("Local port"), m_localPort);
layout->addRow(tr("Rate"), m_rate);
layout->addRow(new QLabel("Supported extensions: "));
layout->addRow(m_dense);

layout->addRow(
"",
Expand All @@ -60,15 +69,11 @@ OSCQueryProtocolSettingsWidget::OSCQueryProtocolSettingsWidget(QWidget* parent)

setLayout(layout);

setDefaults();
}

void OSCQueryProtocolSettingsWidget::setDefaults()
{
m_deviceNameEdit->setText("newDevice");
m_localHostEdit->setText("ws://127.0.0.1:5678");
m_rate->setRate({});
m_localPort->setValue(0);
m_dense->setChecked(false);
}

Device::DeviceSettings OSCQueryProtocolSettingsWidget::getSettings() const
Expand All @@ -81,6 +86,7 @@ Device::DeviceSettings OSCQueryProtocolSettingsWidget::getSettings() const
OSCQuery.host = m_localHostEdit->text();
OSCQuery.rate = m_rate->rate();
OSCQuery.localPort = m_localPort->value();
OSCQuery.dense = m_dense->isChecked();

s.deviceSpecificSettings = QVariant::fromValue(OSCQuery);
return s;
Expand All @@ -96,6 +102,7 @@ void OSCQueryProtocolSettingsWidget::setSettings(const Device::DeviceSettings& s
m_localHostEdit->setText(OSCQuery.host);
m_localPort->setValue(OSCQuery.localPort);
m_rate->setRate(OSCQuery.rate);
m_dense->setChecked(OSCQuery.dense);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ class OSCQueryProtocolSettingsWidget final : public Device::ProtocolSettingsWidg

void setSettings(const Device::DeviceSettings& settings) override;

protected:
void setDefaults();

protected:
QLineEdit* m_deviceNameEdit{};
QLineEdit* m_localHostEdit{};
QSpinBox* m_localPort{};
QCheckBox* m_dense{};

RateWidget* m_rate{};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct OSCQuerySpecificSettings
QString host;
std::optional<int> rate{};
int localPort{};
bool dense{};
};
}
Q_DECLARE_METATYPE(Protocols::OSCQuerySpecificSettings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
template <>
void DataStreamReader::read(const Protocols::OSCQuerySpecificSettings& n)
{
m_stream << n.host << n.rate << n.localPort;
m_stream << n.host << n.rate << n.localPort << n.dense;
insertDelimiter();
}

template <>
void DataStreamWriter::write(Protocols::OSCQuerySpecificSettings& n)
{
m_stream >> n.host >> n.rate >> n.localPort;
m_stream >> n.host >> n.rate >> n.localPort >> n.dense;
checkDelimiter();
}

Expand All @@ -27,6 +27,8 @@ void JSONReader::read(const Protocols::OSCQuerySpecificSettings& n)
obj["Rate"] = *n.rate;

obj["LocalPort"] = n.localPort;
if(n.dense)
obj["Dense"] = true;
}

template <>
Expand All @@ -37,4 +39,6 @@ void JSONWriter::write(Protocols::OSCQuerySpecificSettings& n)
n.rate = it->toInt();
if(auto it = obj.tryGet("LocalPort"))
n.localPort = it->toInt();
if(auto it = obj.tryGet("Dense"))
n.localPort = it->toBool();
}

0 comments on commit cfff7f7

Please sign in to comment.