2016-12-23 01:33:52 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-10-28 18:12:17 +01:00
|
|
|
#include "../../core/ipc.h"
|
2016-12-23 01:33:52 +01:00
|
|
|
|
|
|
|
#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-28 18:12:17 +01:00
|
|
|
int ret = 0;
|
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-28 18:12:17 +01:00
|
|
|
struct ipc_event event;
|
|
|
|
memset (&event, 0, sizeof (struct ipc_event));
|
|
|
|
|
|
|
|
struct ipc_services services;
|
|
|
|
memset (&services, 0, sizeof (struct ipc_services));
|
|
|
|
ipc_service_add (&services, &srv);
|
|
|
|
|
2016-12-23 01:33:52 +01:00
|
|
|
while (1) {
|
|
|
|
printf ("msg to send: ");
|
|
|
|
fflush (stdout);
|
2018-10-28 18:12:17 +01:00
|
|
|
ret = ipc_application_loop_interactive (&services, &event);
|
2016-12-23 01:33:52 +01:00
|
|
|
|
2018-10-28 18:12:17 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
handle_error("ipc_application_loop != 0");
|
|
|
|
exit (EXIT_FAILURE);
|
2018-10-04 22:51:31 +02:00
|
|
|
}
|
|
|
|
|
2018-10-28 18:12:17 +01:00
|
|
|
switch (event.type) {
|
|
|
|
case IPC_EVENT_TYPE_STDIN:
|
|
|
|
{
|
|
|
|
struct ipc_message *m = event.m;
|
|
|
|
if ( m->length == 0 || strncmp (m->payload, "exit", 4) == 0) {
|
|
|
|
|
|
|
|
ipc_message_empty (m);
|
|
|
|
free (m);
|
|
|
|
|
|
|
|
ipc_services_free (&services);
|
|
|
|
|
|
|
|
if (ipc_application_close (&srv) < 0) {
|
|
|
|
handle_err("main", "application_close < 0");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ipc_application_write (&srv, m) < 0) {
|
|
|
|
handle_err("main", "application_write < 0");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_MESSAGE:
|
|
|
|
{
|
|
|
|
struct ipc_message *m = event.m;
|
|
|
|
printf ("msg recv: %.*s", m->length, m->payload);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_DISCONNECTION:
|
|
|
|
case IPC_EVENT_TYPE_NOT_SET:
|
|
|
|
case IPC_EVENT_TYPE_CONNECTION:
|
|
|
|
case IPC_EVENT_TYPE_ERROR:
|
|
|
|
default :
|
|
|
|
fprintf (stderr, "should not happen, event type %d\n", event.type);
|
|
|
|
}
|
2016-12-23 01:33:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|