2015-03-31 13:28:11 +02:00
|
|
|
/** Node type: File
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2015-03-31 13:28:11 +02:00
|
|
|
|
|
|
|
#include <unistd.h>
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <cinttypes>
|
2018-10-20 15:13:29 +02:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <sys/stat.h>
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cerrno>
|
2015-03-31 13:28:11 +02:00
|
|
|
|
2019-04-23 00:12:31 +02:00
|
|
|
#include <villas/nodes/file.hpp>
|
2019-04-23 13:15:00 +02:00
|
|
|
#include <villas/utils.hpp>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/timing.h>
|
|
|
|
#include <villas/queue.h>
|
|
|
|
#include <villas/plugin.h>
|
|
|
|
#include <villas/io.h>
|
2021-02-16 14:15:14 +01:00
|
|
|
#include <villas/exceptions.hpp>
|
2015-11-23 16:42:43 +01:00
|
|
|
|
2020-07-04 16:22:10 +02:00
|
|
|
using namespace villas;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
static char * file_format_name(const char *format, struct timespec *ts)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
2020-01-21 16:26:51 +01:00
|
|
|
char *buf = new char[FILE_MAX_PATHLEN];
|
2020-07-04 16:22:10 +02:00
|
|
|
if (!buf)
|
|
|
|
throw MemoryAllocationError();
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
/* Convert time */
|
|
|
|
gmtime_r(&ts->tv_sec, &tm);
|
|
|
|
|
|
|
|
strftime(buf, FILE_MAX_PATHLEN, format, &tm);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
static struct timespec file_calc_offset(const struct timespec *first, const struct timespec *epoch, enum file::EpochMode mode)
|
2017-05-08 00:46:48 +02:00
|
|
|
{
|
|
|
|
/* Get current time */
|
|
|
|
struct timespec now = time_now();
|
|
|
|
struct timespec offset;
|
|
|
|
|
2019-04-22 23:43:46 +02:00
|
|
|
/* Set offset depending on epoch */
|
2017-05-08 00:46:48 +02:00
|
|
|
switch (mode) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EpochMode::DIRECT: /* read first value at now + epoch */
|
2017-05-08 00:46:48 +02:00
|
|
|
offset = time_diff(first, &now);
|
2017-07-24 15:30:47 +02:00
|
|
|
return time_add(&offset, epoch);
|
2017-05-08 00:46:48 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EpochMode::WAIT: /* read first value at now + first + epoch */
|
2017-05-08 00:46:48 +02:00
|
|
|
offset = now;
|
|
|
|
return time_add(&now, epoch);
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EpochMode::RELATIVE: /* read first value at first + epoch */
|
2017-05-08 00:46:48 +02:00
|
|
|
return *epoch;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EpochMode::ABSOLUTE: /* read first value at f->epoch */
|
2017-05-08 00:46:48 +02:00
|
|
|
return time_diff(first, epoch);
|
|
|
|
|
2017-07-24 15:30:47 +02:00
|
|
|
default:
|
|
|
|
return (struct timespec) { 0 };
|
2017-05-08 00:46:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
int file_parse(struct vnode *n, json_t *json)
|
2015-10-14 12:11:33 +02:00
|
|
|
{
|
2019-04-22 23:43:46 +02:00
|
|
|
struct file *f = (struct file *) n->_vd;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
const char *uri_tmpl = nullptr;
|
2018-05-09 10:27:38 +02:00
|
|
|
const char *format = "villas.human";
|
2019-04-22 23:45:38 +02:00
|
|
|
const char *eof = nullptr;
|
|
|
|
const char *epoch = nullptr;
|
2017-08-14 14:42:07 +02:00
|
|
|
double epoch_flt = 0;
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s: s, s?: s, s?: { s?: s, s?: F, s?: s, s?: F, s?: i, s?: i }, s?: { s?: b, s?: i } }",
|
2017-08-14 14:42:07 +02:00
|
|
|
"uri", &uri_tmpl,
|
2018-07-23 16:07:26 +02:00
|
|
|
"format", &format,
|
|
|
|
"in",
|
|
|
|
"eof", &eof,
|
|
|
|
"rate", &f->rate,
|
2019-04-23 11:19:42 +02:00
|
|
|
"epoch_mode", &epoch,
|
2018-07-23 16:07:26 +02:00
|
|
|
"epoch", &epoch_flt,
|
2018-08-06 23:48:13 +02:00
|
|
|
"buffer_size", &f->buffer_size_in,
|
2020-07-16 14:18:05 +02:00
|
|
|
"skip", &f->skip_lines,
|
2018-07-23 16:07:26 +02:00
|
|
|
"out",
|
2018-08-06 23:48:13 +02:00
|
|
|
"flush", &f->flush,
|
|
|
|
"buffer_size", &f->buffer_size_out
|
2017-08-03 00:19:27 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, err, "node-config-node-file");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
f->epoch = time_from_double(epoch_flt);
|
2019-04-22 23:45:38 +02:00
|
|
|
f->uri_tmpl = uri_tmpl ? strdup(uri_tmpl) : nullptr;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-05-12 13:56:12 +02:00
|
|
|
f->format = format_type_lookup(format);
|
2017-08-14 14:42:07 +02:00
|
|
|
if (!f->format)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Invalid format '{}'", format);
|
2017-08-14 14:42:07 +02:00
|
|
|
|
|
|
|
if (eof) {
|
2019-02-06 15:09:44 +01:00
|
|
|
if (!strcmp(eof, "exit") || !strcmp(eof, "stop"))
|
2019-06-23 16:13:23 +02:00
|
|
|
f->eof_mode = file::EOFBehaviour::STOP;
|
2017-08-14 14:42:07 +02:00
|
|
|
else if (!strcmp(eof, "rewind"))
|
2019-06-23 16:13:23 +02:00
|
|
|
f->eof_mode = file::EOFBehaviour::REWIND;
|
2017-08-14 14:42:07 +02:00
|
|
|
else if (!strcmp(eof, "wait"))
|
2019-06-23 16:13:23 +02:00
|
|
|
f->eof_mode = file::EOFBehaviour::SUSPEND;
|
2017-08-14 14:42:07 +02:00
|
|
|
else
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Invalid mode '{}' for 'eof' setting", eof);
|
2015-10-14 12:11:33 +02:00
|
|
|
}
|
|
|
|
|
2019-04-22 23:43:46 +02:00
|
|
|
if (epoch) {
|
|
|
|
if (!strcmp(epoch, "direct"))
|
2019-06-23 16:13:23 +02:00
|
|
|
f->epoch_mode = file::EpochMode::DIRECT;
|
2019-04-22 23:43:46 +02:00
|
|
|
else if (!strcmp(epoch, "wait"))
|
2019-06-23 16:13:23 +02:00
|
|
|
f->epoch_mode = file::EpochMode::WAIT;
|
2019-04-22 23:43:46 +02:00
|
|
|
else if (!strcmp(epoch, "relative"))
|
2019-06-23 16:13:23 +02:00
|
|
|
f->epoch_mode = file::EpochMode::RELATIVE;
|
2019-04-22 23:43:46 +02:00
|
|
|
else if (!strcmp(epoch, "absolute"))
|
2019-06-23 16:13:23 +02:00
|
|
|
f->epoch_mode = file::EpochMode::ABSOLUTE;
|
2019-04-22 23:43:46 +02:00
|
|
|
else if (!strcmp(epoch, "original"))
|
2019-06-23 16:13:23 +02:00
|
|
|
f->epoch_mode = file::EpochMode::ORIGINAL;
|
2017-08-14 14:42:07 +02:00
|
|
|
else
|
2021-02-16 14:15:14 +01:00
|
|
|
throw RuntimeError("Invalid value '{}' for setting 'epoch'", epoch);
|
2015-10-14 12:11:33 +02:00
|
|
|
}
|
|
|
|
|
2015-11-29 22:47:57 +01:00
|
|
|
n->_vd = f;
|
2015-10-14 12:11:33 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
char * file_print(struct vnode *n)
|
2015-03-31 13:28:11 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct file *f = (struct file *) n->_vd;
|
2019-04-22 23:45:38 +02:00
|
|
|
char *buf = nullptr;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-22 23:45:38 +02:00
|
|
|
const char *epoch_str = nullptr;
|
|
|
|
const char *eof_str = nullptr;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
switch (f->epoch_mode) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EpochMode::DIRECT:
|
2019-04-22 23:43:46 +02:00
|
|
|
epoch_str = "direct";
|
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EpochMode::WAIT:
|
2019-04-22 23:43:46 +02:00
|
|
|
epoch_str = "wait";
|
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EpochMode::RELATIVE:
|
2019-04-22 23:43:46 +02:00
|
|
|
epoch_str = "relative";
|
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EpochMode::ABSOLUTE:
|
2019-04-22 23:43:46 +02:00
|
|
|
epoch_str = "absolute";
|
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EpochMode::ORIGINAL:
|
2019-04-22 23:43:46 +02:00
|
|
|
epoch_str = "original";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
epoch_str = "";
|
|
|
|
break;
|
2015-10-09 13:08:39 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-22 23:43:46 +02:00
|
|
|
switch (f->eof_mode) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EOFBehaviour::STOP:
|
2019-04-22 23:43:46 +02:00
|
|
|
eof_str = "stop";
|
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EOFBehaviour::SUSPEND:
|
2019-04-22 23:43:46 +02:00
|
|
|
eof_str = "wait";
|
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EOFBehaviour::REWIND:
|
2019-04-22 23:43:46 +02:00
|
|
|
eof_str = "rewind";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
eof_str = "";
|
|
|
|
break;
|
2015-10-09 13:08:39 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2020-07-16 14:18:05 +02:00
|
|
|
strcatf(&buf, "uri=%s, format=%s, out.flush=%s, in.skip=%d, in.eof=%s, in.epoch=%s, in.epoch=%.2f",
|
2017-08-14 14:42:07 +02:00
|
|
|
f->uri ? f->uri : f->uri_tmpl,
|
2018-08-20 18:16:09 +02:00
|
|
|
format_type_name(f->format),
|
2017-08-14 14:42:07 +02:00
|
|
|
f->flush ? "yes" : "no",
|
2020-07-16 14:18:05 +02:00
|
|
|
f->skip_lines,
|
2017-08-14 14:42:07 +02:00
|
|
|
eof_str,
|
|
|
|
epoch_str,
|
|
|
|
time_to_double(&f->epoch)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (f->rate)
|
2020-07-16 14:18:05 +02:00
|
|
|
strcatf(&buf, ", in.rate=%.1f", f->rate);
|
2017-08-14 14:42:07 +02: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
|
|
|
|
2017-08-14 14:42:07 +02: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
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
if ((f->first.tv_sec || f->first.tv_nsec) &&
|
|
|
|
(f->offset.tv_sec || f->offset.tv_nsec)) {
|
2015-10-14 12:11:33 +02:00
|
|
|
struct timespec eta, now = time_now();
|
2015-10-09 13:08:39 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
eta = time_add(&f->first, &f->offset);
|
2015-10-14 12:11:33 +02:00
|
|
|
eta = time_diff(&now, &eta);
|
2015-10-09 13:08:39 +02:00
|
|
|
|
|
|
|
if (eta.tv_sec || eta.tv_nsec)
|
2017-06-17 18:54:03 +02:00
|
|
|
strcatf(&buf, ", eta=%.2f sec", time_to_double(&eta));
|
2015-10-09 13:08:39 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-10-09 13:08:39 +02:00
|
|
|
return buf;
|
2015-03-31 13:28:11 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int file_start(struct vnode *n)
|
2015-03-31 13:28:11 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct file *f = (struct file *) n->_vd;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-10-14 12:11:33 +02:00
|
|
|
struct timespec now = time_now();
|
2017-08-14 14:42:07 +02:00
|
|
|
int ret, flags;
|
2015-03-31 13:28:11 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
/* Prepare file name */
|
|
|
|
f->uri = file_format_name(f->uri_tmpl, &now);
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
/* 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) {
|
2018-10-21 10:32:09 +02:00
|
|
|
ret = mkdir(dir, 0644);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw SystemError("Failed to create directory");
|
2018-10-21 10:32:09 +02:00
|
|
|
}
|
2021-02-16 14:15:14 +01:00
|
|
|
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");
|
2018-12-04 10:54:31 +01:00
|
|
|
}
|
2018-10-21 11:09:20 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
free(cpy);
|
|
|
|
|
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
/* Open file */
|
2019-06-23 16:13:23 +02:00
|
|
|
flags = (int) SampleFlags::HAS_ALL;
|
2017-08-14 14:42:07 +02:00
|
|
|
if (f->flush)
|
2019-06-23 16:13:23 +02:00
|
|
|
flags |= (int) IOFlags::FLUSH;
|
2017-08-14 14:42:07 +02:00
|
|
|
|
2019-02-06 13:11:57 +01:00
|
|
|
ret = io_init(&f->io, f->format, &n->in.signals, flags);
|
2018-08-20 18:29:23 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
ret = io_open(&f->io, f->uri);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-08-06 23:48:13 +02:00
|
|
|
if (f->buffer_size_in) {
|
2021-02-16 14:15:14 +01:00
|
|
|
ret = setvbuf(f->io.in.stream, nullptr, _IOFBF, f->buffer_size_in);
|
2018-08-20 18:29:23 +02:00
|
|
|
if (ret)
|
2018-08-04 15:20:21 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-08-06 23:48:13 +02:00
|
|
|
if (f->buffer_size_out) {
|
2021-02-16 14:15:14 +01:00
|
|
|
ret = setvbuf(f->io.out.stream, nullptr, _IOFBF, f->buffer_size_out);
|
2018-08-20 18:29:23 +02:00
|
|
|
if (ret)
|
2018-08-06 23:48:13 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2018-08-04 15:20:21 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
/* Create timer */
|
2020-03-04 13:06:28 +01:00
|
|
|
f->task.setRate(f->rate);
|
2017-08-14 14:42:07 +02:00
|
|
|
|
|
|
|
/* Get timestamp of first line */
|
2019-06-23 16:13:23 +02:00
|
|
|
if (f->epoch_mode != file::EpochMode::ORIGINAL) {
|
2017-08-14 14:42:07 +02:00
|
|
|
io_rewind(&f->io);
|
|
|
|
|
2017-09-05 10:11:23 +02:00
|
|
|
if (io_eof(&f->io)) {
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Empty file");
|
2017-09-05 10:11:23 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-04-23 09:57:43 +02:00
|
|
|
struct sample s;
|
|
|
|
struct sample *smps[] = { &s };
|
|
|
|
|
|
|
|
s.capacity = 0;
|
|
|
|
|
2017-09-05 10:11:23 +02:00
|
|
|
ret = io_scan(&f->io, smps, 1);
|
|
|
|
if (ret == 1) {
|
|
|
|
f->first = s.ts.origin;
|
|
|
|
f->offset = file_calc_offset(&f->first, &f->epoch, f->epoch_mode);
|
|
|
|
}
|
|
|
|
else
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Failed to read first timestamp");
|
2017-09-05 10:11:23 +02:00
|
|
|
}
|
2015-03-31 13:28:11 +02:00
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
io_rewind(&f->io);
|
|
|
|
|
2020-07-16 14:18:05 +02:00
|
|
|
/* Fast-forward */
|
|
|
|
struct sample *smp = sample_alloc_mem(vlist_length(&n->in.signals));
|
|
|
|
for (unsigned i = 0; i < f->skip_lines; i++)
|
|
|
|
io_scan(&f->io, &smp, 1);
|
2020-08-25 20:22:19 +02:00
|
|
|
|
2020-07-16 14:18:05 +02:00
|
|
|
sample_free(smp);
|
|
|
|
|
2015-03-31 13:28:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int file_stop(struct vnode *n)
|
2015-03-31 13:28:11 +02:00
|
|
|
{
|
2017-08-14 14:42:07 +02:00
|
|
|
int ret;
|
2018-05-12 18:03:40 +02:00
|
|
|
struct file *f = (struct file *) n->_vd;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2020-03-04 13:06:28 +01:00
|
|
|
f->task.stop();
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
ret = io_close(&f->io);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = io_destroy(&f->io);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2020-01-21 16:26:51 +01:00
|
|
|
delete f->uri;
|
2018-07-26 16:38:26 +02:00
|
|
|
|
2015-03-31 13:28:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int file_read(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2015-03-31 13:28:11 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct file *f = (struct file *) n->_vd;
|
2017-08-14 14:42:07 +02:00
|
|
|
int ret;
|
|
|
|
uint64_t steps;
|
2016-06-08 22:39:43 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
assert(cnt == 1);
|
2016-06-08 22:39:43 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
retry: ret = io_scan(&f->io, smps, cnt);
|
2018-08-20 18:48:30 +02:00
|
|
|
if (ret <= 0) {
|
2017-08-14 14:42:07 +02:00
|
|
|
if (io_eof(&f->io)) {
|
2019-04-22 23:43:46 +02:00
|
|
|
switch (f->eof_mode) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EOFBehaviour::REWIND:
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->info("Rewind input file");
|
2017-06-17 18:53:00 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
f->offset = file_calc_offset(&f->first, &f->epoch, f->epoch_mode);
|
|
|
|
io_rewind(&f->io);
|
2017-06-17 18:53:00 +02:00
|
|
|
goto retry;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EOFBehaviour::SUSPEND:
|
2017-08-14 14:42:07 +02:00
|
|
|
/* We wait 10ms before fetching again. */
|
|
|
|
usleep(100000);
|
|
|
|
|
|
|
|
/* Try to download more data if this is a remote file. */
|
2018-12-04 10:54:31 +01:00
|
|
|
switch (f->io.mode) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case IOMode::STDIO:
|
2021-02-16 14:15:14 +01:00
|
|
|
clearerr(f->io.in.stream);
|
2018-12-04 10:54:31 +01:00
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case IOMode::CUSTOM:
|
2018-12-04 10:54:31 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-08-14 14:42:07 +02:00
|
|
|
|
2017-06-17 18:53:00 +02:00
|
|
|
goto retry;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case file::EOFBehaviour::STOP:
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->info("Reached end-of-file.");
|
2017-08-14 14:42:07 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
n->state = State::STOPPING;
|
2019-02-11 16:37:59 +01:00
|
|
|
|
|
|
|
return -1;
|
2019-04-22 23:43:46 +02:00
|
|
|
|
|
|
|
default: { }
|
2017-03-29 20:14:01 +02:00
|
|
|
}
|
2015-08-09 23:52:44 +02:00
|
|
|
}
|
2016-06-08 22:39:43 +02:00
|
|
|
else
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Failed to read messages: reason={}", ret);
|
2016-06-08 22:39:43 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
/* We dont wait in FILE_EPOCH_ORIGINAL mode */
|
2019-06-23 16:13:23 +02:00
|
|
|
if (f->epoch_mode == file::EpochMode::ORIGINAL)
|
2018-07-11 18:14:29 +02:00
|
|
|
return cnt;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
if (f->rate) {
|
2020-03-04 13:06:28 +01:00
|
|
|
steps = f->task.wait();
|
2017-05-08 00:46:48 +02:00
|
|
|
|
2017-08-14 14:42:07 +02: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
|
|
|
|
2020-03-04 13:06:28 +01:00
|
|
|
f->task.setNext(&smps[0]->ts.origin);
|
|
|
|
steps = f->task.wait();
|
2015-05-06 11:49:13 +02:00
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
/* Check for overruns */
|
|
|
|
if (steps == 0)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw SystemError("Failed to wait for timer");
|
2017-08-14 14:42:07 +02:00
|
|
|
else if (steps != 1)
|
2021-02-16 14:15:14 +01:00
|
|
|
n->logger->warn("Missed steps: {}", steps - 1);
|
2017-08-14 14:42:07 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
return cnt;
|
2015-03-31 13:28:11 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int file_write(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2015-03-31 13:28:11 +02:00
|
|
|
{
|
2018-08-20 18:29:23 +02:00
|
|
|
int ret;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct file *f = (struct file *) n->_vd;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
assert(cnt == 1);
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2018-08-20 18:29:23 +02:00
|
|
|
ret = io_print(&f->io, smps, cnt);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
return cnt;
|
2015-08-07 01:11:43 +02:00
|
|
|
}
|
2015-09-19 15:26:30 +02:00
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int file_poll_fds(struct vnode *n, int fds[])
|
2017-08-30 00:25:42 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct file *f = (struct file *) n->_vd;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2019-01-21 15:47:34 +01:00
|
|
|
if (f->rate) {
|
2020-03-04 13:06:28 +01:00
|
|
|
fds[0] = f->task.getFD();
|
2019-04-22 23:43:46 +02:00
|
|
|
|
2019-01-21 15:47:34 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2019-06-23 16:13:23 +02:00
|
|
|
else if (f->epoch_mode == file::EpochMode::ORIGINAL) {
|
2019-04-22 23:43:46 +02:00
|
|
|
fds[0] = io_fd(&f->io);
|
|
|
|
|
|
|
|
return 1;
|
2017-08-30 00:25:42 +02:00
|
|
|
}
|
2019-04-22 23:43:46 +02:00
|
|
|
|
|
|
|
return -1; /** @todo not supported yet */
|
2017-08-30 00:25:42 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int file_init(struct vnode *n)
|
2020-03-04 13:06:28 +01:00
|
|
|
{
|
|
|
|
struct file *f = (struct file *) n->_vd;
|
|
|
|
|
|
|
|
new (&f->task) Task(CLOCK_REALTIME);
|
|
|
|
|
2020-07-16 14:18:05 +02: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-03-04 13:06:28 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int file_destroy(struct vnode *n)
|
2020-03-04 13:06:28 +01:00
|
|
|
{
|
|
|
|
struct file *f = (struct file *) n->_vd;
|
|
|
|
|
|
|
|
f->task.~Task();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-23 00:36:06 +02:00
|
|
|
static struct plugin p;
|
|
|
|
|
|
|
|
__attribute__((constructor(110)))
|
|
|
|
static void register_plugin() {
|
|
|
|
p.name = "file";
|
|
|
|
p.description = "support for file log / replay node type";
|
2019-06-23 16:13:23 +02:00
|
|
|
p.type = PluginType::NODE;
|
|
|
|
p.node.instances.state = State::DESTROYED;
|
2019-04-23 00:36:06 +02:00
|
|
|
p.node.vectorize = 1;
|
|
|
|
p.node.size = sizeof(struct file);
|
2020-03-04 13:06:28 +01:00
|
|
|
p.node.init = file_init;
|
|
|
|
p.node.destroy = file_destroy;
|
2019-04-23 00:36:06 +02:00
|
|
|
p.node.parse = file_parse;
|
|
|
|
p.node.print = file_print;
|
|
|
|
p.node.start = file_start;
|
|
|
|
p.node.stop = file_stop;
|
|
|
|
p.node.read = file_read;
|
|
|
|
p.node.write = file_write;
|
|
|
|
p.node.poll_fds = file_poll_fds;
|
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
int ret = vlist_init(&p.node.instances);
|
|
|
|
if (!ret)
|
|
|
|
vlist_init_and_push(&plugins, &p);
|
2019-04-23 00:36:06 +02:00
|
|
|
}
|
2015-11-23 16:44:01 +01:00
|
|
|
|
2019-04-23 00:36:06 +02:00
|
|
|
__attribute__((destructor(110)))
|
|
|
|
static void deregister_plugin() {
|
2020-06-16 02:35:34 +02:00
|
|
|
vlist_remove_all(&plugins, &p);
|
2019-04-23 00:36:06 +02:00
|
|
|
}
|