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.
|
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
|
|
|
*********************************************************************************/
|
2015-03-31 13:28:11 +02:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/timerfd.h>
|
|
|
|
|
|
|
|
#include "file.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
|
|
|
|
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 */
|
|
|
|
}
|
|
|
|
|
2015-03-31 13:28:11 +02:00
|
|
|
int file_print(struct node *n, char *buf, int len)
|
|
|
|
{
|
|
|
|
struct file *f = n->file;
|
|
|
|
|
2015-05-07 13:01:18 +02:00
|
|
|
return snprintf(buf, len, "in=%s, out=%s, mode=%s, rate=%.1f",
|
2015-03-31 13:28:11 +02:00
|
|
|
f->path_in, f->path_out, f->mode, f->rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
int file_parse(config_setting_t *cfg, struct node *n)
|
|
|
|
{
|
|
|
|
struct file *f = alloc(sizeof(struct file));
|
|
|
|
|
2015-05-19 16:55:10 +02:00
|
|
|
const char *out;
|
|
|
|
if (config_setting_lookup_string(cfg, "out", &out)) {
|
|
|
|
time_t t = time(NULL);
|
|
|
|
struct tm *tm = localtime(&t);
|
|
|
|
|
|
|
|
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-03-31 13:28:11 +02:00
|
|
|
config_setting_lookup_string(cfg, "in", &f->path_in);
|
|
|
|
|
|
|
|
if (!config_setting_lookup_string(cfg, "mode", &f->mode))
|
|
|
|
f->mode = "w+";
|
|
|
|
|
|
|
|
if (!config_setting_lookup_float(cfg, "rate", &f->rate))
|
|
|
|
f->rate = 1;
|
|
|
|
|
2015-05-19 16:55:39 +02:00
|
|
|
if (!config_setting_lookup_bool(cfg, "timestamp", &f->timestamp))
|
|
|
|
f->timestamp = 0;
|
|
|
|
|
2015-03-31 13:28:11 +02:00
|
|
|
n->file = f;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int file_open(struct node *n)
|
|
|
|
{
|
|
|
|
struct file *f = n->file;
|
|
|
|
|
|
|
|
if (f->path_in) {
|
|
|
|
f->in = fopen(f->path_in, "r");
|
|
|
|
if (!f->in)
|
|
|
|
serror("Failed to open file for reading: '%s'", f->path_in);
|
|
|
|
|
|
|
|
f->tfd = timerfd_create(CLOCK_MONOTONIC, 0);
|
|
|
|
if (f->tfd < 0)
|
|
|
|
serror("Failed to create timer");
|
|
|
|
|
|
|
|
struct itimerspec its = {
|
|
|
|
.it_interval = timespec_rate(f->rate),
|
|
|
|
.it_value = { 1, 0 }
|
|
|
|
};
|
|
|
|
int ret = timerfd_settime(f->tfd, 0, &its, NULL);
|
|
|
|
if (ret)
|
|
|
|
serror("Failed to start timer");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f->path_out) {
|
|
|
|
f->out = fopen(f->path_out, f->mode);
|
|
|
|
if (!f->out)
|
|
|
|
serror("Failed to open file for writing: '%s'", f->path_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int file_close(struct node *n)
|
|
|
|
{
|
|
|
|
struct file *f = n->file;
|
|
|
|
|
|
|
|
if (f->tfd)
|
|
|
|
close(f->tfd);
|
|
|
|
if (f->in)
|
|
|
|
fclose(f->in);
|
|
|
|
if (f->out)
|
|
|
|
fclose(f->out);
|
|
|
|
|
2015-05-19 16:55:39 +02:00
|
|
|
free(f->path_out);
|
|
|
|
|
2015-03-31 13:28:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
int file_read(struct node *n, struct msg *pool, int poolsize, int first, int cnt)
|
2015-03-31 13:28:11 +02:00
|
|
|
{
|
2015-05-06 11:49:13 +02:00
|
|
|
int i = 0;
|
2015-03-31 13:28:11 +02:00
|
|
|
struct file *f = n->file;
|
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
if (f->in) {
|
2015-05-06 13:20:02 +02:00
|
|
|
/* Blocking for 1/f->rate seconds */
|
|
|
|
if (timerfd_wait(f->tfd)) {
|
2015-05-07 13:01:18 +02:00
|
|
|
for (i = 0; i < cnt; i++) {
|
2015-05-06 13:20:02 +02:00
|
|
|
struct msg *m = &pool[(first+i) % poolsize];
|
2015-05-06 11:49:13 +02:00
|
|
|
|
2015-05-06 13:20:02 +02:00
|
|
|
msg_fscan(f->in, m);
|
|
|
|
}
|
2015-05-06 11:49:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
warn("Can not read from node '%s'", n->name);
|
2015-03-31 13:28:11 +02:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
return i;
|
2015-03-31 13:28:11 +02:00
|
|
|
}
|
|
|
|
|
2015-05-06 11:49:13 +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
|
|
|
{
|
2015-05-06 11:49:13 +02:00
|
|
|
int i = 0;
|
2015-03-31 13:28:11 +02:00
|
|
|
struct file *f = n->file;
|
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
if (f->out) {
|
2015-05-07 13:01:18 +02:00
|
|
|
for (i = 0; i < cnt; i++) {
|
2015-05-06 11:49:13 +02:00
|
|
|
struct msg *m = &pool[(first+i) % poolsize];
|
|
|
|
|
2015-05-19 16:55:39 +02:00
|
|
|
if (f->timestamp) {
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
fprintf(f->out, "%lu.%06lu\t", ts.tv_sec, (long) (ts.tv_nsec / 1e3));
|
|
|
|
}
|
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
msg_fprint(f->out, m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
warn("Can not write to node '%s", n->name);
|
2015-03-31 13:28:11 +02:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
return i;
|
2015-03-31 13:28:11 +02:00
|
|
|
}
|