2 #include <ossia/network/sockets/udp_socket.hpp>
4 #include <boost/asio/ip/basic_resolver.hpp>
5 #include <boost/asio/ip/udp.hpp>
9 #include <AvndProcesses/AddressTools.hpp>
16 halp_meta(name,
"Teleplot")
17 halp_meta(author,
"ossia team")
18 halp_meta(category,
"Monitoring")
19 halp_meta(description,
"Forwards a set of device explorer messages to ")
20 halp_meta(c_name,
"avnd_teleplot")
21 halp_meta(manual_url,
"https://ossia.io/score-docs/processes/teleplot.html")
22 halp_meta(uuid,
"e1d5b9a0-4df9-4281-87a6-9f427dfb6e31")
25 boost::asio::io_context* io_context{};
30 struct : halp::lineedit<
"Host",
"">
32 void update(
Teleplot&
self) {
self.update(); }
42 std::pair<std::string, uint16_t> resolve_ip(
const std::string& host)
46 std::string m_queryPort;
47 auto m_queryHost = host;
48 auto port_idx = m_queryHost.find_last_of(
':');
49 if(port_idx != std::string::npos)
51 m_queryPort = m_queryHost.substr(port_idx + 1);
52 m_queryHost = m_queryHost.substr(0, port_idx);
57 boost::asio::io_context io_service;
58 boost::asio::ip::udp::resolver resolver(io_service);
59 boost::asio::ip::udp::resolver::query query(m_queryHost, m_queryPort);
60 boost::asio::ip::udp::resolver::iterator iter = resolver.resolve(query);
62 auto addr = iter->endpoint().address().to_string();
64 return {addr, std::stoi(m_queryPort)};
66 catch(
const std::exception& e)
77 for(
auto& [param, cb] : params)
79 param->remove_callback(cb);
92 auto [ip, port] = resolve_ip(inputs.host.value);
93 if(!ip.empty() && port > 1)
95 socket = std::make_shared<ossia::net::udp_send_socket>(
96 ossia::net::outbound_socket_configuration{.host = ip, .port = port},
103 for(
auto nodes : this->roots)
105 if(
auto p = nodes->get_parameter(); p && !params.contains(p))
107 auto it = p->add_callback(
108 [p, socket = socket](
const ossia::value& v) { push(*socket, *p, v); });
109 params.emplace(p, it);
116 ossia::net::udp_send_socket& socket,
const ossia::net::parameter_base& param,
117 const ossia::value& v)
119 using clk = std::chrono::system_clock;
121 thread_local fmt::memory_buffer buf;
127 const std::string& addr;
128 int64_t t = std::chrono::duration_cast<std::chrono::milliseconds>(
129 clk::now().time_since_epoch())
132 void operator()(ossia::impulse v)
134 fmt::format_to(fmt::appender(buf),
"{}:{}:1\n", addr, t);
136 void operator()(
int v)
138 fmt::format_to(fmt::appender(buf),
"{}:{}:{}\n", addr, t, v);
140 void operator()(
float v)
142 fmt::format_to(fmt::appender(buf),
"{}:{}:{}\n", addr, t, v);
144 void operator()(std::string v)
146 fmt::format_to(fmt::appender(buf),
"{}:{}:{}\n", addr, t, v);
148 void operator()(
bool v)
150 fmt::format_to(fmt::appender(buf),
"{}:{}:{}\n", addr, t, v ? 1 : 0);
152 void operator()(ossia::vec2f v)
155 fmt::appender(buf),
"{}.x:{}:{}\n{}.y:{}:{}\n", addr, t, v[0], addr, t,
158 void operator()(ossia::vec3f v)
161 fmt::appender(buf),
"{}.x:{}:{}\n{}.y:{}:{}\n{}.z:{}:{}\n", addr, t, v[0],
162 addr, t, v[1], addr, t, v[2]);
164 void operator()(ossia::vec4f v)
167 fmt::appender(buf),
"{}.x:{}:{}\n{}.y:{}:{}\n{}.z:{}:{}\n{}.w:{}:{}\n", addr,
168 t, v[0], addr, t, v[1], addr, t, v[2], addr, t, v[3]);
170 void operator()(
const std::vector<ossia::value>& v)
177 fmt::appender(buf),
"{}[{}]:{}:{}\n", addr, i, t,
178 ossia::convert<double>(val));
182 void operator()(
const ossia::value_map_type& v)
184 for(
auto& [k, val] : v)
188 fmt::appender(buf),
"{}[{}]:{}:{}\n", addr, k, t,
189 ossia::convert<double>(val));
192 void operator()() { }
194 } vis{.addr = param.get_node().osc_address()};
197 socket.write(buf.begin(), buf.size());
204 socket = std::make_shared<ossia::net::udp_send_socket>(
205 ossia::net::outbound_socket_configuration{.host =
"127.0.0.1", .port = 47269},
215 for(
auto nodes : this->roots)
217 if(
auto p = nodes->get_parameter(); p && !params.contains(p))
219 auto it = p->add_callback(
220 [p, socket = socket](
const ossia::value& v) { push(*socket, *p, v); });
221 params.emplace(p, it);
226 for(
auto it = params.begin(); it != params.end();)
228 if(!ossia::contains(this->roots, &(it->first)->get_node()))
230 it->first->remove_callback(it->second);
231 it = params.erase(it);
240 boost::container::flat_map<
241 ossia::net::parameter_base*,
242 ossia::callback_container<ossia::value_callback>::iterator>
244 std::shared_ptr<ossia::net::udp_send_socket> socket;