1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

automatically detect the number of values when parsing messages via msg_fscan()

git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@216 8ec27952-4edc-4aab-86aa-e87bb2611832
This commit is contained in:
Steffen Vogel 2014-09-07 16:29:05 +00:00
parent 0f8a0ceb52
commit d6dc332ea9
2 changed files with 31 additions and 19 deletions

View file

@ -7,6 +7,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#ifdef __linux__
#include <byteswap.h>
@ -33,10 +34,10 @@ int msg_fprint(FILE *f, struct msg *m)
if (m->endian != MSG_ENDIAN_HOST)
msg_swap(m);
fprintf(f, "%-8hu", m->sequence);
fprintf(f, "%hu", m->sequence);
for (int i = 0; i < m->length; i++)
fprintf(f, "%-12.6f ", m->data[i].f);
fprintf(f, "\t%.6f", m->data[i].f);
fprintf(f, "\n");
@ -45,16 +46,28 @@ int msg_fprint(FILE *f, struct msg *m)
int msg_fscan(FILE *f, struct msg *m)
{
fscanf(f, "%8hu ", &m->sequence);
char line[MSG_VALUES * 16];
char *ptr = line;
for (int i = 0; i < m->length; i++)
fscanf(f, "%12f ", &m->data[i].f);
if (!fgets(line, sizeof(line), f))
return 0;
fscanf(f, "\n");
m->sequence = (uint16_t) strtol(ptr, &ptr, 10);
int i;
for (i = 0; i <= MSG_VALUES; i++) {
while(isblank(*ptr++));
if (*ptr == '\n' || *ptr == '\0')
break;
m->data[i].f = strtod(ptr, &ptr);
info("read value %u => %f", i, m->data[i].f);
}
m->length = i;
m->endian = MSG_ENDIAN_HOST;
return 0;
return m->length;
}
void msg_random(struct msg *m)

View file

@ -31,18 +31,17 @@ void quit(int sig, siginfo_t *si, void *ptr)
int main(int argc, char *argv[])
{
if (argc != 3 && argc != 4) {
printf("Usage: %s VALUES REMOTE [LOCAL]\n", argv[0]);
if (argc != 2 && argc != 3) {
printf("Usage: %s REMOTE [LOCAL]\n", argv[0]);
printf(" REMOTE is a IP:PORT combination of the remote host\n");
printf(" LOCAL is an optional IP:PORT combination of the local host\n");
printf(" VALUES is the number of values to be read from stdin\n\n");
printf("Simulator2Simulator Server %s (built on %s %s)\n", BLU(VERSION), MAG(__DATE__), MAG(__TIME__));
printf("Copyright 2014, Institute for Automation of Complex Power Systems, EONERC\n");
exit(EXIT_FAILURE);
}
struct node n = NODE_INIT("remote");
struct msg m = MSG_INIT(atoi(argv[1]));
struct msg m = MSG_INIT(0);
/* Setup signals */
struct sigaction sa_quit = {
@ -55,11 +54,11 @@ int main(int argc, char *argv[])
sigaction(SIGINT, &sa_quit, NULL);
/* Resolve addresses */
if (resolve_addr(argv[2], &n.remote, 0))
error("Failed to resolve remote address: %s", argv[2]);
if (resolve_addr(argv[1], &n.remote, 0))
error("Failed to resolve remote address: %s", argv[1]);
if (argc == 4 && resolve_addr(argv[3], &n.local, 0))
error("Failed to resolve local address: %s", argv[3]);
if (argc == 3 && resolve_addr(argv[2], &n.local, 0))
error("Failed to resolve local address: %s", argv[2]);
else {
n.local.sin_family = AF_INET;
n.local.sin_addr.s_addr = INADDR_ANY;
@ -71,15 +70,15 @@ int main(int argc, char *argv[])
while (!feof(stdin)) {
msg_fscan(stdin, &m);
msg_send(&m, &n);
#if 1
#if 1 /* Preprend timestamp */
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
fprintf(stdout, "%17.6f", ts.tv_sec + ts.tv_nsec / 1e9);
fprintf(stdout, "%17.3f\t", ts.tv_sec + ts.tv_nsec / 1e9);
#endif
msg_fprint(stdout, &m);
msg_send(&m, &n);
}
return 0;