aboutsummaryrefslogtreecommitdiff
path: root/testing/yup-comm/yup-comm-client.c
diff options
context:
space:
mode:
authorJan Sucan <sucanjan@fit.cvut.cz>2019-06-04 14:34:27 +0200
committerJan Sucan <sucanjan@fit.cvut.cz>2019-06-04 14:34:27 +0200
commitdc8703206e3f0f69605c56d0e1127f7e17f3476a (patch)
tree166823a741dc420c10d54250cb53d1e3a6b74faf /testing/yup-comm/yup-comm-client.c
Initial commit
Diffstat (limited to 'testing/yup-comm/yup-comm-client.c')
-rw-r--r--testing/yup-comm/yup-comm-client.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/testing/yup-comm/yup-comm-client.c b/testing/yup-comm/yup-comm-client.c
new file mode 100644
index 0000000..d6cc8e4
--- /dev/null
+++ b/testing/yup-comm/yup-comm-client.c
@@ -0,0 +1,80 @@
+/* Author: Jan Sucan */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+
+#include "mailslot.h"
+
+#define MAILSLOT_SERVER_NAME "\\\\.\\Mailslot\\yup-comm-server"
+#define MAILSLOT_CLIENT_NAME "\\\\.\\Mailslot\\yup-comm-client"
+#define MAILSLOT_MAX_MSG_SIZE 400
+
+HANDLE mailslot_server = INVALID_HANDLE_VALUE;
+HANDLE mailslot_client = INVALID_HANDLE_VALUE;
+
+void
+print_msg(FILE * stream, const char * format, ...)
+{
+ char buf[1024];
+
+ va_list args;
+ va_start(args, format);
+ vsnprintf(buf, 1024, format, args);
+ va_end(args);
+
+ fprintf(stream, " yup-comm-client: %s%s", (stream == stderr) ? "ERROR: " : "" , buf);
+ fflush(stream);
+}
+
+void
+clean_exit(int code)
+{
+ if (mailslot_server != INVALID_HANDLE_VALUE) {
+ CloseHandle(mailslot_server);
+ }
+
+ exit(code);
+}
+
+void
+usage(void)
+{
+ printf("Usage: yup-comm-client CMD\n");
+ printf(" CMD command for yup-comm-server\n");
+ clean_exit(1);
+}
+
+int
+main(int argc, char ** argv)
+{
+ if (argc != 2) {
+ print_msg(stderr, "Missing argument\n");
+ usage();
+ } if (strlen(argv[1]) > MAILSLOT_MAX_MSG_SIZE) {
+ print_msg(stderr, "The command is too long (> 400 characters)\n");
+ clean_exit(1);
+ }
+
+ if ((mailslot_client = mailslot_create(MAILSLOT_CLIENT_NAME)) == INVALID_HANDLE_VALUE) {
+ print_msg(stderr, "Cannot create mailslot for receiving reply from the server\n");
+ clean_exit(1);
+ }
+
+ if ((mailslot_server = mailslot_connect(MAILSLOT_SERVER_NAME)) == INVALID_HANDLE_VALUE) {
+ print_msg(stderr, "Cannot open server's mailslot\n");
+ clean_exit(1);
+ }
+
+ if (mailslot_write(mailslot_server, argv[1], strlen(argv[1]) + 1U) != 0) {
+ print_msg(stderr, "Cannot send command to the server\n");
+ clean_exit(1);
+ }
+
+ print_msg(stdout, "Waiting for reply from the server ...\n");
+
+ char c;
+ mailslot_read(mailslot_client, &c, sizeof(c));
+
+ clean_exit(0);
+}