2016-12-23 01:33:52 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-10-09 13:20:26 +02:00
|
|
|
#include "../../core/message.h"
|
2016-12-23 01:33:52 +01:00
|
|
|
#include "../../core/error.h"
|
|
|
|
#include "../../core/communication.h"
|
|
|
|
|
|
|
|
#define MSG "coucou"
|
|
|
|
#define SERVICE_NAME "pongd"
|
|
|
|
|
2018-10-09 13:16:40 +02:00
|
|
|
void non_interactive (char *env[])
|
2016-12-23 01:33:52 +01:00
|
|
|
{
|
2018-10-03 22:02:37 +02:00
|
|
|
struct ipc_message m;
|
|
|
|
memset (&m, 0, sizeof (struct ipc_message));
|
2018-10-04 00:30:47 +02:00
|
|
|
struct ipc_service srv;
|
|
|
|
memset (&srv, 0, sizeof (struct ipc_service));
|
2016-12-23 01:33:52 +01:00
|
|
|
|
|
|
|
// index and version should be filled
|
|
|
|
srv.index = 0;
|
|
|
|
srv.version = 0;
|
|
|
|
|
|
|
|
// init service
|
2018-10-09 13:16:40 +02:00
|
|
|
if (ipc_application_connection (env, &srv, SERVICE_NAME) < 0) {
|
|
|
|
handle_err("main", "ipc_application_connection < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2018-10-12 18:06:16 +02:00
|
|
|
printf ("msg to send: %.*s\n", (int) strlen(MSG), MSG);
|
2018-10-03 21:52:11 +02:00
|
|
|
ipc_message_format_data (&m, MSG, strlen(MSG) +1);
|
2018-10-12 18:06:16 +02:00
|
|
|
// printf ("msg to send in the client: ");
|
|
|
|
// ipc_message_print (&m);
|
2018-10-03 21:24:20 +02:00
|
|
|
if (ipc_application_write (&srv, &m) < 0) {
|
|
|
|
handle_err("main", "application_write < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2018-10-08 16:15:35 +02:00
|
|
|
ipc_message_empty (&m);
|
2016-12-23 01:33:52 +01:00
|
|
|
|
2018-10-03 21:24:20 +02:00
|
|
|
if (ipc_application_read (&srv, &m) < 0) {
|
|
|
|
handle_err("main", "application_read < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
printf ("msg recv: %s\n", m.payload);
|
2018-10-08 16:15:35 +02:00
|
|
|
ipc_message_empty (&m);
|
2016-12-23 01:33:52 +01:00
|
|
|
|
2018-10-03 21:24:20 +02:00
|
|
|
if (ipc_application_close (&srv) < 0) {
|
|
|
|
handle_err("main", "application_close < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 13:16:40 +02:00
|
|
|
void interactive (char *env[])
|
2016-12-23 01:33:52 +01:00
|
|
|
{
|
2018-10-03 22:02:37 +02:00
|
|
|
struct ipc_message m;
|
|
|
|
memset (&m, 0, sizeof (struct ipc_message));
|
2018-10-04 00:30:47 +02:00
|
|
|
struct ipc_service srv;
|
|
|
|
memset (&srv, 0, sizeof (struct ipc_service));
|
2016-12-23 01:33:52 +01:00
|
|
|
|
2018-10-12 18:06:16 +02:00
|
|
|
char buf[IPC_MAX_MESSAGE_SIZE];
|
|
|
|
memset (buf, 0, IPC_MAX_MESSAGE_SIZE);
|
2016-12-23 01:33:52 +01:00
|
|
|
int n;
|
|
|
|
|
2018-10-04 22:51:31 +02:00
|
|
|
int ask_server_to_quit = 0;
|
|
|
|
|
2016-12-23 01:33:52 +01:00
|
|
|
// index and version should be filled
|
|
|
|
srv.index = 0;
|
|
|
|
srv.version = 0;
|
|
|
|
|
|
|
|
// init service
|
2018-10-09 13:16:40 +02:00
|
|
|
if (ipc_application_connection (env, &srv, SERVICE_NAME) < 0) {
|
|
|
|
handle_err ("main", "ipc_application_connection < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
printf ("msg to send: ");
|
|
|
|
fflush (stdout);
|
2018-10-12 18:06:16 +02:00
|
|
|
n = read (0, buf, IPC_MAX_MESSAGE_SIZE);
|
2016-12-23 01:33:52 +01:00
|
|
|
|
|
|
|
if (n == 0 || strncmp (buf, "exit", 4) == 0)
|
|
|
|
break;
|
|
|
|
|
2018-10-04 22:51:31 +02:00
|
|
|
if (strncmp(buf, "close server", 12) == 0) {
|
|
|
|
ask_server_to_quit = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-10-03 21:52:11 +02:00
|
|
|
ipc_message_format_data (&m, buf, strlen(buf) +1);
|
2018-10-12 18:06:16 +02:00
|
|
|
memset (buf, 0, IPC_MAX_MESSAGE_SIZE);
|
2016-12-23 01:33:52 +01:00
|
|
|
|
|
|
|
// print_msg (&m);
|
|
|
|
|
2018-10-03 21:24:20 +02:00
|
|
|
if (ipc_application_write (&srv, &m) < 0) {
|
|
|
|
handle_err("main", "application_write < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2018-10-08 16:15:35 +02:00
|
|
|
ipc_message_empty (&m);
|
2016-12-23 01:33:52 +01:00
|
|
|
|
2018-10-03 21:24:20 +02:00
|
|
|
if (ipc_application_read (&srv, &m) < 0) {
|
|
|
|
handle_err("main", "application_read < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2018-10-04 01:54:12 +02:00
|
|
|
printf ("msg recv: %s", m.payload);
|
2018-10-08 16:15:35 +02:00
|
|
|
ipc_message_empty (&m);
|
2016-12-23 01:33:52 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 22:51:31 +02:00
|
|
|
if (ask_server_to_quit) {
|
|
|
|
ipc_message_format_server_close (&m);
|
|
|
|
|
|
|
|
if (ipc_application_write (&srv, &m) < 0) {
|
|
|
|
handle_err("main", "application_write < 0");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2018-10-08 16:15:35 +02:00
|
|
|
ipc_message_empty (&m);
|
2018-10-04 22:51:31 +02:00
|
|
|
} else if (ipc_application_close (&srv) < 0) {
|
2018-10-03 21:24:20 +02:00
|
|
|
handle_err("main", "application_close < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char *argv[], char *env[])
|
|
|
|
{
|
2018-10-09 13:16:40 +02:00
|
|
|
argc = argc; // warnings
|
|
|
|
argv = argv; // warnings
|
|
|
|
|
2016-12-23 01:33:52 +01:00
|
|
|
if (argc == 1)
|
2018-10-09 13:16:40 +02:00
|
|
|
non_interactive (env);
|
2016-12-23 01:33:52 +01:00
|
|
|
else
|
2018-10-09 13:16:40 +02:00
|
|
|
interactive (env);
|
2016-12-23 01:33:52 +01:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|