2019-07-27 15:48:56 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "../src/ipc.h"
|
|
|
|
|
|
|
|
#define MSG "coucou"
|
|
|
|
#define SERVICE_NAME "pong"
|
|
|
|
|
|
|
|
#define PRINTERR(ret,msg) {\
|
2020-01-01 12:11:34 +01:00
|
|
|
const char * err = ipc_errors_get (ret.error_code);\
|
2019-07-27 15:48:56 +02:00
|
|
|
fprintf(stderr, "error while %s: %s\n", msg, err);\
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:07:19 +02:00
|
|
|
void non_interactive ()
|
2019-07-27 15:48:56 +02:00
|
|
|
{
|
2020-06-29 21:07:19 +02:00
|
|
|
SECURE_DECLARATION(struct ipc_message, m);
|
|
|
|
SECURE_DECLARATION(struct ipc_ctx, ctx);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
|
|
|
// init service
|
2020-06-29 21:07:19 +02:00
|
|
|
TEST_IPC_Q(ipc_connection (&ctx, SERVICE_NAME), EXIT_FAILURE);
|
|
|
|
|
|
|
|
int server_fd = ctx.pollfd[0].fd;
|
2019-07-27 15:48:56 +02:00
|
|
|
|
2020-06-30 12:59:05 +02:00
|
|
|
printf ("msg for fd %d to send (%ld): %.*s\n"
|
|
|
|
, server_fd
|
|
|
|
, (ssize_t) strlen(MSG) +1, (int) strlen(MSG), MSG);
|
2020-01-01 12:11:34 +01:00
|
|
|
TEST_IPC_Q(ipc_message_format_data (&m, /* type */ 'a', MSG, (ssize_t) strlen(MSG) +1), EXIT_FAILURE);
|
2020-06-29 21:07:19 +02:00
|
|
|
|
|
|
|
m.fd = server_fd;
|
2020-06-30 12:59:05 +02:00
|
|
|
TEST_IPC_Q(ipc_write_fd (server_fd, &m), EXIT_FAILURE);
|
2020-06-29 21:07:19 +02:00
|
|
|
TEST_IPC_Q(ipc_read (&ctx, 0 /* only one option */, &m), EXIT_FAILURE);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
|
|
|
printf ("msg recv: %s\n", m.payload);
|
|
|
|
ipc_message_empty (&m);
|
|
|
|
|
2020-06-29 21:07:19 +02:00
|
|
|
TEST_IPC_Q(ipc_close_all (&ctx), EXIT_FAILURE);
|
2020-07-01 09:53:24 +02:00
|
|
|
ipc_ctx_free (&ctx);
|
2019-07-27 15:48:56 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 21:07:19 +02:00
|
|
|
#if 0
|
|
|
|
void interactive ()
|
2019-07-27 15:48:56 +02:00
|
|
|
{
|
2020-06-29 21:07:19 +02:00
|
|
|
SECURE_DECLARATION(struct ipc_ctx, ctx);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
|
|
|
// init service
|
2020-06-29 21:07:19 +02:00
|
|
|
TEST_IPC_Q(ipc_connection (&ctx, SERVICE_NAME), EXIT_FAILURE);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
2020-06-29 21:07:19 +02:00
|
|
|
int server_fd = ctx.pollfd[0].fd;
|
2019-07-27 15:48:56 +02:00
|
|
|
|
2020-06-29 21:07:19 +02:00
|
|
|
SECURE_DECLARATION(struct ipc_event, event);
|
2020-01-01 12:11:34 +01:00
|
|
|
|
|
|
|
long timer = 10;
|
2019-07-27 15:48:56 +02:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
printf ("msg to send: ");
|
|
|
|
fflush (stdout);
|
2020-07-06 08:43:06 +02:00
|
|
|
TEST_IPC_Q(ipc_wait_event (&ctx, &event, &timer), EXIT_FAILURE);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
|
|
|
switch (event.type) {
|
2020-01-01 12:11:34 +01:00
|
|
|
case IPC_EVENT_TYPE_TIMER: {
|
|
|
|
printf("time up!\n");
|
|
|
|
|
|
|
|
timer = 10;
|
|
|
|
};
|
|
|
|
break;
|
2019-07-27 15:48:56 +02:00
|
|
|
case IPC_EVENT_TYPE_EXTRA_SOCKET:
|
|
|
|
{
|
|
|
|
struct ipc_message *m = event.m;
|
|
|
|
if ( m->length == 0 || strncmp (m->payload, "exit", 4) == 0) {
|
|
|
|
|
|
|
|
ipc_message_empty (m);
|
|
|
|
free (m);
|
|
|
|
|
2020-06-29 21:07:19 +02:00
|
|
|
TEST_IPC_Q(ipc_close_all (&ctx), EXIT_FAILURE);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
2020-07-01 12:47:04 +02:00
|
|
|
ipc_ctx_free (&ctx);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:07:19 +02:00
|
|
|
m->fd = server_fd;
|
|
|
|
|
|
|
|
TEST_IPC_Q(ipc_write (&ctx, m), EXIT_FAILURE);
|
2019-07-27 15:48:56 +02:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 21:07:19 +02:00
|
|
|
#endif
|
2019-07-27 15:48:56 +02:00
|
|
|
|
2020-06-29 21:07:19 +02:00
|
|
|
//int main (int argc, char *argv[])
|
|
|
|
int main (void)
|
2019-07-27 15:48:56 +02:00
|
|
|
{
|
2020-06-29 21:07:19 +02:00
|
|
|
non_interactive ();
|
2019-07-27 15:48:56 +02:00
|
|
|
|
2020-06-29 21:07:19 +02:00
|
|
|
#if 0
|
2019-07-27 15:48:56 +02:00
|
|
|
if (argc == 1)
|
2020-06-29 21:07:19 +02:00
|
|
|
non_interactive ();
|
2019-07-27 15:48:56 +02:00
|
|
|
else
|
2020-06-29 21:07:19 +02:00
|
|
|
interactive ();
|
|
|
|
#endif
|
2019-07-27 15:48:56 +02:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|