2019-07-27 15:48:56 +02:00
|
|
|
#include "../src/ipc.h"
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define PONGD_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);\
|
|
|
|
}
|
|
|
|
|
|
|
|
int cpt = 0;
|
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
struct ipc_ctx *ctx;
|
2019-07-27 15:48:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
void main_loop ()
|
|
|
|
{
|
2020-01-01 12:11:34 +01:00
|
|
|
SECURE_DECLARATION(struct ipc_error, ret);
|
2020-07-13 14:12:08 +02:00
|
|
|
SECURE_DECLARATION(struct ipc_event, event);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
int timer = 10000;
|
2020-01-01 12:11:34 +01:00
|
|
|
|
2019-07-27 15:48:56 +02:00
|
|
|
while(1) {
|
|
|
|
// ipc_wait_event provides one event at a time
|
|
|
|
// warning: event->m is free'ed if not NULL
|
2020-07-13 14:12:08 +02:00
|
|
|
ret = ipc_wait_event (ctx, &event, &timer);
|
2020-01-01 12:11:34 +01:00
|
|
|
if (ret.error_code != IPC_ERROR_NONE && ret.error_code != IPC_ERROR_CLOSED_RECIPIENT) {
|
2019-07-27 15:48:56 +02:00
|
|
|
PRINTERR(ret,"service poll event");
|
|
|
|
|
|
|
|
// the application will shut down, and close the service
|
2020-07-13 14:12:08 +02:00
|
|
|
TEST_IPC_Q(ipc_close_all (ctx), EXIT_FAILURE);
|
2019-07-27 15:48:56 +02:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (event.type) {
|
2020-01-01 12:11:34 +01:00
|
|
|
case IPC_EVENT_TYPE_TIMER: {
|
|
|
|
fprintf(stderr, "time up!\n");
|
2020-07-13 14:12:08 +02:00
|
|
|
timer = 10000;
|
2020-01-01 12:11:34 +01:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_CONNECTION: {
|
2019-07-27 15:48:56 +02:00
|
|
|
cpt++;
|
2020-07-13 14:12:08 +02:00
|
|
|
// printf ("new connection (fd %d): %d ctx connected\n", event.origin, cpt);
|
2019-07-27 15:48:56 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_DISCONNECTION:
|
|
|
|
{
|
|
|
|
cpt--;
|
2020-07-13 14:12:08 +02:00
|
|
|
// printf ("disconnection (fd %d): %d clients remaining\n", event.origin, cpt);
|
2019-07-27 15:48:56 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_MESSAGE:
|
|
|
|
{
|
|
|
|
struct ipc_message *m = event.m;
|
|
|
|
if (m->length > 0) {
|
2020-07-13 14:12:08 +02:00
|
|
|
// printf ("message received (type %d): %.*s\n", m->type, m->length, m->payload);
|
2019-07-27 15:48:56 +02:00
|
|
|
}
|
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
m->fd = event.origin;
|
|
|
|
ret = ipc_write (ctx, m);
|
2020-01-01 12:11:34 +01:00
|
|
|
if (ret.error_code != IPC_ERROR_NONE) {
|
2019-07-27 15:48:56 +02:00
|
|
|
PRINTERR(ret,"server write");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
2020-07-13 14:12:08 +02:00
|
|
|
case IPC_EVENT_TYPE_TX:
|
|
|
|
{
|
|
|
|
// printf ("a message was sent\n");
|
|
|
|
}
|
|
|
|
break;
|
2019-07-27 15:48:56 +02:00
|
|
|
case IPC_EVENT_TYPE_ERROR:
|
|
|
|
{
|
2020-07-13 14:12:08 +02:00
|
|
|
fprintf (stderr, "a problem happened with client %d\n", event.origin);
|
2019-07-27 15:48:56 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
{
|
|
|
|
fprintf (stderr, "there must be a problem, event not set\n");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// should never go there
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void exit_program(int signal)
|
|
|
|
{
|
|
|
|
printf("Quitting, signal: %d\n", signal);
|
|
|
|
|
|
|
|
// the application will shut down, and close the service
|
2020-07-13 14:12:08 +02:00
|
|
|
TEST_IPC_Q(ipc_close_all (ctx), EXIT_FAILURE);
|
|
|
|
|
|
|
|
// free remaining ctx
|
|
|
|
ipc_ctx_free (ctx);
|
|
|
|
free (ctx);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-07-13 14:12:08 +02:00
|
|
|
* service ping-pong: send back everything sent by the ctx
|
2019-07-27 15:48:56 +02:00
|
|
|
* stop the program on SIG{TERM,INT,ALRM,USR{1,2},HUP} signals
|
|
|
|
*/
|
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
int main(void)
|
2019-07-27 15:48:56 +02:00
|
|
|
{
|
|
|
|
printf ("pid = %d\n", getpid ());
|
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
ctx = malloc (sizeof (struct ipc_ctx));
|
|
|
|
if (ctx == NULL) {
|
2019-07-27 15:48:56 +02:00
|
|
|
exit (1);
|
|
|
|
}
|
2020-07-13 14:12:08 +02:00
|
|
|
memset (ctx, 0, sizeof (struct ipc_ctx));
|
2019-07-27 15:48:56 +02:00
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
TEST_IPC_Q(ipc_server_init (ctx, PONGD_SERVICE_NAME), EXIT_FAILURE);
|
2019-07-27 15:48:56 +02:00
|
|
|
|
2020-07-13 14:12:08 +02:00
|
|
|
struct ipc_connection_info * srv = &ctx->cinfos[0];
|
2019-07-27 15:48:56 +02:00
|
|
|
printf ("Listening on %s.\n", srv->spath);
|
|
|
|
|
|
|
|
printf("MAIN: server created\n" );
|
|
|
|
|
|
|
|
signal (SIGHUP, exit_program);
|
|
|
|
signal (SIGALRM, exit_program);
|
|
|
|
signal (SIGUSR1, exit_program);
|
|
|
|
signal (SIGUSR2, exit_program);
|
|
|
|
signal (SIGTERM, exit_program);
|
|
|
|
signal (SIGINT, exit_program);
|
|
|
|
|
|
|
|
// the service will loop until the end of time, or a signal
|
|
|
|
main_loop ();
|
|
|
|
|
|
|
|
// main_loop should not return
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|