Skip to content

Commit

Permalink
[sockets] Clarify the various socket types; allow oscquery to expose …
Browse files Browse the repository at this point in the history
…multiple kind of OSC servers
  • Loading branch information
jcelerier committed Nov 8, 2024
1 parent 6727dcd commit d4cf2c3
Show file tree
Hide file tree
Showing 34 changed files with 387 additions and 211 deletions.
6 changes: 3 additions & 3 deletions examples/Dataflow/SynthOSC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ int main(int argc, char** argv)
ossia::net::make_osc_protocol(
ctx, {conf::HOST, conf::OSC1_1, conf::SLIP,
ossia::net::udp_configuration{
{ossia::net::receive_socket_configuration{
{.host = "0.0.0.0", .port = listen_port}},
ossia::net::send_socket_configuration{{"127.0.0.1", 9977}}}}}),
{ossia::net::inbound_socket_configuration{
.bind = "0.0.0.0", .port = listen_port},
ossia::net::outbound_socket_configuration{"127.0.0.1", 9977}}}}),
"P"};

auto b = [](const std::string s, const ossia::value& val) {
Expand Down
2 changes: 1 addition & 1 deletion examples/Editor/example_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ struct my_node final : ossia::graph_node
m_outlets.push_back(new ossia::value_outlet);
}

std::string label() const noexcept override { return "my_node"; }
std::string label() const noexcept override { return "my_node"; }
void run(const ossia::token_request& t, ossia::exec_state_facade) noexcept override
{
if(auto a_float = pop_value<float>(this->root_inputs()[0]))
Expand Down
2 changes: 1 addition & 1 deletion examples/Network/Mqtt_publication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int main(int argc, char** argv)
auto ctx = std::make_shared<ossia::net::network_context>();

ossia::net::mqtt5_configuration conf{
ossia::net::tcp_configuration{{.host = "127.0.0.1", .port = 1883}}};
ossia::net::tcp_client_configuration{{.host = "127.0.0.1", .port = 1883}}};
auto proto = std::make_unique<ossia::net::mqtt5_protocol>(ctx, conf);
ossia::net::generic_device device{std::move(proto), "P"};

Expand Down
7 changes: 4 additions & 3 deletions examples/Network/asio/OSC_playback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ int main(int argc, char** argv)
// Create the OSC device
auto ctx = std::make_shared<ossia::net::network_context>();
auto protocol = ossia::net::make_osc_protocol(
ctx, {.transport = ossia::net::udp_configuration{
{.local = {},
.remote = ossia::net::send_socket_configuration{"127.0.0.1", 4456}}}});
ctx,
{.transport = ossia::net::udp_configuration{
{.local = {},
.remote = ossia::net::outbound_socket_configuration{"127.0.0.1", 4456}}}});

ossia::net::generic_device device{std::move(protocol), "B"};

Expand Down
4 changes: 2 additions & 2 deletions examples/Network/udp/OSC_send_receive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void send_main()
.version = conf::OSC1_1,
.transport = ossia::net::udp_configuration{
{.local = std::nullopt,
.remote = ossia::net::send_socket_configuration{{"127.0.0.1", 1234}}}}};
.remote = ossia::net::outbound_socket_configuration{"127.0.0.1", 1234}}}};

ossia::net::generic_device device{
ossia::net::make_osc_protocol(ctx, mirror_config), "P"};
Expand All @@ -39,7 +39,7 @@ void receive_main()
.mode = conf::HOST,
.version = conf::OSC1_1,
.transport = ossia::net::udp_configuration{
{.local = ossia::net::receive_socket_configuration{{"0.0.0.0", 1234}},
{.local = ossia::net::inbound_socket_configuration{"0.0.0.0", 1234},
.remote = std::nullopt}}};

ossia::net::generic_device device{
Expand Down
2 changes: 1 addition & 1 deletion src/ossia/dataflow/nodes/gain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct gain_node final : public ossia::nonowning_graph_node
m_outlets.push_back(&audio_out);
}

