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/file.c

280 lines
6.7 KiB
C
Raw Normal View History

2015-03-31 13:28:11 +02:00
/** Node type: File
*
* This file implements the file type for 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
*********************************************************************************/
2015-03-31 13:28:11 +02:00
#include <unistd.h>
2015-08-09 23:52:44 +02:00
#include <string.h>
2015-03-31 13:28:11 +02:00
2015-08-09 23:52:44 +02:00
#include "msg.h"
2015-03-31 13:28:11 +02:00
#include "file.h"
#include "utils.h"
#include "timing.h"
int file_init(int argc, char *argv[], struct settings *set)
{ INDENT
return 0; /* nothing todo here */
}
int file_deinit()
{ INDENT
return 0; /* nothing todo here */
}
char * file_print(struct node *n)
2015-03-31 13:28:11 +02:00
{
struct file *f = n->file;
char *buf = NULL;
if (f->path_in) {
const char *epoch_mode_str = NULL;
switch (f->epoch_mode) {
case EPOCH_DIRECT: epoch_mode_str = "direct"; break;
case EPOCH_WAIT: epoch_mode_str = "wait"; break;
case EPOCH_RELATIVE: epoch_mode_str = "relative"; break;
case EPOCH_ABSOLUTE: epoch_mode_str = "absolute"; break;
}
strcatf(&buf, "in=%s, epoch_mode=%s, epoch=%.2f, ",
f->path_in,
epoch_mode_str,
time_to_double(&f->epoch)
);
if (f->rate)
strcatf(&buf, "rate=%.1f, ", f->rate);
}
if (f->path_out) {
strcatf(&buf, "out=%s, mode=%s, ",
f->path_out,
f->file_mode
);
}
if (f->first.tv_sec || f->first.tv_nsec)
strcatf(&buf, "first=%.2f, ", time_to_double(&f->first));
if (f->offset.tv_sec || f->offset.tv_nsec)
strcatf(&buf, "offset=%.2f, ", time_to_double(&f->offset));
if ((f->first.tv_sec || f->first.tv_nsec) &&
(f->offset.tv_sec || f->offset.tv_nsec)) {
struct timespec eta, now;
clock_gettime(CLOCK_REALTIME, &now);
eta = time_add(&f->first, &f->offset);
eta = time_diff(&now, &eta);
if (eta.tv_sec || eta.tv_nsec)
strcatf(&buf, "eta=%.2f sec, ", time_to_double(&eta));
}
if (strlen(buf) > 2)
buf[strlen(buf) - 2] = 0;
2015-08-07 01:11:43 +02:00
return buf;
2015-03-31 13:28:11 +02:00
}
int file_parse(config_setting_t *cfg, struct node *n)
{
struct file *f = alloc(sizeof(struct file));
2015-08-07 01:11:43 +02:00
2015-08-09 23:52:44 +02:00
const char *out, *in;
const char *epoch_mode;
double epoch_flt;
if (config_setting_lookup_string(cfg, "out", &out)) {
time_t t = time(NULL);
struct tm *tm = localtime(&t);
2015-08-07 01:11:43 +02:00
f->path_out = alloc(FILE_MAX_PATHLEN);
if (strftime(f->path_out, FILE_MAX_PATHLEN, out, tm) == 0)
cerror(cfg, "Invalid path for output");
2015-08-07 01:11:43 +02:00
}
2015-08-09 23:52:44 +02:00
if (config_setting_lookup_string(cfg, "in", &in))
f->path_in = strdup(in);
2015-08-07 01:11:43 +02:00
2015-08-21 11:35:07 +02:00
if (!config_setting_lookup_string(cfg, "file_mode", &f->file_mode))
2015-08-09 23:52:44 +02:00
f->file_mode = "w+";
2015-08-07 01:11:43 +02:00
2015-08-21 11:35:07 +02:00
if (!config_setting_lookup_float(cfg, "send_rate", &f->rate))
2015-08-09 23:52:44 +02:00
f->rate = 0; /* Disable fixed rate sending. Using timestamps of file instead */
if (config_setting_lookup_float(n->cfg, "epoch", &epoch_flt))
f->epoch = time_from_double(epoch_flt);
if (!config_setting_lookup_string(n->cfg, "epoch_mode", &epoch_mode))
epoch_mode = "direct";
2015-08-09 23:52:44 +02:00
if (!strcmp(epoch_mode, "direct"))
f->epoch_mode = EPOCH_DIRECT;
else if (!strcmp(epoch_mode, "wait"))
f->epoch_mode = EPOCH_WAIT;
2015-08-09 23:52:44 +02:00
else if (!strcmp(epoch_mode, "relative"))
f->epoch_mode = EPOCH_RELATIVE;
else if (!strcmp(epoch_mode, "absolute"))
f->epoch_mode = EPOCH_ABSOLUTE;
else
cerror(n->cfg, "Invalid value '%s' for setting 'epoch_mode'", epoch_mode);
2015-08-07 01:11:43 +02:00
2015-03-31 13:28:11 +02:00
n->file = f;
2015-08-07 01:11:43 +02:00
2015-03-31 13:28:11 +02:00
return 0;
}
int file_open(struct node *n)
{
struct file *f = n->file;
if (f->path_in) {
/* Open file */
f->in = fopen(f->path_in, "r");
2015-03-31 13:28:11 +02:00
if (!f->in)
serror("Failed to open file for reading: '%s'", f->path_in);
2015-08-07 01:11:43 +02:00
/* Create timer */
2015-08-09 23:52:44 +02:00
f->tfd = timerfd_create(CLOCK_REALTIME, 0);
2015-03-31 13:28:11 +02:00
if (f->tfd < 0)
serror("Failed to create timer");
/* Get current time */
struct timespec now, eta;
clock_gettime(CLOCK_REALTIME, &now);
/* Get timestamp of first line */
struct msg m;
int ret = msg_fscan(f->in, &m, NULL, NULL); rewind(f->in);
if (ret < 0)
error("Failed to read first timestamp of node '%s'", n->name);
f->first = MSG_TS(&m);
/* Set offset depending on epoch_mode */
switch (f->epoch_mode) {
case EPOCH_DIRECT: /* read first value at now + epoch */
f->offset = time_diff(&f->first, &now);
f->offset = time_add(&f->offset, &f->epoch);
break;
case EPOCH_WAIT: /* read first value at now + first + epoch */
f->offset = now;
f->offset = time_add(&f->offset, &f->epoch);
break;
case EPOCH_RELATIVE: /* read first value at first + epoch */
f->offset = f->epoch;
break;
case EPOCH_ABSOLUTE: /* read first value at f->epoch */
f->offset = time_diff(&f->first, &f->epoch);
break;
}
/* Arm the timer with a fixed rate */
if (f->rate) {
2015-08-09 23:52:44 +02:00
struct itimerspec its = {
.it_interval = time_from_double(1 / f->rate),
.it_value = { 1, 0 },
};
int ret = timerfd_settime(f->tfd, 0, &its, NULL);
if (ret)
serror("Failed to start timer");
}
char *buf = file_print(n);
debug(4, "Opened file node '%s': %s", n->name, buf);
free(buf);
2015-03-31 13:28:11 +02:00
}
2015-08-07 01:11:43 +02:00
2015-03-31 13:28:11 +02:00
if (f->path_out) {
/* Open output file */
2015-08-09 23:52:44 +02:00
f->out = fopen(f->path_out, f->file_mode);
2015-03-31 13:28:11 +02:00
if (!f->out)
serror("Failed to open file for writing: '%s'", f->path_out);
}
2015-08-07 01:11:43 +02:00
2015-03-31 13:28:11 +02:00
return 0;
}
int file_close(struct node *n)
{
struct file *f = n->file;
2015-08-07 01:11:43 +02:00
2015-03-31 13:28:11 +02:00
if (f->tfd)
close(f->tfd);
if (f->in)
fclose(f->in);
if (f->out)
fclose(f->out);
2015-08-07 01:11:43 +02:00
2015-03-31 13:28:11 +02:00
return 0;
}
int file_read(struct node *n, struct msg *pool, int poolsize, int first, int cnt)
2015-03-31 13:28:11 +02:00
{
int values, flags, i = 0;
2015-03-31 13:28:11 +02:00
struct file *f = n->file;
2015-08-07 01:11:43 +02:00
if (f->in) {
2015-08-09 23:52:44 +02:00
for (i = 0; i < cnt; i++) {
struct msg *cur = &pool[(first+i) % poolsize];
2015-08-21 11:35:07 +02:00
/* Get message and timestamp */
values = msg_fscan(f->in, cur, &flags, NULL);
if (values < 0) {
if (!feof(f->in))
warn("Failed to parse file of node '%s': reason=%d", n->name, values);
return 0;
2015-08-09 23:52:44 +02:00
}
2015-08-21 11:35:07 +02:00
/* Fix missing sequence no */
cur->sequence = f->sequence = (flags & MSG_PRINT_SEQUENCE) ? cur->sequence : f->sequence + 1;
if (!f->rate || ftell(f->in) == 0) {
struct timespec until = time_add(&MSG_TS(cur), &f->offset);
2015-08-21 11:35:07 +02:00
2015-08-09 23:52:44 +02:00
if (timerfd_wait_until(f->tfd, &until) < 0)
serror("Failed to wait for timer");
}
else { /* Wait with fixed rate delay */
if (timerfd_wait(f->tfd) < 0)
serror("Failed to wait for timer");
}
2015-08-09 23:52:44 +02:00
}
}
else
2015-08-09 23:52:44 +02:00
error("Can not read from node '%s'", n->name);
2015-08-07 01:11:43 +02:00
return i;
2015-03-31 13:28:11 +02:00
}
int file_write(struct node *n, struct msg *pool, int poolsize, int first, int cnt)
2015-03-31 13:28:11 +02:00
{
int i = 0;
2015-03-31 13:28:11 +02:00
struct file *f = n->file;
2015-08-07 01:11:43 +02:00
if (f->out) {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
for (i = 0; i < cnt; i++) {
struct msg *m = &pool[(first+i) % poolsize];
msg_fprint(f->out, m, MSG_PRINT_ALL & ~MSG_PRINT_OFFSET, 0);
}
}
else
2015-08-09 23:52:44 +02:00
error("Can not write to node '%s", n->name);
2015-08-07 01:11:43 +02:00
return i;
2015-08-07 01:11:43 +02:00
}
REGISTER_NODE_TYPE(LOG_FILE, "file", file)