OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
oscquery_mirror.hpp
1#pragma once
2
3#include <ossia/detail/json_fwd.hpp>
4#include <ossia/detail/thread.hpp>
5#include <ossia/detail/lockfree_queue.hpp>
6#include <ossia/network/base/listening.hpp>
7#include <ossia/network/base/protocol.hpp>
8#include <ossia/network/oscquery/host_info.hpp>
9
10#include <atomic>
11
12namespace osc
13{
14template <typename T>
15class sender;
16class receiver;
17}
18
19namespace oscpack
20{
21class ReceivedMessage;
22class IpEndpointName;
23}
24
25namespace ossia
26{
27namespace net
28{
29struct parameter_data;
31}
32namespace oscquery
33{
34struct osc_outbound_visitor;
35struct http_client_context;
36struct http_responder;
37
38class OSSIA_EXPORT oscquery_mirror_protocol final : public ossia::net::protocol_base
39{
40public:
41 oscquery_mirror_protocol(std::string host, uint16_t local_osc_port = 10203);
42 ~oscquery_mirror_protocol() override;
43
44 bool pull(net::parameter_base&) override;
45 std::future<void> pull_async(net::parameter_base&) override;
46 void request(net::parameter_base&) override;
47 bool push(const net::parameter_base&, const ossia::value& v) override;
48 bool push_raw(const ossia::net::full_parameter_data& parameter_base) override;
49 bool push_bundle(const std::vector<const ossia::net::parameter_base*>&) override;
50 bool push_raw_bundle(const std::vector<ossia::net::full_parameter_data>&) override;
51 bool observe(net::parameter_base&, bool) override;
52 bool observe_quietly(net::parameter_base&, bool) override;
53 bool update(net::node_base& b) override;
54 bool echo_incoming_message(
55 const ossia::net::message_origin_identifier& id,
56 const ossia::net::parameter_base& addr, const value& val) override;
57
58 std::future<void> update_async(net::node_base& b) override;
59
60 void stop() override;
61 void set_device(net::device_base& dev) override;
62 ossia::net::device_base& get_device() const { return *m_device; }
63
77 void run_commands();
78
87 void set_command_callback(std::function<void()>);
88
94 void request_add_node(net::node_base&, const ossia::net::parameter_data&);
95
100 void request_remove_node(net::node_base&);
101
106 void request_rename_node(net::node_base& node, const std::string& new_name);
107
113 void set_zombie_on_remove(bool zombie_on_remove)
114 {
115 m_zombie_on_remove = zombie_on_remove;
116 }
117
122 bool get_zombie_on_remove() const noexcept { return m_zombie_on_remove; }
123
124 host_info get_host_info() const noexcept;
125
126 bool connected() const noexcept override { return m_hasWS; }
127 void connect() override;
128
129private:
130 friend struct http_answer;
131
132 void init();
133 using connection_handler = std::weak_ptr<void>;
134 bool on_WSMessage(connection_handler hdl, const std::string& message);
135 bool on_BinaryWSMessage(connection_handler hdl, const std::string& message);
136 void
137 on_OSCMessage(const oscpack::ReceivedMessage& m, const oscpack::IpEndpointName& ip);
138
139 void cleanup_connections();
140
141 void http_send_message(const std::string& str);
142 void http_send_message(const rapidjson::StringBuffer& str);
143
144 void ws_send_message(const std::string& str);
145 void ws_send_binary_message(const std::string& str);
146 void ws_send_message(const rapidjson::StringBuffer& str);
147 bool query_connected();
148 void query_stop();
149
150 void on_nodeRenamed(const ossia::net::node_base& n, std::string oldname);
151
152 void start_http();
153
154 void on_ws_disconnected() { m_hasWS = false; }
155
156 std::unique_ptr<osc::sender<oscquery::osc_outbound_visitor>> m_oscSender;
157 std::unique_ptr<osc::receiver> m_oscServer;
158 std::unique_ptr<ossia::net::websocket_client> m_websocketClient;
159 std::atomic_bool m_hasWS{};
160
161 // Listening status of the local software
162 net::listened_parameters m_listening;
163
164 ossia::net::device_base* m_device{};
165
166 std::promise<void> m_namespacePromise;
167
168 struct get_osc_promise
169 {
170 std::promise<void> promise;
171 ossia::net::parameter_base* address{};
172 };
173 struct get_ws_promise
174 {
175 get_ws_promise() = default;
176 get_ws_promise(const get_ws_promise&) = delete;
177 get_ws_promise(get_ws_promise&&) noexcept = default;
178 get_ws_promise& operator=(const get_ws_promise&) = delete;
179 get_ws_promise& operator=(get_ws_promise&&) noexcept = default;
180
181 get_ws_promise(std::promise<void>&& p, const std::string& addr)
182 : promise{std::move(p)}
183 , address{addr}
184 {
185 }
186 std::promise<void> promise;
187 std::string address{};
188 };
189
190 ossia::spsc_queue<get_ws_promise> m_getWSPromises;
191 ossia::spsc_queue<std::function<void()>> m_functionQueue;
192 std::function<void()> m_commandCallback;
193
194 ossia::thread m_wsThread;
195 std::string m_queryHost;
196 std::string m_queryPort;
197 std::string m_httpHost;
198 int m_osc_port{};
199
200 std::unique_ptr<http_client_context> m_http;
201 host_info m_host_info;
202 mutable std::mutex m_host_info_mutex;
203
204 ossia::net::message_origin_identifier m_id;
205
206 void set_feedback(bool fb) override;
207 std::atomic<bool> m_feedback{true};
208 bool m_zombie_on_remove{true};
209};
210
212OSSIA_EXPORT void load_oscquery_device(ossia::net::device_base& dev, std::string json);
213}
214}
The receiver class.
Definition receiver.hpp:158
The sender class.
Definition sender.hpp:27
Low-level Websocket client.
Definition websocket_client.hpp:18
Definition git_info.h:7
The data that can be found inside a parameter.
Definition parameter_data.hpp:21