2018-10-11 01:09:21 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
|
|
|
|
|
|
|
#include "../../core/communication.h"
|
|
|
|
#include "../../core/error.h"
|
|
|
|
|
|
|
|
#define SERVICE_NAME "pongd"
|
|
|
|
|
2018-10-12 01:59:56 +02:00
|
|
|
#define MAX_MESSAGE_SIZE 20000
|
2018-10-12 18:06:16 +02:00
|
|
|
#define MESSAGE "salut ça va ?"
|
2018-10-12 01:59:56 +02:00
|
|
|
|
2018-10-11 01:09:21 +02:00
|
|
|
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) {
|
2018-10-12 01:59:56 +02:00
|
|
|
#if 0 // version with readline, slow, valgrind-incompatible
|
|
|
|
|
2018-10-11 01:09:21 +02:00
|
|
|
char * mtype_str = readline("msg type: ");
|
2018-10-12 01:59:56 +02:00
|
|
|
if (strlen(mtype_str) == 0 || strncmp (mtype_str, "exit", 4) == 0) {
|
|
|
|
free (mtype_str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
msg_type = atoi(mtype_str);
|
2018-10-11 01:09:21 +02:00
|
|
|
free(mtype_str);
|
|
|
|
|
|
|
|
char * buf = readline ("msg: ");
|
2018-10-12 01:59:56 +02:00
|
|
|
if (strlen(buf) == 0 || strncmp (buf, "exit", 4) == 0) {
|
|
|
|
free (buf);
|
2018-10-11 01:09:21 +02:00
|
|
|
break;
|
2018-10-12 01:59:56 +02:00
|
|
|
}
|
2018-10-11 01:09:21 +02:00
|
|
|
|
|
|
|
ipc_message_format (&m, msg_type, buf, strlen(buf));
|
2018-10-12 01:59:56 +02:00
|
|
|
free (buf);
|
2018-10-11 01:09:21 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2018-10-12 01:59:56 +02:00
|
|
|
printf ("\nmsg recv: %s", m.payload);
|
|
|
|
ipc_message_empty (&m);
|
|
|
|
#else
|
|
|
|
|
|
|
|
char mtype_str[50];
|
|
|
|
memset(mtype_str, 0, 50);
|
|
|
|
printf ("message type: ");
|
|
|
|
fflush(stdout);
|
|
|
|
read(0, mtype_str, 50);
|
|
|
|
msg_type = atoi(mtype_str);
|
|
|
|
memset(mtype_str, 0, 50);
|
|
|
|
|
|
|
|
char mpayload[MAX_MESSAGE_SIZE];
|
|
|
|
memset(mpayload, 0, MAX_MESSAGE_SIZE);
|
|
|
|
printf ("message payload: ");
|
|
|
|
fflush(stdout);
|
|
|
|
read(0, mpayload, MAX_MESSAGE_SIZE);
|
|
|
|
if (strlen(mpayload) == 0 || strncmp (mpayload, "exit", 4) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc_message_format (&m, msg_type, mpayload, strlen(mpayload));
|
|
|
|
// print_msg (&m);
|
|
|
|
|
|
|
|
if (ipc_application_write (&srv, &m) < 0) {
|
|
|
|
handle_err("main", "application_write < 0");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2018-10-11 01:09:21 +02:00
|
|
|
ipc_message_empty (&m);
|
2018-10-12 01:59:56 +02:00
|
|
|
|
|
|
|
if (msg_type == 0) { // message type 0 => close the server, no read to do
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ipc_application_read (&srv, &m) < 0) {
|
|
|
|
handle_err("main", "application_read < 0");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m.length > 0) {
|
|
|
|
printf ("msg recv: %.*s", m.length, m.payload);
|
|
|
|
}
|
|
|
|
ipc_message_empty (&m);
|
|
|
|
#endif
|
2018-10-11 01:09:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ipc_application_close (&srv) < 0) {
|
|
|
|
handle_err("main", "application_close < 0");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2018-10-12 01:59:56 +02:00
|
|
|
ipc_message_empty (&m);
|
2018-10-11 01:09:21 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 18:06:16 +02:00
|
|
|
void non_interactive (char msg_type, char *msg, 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc_message_format (&m, msg_type, msg, strlen(msg) + 1);
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m.length > 0) {
|
|
|
|
printf ("msg recv: %.*s\n", m.length, m.payload);
|
|
|
|
}
|
|
|
|
ipc_message_empty (&m);
|
|
|
|
|
|
|
|
if (ipc_application_close (&srv) < 0) {
|
|
|
|
handle_err("main", "application_close < 0");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
ipc_message_empty (&m);
|
|
|
|
}
|
|
|
|
|
2018-10-11 01:09:21 +02:00
|
|
|
int main (int argc, char *argv[], char *env[])
|
|
|
|
{
|
|
|
|
char service_name[100];
|
2018-10-12 01:59:56 +02:00
|
|
|
memset (service_name, 0, 100);
|
2018-10-11 01:09:21 +02:00
|
|
|
|
|
|
|
if (argc != 1) {
|
|
|
|
ssize_t t = strlen(argv[1]) > 100 ? 100 : strlen(argv[1]);
|
|
|
|
memcpy(service_name, argv[1], t);
|
|
|
|
}
|
2018-10-12 18:06:16 +02:00
|
|
|
else { memcpy(service_name, SERVICE_NAME, strlen(SERVICE_NAME)); }
|
|
|
|
|
|
|
|
char mtype = 2;
|
|
|
|
if (argc > 2) { mtype = atoi(argv[2]); }
|
|
|
|
|
|
|
|
char msg[IPC_MAX_MESSAGE_SIZE];
|
|
|
|
memset(msg, 0, IPC_MAX_MESSAGE_SIZE);
|
|
|
|
|
|
|
|
if (argc > 3) { memcpy(msg, argv[3], strlen(argv[3])); }
|
|
|
|
else { memcpy(msg, MESSAGE, strlen(MESSAGE)); }
|
2018-10-11 01:09:21 +02:00
|
|
|
|
2018-10-12 18:06:16 +02:00
|
|
|
non_interactive (mtype, msg, service_name, env);
|
2018-10-11 01:09:21 +02:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|