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

164 lines
3.2 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
2014-12-05 12:39:52 +01:00
#define VTABLE(type, name, fnc) { type, name, \
fnc ## _parse, fnc ## _print, \
fnc ## _open, fnc ## _close, \
fnc ## _read, fnc ## _write, \
fnc ## _init, fnc ## _deinit }
2014-12-05 12:39:52 +01:00
/** Vtable for virtual node sub types */
struct node_vtable vtables[] = {
#ifdef ENABLE_OPAL_ASYNC
VTABLE(OPAL_ASYNC, "opal", opal),
#endif
#ifdef ENABLE_GTFPGA
VTABLE(GTFPGA, "gtfpga", gtfpga),
#endif
#ifdef ENABLE_SOCKET
VTABLE(BSD_SOCKET, "socket", socket),
#endif
VTABLE(LOG_FILE, "file", file)
2014-12-05 12:39:52 +01:00
};
int node_init(int argc, char *argv[], struct settings *set)
{ INDENT
for (int i=0; i<ARRAY_LEN(vtables); i++) {
const struct node_vtable *vt = &vtables[i];
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 */
for (int i=0; i<ARRAY_LEN(vtables); i++) {
struct node_vtable *vt = &vtables[i];
if (vt->refcnt) {
info("De-initializing '%s' node type", vt->name);
vt->deinit();
}
}
return 0;
}
struct node * node_lookup_name(const char *str, struct list *nodes)
{
FOREACH(nodes, it) {
if (!strcmp(str, it->node->name))
return it->node;
2014-12-05 12:39:52 +01:00
}
2014-12-05 12:39:52 +01:00
return NULL;
}
struct node_vtable * node_lookup_vtable(const char *str)
{
2014-12-05 12:39:52 +01:00
for (int i = 0; i < ARRAY_LEN(vtables); i++) {
if (!strcmp(vtables[i].name, str))
return &vtables[i];
}
2014-12-05 12:39:52 +01:00
return NULL;
}
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 str[256];
node_print(n, str, sizeof(str));
2015-08-07 01:11:43 +02:00
2014-12-05 12:39:52 +01:00
debug(1, "Starting node '%s' of type '%s' (%s)", n->name, n->vt->name, str);
{ INDENT
return n->vt->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;
info("Stopping node '%s'", n->name);
2015-08-07 01:11:43 +02:00
2014-12-05 12:39:52 +01:00
{ INDENT
ret = n->vt->close(n);
}
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 (n->vt->type) {
#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
}
struct node * node_create()
{
return alloc(sizeof(struct node));
}
void node_destroy(struct node *n)
{
switch (n->vt->type) {
#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);
}