2014-06-05 09:34:29 +00:00
|
|
|
/**
|
|
|
|
* Simple UDP test client (simulator emulation)
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2014, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2014-06-05 09:34:40 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "msg.h"
|
|
|
|
|
2014-06-05 09:34:40 +00:00
|
|
|
int dev_id;
|
|
|
|
int sd;
|
|
|
|
|
|
|
|
void quit()
|
|
|
|
{
|
2014-06-05 09:34:46 +00:00
|
|
|
debug(1, "Goodbye");
|
2014-06-05 09:34:40 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tick()
|
|
|
|
{
|
|
|
|
struct msg m;
|
|
|
|
|
|
|
|
msg_random(&m, dev_id);
|
|
|
|
msg_fprint(stdout, &m);
|
|
|
|
|
|
|
|
send(sd, &m, sizeof(struct msg), 0);
|
|
|
|
}
|
|
|
|
|
2014-06-05 09:34:29 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc != 4) {
|
|
|
|
printf("Usage: test DEVID IP PORT\n");
|
|
|
|
printf(" DEVID is the device id of this node\n");
|
|
|
|
printf(" IP is the destination ip of our packets\n");
|
|
|
|
printf(" PORT is the port to recv/send from\n\n");
|
|
|
|
printf("s2ss Simulator2Simulator Server v%s\n", VERSION);
|
|
|
|
printf("Copyright 2014, Institute for Automation of Complex Power Systems, EONERC\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-06-05 09:34:40 +00:00
|
|
|
dev_id = atoi(argv[1]);
|
2014-06-05 09:34:29 +00:00
|
|
|
int ret;
|
|
|
|
|
2014-06-05 09:34:46 +00:00
|
|
|
info("Test node started on %s:%s with id=%u", argv[2], argv[3], dev_id);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:40 +00:00
|
|
|
sd = socket(AF_INET, SOCK_DGRAM, 0);
|
2014-06-05 09:34:29 +00:00
|
|
|
if (sd < 0)
|
2014-06-05 09:34:46 +00:00
|
|
|
error("Failed to create socket: %s", strerror(errno));
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:40 +00:00
|
|
|
struct sockaddr_in sa = {
|
|
|
|
.sin_family = AF_INET,
|
|
|
|
.sin_port = htons(atoi(argv[3]))
|
|
|
|
};
|
2014-06-05 09:34:44 +00:00
|
|
|
inet_pton(AF_INET, argv[2], &sa.sin_addr);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:40 +00:00
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGUSR1);
|
|
|
|
//sigprocmask(SIG_SETMASK, &mask, NULL);
|
|
|
|
sigprocmask(SIG_UNBLOCK, &mask, NULL);
|
|
|
|
|
2014-06-05 09:34:44 +00:00
|
|
|
ret = bind(sd, (struct sockaddr *) &sa, sizeof(struct sockaddr_in));
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2014-06-05 09:34:44 +00:00
|
|
|
ret = connect(sd, (struct sockaddr *) &sa, sizeof(struct sockaddr_in));
|
2014-06-05 09:34:29 +00:00
|
|
|
if (ret < 0)
|
2014-06-05 09:34:46 +00:00
|
|
|
error("Failed to connect socket: %s", strerror(errno));
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:40 +00:00
|
|
|
struct sigevent si = {
|
|
|
|
.sigev_notify = SIGEV_SIGNAL,
|
|
|
|
.sigev_signo = SIGUSR1
|
|
|
|
};
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:40 +00:00
|
|
|
struct itimerspec its = {
|
|
|
|
{ 0, 250000000 },
|
|
|
|
{ 0, 500000000 }
|
|
|
|
};
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:40 +00:00
|
|
|
timer_t t;
|
|
|
|
timer_create(CLOCK_MONOTONIC, &si, &t);
|
|
|
|
timer_settime(t, 0, &its, NULL);
|
|
|
|
|
|
|
|
signal(SIGUSR1, tick);
|
|
|
|
signal(SIGINT, quit);
|
|
|
|
|
|
|
|
while(1) pause();
|
|
|
|
|
|
|
|
timer_delete(t);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|