Archived
3
0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues or pull requests.
libipc-old/pong/app/pong.c

138 lines
3.2 KiB
C
Raw Normal View History

2016-12-23 01:33:52 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../../core/ipc.h"
2016-12-23 01:33:52 +01:00
#define MSG "coucou"
#define SERVICE_NAME "pongd"
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
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);
}
}
void interactive (char *env[])
2016-12-23 01:33:52 +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
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);
}
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 19:28:19 +01:00
ret = ipc_application_peek_event (&services, &event);
2016-12-23 01:33:52 +01:00
if (ret != 0) {
2018-10-28 19:28:19 +01:00
handle_error("ipc_application_peek_event != 0");
exit (EXIT_FAILURE);
}
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[])
{
argc = argc; // warnings
argv = argv; // warnings
2016-12-23 01:33:52 +01:00
if (argc == 1)
non_interactive (env);
2016-12-23 01:33:52 +01:00
else
interactive (env);
2016-12-23 01:33:52 +01:00
return EXIT_SUCCESS;
}