From a55e5639c4b52cd6a521c69c8b170574748e000c Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Thu, 11 Oct 2018 01:09:21 +0200
Subject: [PATCH] ipc-debug with readline
---
core/message.h | 1 +
pong/app/Makefile | 4 +--
pong/app/ipc-debug.c | 82 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 85 insertions(+), 2 deletions(-)
create mode 100644 pong/app/ipc-debug.c
diff --git a/core/message.h b/core/message.h
index 190b89d..68651cc 100644
--- a/core/message.h
+++ b/core/message.h
@@ -31,6 +31,7 @@ int ipc_message_read (int fd, struct ipc_message *m);
// write a structure msg to fd
int ipc_message_write (int fd, const struct ipc_message *m);
+int ipc_message_format (struct ipc_message *m, char type, const char *payload, ssize_t length);
int ipc_message_format_data (struct ipc_message *m, const char *payload, ssize_t length);
int ipc_message_format_server_close (struct ipc_message *m);
diff --git a/pong/app/Makefile b/pong/app/Makefile
index 1c7936c..2050902 100644
--- a/pong/app/Makefile
+++ b/pong/app/Makefile
@@ -1,6 +1,6 @@
CC=gcc
CFLAGS=-Wall -g -Wextra
-LDFLAGS= -pthread
+LDFLAGS= -pthread -lreadline
CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
EXEC=$(basename $(wildcard *.c))
SOURCES=$(wildcard ../lib/*.c ../../core/*.c)
@@ -14,7 +14,7 @@ test:
./pongd.bin
$(EXEC): $(OBJECTS) $(CFILES)
- $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@.bin
+ $(CC) $(CFLAGS) $(OBJECTS) $@.c -o $@.bin $(LDFLAGS)
.c.o:
$(CC) -c $(CFLAGS) $< -o $@
diff --git a/pong/app/ipc-debug.c b/pong/app/ipc-debug.c
new file mode 100644
index 0000000..682ae0e
--- /dev/null
+++ b/pong/app/ipc-debug.c
@@ -0,0 +1,82 @@
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include "../../core/communication.h"
+#include "../../core/error.h"
+
+#define SERVICE_NAME "pongd"
+
+void interactive (char * service_name, char *env[])
+{
+ struct ipc_message m;
+ memset (&m, 0, sizeof (struct ipc_message));
+ struct ipc_service srv;
+ memset (&srv, 0, sizeof (struct ipc_service));
+
+ // index and version should be filled
+ srv.index = 0;
+ srv.version = 0;
+
+ // init service
+ if (ipc_application_connection (env, &srv, service_name) < 0) {
+ handle_err ("main", "ipc_application_connection < 0");
+ exit (EXIT_FAILURE);
+ }
+
+ char msg_type = 0;
+
+ while (1) {
+ char * mtype_str = readline("msg type: ");
+ sscanf(mtype_str, "%c", &msg_type);
+ free(mtype_str);
+
+ char * buf = readline ("msg: ");
+ if (strlen(buf) == 0 || strncmp (buf, "exit", 4) == 0)
+ break;
+
+ ipc_message_format (&m, msg_type, buf, strlen(buf));
+ memset (buf, 0, BUFSIZ);
+
+ // print_msg (&m);
+
+ if (ipc_application_write (&srv, &m) < 0) {
+ handle_err("main", "application_write < 0");
+ exit (EXIT_FAILURE);
+ }
+ ipc_message_empty (&m);
+
+ if (ipc_application_read (&srv, &m) < 0) {
+ handle_err("main", "application_read < 0");
+ exit (EXIT_FAILURE);
+ }
+
+ printf ("msg recv: %s", m.payload);
+ ipc_message_empty (&m);
+ }
+
+ if (ipc_application_close (&srv) < 0) {
+ handle_err("main", "application_close < 0");
+ exit (EXIT_FAILURE);
+ }
+}
+
+int main (int argc, char *argv[], char *env[])
+{
+ argc = argc; // warnings
+ argv = argv; // warnings
+
+ char service_name[100];
+
+ if (argc != 1) {
+ ssize_t t = strlen(argv[1]) > 100 ? 100 : strlen(argv[1]);
+ memcpy(service_name, argv[1], t);
+ }
+
+ interactive (service_name, env);
+
+ return EXIT_SUCCESS;
+}