1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/nodes/nanomsg.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

302 lines
7.1 KiB
C++
Raw Permalink Normal View History

/* Node type: nanomsg.
2017-05-23 14:54:25 +02:00
*
2022-03-15 09:18:01 -04:00
* Author: Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
2022-07-04 18:20:03 +02:00
* SPDX-License-Identifier: Apache-2.0
2017-05-23 14:54:25 +02:00
*/
#include <cstring>
2017-05-23 14:54:25 +02:00
#include <nanomsg/nn.h>
#include <nanomsg/pubsub.h>
2021-02-16 14:15:14 +01:00
#include <villas/exceptions.hpp>
#include <villas/node_compat.hpp>
#include <villas/nodes/nanomsg.hpp>
#include <villas/utils.hpp>
2017-05-23 14:54:25 +02:00
2021-02-16 14:15:14 +01:00
using namespace villas;
2021-05-10 00:12:30 +02:00
using namespace villas::node;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
int villas::node::nanomsg_init(NodeCompat *n) {
auto *m = n->getData<struct nanomsg>();
2017-05-23 14:54:25 +02:00
m->formatter = nullptr;
return 0;
}
int villas::node::nanomsg_destroy(NodeCompat *n) {
auto *m = n->getData<struct nanomsg>();
if (m->formatter)
delete m->formatter;
return 0;
}
int villas::node::nanomsg_reverse(NodeCompat *n) {
auto *m = n->getData<struct nanomsg>();
if (list_length(&m->out.endpoints) != 1 || list_length(&m->in.endpoints) != 1)
2017-05-23 14:54:25 +02:00
return -1;
2017-07-24 19:33:35 +02:00
char *subscriber = (char *)list_first(&m->in.endpoints);
char *publisher = (char *)list_first(&m->out.endpoints);
2017-07-24 19:33:35 +02:00
list_set(&m->in.endpoints, 0, publisher);
list_set(&m->out.endpoints, 0, subscriber);
2017-05-23 14:54:25 +02:00
return 0;
}
static int nanomsg_parse_endpoints(struct List *l, json_t *json) {
2017-05-23 14:54:25 +02:00
const char *ep;
2018-08-07 18:40:32 +02:00
size_t i;
2017-10-16 08:08:35 +02:00
json_t *json_val;
2021-02-16 14:15:14 +01:00
switch (json_typeof(json)) {
case JSON_ARRAY:
2021-02-16 14:15:14 +01:00
json_array_foreach(json, i, json_val) {
2017-10-16 08:08:35 +02:00
ep = json_string_value(json_val);
if (!ep)
return -1;
2017-07-24 19:33:35 +02:00
list_push(l, strdup(ep));
2017-05-23 14:54:25 +02:00
}
break;
case JSON_STRING:
2021-02-16 14:15:14 +01:00
ep = json_string_value(json);
2017-07-24 19:33:35 +02:00
list_push(l, strdup(ep));
2017-05-23 14:54:25 +02:00
break;
default:
return -1;
}
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
return 0;
}
int villas::node::nanomsg_parse(NodeCompat *n, json_t *json) {
2017-05-23 14:54:25 +02:00
int ret;
auto *m = n->getData<struct nanomsg>();
json_error_t err;
2021-05-10 00:12:30 +02:00
json_t *json_format = nullptr;
2019-04-22 23:45:38 +02:00
json_t *json_out_endpoints = nullptr;
json_t *json_in_endpoints = nullptr;
ret = list_init(&m->out.endpoints);
if (ret)
return ret;
ret = list_init(&m->in.endpoints);
if (ret)
return ret;
2021-05-10 00:12:30 +02:00
ret = json_unpack_ex(json, &err, 0, "{ s?: o, s?: { s?: o }, s?: { s?: o } }",
"format", &json_format, "out", "endpoints",
&json_out_endpoints, "in", "endpoints",
&json_in_endpoints);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json, err, "node-config-node-nanomsg");
if (json_out_endpoints) {
ret = nanomsg_parse_endpoints(&m->out.endpoints, json_out_endpoints);
2017-05-23 14:54:25 +02:00
if (ret < 0)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Invalid type for 'publish' setting");
2017-05-23 14:54:25 +02:00
}
if (json_in_endpoints) {
ret = nanomsg_parse_endpoints(&m->in.endpoints, json_in_endpoints);
2017-05-23 14:54:25 +02:00
if (ret < 0)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Invalid type for 'subscribe' setting");
2017-05-23 14:54:25 +02:00
}
2021-05-10 00:12:30 +02:00
// Format
if (m->formatter)
delete m->formatter;
2021-05-10 00:12:30 +02:00
m->formatter = json_format ? FormatFactory::make(json_format)
: FormatFactory::make("json");
if (!m->formatter)
throw ConfigError(json_format, "node-config-node-nanomsg-format",
"Invalid format configuration");
2017-05-23 14:54:25 +02:00
return 0;
}
char *villas::node::nanomsg_print(NodeCompat *n) {
auto *m = n->getData<struct nanomsg>();
2017-07-24 19:33:35 +02:00
2019-04-22 23:45:38 +02:00
char *buf = nullptr;
2017-05-23 14:54:25 +02:00
2021-05-10 00:12:30 +02:00
strcatf(&buf, "in.endpoints=[ ");
2017-07-24 19:33:35 +02:00
for (size_t i = 0; i < list_length(&m->in.endpoints); i++) {
char *ep = (char *)list_at(&m->in.endpoints, i);
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
strcatf(&buf, "%s ", ep);
}
2017-07-24 19:33:35 +02:00
strcatf(&buf, "], out.endpoints=[ ");
2017-07-24 19:33:35 +02:00
for (size_t i = 0; i < list_length(&m->out.endpoints); i++) {
char *ep = (char *)list_at(&m->out.endpoints, i);
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
strcatf(&buf, "%s ", ep);
}
strcatf(&buf, "]");
2017-05-23 14:54:25 +02:00
return buf;
}
int villas::node::nanomsg_start(NodeCompat *n) {
2017-05-23 14:54:25 +02:00
int ret;
auto *m = n->getData<struct nanomsg>();
2017-07-24 19:33:35 +02:00
m->formatter->start(n->getInputSignals(false), ~(int)SampleFlags::HAS_OFFSET);
2018-05-12 15:25:29 +02:00
ret = m->in.socket = nn_socket(AF_SP, NN_SUB);
2021-02-16 14:15:14 +01:00
if (ret < 0)
throw RuntimeError("Failed to create nanomsg socket: {}",
nn_strerror(errno));
ret = m->out.socket = nn_socket(AF_SP, NN_PUB);
2021-02-16 14:15:14 +01:00
if (ret < 0)
throw RuntimeError("Failed to create nanomsg socket: {}",
nn_strerror(errno));
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
// Subscribe to all topics
ret = nn_setsockopt(ret = m->in.socket, NN_SUB, NN_SUB_SUBSCRIBE, "", 0);
2017-05-23 14:54:25 +02:00
if (ret < 0)
return ret;
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
// Bind publisher to socket
for (size_t i = 0; i < list_length(&m->out.endpoints); i++) {
char *ep = (char *)list_at(&m->out.endpoints, i);
2017-07-24 19:33:35 +02:00
ret = nn_bind(m->out.socket, ep);
2021-02-16 14:15:14 +01:00
if (ret < 0)
throw RuntimeError("Failed to connect nanomsg socket to endpoint {}: {}",
ep, nn_strerror(errno));
2017-05-23 14:54:25 +02:00
}
2017-07-24 19:33:35 +02:00
// Connect subscribers socket
for (size_t i = 0; i < list_length(&m->in.endpoints); i++) {
char *ep = (char *)list_at(&m->in.endpoints, i);
2017-07-24 19:33:35 +02:00
ret = nn_connect(m->in.socket, ep);
2021-02-16 14:15:14 +01:00
if (ret < 0)
throw RuntimeError("Failed to connect nanomsg socket to endpoint {}: {}",
ep, nn_strerror(errno));
2017-05-23 14:54:25 +02:00
}
return 0;
}
int villas::node::nanomsg_stop(NodeCompat *n) {
2017-05-23 14:54:25 +02:00
int ret;
auto *m = n->getData<struct nanomsg>();
2017-05-23 14:54:25 +02:00
ret = nn_close(m->in.socket);
2017-05-23 14:54:25 +02:00
if (ret < 0)
return ret;
2017-07-24 19:33:35 +02:00
ret = nn_close(m->out.socket);
2017-05-23 14:54:25 +02:00
if (ret < 0)
return ret;
2018-05-12 15:25:29 +02:00
return 0;
}
int villas::node::nanomsg_type_stop() {
2017-05-23 14:54:25 +02:00
nn_term();
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
return 0;
}
int villas::node::nanomsg_read(NodeCompat *n, struct Sample *const smps[],
unsigned cnt) {
auto *m = n->getData<struct nanomsg>();
int bytes;
char data[NANOMSG_MAX_PACKET_LEN];
2017-05-23 14:54:25 +02:00
// Receive payload
bytes = nn_recv(m->in.socket, data, sizeof(data), 0);
if (bytes < 0)
return -1;
2017-05-23 14:54:25 +02:00
2021-05-10 00:12:30 +02:00
return m->formatter->sscan(data, bytes, nullptr, smps, cnt);
2017-05-23 14:54:25 +02:00
}
int villas::node::nanomsg_write(NodeCompat *n, struct Sample *const smps[],
unsigned cnt) {
2017-05-23 14:54:25 +02:00
int ret;
auto *m = n->getData<struct nanomsg>();
2017-05-23 14:54:25 +02:00
size_t wbytes;
2017-07-24 19:33:35 +02:00
char data[NANOMSG_MAX_PACKET_LEN];
2017-05-23 14:54:25 +02:00
2021-05-10 00:12:30 +02:00
ret = m->formatter->sprint(data, sizeof(data), &wbytes, smps, cnt);
if (ret <= 0)
2017-05-23 14:54:25 +02:00
return -1;
ret = nn_send(m->out.socket, data, wbytes, 0);
2017-05-23 14:54:25 +02:00
if (ret < 0)
return ret;
return cnt;
2017-05-23 14:54:25 +02:00
}
int villas::node::nanomsg_poll_fds(NodeCompat *n, int fds[]) {
int ret;
auto *m = n->getData<struct nanomsg>();
int fd;
size_t len = sizeof(fd);
ret = nn_getsockopt(m->in.socket, NN_SOL_SOCKET, NN_RCVFD, &fd, &len);
if (ret)
return ret;
fds[0] = fd;
return 1;
}
int villas::node::nanomsg_netem_fds(NodeCompat *n, int fds[]) {
auto *m = n->getData<struct nanomsg>();
fds[0] = m->out.socket;
return 1;
}
static NodeCompatType p;
__attribute__((constructor(110))) static void register_plugin() {
2021-06-21 16:11:42 -04:00
p.name = "nanomsg";
p.description = "scalability protocols library (libnanomsg)";
p.vectorize = 0;
p.size = sizeof(struct nanomsg);
p.type.stop = nanomsg_type_stop;
p.init = nanomsg_init;
p.destroy = nanomsg_destroy;
2021-06-21 16:11:42 -04:00
p.parse = nanomsg_parse;
p.print = nanomsg_print;
p.start = nanomsg_start;
p.stop = nanomsg_stop;
p.read = nanomsg_read;
p.write = nanomsg_write;
p.reverse = nanomsg_reverse;
p.poll_fds = nanomsg_poll_fds;
p.netem_fds = nanomsg_netem_fds;
static NodeCompatFactory ncp(&p);
}