2017-05-22 20:09:26 +02:00
|
|
|
/** Node type: ZeroMQ
|
|
|
|
*
|
|
|
|
* @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-22 20:09:26 +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/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2017-05-22 20:09:26 +02:00
|
|
|
#include <zmq.h>
|
2017-06-29 21:10:33 +02:00
|
|
|
|
|
|
|
#if ZMQ_VERSION_MAJOR < 4 || (ZMQ_VERSION_MAJOR == 4 && ZMQ_VERSION_MINOR <= 1)
|
|
|
|
#include <zmq_utils.h>
|
|
|
|
#endif
|
2017-05-22 20:09:26 +02:00
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/node.h>
|
2021-06-21 16:11:42 -04:00
|
|
|
#include <villas/nodes/zeromq.hpp>
|
2021-05-10 00:12:30 +02:00
|
|
|
#include <villas/super_node.hpp>
|
2019-04-23 13:15:00 +02:00
|
|
|
#include <villas/utils.hpp>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/queue.h>
|
2019-10-29 20:18:30 +01:00
|
|
|
#include <villas/exceptions.hpp>
|
2017-05-22 20:09:26 +02:00
|
|
|
|
2019-10-29 20:18:30 +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;
|
|
|
|
|
2017-05-22 20:09:26 +02:00
|
|
|
static void *context;
|
|
|
|
|
2017-05-23 09:20:57 +02:00
|
|
|
/** Read one event off the monitor socket; return value and address
|
|
|
|
* by reference, if not null, and event number by value.
|
|
|
|
*
|
|
|
|
* @returnval -1 In case of error. */
|
|
|
|
static int get_monitor_event(void *monitor, int *value, char **address)
|
|
|
|
{
|
|
|
|
/* First frame in message contains event number and value */
|
|
|
|
zmq_msg_t msg;
|
|
|
|
zmq_msg_init (&msg);
|
|
|
|
if (zmq_msg_recv (&msg, monitor, 0) == -1)
|
|
|
|
return -1; /* Interruped, presumably. */
|
|
|
|
|
2017-05-23 11:13:41 +02:00
|
|
|
assert(zmq_msg_more (&msg));
|
2017-05-23 09:20:57 +02:00
|
|
|
|
2017-05-23 11:13:41 +02:00
|
|
|
uint8_t *data = (uint8_t *) zmq_msg_data(&msg);
|
2017-05-23 09:20:57 +02:00
|
|
|
uint16_t event = *(uint16_t *) (data);
|
|
|
|
if (value)
|
|
|
|
*value = *(uint32_t *) (data + 2);
|
|
|
|
|
|
|
|
/* Second frame in message contains event address */
|
2017-05-23 11:13:41 +02:00
|
|
|
zmq_msg_init(&msg);
|
|
|
|
if (zmq_msg_recv(&msg, monitor, 0) == -1)
|
2017-05-23 09:20:57 +02:00
|
|
|
return -1; /* Interruped, presumably. */
|
|
|
|
|
2017-05-23 11:13:41 +02:00
|
|
|
assert(!zmq_msg_more(&msg));
|
2017-05-23 09:20:57 +02:00
|
|
|
|
|
|
|
if (address) {
|
2017-05-23 11:13:41 +02:00
|
|
|
uint8_t *data = (uint8_t *) zmq_msg_data(&msg);
|
|
|
|
size_t size = zmq_msg_size(&msg);
|
|
|
|
*address = (char *) malloc(size + 1);
|
|
|
|
memcpy(*address, data, size);
|
2017-05-23 09:20:57 +02:00
|
|
|
*address [size] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int zeromq_reverse(struct vnode *n)
|
2017-05-22 20:09:26 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct zeromq *z = (struct zeromq *) n->_vd;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
if (vlist_length(&z->out.endpoints) != 1 ||
|
|
|
|
vlist_length(&z->in.endpoints) != 1)
|
2017-05-23 09:33:28 +02:00
|
|
|
return -1;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
char *subscriber = (char *) vlist_first(&z->in.endpoints);
|
|
|
|
char *publisher = (char *) vlist_first(&z->out.endpoints);
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
vlist_set(&z->in.endpoints, 0, publisher);
|
2019-01-07 10:28:55 +01:00
|
|
|
vlist_set(&z->out.endpoints, 0, subscriber);
|
2017-05-22 20:09:26 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int zeromq_init(struct vnode *n)
|
2019-10-29 20:18:30 +01:00
|
|
|
{
|
2020-09-10 11:11:42 +02:00
|
|
|
int ret;
|
2019-10-29 20:18:30 +01:00
|
|
|
struct zeromq *z = (struct zeromq *) n->_vd;
|
|
|
|
|
|
|
|
z->out.bind = 1;
|
|
|
|
z->in.bind = 0;
|
|
|
|
|
|
|
|
z->curve.enabled = false;
|
|
|
|
z->ipv6 = 0;
|
|
|
|
|
|
|
|
z->in.pending = 0;
|
|
|
|
z->out.pending = 0;
|
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
ret = vlist_init(&z->in.endpoints);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = vlist_init(&z->out.endpoints);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-10-29 20:18:30 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int zeromq_parse_endpoints(json_t *json_ep, struct vlist *epl)
|
|
|
|
{
|
|
|
|
json_t *json_val;
|
|
|
|
size_t i;
|
|
|
|
const char *ep;
|
|
|
|
|
|
|
|
switch (json_typeof(json_ep)) {
|
|
|
|
case JSON_ARRAY:
|
|
|
|
json_array_foreach(json_ep, i, json_val) {
|
|
|
|
ep = json_string_value(json_val);
|
|
|
|
if (!ep)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json_val, "node-config-node-publish", "All 'publish' settings must be strings");
|
2019-10-29 20:18:30 +01:00
|
|
|
|
|
|
|
vlist_push(epl, strdup(ep));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_STRING:
|
|
|
|
ep = json_string_value(json_ep);
|
|
|
|
vlist_push(epl, strdup(ep));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
int zeromq_parse(struct vnode *n, json_t *json)
|
2017-05-22 20:09:26 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct zeromq *z = (struct zeromq *) n->_vd;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
int ret;
|
2019-04-22 23:45:38 +02:00
|
|
|
const char *type = nullptr;
|
|
|
|
const char *in_filter = nullptr;
|
|
|
|
const char *out_filter = nullptr;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
json_error_t err;
|
2019-10-29 20:18:30 +01:00
|
|
|
json_t *json_in_ep = nullptr;
|
|
|
|
json_t *json_out_ep = nullptr;
|
2019-04-22 23:45:38 +02:00
|
|
|
json_t *json_curve = nullptr;
|
2021-05-10 00:12:30 +02:00
|
|
|
json_t *json_format = nullptr;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: { s?: o, s?: s, s?: b }, s?: { s?: o, s?: s, s?: b }, s?: o, s?: s, s?: b, s?: o }",
|
2018-08-20 18:25:42 +02:00
|
|
|
"in",
|
2019-10-29 20:18:30 +01:00
|
|
|
"subscribe", &json_in_ep,
|
2018-08-20 18:25:42 +02:00
|
|
|
"filter", &in_filter,
|
2019-10-29 20:18:30 +01:00
|
|
|
"bind", &z->in.bind,
|
2018-08-20 18:25:42 +02:00
|
|
|
"out",
|
2019-10-29 20:18:30 +01:00
|
|
|
"publish", &json_out_ep,
|
2018-08-20 18:25:42 +02:00
|
|
|
"filter", &out_filter,
|
2019-10-29 20:18:30 +01:00
|
|
|
"bind", &z->out.bind,
|
2017-10-16 08:08:35 +02:00
|
|
|
"curve", &json_curve,
|
2017-08-03 00:19:27 +02:00
|
|
|
"pattern", &type,
|
2017-08-14 14:42:07 +02:00
|
|
|
"ipv6", &z->ipv6,
|
2021-05-10 00:12:30 +02:00
|
|
|
"format", &json_format
|
2017-08-03 00:19:27 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, err, "node-config-node-zeromq");
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
z->in.filter = in_filter ? strdup(in_filter) : nullptr;
|
|
|
|
z->out.filter = out_filter ? strdup(out_filter) : nullptr;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
/* Format */
|
|
|
|
z->formatter = json_format
|
|
|
|
? FormatFactory::make(json_format)
|
|
|
|
: FormatFactory::make("villas.binary");
|
|
|
|
if (!z->formatter)
|
|
|
|
throw ConfigError(json_format, "node-config-node-zeromq-format", "Invalid format configuration");
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
if (json_out_ep) {
|
|
|
|
ret = zeromq_parse_endpoints(json_out_ep, &z->out.endpoints);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(json_out_ep, "node-config-node-zeromq-publish", "Failed to parse list of publish endpoints");
|
|
|
|
}
|
2017-05-22 20:09:26 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
if (json_in_ep) {
|
|
|
|
ret = zeromq_parse_endpoints(json_in_ep, &z->in.endpoints);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(json_out_ep, "node-config-node-zeromq-subscribe", "Failed to parse list of subscribe endpoints");
|
2017-05-22 20:09:26 +02:00
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-10-16 08:08:35 +02:00
|
|
|
if (json_curve) {
|
2017-05-23 09:58:11 +02:00
|
|
|
const char *public_key, *secret_key;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
z->curve.enabled = true;
|
2017-05-23 09:58:11 +02:00
|
|
|
|
2017-10-16 08:08:35 +02:00
|
|
|
ret = json_unpack_ex(json_curve, &err, 0, "{ s: s, s: s, s?: b }",
|
2017-08-03 00:19:27 +02:00
|
|
|
"public_key", &public_key,
|
|
|
|
"secret_key", &secret_key,
|
|
|
|
"enabled", &z->curve.enabled
|
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json_curve, err, "node-config-node-zeromq-curve", "Failed to parse setting 'curve'");
|
2017-05-23 09:58:11 +02:00
|
|
|
|
|
|
|
if (strlen(secret_key) != 40)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json_curve, err, "node-config-node-zeromq-curve", "Setting 'curve.secret_key' must be a Z85 encoded CurveZMQ key");
|
2017-05-23 09:58:11 +02:00
|
|
|
|
|
|
|
if (strlen(public_key) != 40)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json_curve, err, "node-config-node-zeromq-curve", "Setting 'curve.public_key' must be a Z85 encoded CurveZMQ key");
|
2017-05-23 09:58:11 +02:00
|
|
|
|
2018-04-04 08:53:00 +02:00
|
|
|
memcpy(z->curve.server.public_key, public_key, 41);
|
|
|
|
memcpy(z->curve.server.secret_key, secret_key, 41);
|
2017-05-23 09:58:11 +02:00
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 09:58:11 +02:00
|
|
|
/** @todo We should fix this. Its mostly done. */
|
|
|
|
if (z->curve.enabled)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json_curve, "node-config-zeromq-curve", "CurveZMQ support is currently broken");
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
if (type) {
|
2017-05-22 20:09:26 +02:00
|
|
|
if (!strcmp(type, "pubsub"))
|
2019-06-23 16:13:23 +02:00
|
|
|
z->pattern = zeromq::Pattern::PUBSUB;
|
2017-06-15 13:59:09 +02:00
|
|
|
#ifdef ZMQ_BUILD_DISH
|
2017-05-22 20:09:26 +02:00
|
|
|
else if (!strcmp(type, "radiodish"))
|
2019-06-23 16:13:23 +02:00
|
|
|
z->pattern = zeromq::Pattern::RADIODISH;
|
2017-06-15 13:59:09 +02:00
|
|
|
#endif
|
2017-05-22 20:09:26 +02:00
|
|
|
else
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, "node-config-node-zeromq-type", "Invalid type for ZeroMQ node: {}", node_name_short(n));
|
2017-05-22 20:09:26 +02:00
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-22 20:09:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
char * zeromq_print(struct vnode *n)
|
2017-05-22 20:09:26 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct zeromq *z = (struct zeromq *) n->_vd;
|
2017-05-22 20:09:26 +02:00
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
char *buf = nullptr;
|
|
|
|
const char *pattern = nullptr;
|
2017-05-22 20:09:26 +02:00
|
|
|
|
|
|
|
switch (z->pattern) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case zeromq::Pattern::PUBSUB:
|
2019-04-22 23:43:46 +02:00
|
|
|
pattern = "pubsub";
|
|
|
|
break;
|
|
|
|
|
2017-06-15 13:59:09 +02:00
|
|
|
#ifdef ZMQ_BUILD_DISH
|
2019-06-23 16:13:23 +02:00
|
|
|
case zeromq::Pattern::RADIODISH:
|
2019-04-22 23:43:46 +02:00
|
|
|
pattern = "radiodish";
|
|
|
|
break;
|
2017-06-15 13:59:09 +02:00
|
|
|
#endif
|
2017-05-22 20:09:26 +02:00
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
strcatf(&buf, "pattern=%s, ipv6=%s, crypto=%s, in.bind=%s, out.bind=%s, in.subscribe=[ ",
|
2017-08-14 14:42:07 +02:00
|
|
|
pattern,
|
|
|
|
z->ipv6 ? "yes" : "no",
|
|
|
|
z->curve.enabled ? "yes" : "no",
|
2019-10-29 20:18:30 +01:00
|
|
|
z->in.bind ? "yes" : "no",
|
|
|
|
z->out.bind ? "yes" : "no"
|
2017-08-14 14:42:07 +02:00
|
|
|
);
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
for (size_t i = 0; i < vlist_length(&z->in.endpoints); i++) {
|
|
|
|
char *ep = (char *) vlist_at(&z->in.endpoints, i);
|
|
|
|
|
|
|
|
strcatf(&buf, "%s ", ep);
|
|
|
|
}
|
|
|
|
|
|
|
|
strcatf(&buf, "], out.publish=[ ");
|
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
for (size_t i = 0; i < vlist_length(&z->out.endpoints); i++) {
|
|
|
|
char *ep = (char *) vlist_at(&z->out.endpoints, i);
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-22 20:09:26 +02:00
|
|
|
strcatf(&buf, "%s ", ep);
|
|
|
|
}
|
|
|
|
|
2017-05-28 19:43:19 +02:00
|
|
|
strcatf(&buf, "]");
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2018-08-20 18:25:42 +02:00
|
|
|
if (z->in.filter)
|
|
|
|
strcatf(&buf, ", in.filter=%s", z->in.filter);
|
|
|
|
|
|
|
|
if (z->out.filter)
|
|
|
|
strcatf(&buf, ", out.filter=%s", z->out.filter);
|
2017-05-22 20:09:26 +02:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int zeromq_check(struct vnode *n)
|
2019-10-29 20:18:30 +01:00
|
|
|
{
|
|
|
|
struct zeromq *z = (struct zeromq *) n->_vd;
|
|
|
|
|
|
|
|
if (vlist_length(&z->in.endpoints) == 0 &&
|
|
|
|
vlist_length(&z->out.endpoints) == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-23 13:14:47 +02:00
|
|
|
int zeromq_type_start(villas::node::SuperNode *sn)
|
2017-05-22 20:09:26 +02:00
|
|
|
{
|
|
|
|
context = zmq_ctx_new();
|
2017-05-23 09:06:22 +02:00
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
return context == nullptr;
|
2017-05-22 20:09:26 +02:00
|
|
|
}
|
|
|
|
|
2018-07-16 08:08:17 +02:00
|
|
|
int zeromq_type_stop()
|
2017-05-22 20:09:26 +02:00
|
|
|
{
|
|
|
|
return zmq_ctx_term(context);
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int zeromq_start(struct vnode *n)
|
2017-05-22 20:09:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct zeromq *z = (struct zeromq *) n->_vd;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
struct zeromq::Dir* dirs[] = { &z->out, &z->in };
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
z->formatter->start(&n->in.signals, ~(int) SampleFlags::HAS_OFFSET);
|
2018-08-20 18:25:42 +02:00
|
|
|
|
2017-05-22 20:09:26 +02:00
|
|
|
switch (z->pattern) {
|
2017-06-15 13:59:09 +02:00
|
|
|
#ifdef ZMQ_BUILD_DISH
|
2019-06-23 16:13:23 +02:00
|
|
|
case zeromq::Pattern::RADIODISH:
|
2019-10-29 20:18:30 +01:00
|
|
|
z->in.socket = zmq_socket(context, ZMQ_DISH);
|
2018-08-20 18:25:42 +02:00
|
|
|
z->out.socket = zmq_socket(context, ZMQ_RADIO);
|
2017-05-22 20:09:26 +02:00
|
|
|
break;
|
2017-06-15 13:59:09 +02:00
|
|
|
#endif
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case zeromq::Pattern::PUBSUB:
|
2019-10-29 20:18:30 +01:00
|
|
|
z->in.socket = zmq_socket(context, ZMQ_SUB);
|
2018-08-20 18:25:42 +02:00
|
|
|
z->out.socket = zmq_socket(context, ZMQ_PUB);
|
2017-05-22 20:09:26 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2018-08-20 18:25:42 +02:00
|
|
|
if (!z->in.socket || !z->out.socket) {
|
2017-05-23 11:13:41 +02:00
|
|
|
ret = -1;
|
|
|
|
goto fail;
|
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 11:15:00 +02:00
|
|
|
/* Join group */
|
|
|
|
switch (z->pattern) {
|
2017-06-15 13:59:09 +02:00
|
|
|
#ifdef ZMQ_BUILD_DISH
|
2019-06-23 16:13:23 +02:00
|
|
|
case zeromq::Pattern::RADIODISH:
|
2018-08-20 18:25:42 +02:00
|
|
|
ret = zmq_join(z->in.socket, z->in.filter);
|
2017-05-23 11:15:00 +02:00
|
|
|
break;
|
2017-06-15 13:59:09 +02:00
|
|
|
#endif
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case zeromq::Pattern::PUBSUB:
|
2018-08-20 18:25:42 +02:00
|
|
|
ret = zmq_setsockopt(z->in.socket, ZMQ_SUBSCRIBE, z->in.filter, z->in.filter ? strlen(z->in.filter) : 0);
|
2017-05-23 11:15:00 +02:00
|
|
|
break;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 15:03:39 +02:00
|
|
|
default:
|
|
|
|
ret = -1;
|
2017-05-23 11:15:00 +02:00
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 11:15:00 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 09:58:11 +02:00
|
|
|
if (z->curve.enabled) {
|
|
|
|
/* Publisher has server role */
|
2018-08-20 18:25:42 +02:00
|
|
|
ret = zmq_setsockopt(z->out.socket, ZMQ_CURVE_SECRETKEY, z->curve.server.secret_key, 41);
|
2017-05-23 09:58:11 +02:00
|
|
|
if (ret)
|
2017-05-23 11:13:41 +02:00
|
|
|
goto fail;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2018-08-20 18:25:42 +02:00
|
|
|
ret = zmq_setsockopt(z->out.socket, ZMQ_CURVE_PUBLICKEY, z->curve.server.public_key, 41);
|
2017-05-23 09:58:11 +02:00
|
|
|
if (ret)
|
2017-05-23 11:13:41 +02:00
|
|
|
goto fail;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 09:58:11 +02:00
|
|
|
int curve_server = 1;
|
2018-08-20 18:25:42 +02:00
|
|
|
ret = zmq_setsockopt(z->out.socket, ZMQ_CURVE_SERVER, &curve_server, sizeof(curve_server));
|
2017-05-23 09:58:11 +02:00
|
|
|
if (ret)
|
2017-05-23 11:13:41 +02:00
|
|
|
goto fail;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 09:58:11 +02:00
|
|
|
/* Create temporary client keys first */
|
|
|
|
ret = zmq_curve_keypair(z->curve.client.public_key, z->curve.client.secret_key);
|
|
|
|
if (ret)
|
2017-05-23 11:13:41 +02:00
|
|
|
goto fail;
|
2017-05-23 09:58:11 +02:00
|
|
|
|
|
|
|
/* Subscriber has client role */
|
2018-08-20 18:25:42 +02:00
|
|
|
ret = zmq_setsockopt(z->in.socket, ZMQ_CURVE_SECRETKEY, z->curve.client.secret_key, 41);
|
2017-05-23 09:58:11 +02:00
|
|
|
if (ret)
|
2017-05-23 11:13:41 +02:00
|
|
|
goto fail;
|
2017-05-23 09:58:11 +02:00
|
|
|
|
2018-08-20 18:25:42 +02:00
|
|
|
ret = zmq_setsockopt(z->in.socket, ZMQ_CURVE_PUBLICKEY, z->curve.client.public_key, 41);
|
2017-05-23 09:58:11 +02:00
|
|
|
if (ret)
|
2017-05-23 11:13:41 +02:00
|
|
|
goto fail;
|
2017-05-23 09:58:11 +02:00
|
|
|
|
2018-08-20 18:25:42 +02:00
|
|
|
ret = zmq_setsockopt(z->in.socket, ZMQ_CURVE_SERVERKEY, z->curve.server.public_key, 41);
|
2017-05-22 20:09:26 +02:00
|
|
|
if (ret)
|
2017-05-23 11:13:41 +02:00
|
|
|
goto fail;
|
2017-05-22 20:09:26 +02:00
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
for (auto d : dirs) {
|
|
|
|
const char *mon_ep = d == &z->in ? "inproc://monitor-in" : "inproc://monitor-out";
|
2017-05-23 09:20:57 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
ret = zmq_setsockopt(d->socket, ZMQ_IPV6, &z->ipv6, sizeof(z->ipv6));
|
|
|
|
if (ret)
|
|
|
|
goto fail;
|
2017-05-23 09:20:57 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
int linger = 1000;
|
|
|
|
ret = zmq_setsockopt(d->socket, ZMQ_LINGER, &linger, sizeof(linger));
|
|
|
|
if (ret)
|
|
|
|
goto fail;
|
2017-05-23 09:20:57 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
/* Monitor events on the server */
|
|
|
|
ret = zmq_socket_monitor(d->socket, mon_ep, ZMQ_EVENT_ALL);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
/* Create socket for collecting monitor events */
|
|
|
|
d->mon_socket = zmq_socket(context, ZMQ_PAIR);
|
|
|
|
if (!d->mon_socket) {
|
|
|
|
ret = -1;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect it to the inproc endpoints so they'll get events */
|
|
|
|
ret = zmq_connect(d->mon_socket, mon_ep);
|
2017-05-28 19:43:19 +02:00
|
|
|
if (ret < 0)
|
2017-05-23 11:13:41 +02:00
|
|
|
goto fail;
|
2019-10-29 20:18:30 +01:00
|
|
|
|
|
|
|
/* Connect / bind sockets to endpoints */
|
|
|
|
for (size_t i = 0; i < vlist_length(&d->endpoints); i++) {
|
|
|
|
char *ep = (char *) vlist_at(&d->endpoints, i);
|
|
|
|
|
|
|
|
if (d->bind) {
|
|
|
|
ret = zmq_bind(d->socket, ep);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = zmq_connect(d->socket, ep);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->pending++;
|
|
|
|
}
|
2017-05-22 20:09:26 +02:00
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
/* Wait for all connections to be connected */
|
|
|
|
for (auto d : dirs) {
|
|
|
|
while (d->pending > 0) {
|
|
|
|
int evt = d->bind ? ZMQ_EVENT_LISTENING : ZMQ_EVENT_CONNECTED;
|
|
|
|
|
|
|
|
ret = get_monitor_event(d->mon_socket, nullptr, nullptr);
|
|
|
|
if (ret == evt)
|
|
|
|
d->pending--;
|
2017-05-23 15:15:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 17:35:01 +02:00
|
|
|
#if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_MAJOR_VERSION >= 4 && ZMQ_MINOR_VERSION >= 2 && ZMQ_MINOR_VERSION >= 3
|
2017-05-23 11:15:58 +02:00
|
|
|
if (z->curve.enabled) {
|
2019-04-22 23:45:38 +02:00
|
|
|
ret = get_monitor_event(z->in.mon_socket, nullptr, nullptr);
|
2018-10-20 14:52:28 +02:00
|
|
|
return ret == ZMQ_EVENT_HANDSHAKE_SUCCEEDED;
|
2017-05-23 11:15:58 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0; /* The handshake events are only emitted for CurveZMQ sessions. */
|
2017-05-23 09:20:57 +02:00
|
|
|
#else
|
2017-05-22 20:09:26 +02:00
|
|
|
return 0;
|
2017-05-23 09:20:57 +02:00
|
|
|
#endif
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 11:13:41 +02:00
|
|
|
fail:
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->info("Failed to start: {}", zmq_strerror(errno));
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 11:13:41 +02:00
|
|
|
return ret;
|
2017-05-22 20:09:26 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int zeromq_stop(struct vnode *n)
|
2017-05-22 20:09:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct zeromq *z = (struct zeromq *) n->_vd;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
struct zeromq::Dir* dirs[] = { &z->out, &z->in };
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
for (auto d : dirs) {
|
|
|
|
ret = zmq_close(d->socket);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = zmq_close(d->mon_socket);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2017-05-22 20:09:26 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
delete z->formatter;
|
2018-08-20 18:25:42 +02:00
|
|
|
|
2019-10-29 20:18:30 +01:00
|
|
|
return 0;
|
2017-05-22 20:09:26 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int zeromq_destroy(struct vnode *n)
|
2018-05-12 15:25:29 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct zeromq *z = (struct zeromq *) n->_vd;
|
|
|
|
|
2018-08-20 18:25:42 +02:00
|
|
|
if (z->in.filter)
|
|
|
|
free(z->in.filter);
|
|
|
|
|
|
|
|
if (z->out.filter)
|
|
|
|
free(z->out.filter);
|
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
ret = vlist_destroy(&z->out.endpoints, nullptr, true);
|
2018-05-12 15:25:29 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
int zeromq_read(struct vnode *n, struct sample * const smps[], unsigned cnt)
|
2017-05-22 20:09:26 +02:00
|
|
|
{
|
2017-05-23 09:33:42 +02:00
|
|
|
int recv, ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct zeromq *z = (struct zeromq *) n->_vd;
|
2017-05-22 20:09:26 +02:00
|
|
|
|
2017-05-23 09:33:42 +02:00
|
|
|
zmq_msg_t m;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 09:33:42 +02:00
|
|
|
ret = zmq_msg_init(&m);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2018-08-20 18:25:42 +02:00
|
|
|
if (z->in.filter) {
|
2017-05-23 11:15:00 +02:00
|
|
|
switch (z->pattern) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case zeromq::Pattern::PUBSUB:
|
2017-05-23 11:15:00 +02:00
|
|
|
/* Discard envelope */
|
2019-04-22 23:45:38 +02:00
|
|
|
zmq_recv(z->in.socket, nullptr, 0, 0);
|
2017-05-23 11:15:00 +02:00
|
|
|
break;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 11:15:00 +02:00
|
|
|
default: { }
|
|
|
|
}
|
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 11:15:00 +02:00
|
|
|
/* Receive payload */
|
2018-08-20 18:25:42 +02:00
|
|
|
ret = zmq_msg_recv(&m, z->in.socket, 0);
|
2017-05-23 09:33:42 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2017-05-22 20:09:26 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
recv = z->formatter->sscan((const char *) zmq_msg_data(&m), zmq_msg_size(&m), nullptr, smps, cnt);
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 09:33:42 +02:00
|
|
|
ret = zmq_msg_close(&m);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 09:33:42 +02:00
|
|
|
return recv;
|
2017-05-22 20:09:26 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
int zeromq_write(struct vnode *n, struct sample * const smps[], unsigned cnt)
|
2017-05-22 20:09:26 +02:00
|
|
|
{
|
2017-05-23 09:33:42 +02:00
|
|
|
int ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct zeromq *z = (struct zeromq *) n->_vd;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
size_t wbytes;
|
2017-05-23 09:33:42 +02:00
|
|
|
zmq_msg_t m;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-08-23 15:48:34 +02:00
|
|
|
char data[4096];
|
2017-05-22 20:09:26 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
ret = z->formatter->sprint(data, sizeof(data), &wbytes, smps, cnt);
|
2017-08-14 14:42:07 +02:00
|
|
|
if (ret <= 0)
|
2017-05-23 09:33:42 +02:00
|
|
|
return -1;
|
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
ret = zmq_msg_init_size(&m, wbytes);
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2018-08-20 18:25:42 +02:00
|
|
|
if (z->out.filter) {
|
2017-05-23 11:15:00 +02:00
|
|
|
switch (z->pattern) {
|
2017-06-15 13:59:09 +02:00
|
|
|
#ifdef ZMQ_BUILD_DISH
|
2019-06-23 16:13:23 +02:00
|
|
|
case zeromq::Pattern::RADIODISH:
|
2018-08-20 18:25:42 +02:00
|
|
|
ret = zmq_msg_set_group(&m, z->out.filter);
|
2017-05-23 11:15:00 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
break;
|
2017-06-15 13:59:09 +02:00
|
|
|
#endif
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case zeromq::Pattern::PUBSUB: /* Send envelope */
|
2018-08-20 18:25:42 +02:00
|
|
|
zmq_send(z->out.socket, z->out.filter, strlen(z->out.filter), ZMQ_SNDMORE);
|
2017-05-23 11:15:00 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-05-23 09:34:53 +02:00
|
|
|
}
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
memcpy(zmq_msg_data(&m), data, wbytes);
|
2017-05-23 09:33:42 +02:00
|
|
|
|
2018-08-20 18:25:42 +02:00
|
|
|
ret = zmq_msg_send(&m, z->out.socket, 0);
|
2017-05-23 09:34:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-05-23 09:34:53 +02:00
|
|
|
ret = zmq_msg_close(&m);
|
2017-05-23 09:33:42 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2017-05-22 20:09:26 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
return cnt;
|
2017-05-23 09:34:53 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
zmq_msg_close(&m);
|
|
|
|
|
|
|
|
return ret;
|
2017-05-22 20:09:26 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int zeromq_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 zeromq *z = (struct zeromq *) 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);
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-08-20 18:25:42 +02:00
|
|
|
ret = zmq_getsockopt(z->in.socket, ZMQ_FD, &fd, &len);
|
2017-08-30 00:25:42 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
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 zeromq_netem_fds(struct vnode *n, int fds[])
|
2019-04-04 22:01:52 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct zeromq *z = (struct zeromq *) n->_vd;
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
size_t len = sizeof(fd);
|
|
|
|
|
|
|
|
ret = zmq_getsockopt(z->out.socket, ZMQ_FD, &fd, &len);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
fds[0] = fd;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:11:42 -04:00
|
|
|
static struct vnode_type p;
|
2019-04-23 00:36:06 +02:00
|
|
|
|
|
|
|
__attribute__((constructor(110)))
|
|
|
|
static void register_plugin() {
|
2021-06-21 16:11:42 -04:00
|
|
|
p.name = "zeromq";
|
|
|
|
p.description = "ZeroMQ Distributed Messaging (libzmq)";
|
|
|
|
p.vectorize = 0;
|
|
|
|
p.size = sizeof(struct zeromq);
|
|
|
|
p.type.start = zeromq_type_start;
|
|
|
|
p.type.stop = zeromq_type_stop;
|
|
|
|
p.init = zeromq_init;
|
|
|
|
p.destroy = zeromq_destroy;
|
|
|
|
p.check = zeromq_check;
|
|
|
|
p.parse = zeromq_parse;
|
|
|
|
p.print = zeromq_print;
|
|
|
|
p.start = zeromq_start;
|
|
|
|
p.stop = zeromq_stop;
|
|
|
|
p.read = zeromq_read;
|
|
|
|
p.write = zeromq_write;
|
|
|
|
p.reverse = zeromq_reverse;
|
|
|
|
p.poll_fds = zeromq_poll_fds;
|
|
|
|
p.netem_fds = zeromq_netem_fds;
|
|
|
|
|
|
|
|
if (!node_types)
|
|
|
|
node_types = new NodeTypeList();
|
|
|
|
|
|
|
|
node_types->push_back(&p);
|
2019-04-23 13:14:47 +02:00
|
|
|
}
|