Skip to content

osc: work on adding more features to rate limiter #1676

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

Open
wants to merge 6 commits into
base: master
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
15 changes: 13 additions & 2 deletions src/lib/score/serialization/JSONVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ class SCORE_LIB_BASE_EXPORT JSONReader : public AbstractVisitor

void read(const QString&) const noexcept = delete;
void read(const float&) const noexcept = delete;
void read(const double&) const noexcept = delete;
void read(const char&) const noexcept = delete;
void read(const int&) const noexcept = delete;
void read(const int16_t&) const noexcept = delete;
void read(const int32_t&) const noexcept = delete;
void read(const int64_t&) const noexcept = delete;
void read(const uint16_t&) const noexcept = delete;
void read(const uint32_t&) const noexcept = delete;
void read(const uint64_t&) const noexcept = delete;
void read(const bool&) const noexcept = delete;
void read(const std::string&) const noexcept = delete;
void read(const unsigned int&) const noexcept = delete;
void read(const unsigned char&) const noexcept = delete;

//! Called by code that wants to serialize.
Expand Down Expand Up @@ -240,6 +245,8 @@ class SCORE_LIB_BASE_EXPORT JSONReader : public AbstractVisitor
void readFromAbstract(const T& in, Fun f);
};

using long_long
= std::conditional_t<std::is_same_v<int64_t, long long>, void******, long long>;
struct JSONReader::assigner
{
JSONReader& self;
Expand Down Expand Up @@ -333,6 +340,10 @@ struct JSONReader::assigner
Q_UNUSED(_);
self.stream.Int(static_cast<int32_t>(t));
}
else if constexpr(std::is_same_v<long long, T>)
{
self.stream.Int64(t);
}
else
{
self.readFrom(t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ bool CoAPDevice::reconnect()
{
if(stgs.rate)
{
ossia::net::rate_limiter_configuration conf{
.duration = std::chrono::milliseconds(*stgs.rate)};
auto rate = std::make_unique<ossia::net::rate_limiting_protocol>(
std::chrono::milliseconds{*stgs.rate}, std::move(proto));
conf, std::move(proto));
m_dev = std::make_unique<ossia::net::generic_device>(std::move(rate), name);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ bool MQTTDevice::reconnect()
{
if(stgs.rate)
{
ossia::net::rate_limiter_configuration conf{
.duration = std::chrono::milliseconds(*stgs.rate)};
auto rate = std::make_unique<ossia::net::rate_limiting_protocol>(
std::chrono::milliseconds{*stgs.rate}, std::move(proto));
conf, std::move(proto));
m_dev = std::make_unique<ossia::net::generic_device>(std::move(rate), name);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ bool MinuitDevice::reconnect()

if(stgs.rate)
{
ossia::net::rate_limiter_configuration conf{
.duration = std::chrono::milliseconds(*stgs.rate)};
ossia_settings = std::make_unique<ossia::net::rate_limiting_protocol>(
std::chrono::milliseconds{*stgs.rate}, std::move(ossia_settings));
conf, std::move(ossia_settings));
}

m_dev = std::make_unique<ossia::net::generic_device>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,14 @@ osc_protocols make_osc_protocol(

if(stgs.rate)
{
auto rate_stgs = *stgs.rate;
if(stgs.configuration.bundle_strategy
== ossia::net::osc_protocol_configuration::ALWAYS_BUNDLE)
{
rate_stgs.bundle = true;
}
auto rl = std::make_unique<ossia::net::rate_limiting_protocol>(
std::chrono::milliseconds{*stgs.rate}, std::move(protos.ret));
rate_stgs, std::move(protos.ret));
protos.ratelimit = rl.get();
protos.ret = std::move(rl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ Device::DeviceSettings OSCProtocolSettingsWidget::getSettings() const
osc.configuration.bundle_strategy = this->m_bundle->isChecked()
? osc_bundle_t::ALWAYS_BUNDLE
: osc_bundle_t::NEVER_BUNDLE;
osc.rate = m_rate->rate();

osc.rate = m_rate->configuration();
osc.bonjour = m_bonjour->isChecked();
osc.oscquery = m_oscquery->value();
if(osc.oscquery.value() <= 0)
Expand Down Expand Up @@ -276,7 +277,7 @@ void OSCProtocolSettingsWidget::setSettings(const Device::DeviceSettings& settin
= decltype(ossia::net::osc_protocol_configuration::bundle_strategy);
m_settings = settings.deviceSpecificSettings.value<OSCSpecificSettings>();
m_oscVersion->setCurrentIndex(m_settings.configuration.version);
m_rate->setRate(m_settings.rate);
m_rate->setConfiguration(m_settings.rate);
m_bonjour->setChecked(m_settings.bonjour);
m_bundle->setChecked(
m_settings.configuration.bundle_strategy == osc_bundle_t::ALWAYS_BUNDLE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <ossia/detail/optional.hpp>
#include <ossia/network/rate_limiter_configuration.hpp>
#include <ossia/protocols/osc/osc_factory.hpp>

#include <QString>
Expand All @@ -11,7 +12,7 @@ namespace Protocols
struct OSCSpecificSettings
{
ossia::net::osc_protocol_configuration configuration;
std::optional<int> rate{};
std::optional<ossia::net::rate_limiter_configuration> rate{};
bool bonjour{};
std::optional<int> oscquery{};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,45 @@ void JSONWriter::write(ossia::net::osc_protocol_configuration& n)
n.transport <<= obj["Transport"];
}

// Note: bundle is already in osc_protocol_configuration
template <>
void DataStreamReader::read(const ossia::net::rate_limiter_configuration& n)
{
m_stream << std::chrono::duration_cast<std::chrono::milliseconds>(n.duration).count()
<< n.send_all << n.repeat;
insertDelimiter();
}

template <>
void DataStreamWriter::write(ossia::net::rate_limiter_configuration& n)
{
int dur{};
m_stream >> dur >> n.send_all >> n.repeat;
n.duration = std::chrono::milliseconds{dur};
checkDelimiter();
}

template <>
void JSONReader::read(const ossia::net::rate_limiter_configuration& n)
{
stream.StartObject();
obj["Duration"]
= std::chrono::duration_cast<std::chrono::milliseconds>(n.duration).count();
obj["SendAll"] = n.send_all;
obj["Repeat"] = n.repeat;
stream.EndObject();
}

template <>
void JSONWriter::write(ossia::net::rate_limiter_configuration& n)
{
int dur{};
dur <<= obj["Duration"];
n.duration = std::chrono::milliseconds{dur};
n.send_all <<= obj["SendAll"];
n.repeat <<= obj["Repeat"];
}

template <>
void DataStreamReader::read(const Protocols::OSCSpecificSettings& n)
{
Expand Down Expand Up @@ -464,7 +503,22 @@ void JSONWriter::write(Protocols::OSCSpecificSettings& n)
}

if(auto it = obj.tryGet("Rate"))
n.rate = it->toInt();
{

if(it->obj.IsInt())
{
// pre v3.4.1
n.rate = ossia::net::rate_limiter_configuration{
.duration = std::chrono::milliseconds{it->toInt()}};
}
else
{
// post v3.4.1
ossia::net::rate_limiter_configuration conf;
conf <<= *it;
n.rate = conf;
}
}

assign_with_default(n.bonjour, obj.tryGet("Bonjour"), false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,10 @@ void OSCQueryDevice::slot_createDevice()

if(stgs.rate)
{
ossia::net::rate_limiter_configuration conf{
.duration = std::chrono::milliseconds(*stgs.rate)};
ossia_settings = std::make_unique<ossia::net::rate_limiting_protocol>(
std::chrono::milliseconds{*stgs.rate}, std::move(ossia_settings));
conf, std::move(ossia_settings));
}

// run the commands in the Qt event loop
Expand Down
36 changes: 36 additions & 0 deletions src/plugins/score-plugin-protocols/Protocols/RateWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <score/widgets/MarginLess.hpp>

#include <ossia/detail/optional.hpp>
#include <ossia/network/rate_limiter_configuration.hpp>

#include <QCheckBox>
#include <QHBoxLayout>
Expand All @@ -20,11 +21,15 @@ class RateWidget final : public QWidget
: QWidget{parent}
, m_check{new QCheckBox{this}}
, m_spin{new QSpinBox{this}}
, m_repeat{new QCheckBox{tr("Repeat"), this}}
, m_send_all{new QCheckBox{tr("Send all"), this}}
{
auto lay = new score::MarginLess<QHBoxLayout>;

lay->addWidget(m_check);
lay->addWidget(m_spin);
lay->addWidget(m_repeat);
lay->addWidget(m_send_all);
m_spin->setSizePolicy(QSizePolicy::MinimumExpanding, {});
m_spin->setSuffix("ms");
m_spin->setRange(1, 5000);
Expand All @@ -42,6 +47,35 @@ class RateWidget final : public QWidget
setLayout(lay);
}

std::optional<ossia::net::rate_limiter_configuration> configuration() const noexcept
{
if(!m_check->isChecked())
{
return std::optional<ossia::net::rate_limiter_configuration>{};
}

ossia::net::rate_limiter_configuration res;
res.duration = std::chrono::milliseconds{m_spin->value()};
res.repeat = m_repeat->isChecked();
res.send_all = m_send_all->isChecked();
return res;
}

void setConfiguration(std::optional<ossia::net::rate_limiter_configuration> r) noexcept
{
if(r)
{
m_check->setChecked(true);
m_spin->setValue(r->duration.count());
m_repeat->setChecked(r->repeat);
m_send_all->setChecked(r->send_all);
}
else
{
m_check->setChecked(false);
}
}

std::optional<int> rate() const noexcept
{
if(!m_check->isChecked())
Expand Down Expand Up @@ -72,6 +106,8 @@ class RateWidget final : public QWidget
private:
QCheckBox* m_check{};
QSpinBox* m_spin{};
QCheckBox* m_repeat{};
QCheckBox* m_send_all{};
};

}
Expand Down
Loading