Skip to content

Commit

Permalink
[oscquery] Toggle TCP on/off when reloading a cue
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Aug 31, 2024
1 parent e7d5425 commit c43ce26
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 31 deletions.
5 changes: 3 additions & 2 deletions src/ossia-max/src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,9 @@ void device::expose(device* x, t_symbol*, long argc, t_atom* argv)

try
{
auto oscq_proto = std::make_unique<ossia::oscquery::oscquery_server_protocol>(
settings.oscport, settings.wsport);
auto oscq_proto
= std::make_unique<ossia::oscquery_asio::oscquery_server_protocol>(
x->network_context, settings.oscport, settings.wsport, false);
x->m_device->set_echo(true);

A_SETSYM(a + 1, gensym("oscquery"));
Expand Down
103 changes: 78 additions & 25 deletions src/ossia/preset/cue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
#include <boost/container/map.hpp>
#include <boost/container/small_vector.hpp>

#if defined(OSSIA_PROTOCOL_OSCQUERY)
#include <ossia/network/local/local.hpp>
#include <ossia/protocols/oscquery/oscquery_server_asio.hpp>
#endif

#include <cassert>

namespace ossia
Expand Down Expand Up @@ -112,7 +117,7 @@ void namespace_selection::set_device(ossia::net::device_base* dev)
void cues::recall(ossia::net::node_base& root, namespace_selection& sel, int idx)
{
if(!has_cue(idx))
return;
return;
m_current = idx;
recall(root, sel);
}
Expand All @@ -127,10 +132,54 @@ struct priority_sort
}
};

struct protocol_safe_mode_enabler
{
#if defined(OSSIA_PROTOCOL_OSCQUERY)
std::vector<std::pair<ossia::oscquery_asio::oscquery_server_protocol*, bool>>
protocols{};
#endif

explicit protocol_safe_mode_enabler(ossia::net::node_base& root)
{
#if defined(OSSIA_PROTOCOL_OSCQUERY)
auto& proto = root.get_device().get_protocol();
if(auto oscq = dynamic_cast<ossia::oscquery_asio::oscquery_server_protocol*>(&proto))
{
protocols.emplace_back(oscq, oscq->force_ws());
oscq->set_force_ws(true);
}
else if(auto mplex = dynamic_cast<ossia::net::multiplex_protocol*>(&proto))
{
for(auto& proto : mplex->get_protocols())
{
if(auto oscq
= dynamic_cast<ossia::oscquery_asio::oscquery_server_protocol*>(proto.get()))
{
protocols.emplace_back(oscq, oscq->force_ws());
oscq->set_force_ws(true);
}
}
}
#endif
}

~protocol_safe_mode_enabler()
{
#if defined(OSSIA_PROTOCOL_OSCQUERY)
for(auto [oscq, prev_val] : protocols)
{
oscq->set_force_ws(prev_val);
}
#endif
}
};

void cues::recall(ossia::net::node_base& root, namespace_selection& sel)
{
if(!has_cue(m_current))
return;
return;

auto oscq_safe_mode = protocol_safe_mode_enabler{root};

sel.m_selection.clear();
boost::container::small_flat_multimap<
Expand Down Expand Up @@ -222,7 +271,6 @@ void namespace_selection::namespace_deselect(std::string_view pattern)
}
}


void namespace_selection::namespace_switch(std::string_view name)
{
if(!dev)
Expand All @@ -231,17 +279,17 @@ void namespace_selection::namespace_switch(std::string_view name)
auto nodes = ossia::net::find_nodes(dev->get_root_node(), name);
for(auto n : nodes)
{
if(!this->m_selection.contains(n))
{
// Select
this->m_selection.insert(n);
list_all_children_unsorted(n, this->m_selection);
}
else
{
// Deselect
remove_node_from_selection_recursively(m_selection, *n);
}
if(!this->m_selection.contains(n))
{
// Select
this->m_selection.insert(n);
list_all_children_unsorted(n, this->m_selection);
}
else
{
// Deselect
remove_node_from_selection_recursively(m_selection, *n);
}
}
}

