2019-06-03 21:25:59 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "../src/ipc.h"
|
|
|
|
|
|
|
|
#define MSG "coucou"
|
2019-06-12 15:02:43 +02:00
|
|
|
#define SERVICE_NAME "pong"
|
2019-06-03 21:25:59 +02:00
|
|
|
|
|
|
|
#define PRINTERR(ret,msg) {\
|
2020-01-01 12:11:34 +01:00
|
|
|
const char * err = ipc_errors_get (ret.error_code);\
|
2019-06-03 21:25:59 +02:00
|
|
|
fprintf(stderr, "error while %s: %s\n", msg, err);\
|
|
|
|
}
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
void chomp (char *str, ssize_t len)
|
|
|
|
{
|
|
|
|
if (str[len - 1] == '\n') {
|
|
|
|
str[len - 1] = '\0';
|
2019-07-27 15:48:10 +02:00
|
|
|
}
|
2020-01-01 12:11:34 +01:00
|
|
|
if (str[len - 2] == '\n') {
|
|
|
|
str[len - 2] = '\0';
|
2019-07-27 15:48:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 15:02:43 +02:00
|
|
|
struct ipc_connection_info *srv;
|
|
|
|
|
2020-01-28 13:39:06 +01:00
|
|
|
void non_interactive (int verbosity, size_t nb_msg, char *msg_str, char *env[])
|
2019-06-03 21:25:59 +02:00
|
|
|
{
|
2020-01-01 12:11:34 +01:00
|
|
|
SECURE_DECLARATION (struct ipc_message, m);
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
// init service
|
|
|
|
TEST_IPC_QUIT_ON_ERROR (ipc_connection (env, srv, SERVICE_NAME), EXIT_FAILURE);
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2020-01-28 13:39:06 +01:00
|
|
|
if (verbosity > 1) {
|
|
|
|
printf ("msg to send (%ld): %.*s\n", (ssize_t) strlen (MSG) + 1, (int)strlen (MSG), MSG);
|
|
|
|
}
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2020-01-28 13:39:06 +01:00
|
|
|
for (size_t i = 0 ; i < nb_msg ; i++) {
|
|
|
|
TEST_IPC_QUIT_ON_ERROR (ipc_message_format_data (&m, 42, msg_str, (ssize_t) strlen (msg_str) + 1), EXIT_FAILURE);
|
|
|
|
TEST_IPC_QUIT_ON_ERROR (ipc_write (srv, &m), EXIT_FAILURE);
|
|
|
|
ipc_message_empty (&m);
|
|
|
|
TEST_IPC_QUIT_ON_ERROR (ipc_read (srv, &m), EXIT_FAILURE);
|
|
|
|
|
|
|
|
if (verbosity > 1) {
|
|
|
|
printf ("msg recv (type: %u): %s\n", m.user_type, m.payload);
|
|
|
|
}
|
|
|
|
ipc_message_empty (&m);
|
|
|
|
}
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
TEST_IPC_QUIT_ON_ERROR (ipc_close (srv), EXIT_FAILURE);
|
2019-06-03 21:25:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void interactive (char *env[])
|
|
|
|
{
|
2020-01-01 12:11:34 +01:00
|
|
|
// init service
|
|
|
|
TEST_IPC_QUIT_ON_ERROR (ipc_connection (env, srv, SERVICE_NAME), EXIT_FAILURE);
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
SECURE_DECLARATION (struct ipc_error, ret);
|
2019-07-27 15:48:10 +02:00
|
|
|
SECURE_DECLARATION (struct ipc_event, event);
|
2020-06-27 19:16:07 +02:00
|
|
|
SECURE_DECLARATION (struct ipc_ctx, services);
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2019-06-12 15:02:43 +02:00
|
|
|
ipc_add (&services, srv);
|
2020-01-01 12:11:34 +01:00
|
|
|
ipc_add_fd (&services, 0); // add STDIN
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
ipc_connections_print (&services);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
long timer = 10;
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
while (1) {
|
|
|
|
printf ("msg to send: ");
|
|
|
|
fflush (stdout);
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
TEST_IPC_WAIT_EVENT_Q (ipc_wait_event (&services, NULL, &event, &timer), EXIT_FAILURE);
|
2019-06-03 21:25:59 +02:00
|
|
|
switch (event.type) {
|
2020-01-01 12:11:34 +01:00
|
|
|
case IPC_EVENT_TYPE_EXTRA_SOCKET:
|
|
|
|
{
|
|
|
|
// structure not read, should read the message here
|
|
|
|
ssize_t len;
|
|
|
|
char buf[4096];
|
|
|
|
memset (buf, 0, 4096);
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
len = read (event.origin->fd, buf, 4096);
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
buf[len - 1] = '\0';
|
|
|
|
chomp (buf, len);
|
2019-07-27 15:48:10 +02:00
|
|
|
|
|
|
|
#if 0
|
2020-01-01 12:11:34 +01:00
|
|
|
printf ("\n");
|
|
|
|
printf ("message to send: %.*s\n", (int)len, buf);
|
2019-07-27 15:48:10 +02:00
|
|
|
#endif
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
// in case we want to quit the program
|
|
|
|
if (len == 0 || strncmp (buf, "quit", 4) == 0 || strncmp (buf, "exit", 4) == 0) {
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
struct ipc_error ret = ipc_close (srv);
|
|
|
|
if (ret.error_code != IPC_ERROR_NONE) {
|
|
|
|
fprintf (stderr, "%s", ret.error_message);
|
|
|
|
exit (EXIT_FAILURE);
|
2019-06-03 21:25:59 +02:00
|
|
|
}
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
ipc_connections_free (&services);
|
2019-07-27 15:48:10 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
// send the message read on STDIN
|
|
|
|
struct ipc_message *m = NULL;
|
|
|
|
SECURE_BUFFER_HEAP_ALLOCATION (m, sizeof (struct ipc_message),, return; );
|
|
|
|
|
|
|
|
struct ipc_error ret = ipc_message_format_data (m, 42, buf, len);
|
|
|
|
if (ret.error_code != IPC_ERROR_NONE) {
|
|
|
|
fprintf (stderr, "%s", ret.error_message);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2019-07-27 15:48:10 +02:00
|
|
|
#if 0
|
2020-01-01 12:11:34 +01:00
|
|
|
printf ("\n");
|
|
|
|
printf ("right before sending a message\n");
|
2019-07-27 15:48:10 +02:00
|
|
|
#endif
|
2020-01-01 12:11:34 +01:00
|
|
|
ret = ipc_write (srv, m);
|
|
|
|
if (ret.error_code != IPC_ERROR_NONE) {
|
|
|
|
fprintf (stderr, "%s", ret.error_message);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2019-07-27 15:48:10 +02:00
|
|
|
#if 0
|
2020-01-01 12:11:34 +01:00
|
|
|
printf ("right after sending a message\n");
|
2019-07-27 15:48:10 +02:00
|
|
|
#endif
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
ipc_message_empty (m);
|
|
|
|
free (m);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_MESSAGE:
|
|
|
|
{
|
|
|
|
struct ipc_message *m = event.m;
|
|
|
|
printf ("\rmsg recv: %.*s\n", 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);
|
2019-06-03 21:25:59 +02:00
|
|
|
}
|
2020-01-01 12:11:34 +01:00
|
|
|
}
|
2019-06-03 21:25:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char *argv[], char *env[])
|
|
|
|
{
|
2020-01-28 13:39:06 +01:00
|
|
|
printf("usage: %s [verbosity #messages message]", argv[0]);
|
|
|
|
|
|
|
|
// $0:
|
2020-01-01 12:11:34 +01:00
|
|
|
argv = argv; // warnings
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2019-06-12 15:02:43 +02:00
|
|
|
srv = malloc (sizeof (struct ipc_connection_info));
|
2020-01-01 12:11:34 +01:00
|
|
|
memset (srv, 0, sizeof (struct ipc_connection_info));
|
|
|
|
|
2020-01-28 13:39:06 +01:00
|
|
|
if (argc == 4)
|
|
|
|
non_interactive (atoi(argv[1]), (size_t) atoi(argv[2]), argv[3], env);
|
2020-01-01 12:11:34 +01:00
|
|
|
else
|
|
|
|
interactive (env);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2019-06-03 21:25:59 +02:00
|
|
|
}
|