std::string label() const noexcept override { return "gain"; }
std::string label() const noexcept override { return "gain"; }
void run(const ossia::token_request& t, ossia::exec_state_facade st) noexcept override
{
auto& vals = gain_in.target<ossia::value_port>()->get_data();
Expand Down
2 changes: 1 addition & 1 deletion src/ossia/dataflow/nodes/rand_float.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct rand_float final : public ossia::nonowning_graph_node
m_outlets.push_back(&value_out);
}

std::string label() const noexcept override { return "rand_float"; }
std::string label() const noexcept override { return "rand_float"; }
void run(const ossia::token_request& t, ossia::exec_state_facade e) noexcept override
{
thread_local std::mt19937 gen;
Expand Down
2 changes: 1 addition & 1 deletion src/ossia/dataflow/nodes/sine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct sine final : public ossia::nonowning_graph_node
m_outlets.push_back(&audio_out);
}

std::string label() const noexcept override { return "sine"; }
std::string label() const noexcept override { return "sine"; }
void run(const ossia::token_request& t, ossia::exec_state_facade st) noexcept override
{
auto& vals = freq_in.target<ossia::value_port>()->get_data();
Expand Down
17 changes: 16 additions & 1 deletion src/ossia/network/oscquery/detail/get_query_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,23 @@ class get_query_answerer
auto& socket = sockets.get_socket();

std::string local_client_ip = socket.local_endpoint().address().to_string();

auto transports = [&]() -> std::vector<ossia::net::osc_server_configuration> {
if constexpr(requires { proto.get_osc_port(); })
{
ossia::net::udp_server_configuration conf;
conf.bind = "";
conf.port = proto.get_osc_port();
return {conf};
}
else
{
return proto.get_transports();
}
};

return oscquery::json_writer::query_host_info(
proto.get_device().get_name(), proto.get_osc_port(), local_client_ip,
proto.get_device().get_name(), transports(), local_client_ip,
proto.get_ws_port());
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/ossia/network/oscquery/detail/json_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <ossia/detail/for_each.hpp>
#include <ossia/network/oscquery/detail/attributes.hpp>
#include <ossia/network/oscquery/detail/json_writer_detail.hpp>

#include <ossia/protocols/osc/osc_factory.hpp>
namespace ossia
{
namespace net
Expand All @@ -24,8 +24,9 @@ class OSSIA_EXPORT json_writer
static string_t device_info(int port);

static string_t query_host_info(
const std::string& name, const int osc_port, const std::string& local_ip,
int ws_port);
std::string_view name,
const std::vector<ossia::net::osc_server_configuration>& osc_port,
std::string_view local_ip, int ws_port);

// Format interface
// Queries
Expand Down Expand Up @@ -71,10 +72,10 @@ class OSSIA_EXPORT json_writer
static string_t path_changed(const ossia::net::node_base& n);

//! Sent when a node is being removed
static string_t path_removed(const std::string& path);
static string_t path_removed(std::string_view path);

//! Sent when a node is renamed
static string_t path_renamed(const std::string& old_path, const std::string& new_path);
static string_t path_renamed(std::string_view old_path, std::string_view new_path);

static string_t
attributes_changed(const ossia::net::node_base& n, std::string_view attribute);
Expand All @@ -97,9 +98,9 @@ class OSSIA_EXPORT json_writer
path_added_impl(detail::json_writer_impl& p, const ossia::net::node_base& n);
static void
path_changed_impl(detail::json_writer_impl& p, const ossia::net::node_base& n);
static void path_removed_impl(writer_t& wr, const std::string& path);
static void path_removed_impl(writer_t& wr, std::string_view path);
static void path_renamed_impl(
json_writer::writer_t& wr, const std::string& path, const std::string& old);
json_writer::writer_t& wr, std::string_view path, std::string_view old);
static void attribute_changed_impl(
detail::json_writer_impl& p, const ossia::net::node_base& n,
std::string_view attribute);
Expand Down
81 changes: 65 additions & 16 deletions src/ossia/network/oscquery/detail/json_writer_detail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ void json_writer_impl::writeValue(bool i) const
{
writer.Bool(i);
}
void json_writer_impl::writeValue(const std::string& i) const
void json_writer_impl::writeValue(std::string_view i) const
{
writer.String(i);
writer.String(i.data(), i.size());
}
void json_writer_impl::writeValue(const repetition_filter& i) const
{
Expand Down Expand Up @@ -375,21 +375,21 @@ void json_writer::path_changed_impl(detail::json_writer_impl& p, const net::node
wr.EndObject();
}

void json_writer::path_removed_impl(json_writer::writer_t& wr, const std::string& path)
void json_writer::path_removed_impl(json_writer::writer_t& wr, std::string_view path)
{
wr.StartObject();

write_json_key(wr, detail::command());
write_json(wr, detail::path_removed());

write_json_key(wr, detail::data());
wr.String(path);
write_json(wr, path);

wr.EndObject();
}

void json_writer::path_renamed_impl(
json_writer::writer_t& wr, const std::string& old_path, const std::string& new_path)
json_writer::writer_t& wr, std::string_view old_path, std::string_view new_path)
{
wr.StartObject();

Expand Down Expand Up @@ -491,26 +491,75 @@ json_writer::string_t json_writer::device_info(int port)
return buf;
}