Expand Down Expand Up @@ -310,8 +358,8 @@ void cues::create(std::string_view name)

void cues::remove(int idx)
{
if(!has_cue(idx))
return;
if(!has_cue(idx))
return;

m_cues.erase(m_cues.begin() + idx);

Expand Down Expand Up @@ -356,10 +404,10 @@ void cues::remove(std::string_view name)

void cues::rename(int idx, std::string_view newname)
{
if(!has_cue(idx))
return;
if(!has_cue(idx))
return;

m_cues[idx].name.assign(newname.begin(), newname.end());
m_cues[idx].name.assign(newname.begin(), newname.end());
}

void cues::rename(std::string_view name, std::string_view newname)
Expand All @@ -377,19 +425,21 @@ void cues::rename(std::string_view name, std::string_view newname)

void cues::rename(std::string_view newname)
{
rename(m_current, newname);
rename(m_current, newname);
}

void cues::recall(ossia::net::node_base& root, namespace_selection& sel, std::string_view name)
void cues::recall(
ossia::net::node_base& root, namespace_selection& sel, std::string_view name)
{
m_current = get_cue(name);
recall(root, sel);
}

void cues::update(ossia::net::node_base& root, const namespace_selection& selection, int idx)
void cues::update(
ossia::net::node_base& root, const namespace_selection& selection, int idx)
{
if(!has_cue(idx))
return;
return;

auto& cue = this->m_cues[idx];

Expand Down Expand Up @@ -448,7 +498,9 @@ void cues::update(ossia::net::node_base& root, const namespace_selection& select
update(root, selection, m_current);
}

void cues::update(ossia::net::node_base& root, const namespace_selection& selection, std::string_view name)
void cues::update(
ossia::net::node_base& root, const namespace_selection& selection,
std::string_view name)
{
// Question:
// If we add the pattern: /foo.* to a cue
Expand Down Expand Up @@ -476,7 +528,8 @@ void cues::move(std::string_view name, int to)

void cues::move(int from, int to)
{
if(from < 0 || to < 0 || from == to || from >= std::ssize(m_cues) || to >= std::ssize(m_cues))
if(from < 0 || to < 0 || from == to || from >= std::ssize(m_cues)
|| to >= std::ssize(m_cues))
return;
change_item_position(m_cues, from, to);
}
Expand Down
5 changes: 5 additions & 0 deletions src/ossia/protocols/oscquery/oscquery_server_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,11 @@ void oscquery_server_protocol::stop()
}
}

void oscquery_server_protocol::set_force_ws(bool forceWS) noexcept
{
m_forceWS.store(forceWS, std::memory_order_relaxed);
}

oscquery_client* oscquery_server_protocol::find_client(const connection_handler& hdl)
{
lock_t lock(m_clientsMutex);
Expand Down
11 changes: 7 additions & 4 deletions src/ossia/protocols/oscquery/oscquery_server_asio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ class OSSIA_EXPORT oscquery_server_protocol final : public ossia::net::protocol_

void set_device(net::device_base& dev) override;
void stop() override;
ossia::net::device_base& get_device() const { return *m_device; }
ossia::net::device_base& get_device() const noexcept { return *m_device; }

int get_osc_port() const { return m_oscPort; }
int get_osc_port() const noexcept { return m_oscPort; }

int get_ws_port() const { return m_wsPort; }
int get_ws_port() const noexcept { return m_wsPort; }

bool force_ws() const noexcept { return m_forceWS.load(std::memory_order_relaxed); }
void set_force_ws(bool forceWS) noexcept;

Nano::Signal<void(const std::string&)> onClientConnected;
Nano::Signal<void(const std::string&)> onClientDisconnected;
Expand Down Expand Up @@ -142,7 +145,7 @@ class OSSIA_EXPORT oscquery_server_protocol final : public ossia::net::protocol_
uint16_t m_wsPort{};

// Will only send changes through WS
bool m_forceWS{};
std::atomic_bool m_forceWS{};
};
}
}

0 comments on commit c43ce26

Please sign in to comment.