2019-07-27 15:48:56 +02:00
|
|
|
#include "../src/ipc.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define SERVICE_NAME "pong"
|
2022-02-04 00:53:22 +01:00
|
|
|
struct ipc_ctx *pctx = NULL;
|
|
|
|
|
|
|
|
void exit_program(int signal)
|
|
|
|
{
|
|
|
|
printf("Quitting, signal: %d\n", signal);
|
|
|
|
|
|
|
|
// the application will shut down, and close the service
|
|
|
|
TEST_IPC_Q(ipc_close_all (pctx), EXIT_FAILURE);
|
|
|
|
|
|
|
|
// free remaining ctx
|
|
|
|
ipc_ctx_free (pctx);
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
2019-07-27 15:48:56 +02:00
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
int main_loop(int argc, char * argv[])
|
2019-07-27 15:48:56 +02:00
|
|
|
{
|
2020-01-01 12:11:34 +01:00
|
|
|
argc = argc;
|
|
|
|
argv = argv;
|
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
SECURE_DECLARATION (struct ipc_ctx, ctx);
|
2022-02-04 00:53:22 +01:00
|
|
|
pctx = &ctx;
|
2020-07-13 14:12:08 +02:00
|
|
|
int timer = 10000; // in ms
|
2019-07-27 15:48:56 +02:00
|
|
|
|
|
|
|
printf ("func 03 - server init...\n");
|
2020-07-13 14:12:08 +02:00
|
|
|
TEST_IPC_Q (ipc_server_init (&ctx, SERVICE_NAME), EXIT_FAILURE);
|
2019-07-27 15:48:56 +02:00
|
|
|
printf ("func 03 - server init ok\n");
|
|
|
|
|
|
|
|
SECURE_DECLARATION (struct ipc_event, event);
|
|
|
|
|
|
|
|
printf ("func 01 - service polling...\n");
|
|
|
|
// listen only for a single client
|
|
|
|
while (1) {
|
2022-02-04 03:22:01 +01:00
|
|
|
// TEST_IPC_WAIT_EVENT_Q (ipc_wait_event (&ctx, &event, &timer), EXIT_FAILURE);
|
|
|
|
struct ipc_error ret = ipc_wait_event (&ctx, &event, &timer);
|
|
|
|
|
|
|
|
if (ret.error_code != IPC_ERROR_NONE &&
|
|
|
|
ret.error_code != IPC_ERROR_CLOSED_RECIPIENT) {
|
|
|
|
printf ("An error happened :(\n");
|
|
|
|
}
|
2019-07-27 15:48:56 +02:00
|
|
|
|
|
|
|
switch (event.type) {
|
2020-01-01 12:11:34 +01:00
|
|
|
case IPC_EVENT_TYPE_TIMER : {
|
2020-07-13 14:12:08 +02:00
|
|
|
fprintf (stderr, "time up!\n");
|
|
|
|
timer = 10000;
|
2020-01-01 12:11:34 +01:00
|
|
|
}
|
|
|
|
break;
|
2019-07-27 15:48:56 +02:00
|
|
|
case IPC_EVENT_TYPE_CONNECTION : {
|
2020-07-13 14:12:08 +02:00
|
|
|
printf ("connection establishment: %d \n", event.origin);
|
2019-07-27 15:48:56 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_DISCONNECTION : {
|
2020-07-13 14:12:08 +02:00
|
|
|
printf ("client %d disconnecting\n", event.origin);
|
2019-07-27 15:48:56 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_MESSAGE : {
|
2022-02-04 00:53:22 +01:00
|
|
|
struct ipc_message *m = (struct ipc_message*) event.m;
|
|
|
|
printf ("received message (%d bytes): %.*s\n"
|
|
|
|
, m->length
|
|
|
|
, m->length
|
|
|
|
, m->payload);
|
|
|
|
ipc_write (&ctx, m);
|
2020-07-13 14:12:08 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_TX : {
|
|
|
|
printf ("message sent to fd %d\n", event.origin);
|
2019-07-27 15:48:56 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_NOT_SET :
|
|
|
|
case IPC_EVENT_TYPE_ERROR :
|
|
|
|
case IPC_EVENT_TYPE_EXTRA_SOCKET :
|
|
|
|
default :
|
|
|
|
printf ("not ok - should not happen\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("func 03 - closing server...\n");
|
2020-07-13 14:12:08 +02:00
|
|
|
TEST_IPC_Q (ipc_close_all(&ctx), EXIT_FAILURE);
|
|
|
|
|
|
|
|
printf ("func 03 - freeing the context\n");
|
|
|
|
ipc_ctx_free (&ctx);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
int main(int argc, char * argv[])
|
2019-07-27 15:48:56 +02:00
|
|
|
{
|
2022-02-04 00:53:22 +01:00
|
|
|
// In case we want to quit the program, do it cleanly.
|
2019-07-27 15:48:56 +02:00
|
|
|
signal (SIGHUP, exit_program);
|
|
|
|
signal (SIGALRM, exit_program);
|
|
|
|
signal (SIGUSR1, exit_program);
|
|
|
|
signal (SIGUSR2, exit_program);
|
|
|
|
signal (SIGTERM, exit_program);
|
|
|
|
signal (SIGINT, exit_program);
|
2022-02-04 00:53:22 +01:00
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
main_loop (argc, argv);
|
2019-07-27 15:48:56 +02:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|