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/src/node.c

101 lines
2.2 KiB
C
Raw Normal View History

/**
* Nodes
*
* The S2SS server connects multiple nodes.
* There are multiple types of nodes:
* - simulators
* - servers
* - workstations
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014, Institute for Automation of Complex Power Systems, EONERC
*/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "cfg.h"
#include "utils.h"
#include "msg.h"
#include "node.h"
extern struct config config;
int node_create(struct node *n, const char *name, enum node_type type,
struct sockaddr_in local, struct sockaddr_in remote)
{
int ret;
n->name = name;
n->type = type;
n->local = local;
n->remote = remote;
return 0;
}
int node_connect(struct node *n)
{
/* Create socket */
n->sd = socket(AF_INET, SOCK_DGRAM, 0);
if (n->sd < 0)
error("Failed to create socket: %s", strerror(errno));
/* Set socket options */
int prio = SOCKET_PRIO;
if (setsockopt(n->sd, SOL_SOCKET, SOCKET_PRIO, &prio, sizeof(prio)))
perror("Failed to set socket options");
/* Bind socket for receiving */
if (bind(n->sd, (struct sockaddr *) &n->local, sizeof(struct sockaddr_in)))
error("Failed to bind socket: %s", strerror(errno));
debug(1, " We listen for node %s at %s:%u", n->name, inet_ntoa(n->local.sin_addr), ntohs(n->local.sin_port));
/* Connect socket for sending */
/*if (connect(n->sd, (struct sockaddr *) &n->remote, sizeof(struct sockaddr_in)))
error("Failed to connect socket: %s", strerror(errno));*/
debug(1, " We sent to node %s at %s:%u", n->name, inet_ntoa(n->remote.sin_addr), ntohs(n->remote.sin_port));
return 0;
}
void node_destroy(struct node* n)
{
assert(n);
close(n->sd);
}
enum node_type node_lookup_type(const char *str)
{
if (!strcmp(str, "workstation"))
return NODE_WORKSTATION;
else if (!strcmp(str, "server"))
return NODE_SERVER;
else if (!strcmp(str, "rtds"))
return NODE_SIM_RTDS;
else if (!strcmp(str, "opal"))
return NODE_SIM_OPAL;
else if (!strcmp(str, "dsp"))
return NODE_SIM_DSP;
else
return NODE_INVALID;
}
struct node* node_lookup_name(const char *str, struct node *nodes, int len)
{
for (int i = 0; i < len; i++) {
if (!strcmp(str, nodes[i].name)) {
return &nodes[i];
}
}
return NULL;
}