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

118 lines
2.9 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/msg.h"
#include "../../core/error.h"
#include "../../core/communication.h"
#define MSG "coucou"
#define SERVICE_NAME "pongd"
void non_interactive (int argc, char *argv[], char *env[])
{
struct msg m;
memset (&m, 0, sizeof (struct msg));
struct service srv;
memset (&srv, 0, sizeof (struct service));
// index and version should be filled
srv.index = 0;
srv.version = 0;
// init service
2018-10-03 21:24:20 +02:00
if (ipc_application_connection (argc, argv, env, &srv, SERVICE_NAME, NULL, 0) < 0) {
handle_err("main", "server_init < 0");
2016-12-23 01:33:52 +01:00
exit (EXIT_FAILURE);
}
printf ("msg to send: %s\n", MSG);
2018-10-03 21:52:11 +02:00
ipc_message_format_data (&m, MSG, strlen(MSG) +1);
2016-12-23 01:33:52 +01:00
printf ("msg to send in the client: ");
2018-10-03 21:52:11 +02:00
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-03 21:52:11 +02:00
ipc_message_free (&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);
}
printf ("msg recv: %s\n", m.val);
2018-10-03 21:52:11 +02:00
ipc_message_free (&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 (int argc, char *argv[], char *env[])
{
struct msg m;
memset (&m, 0, sizeof (struct msg));
struct service srv;
memset (&srv, 0, sizeof (struct service));
char buf[BUFSIZ];
memset (buf, 0, BUFSIZ);
int n;
// index and version should be filled
srv.index = 0;
srv.version = 0;
// init service
2018-10-03 21:24:20 +02:00
if (ipc_application_connection (argc, argv, env, &srv, SERVICE_NAME, NULL, 0) < 0) {
handle_err ("main", "server_init < 0");
2016-12-23 01:33:52 +01:00
exit (EXIT_FAILURE);
}
while (1) {
printf ("msg to send: ");
fflush (stdout);
n = read (0, buf, BUFSIZ);
if (n == 0 || strncmp (buf, "exit", 4) == 0)
break;
2018-10-03 21:52:11 +02:00
ipc_message_format_data (&m, buf, strlen(buf) +1);
2016-12-23 01:33:52 +01:00
memset (buf, 0, BUFSIZ);
// 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-03 21:52:11 +02:00
ipc_message_free (&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);
}
printf ("msg recv: %s", m.val);
2018-10-03 21:52:11 +02:00
ipc_message_free (&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);
}
}
int main (int argc, char *argv[], char *env[])
{
if (argc == 1)
non_interactive (argc, argv, env);
else
interactive (argc, argv, env);
return EXIT_SUCCESS;
}