2017-05-23 14:54:25 +02:00
|
|
|
/** Node type: nanomsg
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2017-05-23 14:54:25 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <nanomsg/nn.h>
|
|
|
|
#include <nanomsg/pubsub.h>
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2017-05-23 14:54:25 +02:00
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/plugin.h>
|
2019-04-23 00:12:31 +02:00
|
|
|
#include <villas/nodes/nanomsg.hpp>
|
2019-04-23 13:15:00 +02:00
|
|
|
#include <villas/utils.hpp>
|
2018-05-12 13:56:12 +02:00
|
|
|
#include <villas/format_type.h>
|
2017-05-23 14:54:25 +02:00
|
|
|
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int nanomsg_reverse(struct vnode *n)
|
2017-05-23 14:54:25 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct nanomsg *m = (struct nanomsg *) n->_vd;
|
2017-05-23 14:54:25 +02:00
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
if (vlist_length(&m->out.endpoints) != 1 ||
|
|
|
|
vlist_length(&m->in.endpoints) != 1)
|
2017-05-23 14:54:25 +02:00
|
|
|
return -1;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-04-22 23:43:46 +02:00
|
|
|
char *subscriber = (char *) vlist_first(&m->in.endpoints);
|
|
|
|
char *publisher = (char *) vlist_first(&m->out.endpoints);
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
vlist_set(&m->in.endpoints, 0, publisher);
|
|
|
|
vlist_set(&m->out.endpoints, 0, subscriber);
|
2017-05-23 14:54:25 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
static int nanomsg_parse_endpoints(struct vlist *l, json_t *cfg)
|
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;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
switch (json_typeof(cfg)) {
|
|
|
|
case JSON_ARRAY:
|
2018-08-07 18:40:32 +02:00
|
|
|
json_array_foreach(cfg, i, json_val) {
|
2017-10-16 08:08:35 +02:00
|
|
|
ep = json_string_value(json_val);
|
2017-08-03 00:19:27 +02:00
|
|
|
if (!ep)
|
|
|
|
return -1;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
vlist_push(l, strdup(ep));
|
2017-05-23 14:54:25 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
case JSON_STRING:
|
|
|
|
ep = json_string_value(cfg);
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
vlist_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;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int nanomsg_parse(struct vnode *n, json_t *cfg)
|
2017-05-23 14:54:25 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct nanomsg *m = (struct nanomsg *) n->_vd;
|
2017-05-23 14:54:25 +02:00
|
|
|
|
2018-10-20 15:38:31 +02:00
|
|
|
const char *format = "villas.binary";
|
2017-08-14 14:42:07 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
json_error_t err;
|
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
json_t *json_out_endpoints = nullptr;
|
|
|
|
json_t *json_in_endpoints = nullptr;
|
2017-05-23 14:54:25 +02:00
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
ret = vlist_init(&m->out.endpoints);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = vlist_init(&m->in.endpoints);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-05-23 14:54:25 +02:00
|
|
|
|
2018-08-20 18:25:25 +02:00
|
|
|
ret = json_unpack_ex(cfg, &err, 0, "{ s?: s, s?: { s?: o }, s?: { s?: o } }",
|
|
|
|
"format", &format,
|
|
|
|
"out",
|
|
|
|
"endpoints", &json_out_endpoints,
|
|
|
|
"in",
|
|
|
|
"endpoints", &json_in_endpoints
|
2017-08-03 00:19:27 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
jerror(&err, "Failed to parse configuration of node %s", node_name(n));
|
|
|
|
|
2018-08-20 18:25:25 +02:00
|
|
|
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)
|
2017-08-03 00:19:27 +02:00
|
|
|
error("Invalid type for 'publish' setting of node %s", node_name(n));
|
2017-05-23 14:54:25 +02:00
|
|
|
}
|
|
|
|
|
2018-08-20 18:25: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)
|
2017-08-03 00:19:27 +02:00
|
|
|
error("Invalid type for 'subscribe' setting of node %s", node_name(n));
|
2017-05-23 14:54:25 +02:00
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-05-12 13:56:12 +02:00
|
|
|
m->format = format_type_lookup(format);
|
2017-08-14 14:42:07 +02:00
|
|
|
if (!m->format)
|
|
|
|
error("Invalid format '%s' for node %s", format, node_name(n));
|
2017-05-23 14:54:25 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
char * nanomsg_print(struct vnode *n)
|
2017-05-23 14:54:25 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct nanomsg *m = (struct nanomsg *) n->_vd;
|
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
|
|
|
|
2018-08-20 18:16:09 +02:00
|
|
|
strcatf(&buf, "format=%s, in.endpoints=[ ", format_type_name(m->format));
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
for (size_t i = 0; i < vlist_length(&m->in.endpoints); i++) {
|
|
|
|
char *ep = (char *) vlist_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
|
|
|
|
2018-08-20 18:25:25 +02:00
|
|
|
strcatf(&buf, "], out.endpoints=[ ");
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
for (size_t i = 0; i < vlist_length(&m->out.endpoints); i++) {
|
|
|
|
char *ep = (char *) vlist_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);
|
|
|
|
}
|
|
|
|
|
2017-05-28 19:43:19 +02:00
|
|
|
strcatf(&buf, "]");
|
2017-05-23 14:54:25 +02:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int nanomsg_start(struct vnode *n)
|
2017-05-23 14:54:25 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct nanomsg *m = (struct nanomsg *) n->_vd;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
ret = io_init(&m->io, m->format, &n->in.signals, (int) SampleFlags::HAS_ALL & ~(int) SampleFlags::HAS_OFFSET);
|
2018-05-12 15:25:29 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-08-20 18:25:25 +02:00
|
|
|
ret = m->in.socket = nn_socket(AF_SP, NN_SUB);
|
|
|
|
if (ret < 0) {
|
2018-10-21 21:36:08 +01:00
|
|
|
warning("Failed to create nanomsg socket: node=%s, error=%s", node_name(n), nn_strerror(errno));
|
2017-05-23 14:54:25 +02:00
|
|
|
return ret;
|
2018-08-20 18:25:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = m->out.socket = nn_socket(AF_SP, NN_PUB);
|
|
|
|
if (ret < 0) {
|
2018-10-21 21:36:08 +01:00
|
|
|
warning("Failed to create nanomsg socket: node=%s, error=%s", node_name(n), nn_strerror(errno));
|
2018-08-20 18:25:25 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 14:54:25 +02:00
|
|
|
/* Subscribe to all topics */
|
2018-08-20 18:25:25 +02:00
|
|
|
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 */
|
2019-01-07 10:28:55 +01:00
|
|
|
for (size_t i = 0; i < vlist_length(&m->out.endpoints); i++) {
|
|
|
|
char *ep = (char *) vlist_at(&m->out.endpoints, i);
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2018-08-20 18:25:25 +02:00
|
|
|
ret = nn_bind(m->out.socket, ep);
|
2017-05-23 14:54:25 +02:00
|
|
|
if (ret < 0) {
|
2018-10-21 21:36:08 +01:00
|
|
|
warning("Failed to connect nanomsg socket: node=%s, endpoint=%s, error=%s", node_name(n), ep, nn_strerror(errno));
|
2017-05-23 14:54:25 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2018-08-20 18:25:25 +02:00
|
|
|
/* Connect subscribers socket */
|
2019-01-07 10:28:55 +01:00
|
|
|
for (size_t i = 0; i < vlist_length(&m->in.endpoints); i++) {
|
|
|
|
char *ep = (char *) vlist_at(&m->in.endpoints, i);
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2018-08-20 18:25:25 +02:00
|
|
|
ret = nn_connect(m->in.socket, ep);
|
2017-05-23 14:54:25 +02:00
|
|
|
if (ret < 0) {
|
2018-10-21 21:36:08 +01:00
|
|
|
warning("Failed to connect nanomsg socket: node=%s, endpoint=%s, error=%s", node_name(n), ep, nn_strerror(errno));
|
2017-05-23 14:54:25 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int nanomsg_stop(struct vnode *n)
|
2017-05-23 14:54:25 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct nanomsg *m = (struct nanomsg *) n->_vd;
|
2017-05-23 14:54:25 +02:00
|
|
|
|
2018-08-20 18:25: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
|
|
|
|
2018-08-20 18:25:25 +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
|
|
|
ret = io_destroy(&m->io);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-16 08:08:17 +02:00
|
|
|
int 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;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int nanomsg_read(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2017-05-23 14:54:25 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct nanomsg *m = (struct nanomsg *) n->_vd;
|
2017-08-14 14:42:07 +02:00
|
|
|
int bytes;
|
|
|
|
char data[NANOMSG_MAX_PACKET_LEN];
|
2017-05-23 14:54:25 +02:00
|
|
|
|
|
|
|
/* Receive payload */
|
2018-08-20 18:25:25 +02:00
|
|
|
bytes = nn_recv(m->in.socket, data, sizeof(data), 0);
|
2017-08-14 14:42:07 +02:00
|
|
|
if (bytes < 0)
|
|
|
|
return -1;
|
2017-05-23 14:54:25 +02:00
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
return io_sscan(&m->io, data, bytes, nullptr, smps, cnt);
|
2017-05-23 14:54:25 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int nanomsg_write(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2017-05-23 14:54:25 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct nanomsg *m = (struct nanomsg *) n->_vd;
|
2017-05-23 14:54:25 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
size_t wbytes;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
char data[NANOMSG_MAX_PACKET_LEN];
|
2017-05-23 14:54:25 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
ret = io_sprint(&m->io, data, sizeof(data), &wbytes, smps, cnt);
|
2017-08-14 14:42:07 +02:00
|
|
|
if (ret <= 0)
|
2017-05-23 14:54:25 +02:00
|
|
|
return -1;
|
|
|
|
|
2018-08-20 18:25:25 +02:00
|
|
|
ret = nn_send(m->out.socket, data, wbytes, 0);
|
2017-05-23 14:54:25 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
return cnt;
|
2017-05-23 14:54:25 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int nanomsg_poll_fds(struct vnode *n, int fds[])
|
2017-08-30 00:25:42 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct nanomsg *m = (struct nanomsg *) n->_vd;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-30 00:25:42 +02:00
|
|
|
int fd;
|
|
|
|
size_t len = sizeof(fd);
|
|
|
|
|
2018-08-20 18:25:25 +02:00
|
|
|
ret = nn_getsockopt(m->in.socket, NN_SOL_SOCKET, NN_RCVFD, &fd, &len);
|
2017-08-30 00:25:42 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2019-01-21 15:47:34 +01:00
|
|
|
fds[0] = fd;
|
|
|
|
|
|
|
|
return 1;
|
2017-08-30 00:25:42 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int nanomsg_netem_fds(struct vnode *n, int fds[])
|
2019-04-04 22:01:40 +02:00
|
|
|
{
|
|
|
|
struct nanomsg *m = (struct nanomsg *) n->_vd;
|
|
|
|
|
|
|
|
fds[0] = m->out.socket;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-04-23 00:36:06 +02:00
|
|
|
static struct plugin p;
|
|
|
|
|
|
|
|
__attribute__((constructor(110)))
|
|
|
|
static void register_plugin() {
|
|
|
|
p.name = "nanomsg";
|
|
|
|
p.description = "scalability protocols library (libnanomsg)";
|
2019-06-23 16:13:23 +02:00
|
|
|
p.type = PluginType::NODE;
|
|
|
|
p.node.instances.state = State::DESTROYED;
|
2019-04-23 00:36:06 +02:00
|
|
|
p.node.vectorize = 0;
|
|
|
|
p.node.size = sizeof(struct nanomsg);
|
|
|
|
p.node.type.stop = nanomsg_type_stop;
|
|
|
|
p.node.parse = nanomsg_parse;
|
|
|
|
p.node.print = nanomsg_print;
|
|
|
|
p.node.start = nanomsg_start;
|
|
|
|
p.node.stop = nanomsg_stop;
|
|
|
|
p.node.read = nanomsg_read;
|
|
|
|
p.node.write = nanomsg_write;
|
|
|
|
p.node.reverse = nanomsg_reverse;
|
|
|
|
p.node.poll_fds = nanomsg_poll_fds;
|
|
|
|
p.node.netem_fds = nanomsg_netem_fds;
|
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
int ret = vlist_init(&p.node.instances);
|
|
|
|
if (!ret)
|
|
|
|
vlist_init_and_push(&plugins, &p);
|
2019-04-23 00:36:06 +02:00
|
|
|
}
|
2017-05-23 14:54:25 +02:00
|
|
|
|
2019-04-23 00:36:06 +02:00
|
|
|
__attribute__((destructor(110)))
|
|
|
|
static void deregister_plugin() {
|
2020-06-16 02:35:34 +02:00
|
|
|
vlist_remove_all(&plugins, &p);
|
2019-04-23 13:15:00 +02:00
|
|
|
}
|