Obsolete
/
libipc-old
Archived
3
0
Fork 0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues/pull-requests.
libipc-old/pong/app/pongd.c

156 lines
3.5 KiB
C
Raw Normal View History

2018-10-28 17:09:35 +01:00
#include "../../core/ipc.h"
#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"
int cpt = 0;
struct ipc_service *srv = 0;
2018-10-28 17:09:35 +01:00
struct ipc_clients *clients;
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-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) {
// ipc_service_loop provides one event at a time
// warning: event->m is free'ed if not NULL
ret = ipc_service_loop (clients, srv, &event);
if (ret != 0) {
handle_error("ipc_service_loop != 0");
// the application will shut down, and close the service
if (ipc_server_close (srv) < 0) {
2018-10-28 17:09:35 +01:00
handle_error("ipc_server_close < 0");
}
2018-10-28 17:09:35 +01:00
exit (EXIT_FAILURE);
}
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++;
printf ("connection: %d clients connected\n", cpt);
printf ("new client has the fd %d\n", ((struct ipc_client*) event.origin)->proc_fd);
};
break;
case IPC_EVENT_TYPE_DISCONNECTION:
{
cpt--;
printf ("disconnection: %d clients remaining\n", cpt);
// free the ipc_client structure
free (event.origin);
};
break;
case IPC_EVENT_TYPE_MESSAGE:
{
struct ipc_message *m = event.m;
if (m->length > 0) {
printf ("message received (type %d): %.*s\n", m->type, m->length, m->payload);
}
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-28 17:09:35 +01:00
clients->clients[i] = NULL;
}
ipc_clients_free (clients);
free (clients);
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
* stop the program on SIGTERM, SIGALRM, SIGUSR{1,2}, SIGHUP signals
2016-12-23 01:33:52 +01:00
*/
int main(int argc, char * argv[], char **env)
{
argc = argc; // warnings
argv = argv; // warnings
2018-10-28 17:09:35 +01:00
printf ("pid = %d\n", getpid ());
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");
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;
}
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);
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
}