2025-01-14 14:42:39 +00:00
|
|
|
/* Node type: File.
|
2015-03-31 13:28:11 +02:00
|
|
|
*
|
2025-01-14 14:42:39 +00:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2015-03-31 13:28:11 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <cerrno>
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2018-10-20 15:13:29 +02:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <sys/stat.h>
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <unistd.h>
|
2015-03-31 13:28:11 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <villas/exceptions.hpp>
|
|
|
|
#include <villas/format.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node_compat.hpp>
|
2019-04-23 00:12:31 +02:00
|
|
|
#include <villas/nodes/file.hpp>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/queue.h>
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <villas/timing.hpp>
|
|
|
|
#include <villas/utils.hpp>
|
2015-11-23 16:42:43 +01:00
|
|
|
|
2020-07-04 16:22:10 +02:00
|
|
|
using namespace villas;
|
2021-05-10 00:12:30 +02:00
|
|
|
using namespace villas::node;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
static char *file_format_name(const char *format, struct timespec *ts) {
|
|
|
|
struct tm tm;
|
|
|
|
char *buf = new char[FILE_MAX_PATHLEN];
|
|
|
|
if (!buf)
|
|
|
|
throw MemoryAllocationError();
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Convert time
|
|
|
|
gmtime_r(&ts->tv_sec, &tm);
|
2015-10-14 12:11:33 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
strftime(buf, FILE_MAX_PATHLEN, format, &tm);
|
2015-10-14 12:11:33 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return buf;
|
2015-10-14 12:11:33 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
static struct timespec file_calc_offset(const struct timespec *first,
|
|
|
|
const struct timespec *epoch,
|
|
|
|
enum file::EpochMode mode) {
|
|
|
|
// Get current time
|
|
|
|
struct timespec now = time_now();
|
|
|
|
struct timespec offset;
|
2017-05-08 00:46:48 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Set offset depending on epoch
|
|
|
|
switch (mode) {
|
|
|
|
case file::EpochMode::DIRECT: // read first value at now + epoch
|
|
|
|
offset = time_diff(first, &now);
|
|
|
|
return time_add(&offset, epoch);
|
2017-05-08 00:46:48 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case file::EpochMode::WAIT: // read first value at now + first + epoch
|
|
|
|
offset = now;
|
|
|
|
return time_add(&now, epoch);
|
2017-05-08 00:46:48 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case file::EpochMode::RELATIVE: // read first value at first + epoch
|
|
|
|
return *epoch;
|
2017-05-08 00:46:48 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case file::EpochMode::ABSOLUTE: // read first value at f->epoch
|
|
|
|
return time_diff(first, epoch);
|
2017-05-08 00:46:48 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
default:
|
|
|
|
return (struct timespec){0};
|
|
|
|
}
|
2017-05-08 00:46:48 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int villas::node::file_parse(NodeCompat *n, json_t *json) {
|
|
|
|
auto *f = n->getData<struct file>();
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
|
|
|
json_t *json_format = nullptr;
|
|
|
|
|
|
|
|
const char *uri_tmpl = nullptr;
|
|
|
|
const char *eof = nullptr;
|
|
|
|
const char *epoch = nullptr;
|
|
|
|
double epoch_flt = 0;
|
|
|
|
|
|
|
|
ret = json_unpack_ex(json, &err, 0,
|
|
|
|
"{ s: s, s?: o, s?: { s?: s, s?: F, s?: s, s?: F, s?: "
|
|
|
|
"i, s?: i }, s?: { s?: b, s?: i } }",
|
|
|
|
"uri", &uri_tmpl, "format", &json_format, "in", "eof",
|
|
|
|
&eof, "rate", &f->rate, "epoch_mode", &epoch, "epoch",
|
|
|
|
&epoch_flt, "buffer_size", &f->buffer_size_in, "skip",
|
|
|
|
&f->skip_lines, "out", "flush", &f->flush, "buffer_size",
|
|
|
|
&f->buffer_size_out);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(json, err, "node-config-node-file");
|
|
|
|
|
|
|
|
f->epoch = time_from_double(epoch_flt);
|
|
|
|
f->uri_tmpl = uri_tmpl ? strdup(uri_tmpl) : nullptr;
|
|
|
|
|
|
|
|
// Format
|
|
|
|
if (f->formatter)
|
|
|
|
delete f->formatter;
|
|
|
|
f->formatter = json_format ? FormatFactory::make(json_format)
|
|
|
|
: FormatFactory::make("villas.human");
|
|
|
|
if (!f->formatter)
|
|
|
|
throw ConfigError(json_format, "node-config-node-file-format",
|
|
|
|
"Invalid format configuration");
|
|
|
|
|
|
|
|
if (eof) {
|
|
|
|
if (!strcmp(eof, "exit") || !strcmp(eof, "stop"))
|
|
|
|
f->eof_mode = file::EOFBehaviour::STOP;
|
|
|
|
else if (!strcmp(eof, "rewind"))
|
|
|
|
f->eof_mode = file::EOFBehaviour::REWIND;
|
|
|
|
else if (!strcmp(eof, "wait"))
|
|
|
|
f->eof_mode = file::EOFBehaviour::SUSPEND;
|
|
|
|
else
|
|
|
|
throw RuntimeError("Invalid mode '{}' for 'eof' setting", eof);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (epoch) {
|
|
|
|
if (!strcmp(epoch, "direct"))
|
|
|
|
f->epoch_mode = file::EpochMode::DIRECT;
|
|
|
|
else if (!strcmp(epoch, "wait"))
|
|
|
|
f->epoch_mode = file::EpochMode::WAIT;
|
|
|
|
else if (!strcmp(epoch, "relative"))
|
|
|
|
f->epoch_mode = file::EpochMode::RELATIVE;
|
|
|
|
else if (!strcmp(epoch, "absolute"))
|
|
|
|
f->epoch_mode = file::EpochMode::ABSOLUTE;
|
|
|
|
else if (!strcmp(epoch, "original"))
|
|
|
|
f->epoch_mode = file::EpochMode::ORIGINAL;
|
|
|
|
else
|
|
|
|
throw RuntimeError("Invalid value '{}' for setting 'epoch'", epoch);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-10-14 12:11:33 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
char *villas::node::file_print(NodeCompat *n) {
|
|
|
|
auto *f = n->getData<struct file>();
|
|
|
|
char *buf = nullptr;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
const char *epoch_str = nullptr;
|
|
|
|
const char *eof_str = nullptr;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
switch (f->epoch_mode) {
|
|
|
|
case file::EpochMode::DIRECT:
|
|
|
|
epoch_str = "direct";
|
|
|
|
break;
|
2019-04-22 23:43:46 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case file::EpochMode::WAIT:
|
|
|
|
epoch_str = "wait";
|
|
|
|
break;
|
2019-04-22 23:43:46 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case file::EpochMode::RELATIVE:
|
|
|
|
epoch_str = "relative";
|
|
|
|
break;
|
2019-04-22 23:43:46 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case file::EpochMode::ABSOLUTE:
|
|
|
|
epoch_str = "absolute";
|
|
|
|
break;
|
2019-04-22 23:43:46 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case file::EpochMode::ORIGINAL:
|
|
|
|
epoch_str = "original";
|
|
|
|
break;
|
2019-04-22 23:43:46 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
default:
|
|
|
|
epoch_str = "";
|
|
|
|
break;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
switch (f->eof_mode) {
|
|
|
|
case file::EOFBehaviour::STOP:
|
|
|
|
eof_str = "stop";
|
|
|
|
break;
|
2019-04-22 23:43:46 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case file::EOFBehaviour::SUSPEND:
|
|
|
|
eof_str = "wait";
|
|
|
|
break;
|
2019-04-22 23:43:46 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case file::EOFBehaviour::REWIND:
|
|
|
|
eof_str = "rewind";
|
|
|
|
break;
|
2019-04-22 23:43:46 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
default:
|
|
|
|
eof_str = "";
|
|
|
|
break;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
strcatf(
|
|
|
|
&buf,
|
|
|
|
"uri=%s, out.flush=%s, in.skip=%d, in.eof=%s, in.epoch=%s, in.epoch=%.2f",
|
|
|
|
f->uri ? f->uri : f->uri_tmpl, f->flush ? "yes" : "no", f->skip_lines,
|
|
|
|
eof_str, epoch_str, time_to_double(&f->epoch));
|
2017-08-14 14:42:07 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (f->rate)
|
|
|
|
strcatf(&buf, ", in.rate=%.1f", f->rate);
|
2017-08-14 14:42:07 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (f->first.tv_sec || f->first.tv_nsec)
|
|
|
|
strcatf(&buf, ", first=%.2f", time_to_double(&f->first));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (f->offset.tv_sec || f->offset.tv_nsec)
|
|
|
|
strcatf(&buf, ", offset=%.2f", time_to_double(&f->offset));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if ((f->first.tv_sec || f->first.tv_nsec) &&
|
|
|
|
(f->offset.tv_sec || f->offset.tv_nsec)) {
|
|
|
|
struct timespec eta, now = time_now();
|
2015-10-09 13:08:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
eta = time_add(&f->first, &f->offset);
|
|
|
|
eta = time_diff(&now, &eta);
|
2015-10-09 13:08:39 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (eta.tv_sec || eta.tv_nsec)
|
|
|
|
strcatf(&buf, ", eta=%.2f sec", time_to_double(&eta));
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return buf;
|
2015-03-31 13:28:11 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int villas::node::file_start(NodeCompat *n) {
|
|
|
|
auto *f = n->getData<struct file>();
|
|
|
|
|
|
|
|
struct timespec now = time_now();
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
// Prepare file name
|
|
|
|
if (f->uri)
|
|
|
|
delete[] f->uri;
|
|
|
|
|
|
|
|
f->uri = file_format_name(f->uri_tmpl, &now);
|
|
|
|
|
|
|
|
// Check if directory exists
|
|
|
|
struct stat sb;
|
|
|
|
char *cpy = strdup(f->uri);
|
|
|
|
char *dir = dirname(cpy);
|
|
|
|
|
|
|
|
ret = stat(dir, &sb);
|
|
|
|
if (ret) {
|
|
|
|
if (errno == ENOENT || errno == ENOTDIR) {
|
|
|
|
ret = mkdir(dir, 0755);
|
|
|
|
if (ret)
|
|
|
|
throw SystemError("Failed to create directory");
|
|
|
|
} else if (errno != EISDIR)
|
|
|
|
throw SystemError("Failed to stat");
|
|
|
|
} else if (!S_ISDIR(sb.st_mode)) {
|
|
|
|
ret = mkdir(dir, 0644);
|
|
|
|
if (ret)
|
|
|
|
throw SystemError("Failed to create directory");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(cpy);
|
|
|
|
|
|
|
|
f->formatter->start(n->getInputSignals(false));
|
|
|
|
|
|
|
|
// Open file
|
|
|
|
f->stream_out = fopen(f->uri, "a+");
|
|
|
|
if (!f->stream_out)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
f->stream_in = fopen(f->uri, "r");
|
|
|
|
if (!f->stream_in)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (f->buffer_size_in) {
|
|
|
|
ret = setvbuf(f->stream_in, nullptr, _IOFBF, f->buffer_size_in);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f->buffer_size_out) {
|
|
|
|
ret = setvbuf(f->stream_out, nullptr, _IOFBF, f->buffer_size_out);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create timer
|
|
|
|
f->task.setRate(f->rate);
|
|
|
|
|
|
|
|
// Get timestamp of first line
|
|
|
|
if (f->epoch_mode != file::EpochMode::ORIGINAL) {
|
|
|
|
rewind(f->stream_in);
|
|
|
|
|
|
|
|
if (feof(f->stream_in)) {
|
|
|
|
n->logger->warn("Empty file");
|
|
|
|
} else {
|
|
|
|
struct Sample smp;
|
|
|
|
|
|
|
|
smp.capacity = 0;
|
|
|
|
|
|
|
|
ret = f->formatter->scan(f->stream_in, &smp);
|
|
|
|
if (ret == 1) {
|
|
|
|
f->first = smp.ts.origin;
|
|
|
|
f->offset = file_calc_offset(&f->first, &f->epoch, f->epoch_mode);
|
|
|
|
} else
|
|
|
|
n->logger->warn("Failed to read first timestamp");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rewind(f->stream_in);
|
|
|
|
|
|
|
|
// Fast-forward
|
|
|
|
struct Sample *smp = sample_alloc_mem(n->getInputSignals(false)->size());
|
|
|
|
for (unsigned i = 0; i < f->skip_lines; i++)
|
|
|
|
f->formatter->scan(f->stream_in, smp);
|
|
|
|
|
|
|
|
sample_free(smp);
|
|
|
|
|
|
|
|
return 0;
|
2015-03-31 13:28:11 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int villas::node::file_stop(NodeCompat *n) {
|
|
|
|
auto *f = n->getData<struct file>();
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
f->task.stop();
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
fclose(f->stream_in);
|
|
|
|
fclose(f->stream_out);
|
2017-08-14 14:42:07 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return 0;
|
2015-03-31 13:28:11 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int villas::node::file_read(NodeCompat *n, struct Sample *const smps[],
|
|
|
|
unsigned cnt) {
|
|
|
|
auto *f = n->getData<struct file>();
|
|
|
|
int ret;
|
|
|
|
uint64_t steps;
|
2016-06-08 22:39:43 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
assert(cnt == 1);
|
2016-06-08 22:39:43 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
retry:
|
|
|
|
ret = f->formatter->scan(f->stream_in, smps, cnt);
|
|
|
|
if (ret <= 0) {
|
|
|
|
if (feof(f->stream_in)) {
|
|
|
|
switch (f->eof_mode) {
|
|
|
|
case file::EOFBehaviour::REWIND:
|
|
|
|
n->logger->info("Rewind input file");
|
2017-06-17 18:53:00 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
f->offset = file_calc_offset(&f->first, &f->epoch, f->epoch_mode);
|
|
|
|
rewind(f->stream_in);
|
|
|
|
goto retry;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case file::EOFBehaviour::SUSPEND:
|
|
|
|
// We wait 10ms before fetching again.
|
|
|
|
usleep(100000);
|
2017-08-14 14:42:07 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Try to download more data if this is a remote file.
|
|
|
|
clearerr(f->stream_in);
|
|
|
|
goto retry;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
case file::EOFBehaviour::STOP:
|
|
|
|
n->logger->info("Reached end-of-file.");
|
2017-08-14 14:42:07 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
n->setState(State::STOPPING);
|
2019-02-11 16:37:59 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return -1;
|
2019-04-22 23:43:46 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
default: {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
n->logger->warn("Failed to read messages: reason={}", ret);
|
2016-06-08 22:39:43 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// We dont wait in FILE_EPOCH_ORIGINAL mode
|
|
|
|
if (f->epoch_mode == file::EpochMode::ORIGINAL)
|
|
|
|
return cnt;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (f->rate) {
|
|
|
|
steps = f->task.wait();
|
2017-05-08 00:46:48 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
smps[0]->ts.origin = time_now();
|
|
|
|
} else {
|
|
|
|
smps[0]->ts.origin = time_add(&smps[0]->ts.origin, &f->offset);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
f->task.setNext(&smps[0]->ts.origin);
|
|
|
|
steps = f->task.wait();
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Check for overruns
|
|
|
|
if (steps == 0)
|
|
|
|
throw SystemError("Failed to wait for timer");
|
|
|
|
else if (steps != 1)
|
|
|
|
n->logger->warn("Missed steps: {}", steps - 1);
|
2017-08-14 14:42:07 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return cnt;
|
2015-03-31 13:28:11 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int villas::node::file_write(NodeCompat *n, struct Sample *const smps[],
|
|
|
|
unsigned cnt) {
|
|
|
|
int ret;
|
|
|
|
auto *f = n->getData<struct file>();
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
assert(cnt == 1);
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = f->formatter->print(f->stream_out, smps, cnt);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (f->flush)
|
|
|
|
fflush(f->stream_out);
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return cnt;
|
2015-08-07 01:11:43 +02:00
|
|
|
}
|
2015-09-19 15:26:30 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int villas::node::file_poll_fds(NodeCompat *n, int fds[]) {
|
|
|
|
auto *f = n->getData<struct file>();
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (f->rate) {
|
|
|
|
fds[0] = f->task.getFD();
|
2019-04-22 23:43:46 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return 1;
|
|
|
|
} else if (f->epoch_mode == file::EpochMode::ORIGINAL) {
|
|
|
|
fds[0] = fileno(f->stream_in);
|
2019-04-22 23:43:46 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-04-22 23:43:46 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return -1; // TODO: not supported yet
|
2017-08-30 00:25:42 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int villas::node::file_init(NodeCompat *n) {
|
|
|
|
auto *f = n->getData<struct file>();
|
2020-03-04 13:06:28 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// We require a real-time clock here as we can sync against the
|
|
|
|
// timestamps in the file.
|
|
|
|
new (&f->task) Task(CLOCK_REALTIME);
|
2020-03-04 13:06:28 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Default values
|
|
|
|
f->rate = 0;
|
|
|
|
f->eof_mode = file::EOFBehaviour::STOP;
|
|
|
|
f->epoch_mode = file::EpochMode::DIRECT;
|
|
|
|
f->flush = 0;
|
|
|
|
f->buffer_size_in = 0;
|
|
|
|
f->buffer_size_out = 0;
|
|
|
|
f->skip_lines = 0;
|
2020-07-16 14:18:05 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
f->formatter = nullptr;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return 0;
|
2020-03-04 13:06:28 +01:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int villas::node::file_destroy(NodeCompat *n) {
|
|
|
|
auto *f = n->getData<struct file>();
|
2020-03-04 13:06:28 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
f->task.~Task();
|
2020-03-04 13:06:28 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (f->uri)
|
|
|
|
delete[] f->uri;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (f->formatter)
|
|
|
|
delete f->formatter;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return 0;
|
2020-03-04 13:06:28 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static NodeCompatType p;
|
2019-04-23 00:36:06 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
__attribute__((constructor(110))) static void register_plugin() {
|
|
|
|
p.name = "file";
|
|
|
|
p.description = "support for file log / replay node type";
|
|
|
|
p.vectorize = 1;
|
|
|
|
p.size = sizeof(struct file);
|
|
|
|
p.init = file_init;
|
|
|
|
p.destroy = file_destroy;
|
|
|
|
p.parse = file_parse;
|
|
|
|
p.print = file_print;
|
|
|
|
p.start = file_start;
|
|
|
|
p.stop = file_stop;
|
|
|
|
p.read = file_read;
|
|
|
|
p.write = file_write;
|
|
|
|
p.poll_fds = file_poll_fds;
|
|
|
|
|
|
|
|
static NodeCompatFactory ncp(&p);
|
2019-04-23 00:36:06 +02:00
|
|
|
}
|