json_writer::string_t json_writer::query_host_info(
const std::string& name, const int osc_port, const std::string& local_ip,
int ws_port)
struct transport_visitor
{
json_writer::writer_t& wr;
void operator()(const ossia::net::udp_server_configuration& v) const noexcept
{
wr.Key("OSC_PORT");
wr.Int(v.port);
wr.Key("OSC_TRANSPORT");
wr.String("UDP");
}

void operator()(const ossia::net::tcp_server_configuration& v) const noexcept
{
wr.Key("OSC_PORT");
wr.Int(v.port);
wr.Key("OSC_TRANSPORT");
wr.String("TCP");
}
void operator()(const ossia::net::unix_dgram_server_configuration& v) const noexcept
{
wr.Key("OSC_PORT");
wr.String(v.fd);
wr.Key("OSC_TRANSPORT");
wr.String("UNIX_DATAGRAM");
}
void operator()(const ossia::net::unix_stream_server_configuration& v) const noexcept
{
wr.Key("OSC_PORT");
wr.String(v.fd);
wr.Key("OSC_TRANSPORT");
wr.String("UNIX_STREAM");
}
void operator()(const ossia::net::serial_configuration& v) const noexcept
{
wr.Key("OSC_PORT");
wr.String(v.port);
wr.Key("OSC_TRANSPORT");
wr.String("SERIAL");
}
void operator()(const ossia::net::ws_server_configuration& v) const noexcept
{
wr.Key("OSC_PORT");
wr.Int(v.port);
wr.Key("OSC_TRANSPORT");
wr.String("WEBSOCKET");
}
};

json_writer::string_t json_writer::query_host_info(
std::string_view name,
const std::vector<ossia::net::osc_server_configuration>& osc_port,
std::string_view local_ip, int ws_port)
{
string_t buf;
writer_t wr(buf);

wr.StartObject();
wr.Key("NAME");
wr.String(name);
write_json(wr, name);

wr.Key("WS_IP");
wr.String(local_ip);
write_json(wr, local_ip);
wr.Key("WS_PORT");
wr.Int(ws_port);
wr.Key("OSC_PORT");
wr.Int(osc_port);
wr.Key("OSC_TRANSPORT");
wr.String("UDP");

for(auto& v : osc_port)
{
ossia::visit(transport_visitor{wr}, v);
}
wr.Key("EXTENSIONS");
wr.StartObject();

Expand Down Expand Up @@ -658,7 +707,7 @@ json_writer::string_t json_writer::path_changed(const net::node_base& n)
return buf;
}

