1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/server/src/random.c

80 lines
2 KiB
C
Raw Normal View History

/** Generate random packages on stdout.
*
* @file
2015-06-02 21:53:04 +02:00
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC
* This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
2015-08-07 01:11:43 +02:00
* Unauthorized copying of this file, via any medium is strictly prohibited.
2015-06-02 21:53:04 +02:00
*
* @addtogroup tools Test and debug tools
* @{
2015-06-02 21:53:04 +02:00
**********************************************************************************/
#include <unistd.h>
#include "config.h"
#include "utils.h"
#include "msg.h"
2015-06-02 22:06:15 +02:00
#include "timing.h"
#define CLOCKID CLOCK_REALTIME
int main(int argc, char *argv[])
{
if (argc < 3 || argc > 4) {
printf("Usage: %s VALUES RATE [LIMIT]\n", argv[0]);
printf(" VALUES is the number of values a message contains\n");
printf(" RATE how many messages per second\n");
printf(" LIMIT only send LIMIT messages\n\n");
printf("Simulator2Simulator Server %s (built on %s %s)\n",
BLU(VERSION), MAG(__DATE__), MAG(__TIME__));
2015-06-02 21:53:04 +02:00
printf(" copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC\n");
printf(" Steffen Vogel <StVogel@eonerc.rwth-aachen.de>\n");
2015-03-21 18:03:55 +01:00
exit(EXIT_FAILURE);
}
struct msg m = MSG_INIT(atoi(argv[1]));
double rate = atof(argv[2]);
int limit = argc >= 4 ? atoi(argv[3]) : -1;
/* Setup timer */
struct itimerspec its = {
.it_interval = time_from_double(1 / rate),
.it_value = { 1, 0 }
};
int tfd = timerfd_create(CLOCK_REALTIME, 0);
if (tfd < 0)
serror("Failed to create timer");
if (timerfd_settime(tfd, 0, &its, NULL))
serror("Failed to start timer");
/* Print header */
fprintf(stderr, "# %-20s\t\t%s\n", "sec.nsec(seq)", "data[]");
/* Block until 1/p->rate seconds elapsed */
while (limit-- > 0 || argc < 4) {
m.sequence += timerfd_wait(tfd);
2015-08-07 01:11:43 +02:00
2015-06-02 22:43:57 +02:00
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
2015-08-07 01:11:43 +02:00
2015-06-02 22:43:57 +02:00
m.ts.sec = ts.tv_sec;
m.ts.nsec = ts.tv_nsec;
msg_random(&m);
msg_fprint(stdout, &m, MSG_PRINT_ALL & ~MSG_PRINT_OFFSET, 0);
2015-08-07 01:11:43 +02:00
fflush(stdout);
}
close(tfd);
return 0;
}
2015-08-07 01:11:43 +02:00
/** @} */