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/pubsub/app/pubsubd.c

198 lines
5.0 KiB
C
Raw Normal View History

2018-10-29 23:36:06 +01:00
#include "../../core/ipc.h"
2018-10-30 11:13:30 +01:00
#include "../lib/message.h"
#include "../lib/channels.h"
2016-05-28 19:34:23 +02:00
2018-10-30 11:13:30 +01:00
#include <stdlib.h>
2017-01-19 22:07:52 +01:00
#include <sys/socket.h>
#include <signal.h>
2016-09-11 14:37:41 +02:00
2018-10-30 11:13:30 +01:00
#define PUBSUBD_SERVICE_NAME "pubsubd"
2017-01-19 22:07:52 +01:00
// to quit them properly if a signal occurs
2018-10-04 00:30:47 +02:00
struct ipc_service srv;
2017-01-19 22:07:52 +01:00
struct channels chans;
2018-10-30 11:13:30 +01:00
void pubsubd_send (const struct ipc_clients *clients, const struct pubsub_msg * pubsub_msg)
{
if (clients == NULL) {
fprintf (stderr, "pubsubd_send: clients == NULL");
return;
}
if (pubsub_msg == NULL) {
fprintf (stderr, "pubsubd_send: pubsub_msg == NULL");
return;
}
struct ipc_message m;
2018-10-30 13:18:53 +01:00
memset (&m, 0, sizeof (struct ipc_message));
pubsub_message_to_message (pubsub_msg, &m);
2018-10-30 11:13:30 +01:00
int i;
for (i = 0; i < clients->size ; i++) {
ipc_server_write (clients->clients[i], &m);
}
ipc_message_empty (&m);
}
void pubsubd_main_loop (struct ipc_service *srv, struct channels *chans)
{
int i, ret = 0;
struct ipc_clients clients;
memset(&clients, 0, sizeof(struct ipc_clients));
struct ipc_clients proc_to_read;
memset(&proc_to_read, 0, sizeof(struct ipc_clients));
struct ipc_event event;
memset(&event, 0, sizeof (struct ipc_event));
event.type = IPC_EVENT_TYPE_NOT_SET;
int cpt = 0;
while(1) {
ret = ipc_service_poll_event (&clients, srv, &event);
if (ret != 0) {
handle_error("ipc_service_poll_event != 0");
// the application will shut down, and close the service
if (ipc_server_close (srv) < 0) {
handle_error("ipc_server_close < 0");
}
exit (EXIT_FAILURE);
}
switch (event.type) {
case IPC_EVENT_TYPE_CONNECTION:
{
cpt++;
struct ipc_client *cli = event.origin;
printf ("connection of client %d: %d clients connected\n", cli->proc_fd, cpt);
};
break;
case IPC_EVENT_TYPE_DISCONNECTION:
{
cpt--;
struct ipc_client *cli = event.origin;
printf ("disconnection of client %d: %d clients remaining\n", cli->proc_fd, cpt);
pubsubd_channels_unsubscribe_everywhere (chans, cli);
// free the ipc_client structure
free (event.origin);
};
break;
case IPC_EVENT_TYPE_MESSAGE:
{
struct ipc_message *m = event.m;
2018-10-30 13:18:53 +01:00
// print_hexa ("received msg hexa", (unsigned char *) m->payload, m->length);
2018-10-30 11:13:30 +01:00
struct ipc_client *cli = event.origin;
struct pubsub_msg pm;
memset (&pm, 0, sizeof (struct pubsub_msg));
pubsub_message_from_message (&pm, m);
if (pm.type == PUBSUB_MSG_TYPE_SUB) {
printf ("client %d subscribing to %s\n"
, cli->proc_fd
, pm.chan);
pubsubd_channels_subscribe (chans
, pm.chan, cli);
}
if (pm.type == PUBSUB_MSG_TYPE_UNSUB) {
printf ("client %d unsubscribing to %s\n", cli->proc_fd, pm.chan);
pubsubd_channels_unsubscribe (chans, pm.chan, cli);
}
if (pm.type == PUBSUB_MSG_TYPE_PUB) {
// printf ("client %d: publishing to %s: %s\n", cli->proc_fd, pm.chan, pm.data);
printf ("client %d: ", cli->proc_fd);
pubsub_message_print (&pm);
struct channel *chan = pubsubd_channel_search (chans, pm.chan);
if (chan == NULL) {
handle_err ("handle_new_msg", "publish on nonexistent channel");
ipc_message_empty (m);
continue;
}
pubsubd_send (chan->subs, &pm);
}
pubsub_message_empty (&pm);
};
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");
};
}
}
for (i = 0; i < clients.size; i++) {
if (ipc_server_close_client (clients.clients[i]) < 0) {
handle_error( "server_close_client < 0");
}
}
pubsubd_channels_del_all (chans);
}
2017-01-19 22:07:52 +01:00
void handle_signal (int signalnumber)
{
// the application will shut down, and remove the service named pipe
2018-10-04 00:18:08 +02:00
if (ipc_server_close (&srv) < 0) {
handle_error("ipc_server_close < 0");
2017-01-19 22:07:52 +01:00
}
pubsubd_channels_del_all (&chans);
fprintf (stderr, "received a signal %d\n", signalnumber);
exit (EXIT_SUCCESS);
2016-05-28 19:34:23 +02:00
}
2016-09-08 13:23:07 +02:00
int
2016-06-12 14:41:25 +02:00
main(int argc, char **argv, char **env)
2016-06-04 20:33:44 +02:00
{
2018-10-29 23:36:06 +01:00
argc = argc;
argv = argv;
2018-10-30 13:18:53 +01:00
// set the service
2018-10-04 00:30:47 +02:00
memset (&srv, 0, sizeof (struct ipc_service));
2017-01-19 22:07:52 +01:00
srv.index = 0;
srv.version = 0;
2016-05-28 19:34:23 +02:00
2017-01-19 22:07:52 +01:00
signal(SIGHUP, handle_signal);
signal(SIGINT, handle_signal);
signal(SIGQUIT, handle_signal);
2016-05-28 19:34:23 +02:00
2018-10-30 13:18:53 +01:00
// set the channels
memset (&chans, 0, sizeof (struct channels));
2016-06-04 20:33:44 +02:00
pubsubd_channels_init (&chans);
2016-05-28 19:34:23 +02:00
2018-10-29 23:36:06 +01:00
if (ipc_server_init (env, &srv, PUBSUBD_SERVICE_NAME) < 0) {
2018-10-04 00:18:08 +02:00
handle_error("ipc_server_init < 0");
2017-01-19 22:07:52 +01:00
return EXIT_FAILURE;
2016-06-04 20:33:44 +02:00
}
2017-01-19 22:07:52 +01:00
printf ("Listening on %s.\n", srv.spath);
2016-05-28 19:34:23 +02:00
2017-01-19 22:07:52 +01:00
printf("MAIN: server created\n" );
2017-01-19 22:07:52 +01:00
// the service will loop until the end of time, a specific message, a signal
pubsubd_main_loop (&srv, &chans);
2016-09-11 14:37:41 +02:00
2016-06-04 20:33:44 +02:00
// the application will shut down, and remove the service named pipe
2018-10-04 00:18:08 +02:00
if (ipc_server_close (&srv) < 0) {
handle_error("ipc_server_close < 0");
2017-01-19 22:07:52 +01:00
}
2016-05-28 19:34:23 +02:00
2016-06-04 20:33:44 +02:00
return EXIT_SUCCESS;
2016-05-28 19:34:23 +02:00
}