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/examples/pongd.c

170 lines
3.7 KiB
C
Raw Normal View History

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>
#define PONGD_SERVICE_NAME "pong"
#define PONGD_VERBOSE
2019-06-03 21:25:59 +02:00
#define PRINTERR(ret,msg) {\
fprintf(stderr, "error while %s: %s\n", msg, ret.error_message);\
2019-06-03 21:25:59 +02:00
}
2020-06-27 19:16:07 +02:00
/**
* ipc_ctx:
* cinfos: array of ipc_connection_info
* pollfd: array of `pollfd` structure
* Both arrays share the same indices.
*/
/**
******************************************************************************
* Overview of the main loop:
2020-06-28 15:40:02 +02:00
* 1. "ctx" pointer declaration (struct ipc_ctx).
2020-07-04 11:38:11 +02:00
* 1. ipc_server_init (ctx, SERVICE_NAME)
2020-06-27 19:16:07 +02:00
******************************************************************************
*/
2019-06-03 21:25:59 +02:00
int cpt = 0;
2020-01-28 13:39:06 +01:00
int verbosity = 1;
2020-06-28 15:40:02 +02:00
struct ipc_ctx *ctx = NULL;
2019-06-03 21:25:59 +02:00
void main_loop ()
{
2020-07-04 11:38:11 +02:00
int base_timer = 10000;
int timer = base_timer;
SECURE_DECLARATION (struct ipc_error, ret);
2019-06-03 21:25:59 +02:00
SECURE_DECLARATION (struct ipc_event, event);
2019-06-03 21:25:59 +02:00
event.type = IPC_EVENT_TYPE_NOT_SET;
while (1) {
2020-06-27 04:45:43 +02:00
// ipc_wait_event provides one event at a time
2019-06-03 21:25:59 +02:00
// warning: event->m is free'ed if not NULL
2020-07-06 08:43:06 +02:00
TEST_IPC_WAIT_EVENT_Q (ipc_wait_event (ctx, &event, &timer), EXIT_FAILURE);
2019-06-03 21:25:59 +02:00
switch (event.type) {
case IPC_EVENT_TYPE_CONNECTION:
{
cpt++;
2020-01-28 13:39:06 +01:00
if (verbosity > 1) {
2020-07-04 11:38:11 +02:00
printf ("connection: %d ctx connected, new client is %d\n", cpt, event.origin);
2020-01-28 13:39:06 +01:00
}
};
break;
case IPC_EVENT_TYPE_DISCONNECTION:
{
cpt--;
2020-01-28 13:39:06 +01:00
if (verbosity > 1) {
2020-06-28 15:40:02 +02:00
printf ("disconnection: %d ctx remaining\n", cpt);
2020-01-28 13:39:06 +01:00
}
};
break;
case IPC_EVENT_TYPE_MESSAGE:
{
struct ipc_message *m = event.m;
2020-01-28 13:39:06 +01:00
if (verbosity > 1) {
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");
}
}
2020-07-04 11:38:11 +02:00
ret = ipc_write (ctx, m);
if (ret.error_code != IPC_ERROR_NONE) {
PRINTERR (ret, "server write");
}
};
break;
case IPC_EVENT_TYPE_TIMER:{
printf ("timer\n");
2020-01-28 13:39:06 +01:00
timer = base_timer;
};
break;
case IPC_EVENT_TYPE_ERROR:
{
cpt--;
2020-07-04 11:38:11 +02:00
fprintf (stderr, "a problem happened with client %d (now disconnected)", event.origin);
2020-06-28 15:40:02 +02:00
fprintf (stderr, ", %d ctx remaining\n", cpt);
};
break;
2020-07-04 11:45:09 +02:00
case IPC_EVENT_TYPE_TX:
{
if (verbosity > 1) {
printf ("a message was sent\n");
}
}
break;
default:
{
fprintf (stderr, "there must be a problem, event not set\n");
};
2019-06-03 21:25:59 +02:00
}
}
2019-06-03 21:25:59 +02:00
// should never go there
exit (EXIT_FAILURE);
}
void exit_program (int signal)
2019-06-03 21:25:59 +02:00
{
printf ("Quitting, signal: %d\n", signal);
2019-06-03 21:25:59 +02:00
// the application will shut down, and close the service
2020-07-04 11:38:11 +02:00
struct ipc_error ret = ipc_close_all (ctx);
if (ret.error_code != IPC_ERROR_NONE) {
PRINTERR (ret, "server close");
}
2020-07-04 11:38:11 +02:00
ipc_ctx_free (ctx);
free (ctx);
2019-06-03 21:25:59 +02:00
exit (EXIT_SUCCESS);
2019-06-03 21:25:59 +02:00
}
/*
2020-06-28 15:40:02 +02:00
* service ping-pong: send back everything sent by the ctx
2019-06-03 21:25:59 +02:00
* stop the program on SIG{TERM,INT,ALRM,USR{1,2},HUP} signals
*/
2020-07-04 11:38:11 +02:00
int main (int argc, char *argv[])
2019-06-03 21:25:59 +02:00
{
2020-01-28 13:39:06 +01:00
if (argc > 1) {
verbosity = atoi(argv[1]);
}
2019-06-03 21:25:59 +02:00
printf ("pid = %d\n", getpid ());
2020-07-04 11:45:09 +02:00
ctx = malloc (sizeof (struct ipc_ctx));
memset (ctx, 0, sizeof (struct ipc_ctx));
2020-07-04 11:38:11 +02:00
struct ipc_error ret = ipc_server_init (ctx, PONGD_SERVICE_NAME);
if (ret.error_code != IPC_ERROR_NONE) {
PRINTERR (ret, "server init");
return EXIT_FAILURE;
}
2020-06-28 15:40:02 +02:00
printf ("Listening on %s.\n", ctx->cinfos[0].spath);
printf ("MAIN: server created\n");
2019-06-03 21:25:59 +02:00
2020-06-28 15:40:02 +02:00
signal (SIGHUP, exit_program);
2019-06-03 21:25:59 +02:00
signal (SIGALRM, exit_program);
signal (SIGUSR1, exit_program);
signal (SIGUSR2, exit_program);
signal (SIGTERM, exit_program);
2020-06-28 15:40:02 +02:00
signal (SIGINT, exit_program);
2019-06-03 21:25:59 +02: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
return EXIT_FAILURE;
2019-06-03 21:25:59 +02:00
}