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;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *buf = NULL;
|
|
|
|
size_t msize = 0;
|
|
|
|
pubsub_message_serialize (pubsub_msg, &buf, &msize);
|
|
|
|
|
|
|
|
struct ipc_message m;
|
|
|
|
memset (&m, 0, sizeof (struct ipc_message));
|
|
|
|
ipc_message_format_data (&m, buf, msize);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < clients->size ; i++) {
|
|
|
|
ipc_server_write (clients->clients[i], &m);
|
|
|
|
}
|
|
|
|
ipc_message_empty (&m);
|
|
|
|
|
|
|
|
if (buf != NULL) {
|
|
|
|
free (buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// TODO: to test, unsubscribe when closing
|
|
|
|
pubsubd_channels_unsubscribe_everywhere (chans, cli);
|
|
|
|
|
|
|
|
// free the ipc_client structure
|
|
|
|
free (event.origin);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IPC_EVENT_TYPE_MESSAGE:
|
|
|
|
{
|
|
|
|
// TODO: handle a message
|
|
|
|
struct ipc_message *m = event.m;
|
|
|
|
print_hexa ("received msg hexa", m->payload, m->length);
|
|
|
|
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-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
|
|
|
|
2016-06-10 20:02:58 +02:00
|
|
|
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" );
|
2016-09-14 00:20:04 +02:00
|
|
|
|
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
|
|
|
}
|