From 49efbf2e64ca3cebe1153130c87891a70c986a20 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Thu, 5 Jun 2014 09:35:02 +0000 Subject: [PATCH] added new tools to send/receive data to/from stdin/out git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@33 8ec27952-4edc-4aab-86aa-e87bb2611832 --- .gitignore | 2 ++ src/receive.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++ src/send.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 src/receive.c create mode 100644 src/send.c diff --git a/.gitignore b/.gitignore index ab874738e..049f69477 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ doc/latex/ server test random +send +receive diff --git a/src/receive.c b/src/receive.c new file mode 100644 index 000000000..f8c96060a --- /dev/null +++ b/src/receive.c @@ -0,0 +1,73 @@ +/** + * Receive messages from server snd print them on stdout + * + * @author Steffen Vogel + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "config.h" +#include "utils.h" +#include "msg.h" + +int sd; + +void quit(int sig, siginfo_t *si, void *ptr) +{ + close(sd); + exit(EXIT_SUCCESS); +} + +int main(int argc, char *argv[]) +{ + struct sockaddr_in sa; + + if (argc != 2) { + printf("Usage: %s IP:PORT\n", argv[0]); + printf(" IP is the destination ip of our packets\n"); + printf(" PORT is the port to receive 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); + } + + /* Setup signals */ + struct sigaction sa_quit = { + .sa_flags = SA_SIGINFO, + .sa_sigaction = quit + }; + + sigemptyset(&sa_quit.sa_mask); + sigaction(SIGINT, &sa_quit, NULL); + + /* Resolve address */ + if (resolve(argv[1], &sa, 0)) + error("Failed to resolve: %s", argv[1]); + + /* Create socket */ + sd = socket(AF_INET, SOCK_DGRAM, 0); + if (sd < 0) + error("Failed to create socket: %s", strerror(errno)); + + /* Bind socket */ + if (bind(sd, (struct sockaddr *) &sa, sizeof(struct sockaddr_in))) + error("Failed to bind socket: %s", strerror(errno)); + + struct msg m; + while (1) { + recv(sd, &m, sizeof(struct msg), 0); + msg_fprint(stdout, &m); + } + + return 0; +} diff --git a/src/send.c b/src/send.c new file mode 100644 index 000000000..db8ea3de4 --- /dev/null +++ b/src/send.c @@ -0,0 +1,79 @@ +/** + * Send messages from stdin to server + * + * @author Steffen Vogel + * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "config.h" +#include "utils.h" +#include "msg.h" + +int sd; + +void quit(int sig, siginfo_t *si, void *ptr) +{ + close(sd); + exit(EXIT_SUCCESS); +} + +int main(int argc, char *argv[]) +{ + struct sockaddr_in sa; + + if (argc != 2) { + printf("Usage: %s IP:PORT\n", argv[0]); + printf(" IP is the destination ip of our packets\n"); + printf(" PORT is the port to send to\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); + } + + /* Setup signals */ + struct sigaction sa_quit = { + .sa_flags = SA_SIGINFO, + .sa_sigaction = quit + }; + + sigemptyset(&sa_quit.sa_mask); + sigaction(SIGINT, &sa_quit, NULL); + + /* Resolve address */ + if (resolve(argv[1], &sa, 0)) + error("Failed to resolve: %s", argv[1]); + + /* Create socket */ + sd = socket(AF_INET, SOCK_DGRAM, 0); + if (sd < 0) + error("Failed to create socket: %s", strerror(errno)); + + // TODO: remove workaround + struct msg msg = { + .length = 5 * sizeof(double) + }; + + /* Connect socket */ + if (connect(sd, (struct sockaddr *) &sa, sizeof(struct sockaddr_in))) { + error("Failed to connect socket: %s", strerror(errno)); + } + + while (!feof(stdin)) { + msg_fscan(stdin, &msg); + msg_fprint(stdout, &msg); + send(sd, &msg, 8 + msg.length, 0); + } + + return 0; +}