2018-10-28 17:09:35 +01:00
|
|
|
#include "../../core/ipc.h"
|
2018-11-04 09:50:17 +01:00
|
|
|
#include "../../core/error.h"
|
2018-10-28 17:09:35 +01:00
|
|
|
#include <signal.h>
|
2016-12-23 01:33:52 +01:00
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define PONGD_SERVICE_NAME "pongd"
|
2018-11-05 19:01:09 +01:00
|
|
|
#define PONGD_PRINT_MESSAGES
|
2016-12-23 01:33:52 +01:00
|
|
|
|
|
|
|
int cpt = 0;
|
|
|
|
|
2018-10-04 22:51:31 +02:00
|
|
|
struct ipc_service *srv = 0;
|
2018-10-28 17:09:35 +01:00
|
|
|
struct ipc_clients *clients;
|
2018-10-04 22:51:31 +02:00
|
|
|
|
2016-12-23 01:33:52 +01:00
|
|
|
|
2018-10-28 17:09:35 +01:00
|
|
|
void main_loop ()
|
2016-12-23 01:33:52 +01:00
|
|
|
{
|
2018-10-28 17:09:35 +01:00
|
|
|
int ret = 0;
|
2018-10-08 16:15:35 +02:00
|
|
|
|
2018-10-28 17:09:35 +01:00
|
|
|
clients = malloc (sizeof (struct ipc_clients));
|
|
|
|
memset(clients, 0, sizeof(struct ipc_clients));
|
2018-10-04 22:51:31 +02:00
|
|
|
|
2018-10-28 17:09:35 +01:00
|
|
|
struct ipc_event event;
|
|
|
|
memset(&event, 0, sizeof (struct ipc_event));
|
|
|
|
event.type = IPC_EVENT_TYPE_NOT_SET;
|
2018-10-08 16:15:35 +02:00
|
|
|
|
2018-10-28 17:09:35 +01:00
|
|
|
while(1) {
|
2018-10-28 19:28:19 +01:00
|
|
|
// ipc_service_poll_event provides one event at a time
|
2018-10-28 17:09:35 +01:00
|
|
|
// warning: event->m is free'ed if not NULL
|
2018-10-28 19:28:19 +01:00
|
|
|
ret = ipc_service_poll_event (clients, srv, &event);
|
2018-10-28 17:09:35 +01:00
|
|
|
if (ret != 0) {
|
2018-10-28 19:28:19 +01:00
|
|
|
handle_error("ipc_service_poll_event != 0");
|
2018-10-28 17:09:35 +01:00
|
|
|
// the application will shut down, and close the service
|
2018-10-04 22:51:31 +02:00
|
|
|
if (ipc_server_close (srv) < 0) {
|
2018-10-28 17:09:35 +01:00
|
|
|
handle_error("ipc_server_close < 0");
|
2018-10-04 22:51:31 +02:00
|
|
|
}
|
2018-10-28 17:09:35 +01:00
|
|
|
exit (EXIT_FAILURE);
|
2018-10-04 22:51:31 +02:00
|
|
|
}
|
2016-12-23 01:33:52 +01:00
|
|
|
|
2018-10-28 17:09:35 +01:00
|
|
|
switch (event.type) {
|
|
|
|
case IPC_EVENT_TYPE_CONNECTION:
|
|
|
|
{
|
|
|
|
cpt++;
|
2018-11-05 19:01:09 +01:00
|
|
|
#ifdef PONGD_PRINT_MESSAGES
|
|
|
|
printf ("connection: client fd %d, %d clients connected\n"
|
|
|
|
, ((struct ipc_client*) event.origin)->proc_fd, cpt);
|
|
|
|
#endif
|
2018-10-28 17:09:35 +01:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_DISCONNECTION:
|
|
|
|
{
|
|
|
|
cpt--;
|
2018-11-05 19:01:09 +01:00
|
|
|
#ifdef PONGD_PRINT_MESSAGES
|
2018-10-28 17:09:35 +01:00
|
|
|
printf ("disconnection: %d clients remaining\n", cpt);
|
2018-11-05 19:01:09 +01:00
|
|
|
#endif
|
2018-10-28 17:09:35 +01:00
|
|
|
|
|
|
|
// free the ipc_client structure
|
|
|
|
free (event.origin);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_MESSAGE:
|
|
|
|
{
|
|
|
|
struct ipc_message *m = event.m;
|
2018-11-05 19:01:09 +01:00
|
|
|
#ifdef PONGD_PRINT_MESSAGES
|
2018-10-28 17:09:35 +01:00
|
|
|
if (m->length > 0) {
|
|
|
|
printf ("message received (type %d): %.*s\n", m->type, m->length, m->payload);
|
|
|
|
}
|
2018-11-05 19:01:09 +01:00
|
|
|
#endif
|
2018-10-28 17:09:35 +01:00
|
|
|
if (ipc_server_write (event.origin, m) < 0) {
|
|
|
|
handle_err( "handle_new_msg", "server_write < 0");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_ERROR:
|
|
|
|
{
|
|
|
|
fprintf (stderr, "a problem happened with client %d\n"
|
|
|
|
, ((struct ipc_client*) event.origin)->proc_fd);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
{
|
|
|
|
fprintf (stderr, "there must be a problem, event not set\n");
|
|
|
|
};
|
2018-10-12 01:59:56 +02:00
|
|
|
}
|
2016-12-23 01:33:52 +01:00
|
|
|
}
|
2018-10-28 17:09:35 +01:00
|
|
|
|
|
|
|
// should never go there
|
|
|
|
exit (1);
|
2016-12-23 01:33:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-28 17:09:35 +01:00
|
|
|
void exit_program(int signal)
|
2016-12-23 01:33:52 +01:00
|
|
|
{
|
2018-10-28 17:09:35 +01:00
|
|
|
printf("Quitting, signal: %d\n", signal);
|
|
|
|
|
|
|
|
// free remaining clients
|
|
|
|
for (int i = 0; i < clients->size ; i++) {
|
|
|
|
struct ipc_client *cli = clients->clients[i];
|
|
|
|
// TODO: replace with specific ipc_client_empty function
|
|
|
|
if (cli != NULL) {
|
|
|
|
// ipc_client_empty (cli);
|
|
|
|
free (cli);
|
2018-10-04 22:51:31 +02:00
|
|
|
}
|
2018-10-28 17:09:35 +01:00
|
|
|
clients->clients[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc_clients_free (clients);
|
|
|
|
free (clients);
|
2018-10-04 22:51:31 +02:00
|
|
|
|
|
|
|
|
2018-10-28 17:09:35 +01:00
|
|
|
// the application will shut down, and close the service
|
|
|
|
if (ipc_server_close (srv) < 0) {
|
|
|
|
handle_error("ipc_server_close < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
}
|
2018-10-28 17:09:35 +01:00
|
|
|
free (srv);
|
2016-12-23 01:33:52 +01:00
|
|
|
|
2018-10-28 17:09:35 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
2016-12-23 01:33:52 +01:00
|
|
|
|
|
|
|
/*
|
2018-10-28 17:09:35 +01:00
|
|
|
* service ping-pong: send back everything sent by the clients
|
2018-10-28 18:15:55 +01:00
|
|
|
* stop the program on SIG{TERM,INT,ALRM,USR{1,2},HUP} signals
|
2016-12-23 01:33:52 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
int main(int argc, char * argv[], char **env)
|
|
|
|
{
|
2018-10-09 13:16:40 +02:00
|
|
|
argc = argc; // warnings
|
|
|
|
argv = argv; // warnings
|
|
|
|
|
2018-10-28 17:09:35 +01:00
|
|
|
printf ("pid = %d\n", getpid ());
|
|
|
|
|
2018-10-04 22:51:31 +02:00
|
|
|
srv = malloc (sizeof (struct ipc_service));
|
|
|
|
if (srv == NULL) {
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
memset (srv, 0, sizeof (struct ipc_service));
|
|
|
|
srv->index = 0;
|
|
|
|
srv->version = 0;
|
2016-12-23 01:33:52 +01:00
|
|
|
|
|
|
|
// unlink("/tmp/ipc/pongd-0-0");
|
|
|
|
|
2018-10-09 13:16:40 +02:00
|
|
|
if (ipc_server_init (env, srv, PONGD_SERVICE_NAME) < 0) {
|
2018-10-28 17:09:35 +01:00
|
|
|
handle_error("ipc_server_init < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2018-10-04 22:51:31 +02:00
|
|
|
printf ("Listening on %s.\n", srv->spath);
|
2016-12-23 01:33:52 +01:00
|
|
|
|
|
|
|
printf("MAIN: server created\n" );
|
|
|
|
|
2018-10-28 17:09:35 +01:00
|
|
|
signal (SIGHUP, exit_program);
|
|
|
|
signal (SIGALRM, exit_program);
|
|
|
|
signal (SIGUSR1, exit_program);
|
|
|
|
signal (SIGUSR2, exit_program);
|
|
|
|
signal (SIGTERM, exit_program);
|
2018-10-28 18:15:55 +01:00
|
|
|
signal (SIGINT, exit_program);
|
2016-12-23 01:33:52 +01:00
|
|
|
|
2018-10-28 17:09:35 +01:00
|
|
|
// the service will loop until the end of time, or a signal
|
|
|
|
main_loop ();
|
2016-12-23 01:33:52 +01:00
|
|
|
|
2018-10-28 17:09:35 +01:00
|
|
|
// main_loop should not return
|
|
|
|
return EXIT_FAILURE;
|
2016-12-23 01:33:52 +01:00
|
|
|
}
|