2019-06-03 21:25:59 +02:00
|
|
|
#include "../src/ipc.h"
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-06-12 15:02:43 +02:00
|
|
|
#define PONGD_SERVICE_NAME "pong"
|
|
|
|
#define PONGD_VERBOSE
|
2019-06-03 21:25:59 +02:00
|
|
|
|
|
|
|
#define PRINTERR(ret,msg) {\
|
2020-01-01 12:11:34 +01:00
|
|
|
fprintf(stderr, "error while %s: %s\n", msg, ret.error_message);\
|
2019-06-03 21:25:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int cpt = 0;
|
|
|
|
|
|
|
|
struct ipc_connection_info *srv = NULL;
|
|
|
|
struct ipc_connection_infos *clients = NULL;
|
|
|
|
|
|
|
|
void main_loop ()
|
|
|
|
{
|
2020-01-01 12:11:34 +01:00
|
|
|
long timer = 10;
|
|
|
|
SECURE_DECLARATION (struct ipc_error, ret);
|
2019-06-03 21:25:59 +02:00
|
|
|
|
|
|
|
clients = malloc (sizeof (struct ipc_connection_infos));
|
2020-01-01 12:11:34 +01:00
|
|
|
memset (clients, 0, sizeof (struct ipc_connection_infos));
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
SECURE_DECLARATION (struct ipc_event, event);
|
2019-06-03 21:25:59 +02:00
|
|
|
event.type = IPC_EVENT_TYPE_NOT_SET;
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
while (1) {
|
2019-06-03 21:25:59 +02:00
|
|
|
// ipc_service_poll_event provides one event at a time
|
|
|
|
// warning: event->m is free'ed if not NULL
|
2020-01-01 12:11:34 +01:00
|
|
|
TEST_IPC_WAIT_EVENT_Q (ipc_wait_event (clients, srv, &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_CONNECTION:
|
|
|
|
{
|
|
|
|
cpt++;
|
2019-06-12 15:02:43 +02:00
|
|
|
#ifdef PONGD_VERBOSE
|
2020-01-01 12:11:34 +01:00
|
|
|
printf ("connection: %d clients connected, new client is %d\n", cpt, (event.origin)->fd);
|
2019-06-12 15:02:43 +02:00
|
|
|
#endif
|
2020-01-01 12:11:34 +01:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_DISCONNECTION:
|
|
|
|
{
|
|
|
|
cpt--;
|
2019-06-12 15:02:43 +02:00
|
|
|
#ifdef PONGD_VERBOSE
|
2020-01-01 12:11:34 +01:00
|
|
|
printf ("disconnection: %d clients remaining\n", cpt);
|
2019-06-12 15:02:43 +02:00
|
|
|
#endif
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
// free the ipc_client structure
|
|
|
|
free (event.origin);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_MESSAGE:
|
|
|
|
{
|
|
|
|
struct ipc_message *m = event.m;
|
2019-06-12 15:02:43 +02:00
|
|
|
#ifdef PONGD_VERBOSE
|
2020-01-01 12:11:34 +01:00
|
|
|
if (m->length > 0) {
|
|
|
|
printf ("message received (type %d, user type %d, size %u bytes): %.*s\n",
|
|
|
|
m->type, m->user_type, m->length, m->length, m->payload);
|
|
|
|
} else {
|
|
|
|
printf ("message with a 0-byte size :(\n");
|
|
|
|
}
|
2019-10-08 13:54:23 +02:00
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
#endif
|
2019-06-03 21:25:59 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
ret = ipc_write (event.origin, m);
|
|
|
|
if (ret.error_code != IPC_ERROR_NONE) {
|
|
|
|
PRINTERR (ret, "server write");
|
|
|
|
}
|
|
|
|
printf ("message sent\n");
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_TIMER:{
|
|
|
|
printf ("timer\n");
|
|
|
|
|
|
|
|
timer = 10;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_ERROR:
|
|
|
|
{
|
|
|
|
cpt--;
|
|
|
|
fprintf (stderr, "a problem happened with client %d (now disconnected)", (event.origin)->fd);
|
|
|
|
fprintf (stderr, ", %d clients remaining\n", cpt);
|
|
|
|
|
|
|
|
// free the ipc_client structure
|
|
|
|
free (event.origin);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
fprintf (stderr, "there must be a problem, event not set\n");
|
|
|
|
};
|
2019-06-03 21:25:59 +02:00
|
|
|
}
|
2020-01-01 12:11:34 +01:00
|
|
|
}
|
2019-06-03 21:25:59 +02:00
|
|
|
|
|
|
|
// should never go there
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
void exit_program (int signal)
|
2019-06-03 21:25:59 +02:00
|
|
|
{
|
2020-01-01 12:11:34 +01:00
|
|
|
printf ("Quitting, signal: %d\n", signal);
|
2019-06-03 21:25:59 +02:00
|
|
|
|
|
|
|
// free remaining clients
|
2020-01-01 12:11:34 +01:00
|
|
|
for (size_t i = 0; i < clients->size; i++) {
|
2019-06-03 21:25:59 +02:00
|
|
|
struct ipc_connection_info *cli = clients->cinfos[i];
|
|
|
|
if (cli != NULL) {
|
|
|
|
free (cli);
|
|
|
|
}
|
|
|
|
clients->cinfos[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc_connections_free (clients);
|
|
|
|
free (clients);
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
// the application will shut down, and close the service
|
|
|
|
struct ipc_error ret = ipc_server_close (srv);
|
|
|
|
if (ret.error_code != IPC_ERROR_NONE) {
|
|
|
|
PRINTERR (ret, "server close");
|
|
|
|
}
|
2019-06-03 21:25:59 +02:00
|
|
|
free (srv);
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
exit (EXIT_SUCCESS);
|
2019-06-03 21:25:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* service ping-pong: send back everything sent by the clients
|
|
|
|
* stop the program on SIG{TERM,INT,ALRM,USR{1,2},HUP} signals
|
|
|
|
*/
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
int main (int argc, char *argv[], char **env)
|
2019-06-03 21:25:59 +02:00
|
|
|
{
|
2020-01-01 12:11:34 +01:00
|
|
|
argc = argc; // warnings
|
|
|
|
argv = argv; // warnings
|
2019-06-03 21:25:59 +02:00
|
|
|
|
|
|
|
printf ("pid = %d\n", getpid ());
|
|
|
|
|
|
|
|
srv = malloc (sizeof (struct ipc_connection_info));
|
|
|
|
if (srv == NULL) {
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2020-01-01 12:11:34 +01:00
|
|
|
memset (srv, 0, sizeof (struct ipc_connection_info));
|
|
|
|
srv->type = '\0';
|
|
|
|
srv->index = 0;
|
|
|
|
srv->version = 0;
|
|
|
|
srv->fd = 0;
|
|
|
|
srv->spath = NULL;
|
|
|
|
|
|
|
|
struct ipc_error ret = ipc_server_init (env, srv, PONGD_SERVICE_NAME);
|
|
|
|
if (ret.error_code != IPC_ERROR_NONE) {
|
|
|
|
PRINTERR (ret, "server init");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
printf ("Listening on %s.\n", srv->spath);
|
|
|
|
|
|
|
|
printf ("MAIN: server created\n");
|
2019-06-03 21:25:59 +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);
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
// the service will loop until the end of time, or a signal
|
|
|
|
main_loop ();
|
2019-06-03 21:25:59 +02:00
|
|
|
|
|
|
|
// main_loop should not return
|
2020-01-01 12:11:34 +01:00
|
|
|
return EXIT_FAILURE;
|
2019-06-03 21:25:59 +02:00
|
|
|
}
|