2015-03-31 13:28:11 +02:00
|
|
|
/** Node type: File
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
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
|
|
|
|
2016-06-14 01:17:58 +02:00
|
|
|
#include "nodes/file.h"
|
2015-03-31 13:28:11 +02:00
|
|
|
#include "utils.h"
|
2015-06-02 21:59:31 +02:00
|
|
|
#include "timing.h"
|
2016-06-08 22:39:43 +02:00
|
|
|
#include "queue.h"
|
2017-02-12 14:35:05 -03:00
|
|
|
#include "plugin.h"
|
2015-05-06 11:49:13 +02:00
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
int file_reverse(struct node *n)
|
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
struct file *f = n->_vd;
|
2016-06-08 22:39:43 +02:00
|
|
|
struct file_direction tmp;
|
|
|
|
|
|
|
|
tmp = f->read;
|
|
|
|
f->read = f->write;
|
|
|
|
f->write = tmp;
|
2015-11-23 16:42:43 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
static char * file_format_name(const char *format, struct timespec *ts)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
char *buf = alloc(FILE_MAX_PATHLEN);
|
|
|
|
|
|
|
|
/* Convert time */
|
|
|
|
gmtime_r(&ts->tv_sec, &tm);
|
|
|
|
|
|
|
|
strftime(buf, FILE_MAX_PATHLEN, format, &tm);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILE * file_reopen(struct file_direction *dir)
|
|
|
|
{
|
|
|
|
char buf[FILE_MAX_PATHLEN];
|
|
|
|
const char *path = buf;
|
|
|
|
|
|
|
|
/* Append chunk number to filename */
|
|
|
|
if (dir->chunk >= 0)
|
|
|
|
snprintf(buf, FILE_MAX_PATHLEN, "%s_%03u", dir->path, dir->chunk);
|
|
|
|
else
|
|
|
|
path = dir->path;
|
|
|
|
|
|
|
|
if (dir->handle)
|
|
|
|
fclose(dir->handle);
|
|
|
|
|
|
|
|
return fopen(path, dir->mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_parse_direction(config_setting_t *cfg, struct file *f, int d)
|
|
|
|
{
|
|
|
|
struct file_direction *dir = (d == FILE_READ) ? &f->read : &f->write;
|
|
|
|
|
|
|
|
if (!config_setting_lookup_string(cfg, "path", &dir->fmt))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!config_setting_lookup_string(cfg, "mode", &dir->mode))
|
|
|
|
dir->mode = (d == FILE_READ) ? "r" : "w+";
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
int file_parse(struct node *n, config_setting_t *cfg)
|
2015-10-14 12:11:33 +02:00
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
struct file *f = n->_vd;
|
2015-10-14 12:11:33 +02:00
|
|
|
|
|
|
|
config_setting_t *cfg_in, *cfg_out;
|
|
|
|
|
|
|
|
cfg_out = config_setting_get_member(cfg, "out");
|
|
|
|
if (cfg_out) {
|
|
|
|
if (file_parse_direction(cfg_out, f, FILE_WRITE))
|
2015-11-29 22:45:46 +01:00
|
|
|
cerror(cfg_out, "Failed to parse output file for node %s", node_name(n));
|
2015-10-14 12:11:33 +02:00
|
|
|
|
|
|
|
/* More write specific settings */
|
2016-06-08 22:39:43 +02:00
|
|
|
if (config_setting_lookup_int(cfg_out, "split", &f->write.split))
|
|
|
|
f->write.split <<= 20; /* in MiB */
|
|
|
|
else
|
|
|
|
f->write.split = -1; /* Save all samples in a single file */
|
2015-10-14 12:11:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg_in = config_setting_get_member(cfg, "in");
|
|
|
|
if (cfg_in) {
|
|
|
|
if (file_parse_direction(cfg_in, f, FILE_READ))
|
2015-11-29 22:45:46 +01:00
|
|
|
cerror(cfg_in, "Failed to parse input file for node %s", node_name(n));
|
2015-10-14 12:11:33 +02:00
|
|
|
|
|
|
|
/* More read specific settings */
|
|
|
|
if (!config_setting_lookup_bool(cfg_in, "splitted", &f->read.split))
|
2016-06-08 22:39:43 +02:00
|
|
|
f->read.split = 0; /* Input files are suffixed with split indizes (.000, .001) */
|
2015-10-14 12:11:33 +02:00
|
|
|
if (!config_setting_lookup_float(cfg_in, "rate", &f->read_rate))
|
|
|
|
f->read_rate = 0; /* Disable fixed rate sending. Using timestamps of file instead */
|
|
|
|
|
|
|
|
double epoch_flt;
|
|
|
|
if (!config_setting_lookup_float(cfg_in, "epoch", &epoch_flt))
|
|
|
|
epoch_flt = 0;
|
|
|
|
|
|
|
|
f->read_epoch = time_from_double(epoch_flt);
|
|
|
|
|
|
|
|
const char *epoch_mode;
|
|
|
|
if (!config_setting_lookup_string(cfg_in, "epoch_mode", &epoch_mode))
|
|
|
|
epoch_mode = "direct";
|
|
|
|
|
|
|
|
if (!strcmp(epoch_mode, "direct"))
|
|
|
|
f->read_epoch_mode = EPOCH_DIRECT;
|
|
|
|
else if (!strcmp(epoch_mode, "wait"))
|
|
|
|
f->read_epoch_mode = EPOCH_WAIT;
|
|
|
|
else if (!strcmp(epoch_mode, "relative"))
|
|
|
|
f->read_epoch_mode = EPOCH_RELATIVE;
|
|
|
|
else if (!strcmp(epoch_mode, "absolute"))
|
|
|
|
f->read_epoch_mode = EPOCH_ABSOLUTE;
|
|
|
|
else
|
|
|
|
cerror(cfg_in, "Invalid value '%s' for setting 'epoch_mode'", epoch_mode);
|
|
|
|
}
|
|
|
|
|
2015-11-29 22:47:57 +01:00
|
|
|
n->_vd = f;
|
2015-10-14 12:11:33 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
char * file_print(struct node *n)
|
2015-03-31 13:28:11 +02:00
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
struct file *f = n->_vd;
|
2015-09-22 12:58:37 +02:00
|
|
|
char *buf = NULL;
|
2015-10-09 13:08:39 +02:00
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
if (f->read.fmt) {
|
|
|
|
const char *epoch_str = NULL;
|
|
|
|
switch (f->read_epoch_mode) {
|
|
|
|
case EPOCH_DIRECT: epoch_str = "direct"; break;
|
|
|
|
case EPOCH_WAIT: epoch_str = "wait"; break;
|
|
|
|
case EPOCH_RELATIVE: epoch_str = "relative"; break;
|
|
|
|
case EPOCH_ABSOLUTE: epoch_str = "absolute"; break;
|
2015-10-09 13:08:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
strcatf(&buf, "in=%s, epoch_mode=%s, epoch=%.2f, ",
|
2015-10-14 12:11:33 +02:00
|
|
|
f->read.path ? f->read.path : f->read.fmt,
|
|
|
|
epoch_str,
|
|
|
|
time_to_double(&f->read_epoch)
|
2015-10-09 13:08:39 +02:00
|
|
|
);
|
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
if (f->read_rate)
|
|
|
|
strcatf(&buf, "rate=%.1f, ", f->read_rate);
|
2015-10-09 13:08:39 +02:00
|
|
|
}
|
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
if (f->write.fmt) {
|
2015-10-09 13:08:39 +02:00
|
|
|
strcatf(&buf, "out=%s, mode=%s, ",
|
2015-10-14 12:11:33 +02:00
|
|
|
f->write.path ? f->write.path : f->write.fmt,
|
|
|
|
f->write.mode
|
2015-10-09 13:08:39 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
if (f->read_first.tv_sec || f->read_first.tv_nsec)
|
|
|
|
strcatf(&buf, "first=%.2f, ", time_to_double(&f->read_first));
|
2015-10-09 13:08:39 +02:00
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
if (f->read_offset.tv_sec || f->read_offset.tv_nsec)
|
|
|
|
strcatf(&buf, "offset=%.2f, ", time_to_double(&f->read_offset));
|
2015-10-09 13:08:39 +02:00
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
if ((f->read_first.tv_sec || f->read_first.tv_nsec) &&
|
|
|
|
(f->read_offset.tv_sec || f->read_offset.tv_nsec)) {
|
|
|
|
struct timespec eta, now = time_now();
|
2015-10-09 13:08:39 +02:00
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
eta = time_add(&f->read_first, &f->read_offset);
|
|
|
|
eta = time_diff(&now, &eta);
|
2015-10-09 13:08:39 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-10-09 13:08:39 +02:00
|
|
|
return buf;
|
2015-03-31 13:28:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int file_open(struct node *n)
|
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
struct file *f = n->_vd;
|
2015-10-14 12:11:33 +02:00
|
|
|
|
|
|
|
struct timespec now = time_now();
|
2015-03-31 13:28:11 +02:00
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
if (f->read.fmt) {
|
|
|
|
/* Prepare file name */
|
|
|
|
f->read.chunk = f->read.split ? 0 : -1;
|
|
|
|
f->read.path = file_format_name(f->read.fmt, &now);
|
|
|
|
|
2015-10-09 13:08:39 +02:00
|
|
|
/* Open file */
|
2015-10-14 12:11:33 +02:00
|
|
|
f->read.handle = file_reopen(&f->read);
|
|
|
|
if (!f->read.handle)
|
|
|
|
serror("Failed to open file for reading: '%s'", f->read.path);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-10-09 13:08:39 +02:00
|
|
|
/* Create timer */
|
2016-01-14 22:57:39 +01:00
|
|
|
f->read_timer = (f->read_rate)
|
|
|
|
? timerfd_create_rate(f->read_rate)
|
|
|
|
: timerfd_create(CLOCK_REALTIME, 0);
|
2015-10-14 12:11:33 +02:00
|
|
|
if (f->read_timer < 0)
|
2015-03-31 13:28:11 +02:00
|
|
|
serror("Failed to create timer");
|
2015-10-09 13:08:39 +02:00
|
|
|
|
|
|
|
/* Get current time */
|
2015-10-11 14:50:08 +02:00
|
|
|
struct timespec now = time_now();
|
2015-10-09 13:08:39 +02:00
|
|
|
|
|
|
|
/* Get timestamp of first line */
|
2016-06-08 22:39:43 +02:00
|
|
|
struct sample s;
|
|
|
|
int ret = sample_fscan(f->read.handle, &s, NULL); rewind(f->read.handle);
|
2015-10-09 13:08:39 +02:00
|
|
|
if (ret < 0)
|
2015-11-29 22:51:04 +01:00
|
|
|
error("Failed to read first timestamp of node %s", node_name(n));
|
2015-10-09 13:08:39 +02:00
|
|
|
|
2016-06-08 22:39:43 +02:00
|
|
|
f->read_first = s.ts.origin;
|
2015-10-09 13:08:39 +02:00
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
/* Set read_offset depending on epoch_mode */
|
|
|
|
switch (f->read_epoch_mode) {
|
2015-10-09 13:08:39 +02:00
|
|
|
case EPOCH_DIRECT: /* read first value at now + epoch */
|
2015-10-14 12:11:33 +02:00
|
|
|
f->read_offset = time_diff(&f->read_first, &now);
|
|
|
|
f->read_offset = time_add(&f->read_offset, &f->read_epoch);
|
2015-10-09 13:08:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EPOCH_WAIT: /* read first value at now + first + epoch */
|
2015-10-14 12:11:33 +02:00
|
|
|
f->read_offset = now;
|
|
|
|
f->read_offset = time_add(&f->read_offset, &f->read_epoch);
|
2015-10-09 13:08:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EPOCH_RELATIVE: /* read first value at first + epoch */
|
2015-10-14 12:11:33 +02:00
|
|
|
f->read_offset = f->read_epoch;
|
2015-10-09 13:08:39 +02:00
|
|
|
break;
|
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
case EPOCH_ABSOLUTE: /* read first value at f->read_epoch */
|
|
|
|
f->read_offset = time_diff(&f->read_first, &f->read_epoch);
|
2015-10-09 13:08:39 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-03-31 13:28:11 +02:00
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
if (f->write.fmt) {
|
|
|
|
/* Prepare file name */
|
|
|
|
f->write.chunk = f->write.split ? 0 : -1;
|
|
|
|
f->write.path = file_format_name(f->write.fmt, &now);
|
|
|
|
|
|
|
|
/* Open file */
|
|
|
|
f->write.handle = file_reopen(&f->write);
|
|
|
|
if (!f->write.handle)
|
|
|
|
serror("Failed to open file for writing: '%s'", f->write.path);
|
2015-03-31 13:28:11 +02:00
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-31 13:28:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int file_close(struct node *n)
|
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
struct file *f = n->_vd;
|
2015-10-14 12:11:33 +02:00
|
|
|
|
|
|
|
free(f->read.path);
|
|
|
|
free(f->write.path);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
if (f->read_timer)
|
|
|
|
close(f->read_timer);
|
|
|
|
if (f->read.handle)
|
|
|
|
fclose(f->read.handle);
|
|
|
|
if (f->write.handle)
|
|
|
|
fclose(f->write.handle);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-31 13:28:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-08 22:39:43 +02:00
|
|
|
int file_read(struct node *n, struct sample *smps[], unsigned cnt)
|
2015-03-31 13:28:11 +02:00
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
struct file *f = n->_vd;
|
2016-06-08 22:39:43 +02:00
|
|
|
struct sample *s = smps[0];
|
|
|
|
int values, flags;
|
|
|
|
|
|
|
|
assert(f->read.handle);
|
|
|
|
assert(cnt == 1);
|
|
|
|
|
|
|
|
retry: values = sample_fscan(f->read.handle, s, &flags); /* Get message and timestamp */
|
|
|
|
if (values < 0) {
|
|
|
|
if (feof(f->read.handle)) {
|
|
|
|
if (f->read.split) {
|
|
|
|
f->read.chunk++;
|
|
|
|
f->read.handle = file_reopen(&f->read);
|
|
|
|
if (!f->read.handle)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
info("Open new input chunk of node %s: %d", node_name(n), f->read.chunk);
|
2015-08-09 23:52:44 +02:00
|
|
|
}
|
2016-06-08 22:39:43 +02:00
|
|
|
else {
|
|
|
|
info("Rewind input file of node %s", node_name(n));
|
|
|
|
rewind(f->read.handle);
|
|
|
|
goto retry;
|
2015-10-09 13:08:39 +02:00
|
|
|
}
|
2015-08-09 23:52:44 +02:00
|
|
|
}
|
2016-06-08 22:39:43 +02:00
|
|
|
else
|
|
|
|
warn("Failed to read messages from node %s: reason=%d", node_name(n), values);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!f->read_rate || ftell(f->read.handle) == 0) {
|
|
|
|
s->ts.origin = time_add(&s->ts.origin, &f->read_offset);
|
|
|
|
if (timerfd_wait_until(f->read_timer, &s->ts.origin) == 0)
|
|
|
|
serror("Failed to wait for timer");
|
|
|
|
}
|
|
|
|
else { /* Wait with fixed rate delay */
|
|
|
|
if (timerfd_wait(f->read_timer) == 0)
|
|
|
|
serror("Failed to wait for timer");
|
|
|
|
|
|
|
|
/* Update timestamp */
|
|
|
|
s->ts.origin = time_now();
|
2015-05-06 11:49:13 +02:00
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-06-08 22:39:43 +02:00
|
|
|
return 1;
|
2015-03-31 13:28:11 +02:00
|
|
|
}
|
|
|
|
|
2016-06-08 22:39:43 +02:00
|
|
|
int file_write(struct node *n, struct sample *smps[], unsigned cnt)
|
2015-03-31 13:28:11 +02:00
|
|
|
{
|
2015-11-29 22:47:57 +01:00
|
|
|
struct file *f = n->_vd;
|
2016-06-08 22:39:43 +02:00
|
|
|
struct sample *s = smps[0];
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-06-08 22:39:43 +02:00
|
|
|
assert(f->write.handle);
|
|
|
|
assert(cnt == 1);
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2016-06-08 22:39:43 +02:00
|
|
|
/* Split file if requested */
|
|
|
|
if (f->write.split > 0 && ftell(f->write.handle) > f->write.split) {
|
|
|
|
f->write.chunk++;
|
|
|
|
f->write.handle = file_reopen(&f->write);
|
2015-10-14 12:11:33 +02:00
|
|
|
|
2016-06-08 22:39:43 +02:00
|
|
|
info("Splitted output node %s: chunk=%u", node_name(n), f->write.chunk);
|
2015-05-06 11:49:13 +02:00
|
|
|
}
|
2016-06-08 22:39:43 +02:00
|
|
|
|
|
|
|
sample_fprint(f->write.handle, s, SAMPLE_ALL & ~SAMPLE_OFFSET);
|
|
|
|
fflush(f->write.handle);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-06-08 22:39:43 +02:00
|
|
|
return 1;
|
2015-08-07 01:11:43 +02:00
|
|
|
}
|
2015-09-19 15:26:30 +02:00
|
|
|
|
2017-02-12 14:35:05 -03:00
|
|
|
static struct plugin p = {
|
2015-11-23 16:44:01 +01:00
|
|
|
.name = "file",
|
|
|
|
.description = "support for file log / replay node type",
|
2017-02-12 14:35:05 -03:00
|
|
|
.type = PLUGIN_TYPE_NODE,
|
|
|
|
.node = {
|
|
|
|
.vectorize = 1,
|
|
|
|
.size = sizeof(struct file),
|
|
|
|
.reverse = file_reverse,
|
|
|
|
.parse = file_parse,
|
|
|
|
.print = file_print,
|
|
|
|
.open = file_open,
|
|
|
|
.close = file_close,
|
|
|
|
.read = file_read,
|
2017-03-07 07:12:24 -04:00
|
|
|
.write = file_write,
|
2017-02-12 14:35:05 -03:00
|
|
|
}
|
2015-11-23 16:44:01 +01:00
|
|
|
};
|
|
|
|
|
2017-02-12 14:35:05 -03:00
|
|
|
REGISTER_PLUGIN(&p)
|