2017-07-14 13:12:32 +02:00
|
|
|
/** Node-type for loopback connections.
|
|
|
|
*
|
|
|
|
* @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/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2018-02-06 21:26:12 +01:00
|
|
|
#include <villas/node.h>
|
|
|
|
#include <villas/plugin.h>
|
|
|
|
#include <villas/config.h>
|
|
|
|
#include <villas/nodes/loopback.h>
|
|
|
|
#include <villas/memory.h>
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
int loopback_parse(struct node *n, json_t *cfg)
|
2017-07-14 13:12:32 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct loopback *l = (struct loopback *) n->_vd;
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
json_error_t err;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Default values */
|
|
|
|
l->queuelen = DEFAULT_QUEUELEN;
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2017-08-31 09:31:19 +02:00
|
|
|
ret = json_unpack_ex(cfg, &err, 0, "{ s?: i }",
|
|
|
|
"queuelen", &l->queuelen
|
2017-08-03 00:19:27 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
jerror(&err, "Failed to parse configuration of node %s", node_name(n));
|
2017-07-14 16:29:05 +02:00
|
|
|
|
2017-07-14 13:12:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int loopback_open(struct node *n)
|
|
|
|
{
|
2017-07-14 16:29:05 +02:00
|
|
|
int ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct loopback *l = (struct loopback *) n->_vd;
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
ret = pool_init(&l->pool, l->queuelen, SAMPLE_LEN(n->samplelen), &memory_hugepage);
|
2017-07-14 16:29:05 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
return queue_signalled_init(&l->queue, l->queuelen, &memory_hugepage, QUEUE_SIGNALLED_EVENTFD);
|
2017-07-14 13:12:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int loopback_close(struct node *n)
|
|
|
|
{
|
2017-07-14 16:29:05 +02:00
|
|
|
int ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct loopback *l= (struct loopback *) n->_vd;
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2017-07-14 16:29:05 +02:00
|
|
|
ret = pool_destroy(&l->pool);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-07-14 13:12:32 +02:00
|
|
|
return queue_signalled_destroy(&l->queue);
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
int loopback_read(struct node *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2017-07-14 13:12:32 +02:00
|
|
|
{
|
2017-07-14 16:29:05 +02:00
|
|
|
int avail;
|
|
|
|
|
2017-10-18 15:39:53 +02:00
|
|
|
struct loopback *l = (struct loopback *) n->_vd;
|
2018-07-11 18:14:29 +02:00
|
|
|
struct sample *cpys[cnt];
|
2017-07-14 16:29:05 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
avail = queue_signalled_pull_many(&l->queue, (void **) cpys, cnt);
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2017-07-14 16:29:05 +02:00
|
|
|
for (int i = 0; i < avail; i++) {
|
|
|
|
sample_copy(smps[i], cpys[i]);
|
|
|
|
sample_put(cpys[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return avail;
|
2017-07-14 13:12:32 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
int loopback_write(struct node *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2017-07-14 13:12:32 +02:00
|
|
|
{
|
2017-10-18 14:23:50 +02:00
|
|
|
int copied;
|
2017-07-14 16:29:05 +02:00
|
|
|
|
2017-10-18 15:39:53 +02:00
|
|
|
struct loopback *l = (struct loopback *) n->_vd;
|
2018-07-11 18:14:29 +02:00
|
|
|
struct sample *copies[cnt];
|
2017-07-14 16:29:05 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
copied = sample_alloc_many(&l->pool, copies, cnt);
|
|
|
|
if (copied < cnt)
|
2017-10-16 08:08:17 +02:00
|
|
|
warn("Pool underrun for node %s", node_name(n));
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2017-10-18 14:23:50 +02:00
|
|
|
sample_copy_many(copies, smps, copied);
|
|
|
|
|
|
|
|
return queue_signalled_push_many(&l->queue, (void **) copies, copied);
|
2017-07-14 13:12:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char * loopback_print(struct node *n)
|
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct loopback *l = (struct loopback *) n->_vd;
|
2017-07-14 13:12:32 +02:00
|
|
|
char *buf = NULL;
|
|
|
|
|
|
|
|
strcatf(&buf, "queuelen=%d", l->queuelen);
|
|
|
|
|
|
|
|
return buf;
|
2017-10-16 23:08:46 +02:00
|
|
|
}
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2017-08-30 13:30:31 +02:00
|
|
|
int loopback_fd(struct node *n)
|
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct loopback *l = (struct loopback *) n->_vd;
|
2017-10-16 23:08:46 +02:00
|
|
|
|
2017-08-30 13:30:31 +02:00
|
|
|
return queue_signalled_fd(&l->queue);
|
|
|
|
}
|
|
|
|
|
2017-07-14 13:12:32 +02:00
|
|
|
static struct plugin p = {
|
|
|
|
.name = "loopback",
|
|
|
|
.description = "Loopback to connect multiple paths",
|
|
|
|
.type = PLUGIN_TYPE_NODE,
|
|
|
|
.node = {
|
|
|
|
.vectorize = 0,
|
2017-08-30 13:30:31 +02:00
|
|
|
.size = sizeof(struct loopback),
|
|
|
|
.parse = loopback_parse,
|
|
|
|
.print = loopback_print,
|
|
|
|
.start = loopback_open,
|
|
|
|
.stop = loopback_close,
|
|
|
|
.read = loopback_read,
|
|
|
|
.write = loopback_write,
|
|
|
|
.fd = loopback_fd
|
2017-07-14 13:12:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_PLUGIN(&p)
|
|
|
|
LIST_INIT_STATIC(&p.node.instances)
|