json_writer::string_t json_writer::path_removed(const std::string& path)
json_writer::string_t json_writer::path_removed(std::string_view path)
{
string_t buf;
writer_t wr(buf);
Expand All @@ -669,7 +718,7 @@ json_writer::string_t json_writer::path_removed(const std::string& path)
}

json_writer::string_t
json_writer::path_renamed(const std::string& old_path, const std::string& new_path)
json_writer::path_renamed(std::string_view old_path, std::string_view new_path)
{
string_t buf;
writer_t wr(buf);
Expand Down
3 changes: 2 additions & 1 deletion src/ossia/network/oscquery/detail/json_writer_detail.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <ossia/detail/json.hpp>
#include <ossia/network/base/node_attributes.hpp>

namespace ossia::oscquery::detail
{
//! Implementation of the JSON serialisation mechanism for oscquery
Expand All @@ -21,7 +22,7 @@ struct json_writer_impl
void writeValue(float i) const;
void writeValue(double i) const;
void writeValue(bool i) const;
void writeValue(const std::string& i) const;
void writeValue(std::string_view i) const;
void writeValue(const ossia::repetition_filter& i) const;
void writeValue(const ossia::net::instance_bounds& i) const;

Expand Down
29 changes: 20 additions & 9 deletions src/ossia/network/sockets/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@ struct receive_fd_configuration : fd_configuration
{
};

struct socket_configuration
struct outbound_socket_configuration
{
std::string host;
uint16_t port{};
bool broadcast{};
};

struct send_socket_configuration : socket_configuration
{
};
struct receive_socket_configuration : socket_configuration
struct inbound_socket_configuration
{
std::string bind;
uint16_t port{};
};

struct double_fd_configuration
Expand All @@ -47,8 +45,8 @@ struct double_fd_configuration

struct double_socket_configuration
{
std::optional<receive_socket_configuration> local;
std::optional<send_socket_configuration> remote;
std::optional<inbound_socket_configuration> local;
std::optional<outbound_socket_configuration> remote;
};

struct serial_configuration
Expand Down Expand Up @@ -102,7 +100,20 @@ struct udp_configuration : double_socket_configuration
{
};

struct tcp_configuration : socket_configuration
struct tcp_client_configuration : outbound_socket_configuration
{
};

struct udp_server_configuration : inbound_socket_configuration
{
};
struct tcp_server_configuration : inbound_socket_configuration
{
};
struct unix_dgram_server_configuration : receive_fd_configuration
{
};
struct unix_stream_server_configuration : receive_fd_configuration
{
};
}
6 changes: 3 additions & 3 deletions src/ossia/network/sockets/tcp_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ class tcp_server
using socket = typename proto::socket;
using listener = tcp_listener;

tcp_server(const socket_configuration& conf, boost::asio::io_context& ctx)
tcp_server(const inbound_socket_configuration& conf, boost::asio::io_context& ctx)
: m_context{ctx}
, m_acceptor{
boost::asio::make_strand(ctx),
proto::endpoint{boost::asio::ip::make_address(conf.host), conf.port}}
proto::endpoint{boost::asio::ip::make_address(conf.bind), conf.port}}
{
}

Expand All @@ -65,7 +65,7 @@ class tcp_client
using proto = boost::asio::ip::tcp;
using socket = typename proto::socket;

tcp_client(const socket_configuration& conf, boost::asio::io_context& ctx)
tcp_client(const outbound_socket_configuration& conf, boost::asio::io_context& ctx)
: m_context{ctx}
, m_endpoint{boost::asio::ip::make_address(conf.host), conf.port}
, m_socket{boost::asio::make_strand(ctx)}
Expand Down
Loading

0 comments on commit d4cf2c3

Please sign in to comment.