1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/server/src/node.c

142 lines
2.6 KiB
C
Raw Normal View History

/** Nodes.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2015-06-02 21:53:04 +02:00
* @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC
* This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
2015-08-07 01:11:43 +02:00
* Unauthorized copying of this file, via any medium is strictly prohibited.
2015-06-02 21:53:04 +02:00
*********************************************************************************/
#include <string.h>
2014-12-05 12:39:52 +01:00
#include "node.h"
#include "cfg.h"
#include "utils.h"
2014-12-05 12:39:52 +01:00
/* Node types */
2015-03-31 14:36:30 +02:00
#include "file.h"
2015-05-07 13:03:34 +02:00
#ifdef ENABLE_GTFPGA
2015-08-21 10:19:32 +02:00
#include "gtfpga.h"
2015-05-07 13:03:34 +02:00
#endif
#ifdef ENABLE_OPAL_ASYNC
2015-08-21 10:19:32 +02:00
#include "opal.h"
#endif
#ifdef ENABLE_SOCKET
#include "socket.h"
#include <netlink/route/qdisc.h>
#include <netlink/route/classifier.h>
#endif
#ifdef ENABLE_NGSI
#include "ngsi.h"
#include <jansson.h>
#endif
2014-12-05 12:39:52 +01:00
/** Vtable for virtual node sub types */
struct list node_types = LIST_INIT(NULL);
2014-12-05 12:39:52 +01:00
int node_init(int argc, char *argv[], struct settings *set)
{ INDENT
list_foreach(const struct node_type *vt, &node_types) {
if (vt->refcnt) {
info("Initializing '%s' node type", vt->name);
vt->init(argc, argv, set);
}
}
2015-08-07 01:11:43 +02:00
return 0;
}
int node_deinit()
{ INDENT
/* De-initialize node types */
list_foreach(const struct node_type *vt, &node_types) {
if (vt->refcnt) {
info("De-initializing '%s' node type", vt->name);
vt->deinit();
}
}
2015-10-09 13:04:52 +02:00
return 0;
}
2014-12-05 12:39:52 +01:00
int node_start(struct node *n)
2015-03-21 15:29:00 +01:00
{ INDENT
2015-03-18 16:16:44 +01:00
if (!n->refcnt) {
warn("Node '%s' is unused. Skipping...", n->name);
return -1;
2015-03-18 16:16:44 +01:00
}
2014-12-05 12:39:52 +01:00
char *buf = node_print(n);
debug(1, "Starting node '%s' of type '%s' (%s)", n->name, n->_vt->name, buf);
free(buf);
2014-12-05 12:39:52 +01:00
{ INDENT
return node_open(n);
}
2014-12-05 12:39:52 +01:00
}
2014-12-05 12:39:52 +01:00
int node_stop(struct node *n)
2015-03-21 15:29:00 +01:00
{ INDENT
2014-12-05 12:39:52 +01:00
int ret;
if (!n->refcnt) /* Unused and not started. No reason to stop.. */
return -1;
2014-12-05 12:39:52 +01:00
info("Stopping node '%s'", n->name);
2015-08-07 01:11:43 +02:00
2014-12-05 12:39:52 +01:00
{ INDENT
ret = node_close(n);
2014-12-05 12:39:52 +01:00
}
2015-08-07 01:11:43 +02:00
2014-12-05 12:39:52 +01:00
return ret;
}
2015-03-21 15:29:00 +01:00
void node_reverse(struct node *n)
{
switch (node_type(n)) {
#ifdef ENABLE_SOCKET
case BSD_SOCKET:
SWAP(n->socket->remote, n->socket->local);
break;
#endif
2015-08-09 23:52:44 +02:00
case LOG_FILE:
SWAP(n->file->path_in, n->file->path_out);
break;
default: { }
}
2015-03-21 15:29:00 +01:00
}
2015-10-13 15:05:48 +02:00
struct node * node_create(struct node_type *vt)
{
2015-10-13 15:05:48 +02:00
struct node *n = alloc(sizeof(struct node));
n->_vt = vt;
return n;
}
void node_destroy(struct node *n)
{
switch (node_type(n)) {
#ifdef ENABLE_NGSI
case NGSI:
json_decref(n->ngsi->context);
free(n->ngsi->context_map);
break;
#endif
#ifdef ENABLE_SOCKET
case BSD_SOCKET:
rtnl_qdisc_put(n->socket->tc_qdisc);
rtnl_cls_put(n->socket->tc_classifier);
break;
#endif
2015-08-09 23:52:44 +02:00
case LOG_FILE:
free(n->file->path_in);
free(n->file->path_out);
break;
default: { }
}
free(n->socket);
free(n);
}