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.c

302 lines
6.7 KiB
C
Raw Permalink Normal View History

2017-05-23 14:54:25 +02:00
/** Node type: nanomsg
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @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>
#include <string.h>
2017-12-09 02:19:28 +08:00
#include <villas/plugin.h>
#include <villas/nodes/nanomsg.h>
#include <villas/utils.h>
2018-05-12 13:56:12 +02:00
#include <villas/format_type.h>
2017-05-23 14:54:25 +02:00
int nanomsg_reverse(struct node *n)
{
2017-10-18 15:39:53 +02:00
struct nanomsg *m = (struct nanomsg *) n->_vd;
2017-05-23 14:54:25 +02:00
if (list_length(&m->publisher.endpoints) != 1 ||
list_length(&m->subscriber.endpoints) != 1)
return -1;
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
char *subscriber = list_first(&m->subscriber.endpoints);
char *publisher = list_first(&m->publisher.endpoints);
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
list_set(&m->subscriber.endpoints, 0, publisher);
list_set(&m->publisher.endpoints, 0, subscriber);
return 0;
}
static int nanomsg_parse_endpoints(struct list *l, json_t *cfg)
2017-05-23 14:54:25 +02:00
{
const char *ep;
size_t index;
2017-10-16 08:08:35 +02:00
json_t *json_val;
switch (json_typeof(cfg)) {
case JSON_ARRAY:
2017-10-16 08:08:35 +02:00
json_array_foreach(cfg, index, json_val) {
ep = json_string_value(json_val);
if (!ep)
return -1;
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
list_push(l, strdup(ep));
}
break;
case JSON_STRING:
ep = json_string_value(cfg);
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
list_push(l, strdup(ep));
break;
default:
return -1;
}
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
return 0;
}
int nanomsg_parse(struct node *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
const char *format = "villas.human";
json_error_t err;
2017-10-16 08:08:35 +02:00
json_t *json_pub = NULL;
json_t *json_sub = NULL;
2017-05-23 14:54:25 +02:00
list_init(&m->publisher.endpoints);
list_init(&m->subscriber.endpoints);
ret = json_unpack_ex(cfg, &err, 0, "{ s?: o, s?: o, s?: s }",
2017-10-16 08:08:35 +02:00
"publish", &json_pub,
"subscribe", &json_sub,
"format", &format
);
if (ret)
jerror(&err, "Failed to parse configuration of node %s", node_name(n));
2017-10-16 08:08:35 +02:00
if (json_pub) {
ret = nanomsg_parse_endpoints(&m->publisher.endpoints, json_pub);
2017-05-23 14:54:25 +02:00
if (ret < 0)
error("Invalid type for 'publish' setting of node %s", node_name(n));
2017-05-23 14:54:25 +02:00
}
2017-10-16 08:08:35 +02:00
if (json_sub) {
ret = nanomsg_parse_endpoints(&m->subscriber.endpoints, json_sub);
2017-05-23 14:54:25 +02:00
if (ret < 0)
error("Invalid type for 'subscribe' setting of node %s", node_name(n));
2017-05-23 14:54:25 +02:00
}
2018-05-12 13:56:12 +02:00
m->format = format_type_lookup(format);
if (!m->format)
error("Invalid format '%s' for node %s", format, node_name(n));
2017-05-23 14:54:25 +02:00
return 0;
}
char * nanomsg_print(struct node *n)
{
2017-10-18 15:39:53 +02:00
struct nanomsg *m = (struct nanomsg *) n->_vd;
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
char *buf = NULL;
strcatf(&buf, "format=%s, subscribe=[ ", plugin_name(m->format));
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
for (size_t i = 0; i < list_length(&m->subscriber.endpoints); i++) {
2017-10-18 15:39:53 +02:00
char *ep = (char *) list_at(&m->subscriber.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
2017-05-23 14:54:25 +02:00
strcatf(&buf, "], publish=[ ");
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
for (size_t i = 0; i < list_length(&m->publisher.endpoints); i++) {
2017-10-18 15:39:53 +02:00
char *ep = (char *) list_at(&m->publisher.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 nanomsg_start(struct node *n)
{
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
2018-05-12 15:25:29 +02:00
ret = io_init(&m->io, m->format, n, SAMPLE_HAS_ALL);
if (ret)
return ret;
2017-05-23 14:54:25 +02:00
ret = m->subscriber.socket = nn_socket(AF_SP, NN_SUB);
if (ret < 0)
return ret;
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
ret = m->publisher.socket = nn_socket(AF_SP, NN_PUB);
if (ret < 0)
return ret;
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->subscriber.socket, NN_SUB, NN_SUB_SUBSCRIBE, "", 0);
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->publisher.endpoints); i++) {
2017-10-18 15:39:53 +02:00
char *ep = (char *) list_at(&m->publisher.endpoints, i);
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
ret = nn_bind(m->publisher.socket, ep);
if (ret < 0) {
info("Failed to connect nanomsg socket: node=%s, endpoint=%s, error=%s", node_name(n), ep, nn_strerror(errno));
return ret;
}
}
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
/* Sonnect subscribers socket */
for (size_t i = 0; i < list_length(&m->subscriber.endpoints); i++) {
2017-10-18 15:39:53 +02:00
char *ep = (char *) list_at(&m->subscriber.endpoints, i);
2017-07-24 19:33:35 +02:00
2017-05-23 14:54:25 +02:00
ret = nn_connect(m->subscriber.socket, ep);
if (ret < 0) {
info("Failed to connect nanomsg socket: node=%s, endpoint=%s, error=%s", node_name(n), ep, nn_strerror(errno));
return ret;
}
}
return 0;
}
int nanomsg_stop(struct node *n)
{
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-16 12:19:29 +02:00
ret = nn_close(m->subscriber.socket);
2017-05-23 14:54:25 +02:00
if (ret < 0)
return ret;
2017-07-24 19:33:35 +02:00
2017-08-16 12:19:29 +02:00
ret = nn_close(m->publisher.socket);
2017-05-23 14:54:25 +02:00
if (ret < 0)
return ret;
return 0;
}
2018-05-12 15:25:29 +02:00
int nanomsg_destroy(struct node *n)
{
int ret;
struct nanomsg *m = (struct nanomsg *) n->_vd;
ret = io_destroy(&m->io);
if (ret)
return ret;
return 0;
}
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;
}
int nanomsg_read(struct node *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;
int bytes;
char data[NANOMSG_MAX_PACKET_LEN];
2017-05-23 14:54:25 +02:00
/* Receive payload */
bytes = nn_recv(m->subscriber.socket, data, sizeof(data), 0);
if (bytes < 0)
return -1;
2017-05-23 14:54:25 +02:00
return io_sscan(&m->io, data, bytes, NULL, smps, cnt);
2017-05-23 14:54:25 +02:00
}
int nanomsg_write(struct node *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
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
ret = io_sprint(&m->io, data, sizeof(data), &wbytes, smps, cnt);
if (ret <= 0)
2017-05-23 14:54:25 +02:00
return -1;
ret = nn_send(m->publisher.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 nanomsg_fd(struct node *n)
{
int ret;
2017-10-18 15:39:53 +02:00
struct nanomsg *m = (struct nanomsg *) n->_vd;
int fd;
size_t len = sizeof(fd);
ret = nn_getsockopt(m->subscriber.socket, NN_SOL_SOCKET, NN_RCVFD, &fd, &len);
if (ret)
return ret;
return fd;
}
2017-05-23 14:54:25 +02:00
static struct plugin p = {
.name = "nanomsg",
.description = "scalability protocols library (libnanomsg)",
2017-05-23 14:54:25 +02:00
.type = PLUGIN_TYPE_NODE,
.node = {
.vectorize = 0,
.size = sizeof(struct nanomsg),
.type.start = nanomsg_type_stop,
2017-05-23 14:54:25 +02:00
.reverse = nanomsg_reverse,
.parse = nanomsg_parse,
.print = nanomsg_print,
.start = nanomsg_start,
.stop = nanomsg_stop,
2018-05-12 15:25:29 +02:00
.destroy = nanomsg_destroy,
2017-05-23 14:54:25 +02:00
.read = nanomsg_read,
.write = nanomsg_write,
.fd = nanomsg_fd
2017-05-23 14:54:25 +02:00
}
};
2017-07-02 23:53:48 +02:00
REGISTER_PLUGIN(&p)
LIST_INIT_STATIC(&p.node.instances)