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

205 lines
5.8 KiB
C
Raw Normal View History

2016-06-05 20:48:13 +02:00
#include "../lib/pubsubd.h"
2016-05-28 19:34:23 +02:00
#include <stdlib.h>
2016-06-08 17:57:05 +02:00
#include <pthread.h>
2016-05-28 19:34:23 +02:00
2016-09-11 14:37:41 +02:00
#define NB_CLIENTS 3
2016-05-28 19:34:23 +02:00
void
ohshit(int rvalue, const char* str) {
2016-06-04 20:33:44 +02:00
fprintf(stderr, "%s\n", str);
exit(rvalue);
2016-05-28 19:34:23 +02:00
}
2016-06-08 17:57:05 +02:00
// give this structure to the thread worker function
struct worker_params {
struct channels *chans;
struct channel *chan;
struct app_list_elm *ale;
};
void * pubsubd_worker_thread (void *params)
{
2016-09-11 14:37:41 +02:00
int s = 0;
s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if (s != 0)
printf ("pthread_setcancelstate: %d\n", s);
2016-06-08 17:57:05 +02:00
struct worker_params *wp = (struct worker_params *) params;
2016-09-08 13:23:07 +02:00
if (wp == NULL) {
fprintf (stderr, "error pubsubd_worker_thread : params NULL\n");
return NULL;
}
2016-06-08 17:57:05 +02:00
2016-09-11 14:37:41 +02:00
struct channels *chans = wp->chans;
struct channel *chan = wp->chan;
struct app_list_elm *ale = wp->ale;
free (wp);
// main loop
2016-09-09 01:36:44 +02:00
while (1) {
struct pubsub_msg m;
memset (&m, 0, sizeof (struct pubsub_msg));
2016-09-11 14:37:41 +02:00
sleep (2); // TODO DEBUG
pubsubd_msg_recv (ale->p, &m);
2016-09-09 01:36:44 +02:00
if (m.type == PUBSUB_TYPE_DISCONNECT) {
2016-09-11 14:37:41 +02:00
printf ("process %d disconnecting...\n", ale->p->pid);
if ( 0 != pubsubd_subscriber_del (chan->alh, ale)) {
2016-09-09 01:36:44 +02:00
fprintf (stderr, "err : subscriber not registered\n");
}
break;
}
else {
2016-09-11 14:37:41 +02:00
struct channel *ch = pubsubd_channel_search (chans, chan->chan);
if (ch == NULL) {
printf ("CHAN NOT FOUND\n");
2016-09-11 14:37:41 +02:00
}
else {
printf ("what should be sent: ");
2016-09-11 14:37:41 +02:00
pubsubd_msg_print (&m);
printf ("send the message to:\t");
2016-09-11 14:37:41 +02:00
pubsubd_channel_print (ch);
pubsubd_msg_send (ch->alh, &m);
}
2016-09-09 01:36:44 +02:00
}
pubsubd_msg_free (&m);
2016-09-09 01:36:44 +02:00
}
2016-09-11 14:37:41 +02:00
#if 0
#endif
2016-09-09 01:36:44 +02:00
#if 0
2016-06-13 09:47:19 +02:00
while (1) {
// each chan has a list of subscribers
// someone who only push a msg doesn't need to be registered
2016-09-09 01:36:44 +02:00
// if (wp->ale->action == PUBSUB_BOTH || wp->ale->action == PUBSUB_PUB) {
if (wp->ale->action == PUBSUB_PUB) {
2016-06-13 09:47:19 +02:00
// publish a message
2016-09-09 01:36:44 +02:00
printf ("publish to chan %s\n", wp->chan->chan);
2016-06-08 17:57:05 +02:00
2016-06-13 09:47:19 +02:00
struct pubsub_msg m;
memset (&m, 0, sizeof (struct pubsub_msg));
2016-06-08 17:57:05 +02:00
2016-09-08 13:23:07 +02:00
sleep (5); // TODO DEBUG
2016-06-13 09:47:19 +02:00
pubsubd_msg_recv (wp->ale->p, &m);
2016-06-08 17:57:05 +02:00
2016-06-13 09:47:19 +02:00
pubsubd_msg_print (&m);
if (m.type == PUBSUB_TYPE_DISCONNECT) {
// TODO remove the application from the subscribers
if ( 0 != pubsubd_subscriber_del (wp->chan->alh, wp->ale)) {
fprintf (stderr, "err : subscriber not registered\n");
}
break;
}
else {
struct channel *chan = pubsubd_channel_get (wp->chans, wp->chan);
pubsubd_msg_send (chan->alh, &m);
}
}
else if (wp->ale->action == PUBSUB_SUB) {
// subscribe to a channel, no need to loop
// already subscribed
// printf ("subscribe to %s\n", wp->chan->chan);
// pubsubd_subscriber_add (wp->chan->alh, wp->ale);
break;
2016-06-08 17:57:05 +02:00
}
else {
2016-06-13 09:47:19 +02:00
// unrecognized command, no need to loop
printf ("\033[31mdo not know what you want to do\033[00m\n");
printf ("\tale->p : %p\n", (void*) wp->ale->p);
break;
2016-06-08 17:57:05 +02:00
}
}
2016-09-09 01:36:44 +02:00
#endif
2016-06-08 17:57:05 +02:00
2016-09-11 14:37:41 +02:00
pubsubd_app_list_elm_free (ale);
2016-06-08 17:57:05 +02:00
pthread_exit (NULL);
}
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
{
2016-06-05 20:48:13 +02:00
struct service srv;
memset (&srv, 0, sizeof (struct service));
2016-06-13 09:47:19 +02:00
srv_init (argc, argv, env, &srv, PUBSUB_SERVICE_NAME, NULL);
printf ("Listening on %s.\n", srv.spath);
2016-05-28 19:34:23 +02:00
2016-06-04 20:33:44 +02:00
// creates the service named pipe, that listens to client applications
if (srv_create (&srv))
2016-06-04 20:33:44 +02:00
ohshit(1, "service_create error");
2016-05-28 19:34:23 +02:00
2016-06-07 11:45:21 +02:00
// init chans list
2016-06-04 20:33:44 +02:00
struct channels chans;
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
2016-09-11 14:37:41 +02:00
pthread_t *thr = NULL;
thr = malloc (sizeof (pthread_t));
memset (thr, 0, sizeof (pthread_t));
2016-09-11 14:37:41 +02:00
int i = 0;
// for (i = 0; i < NB_CLIENTS; i++)
for (i = 0; ; i++) {
2016-06-07 11:45:21 +02:00
// for each new process
struct app_list_elm ale;
memset (&ale, 0, sizeof (struct app_list_elm));
struct channel *chan = NULL;
2016-09-09 21:52:56 +02:00
pubsubd_get_new_process (srv.spath, &ale, &chans, &chan);
2016-06-07 17:44:18 +02:00
pubsubd_channels_print (&chans);
// end the application
if (ale.action == PUBSUB_QUIT) {
pubsubd_app_list_elm_free (&ale);
break;
2016-06-07 13:49:23 +02:00
}
2016-06-07 17:44:18 +02:00
// TODO thread to handle multiple clients at a time
struct worker_params *wp = NULL;
2016-06-08 17:57:05 +02:00
wp = malloc (sizeof (struct worker_params));
wp->ale = pubsubd_app_list_elm_copy (&ale);
wp->chans = &chans;
wp->chan = chan;
2016-06-07 13:49:23 +02:00
// realloc memory for further workers
pthread_t * tmpthr = realloc (thr, sizeof (pthread_t) * (i+1));
if (tmpthr == NULL) {
fprintf (stderr, "err: can't allocate more thread contexts\n");
pubsubd_app_list_elm_free (&ale);
break;
}
else {
thr = tmpthr;
}
memset (thr + i, 0, sizeof (pthread_t));
2016-09-11 14:37:41 +02:00
pthread_create (thr + i, NULL, pubsubd_worker_thread, wp);
pthread_detach (thr[i]);
2016-06-07 11:45:21 +02:00
2016-06-07 17:44:18 +02:00
pubsubd_app_list_elm_free (&ale);
2016-06-04 20:33:44 +02:00
}
2016-05-28 19:34:23 +02:00
printf ("Quitting ...\n");
2016-09-11 14:37:41 +02:00
// stop threads
for (int j = 0 ; j < i ; j++) {
pthread_cancel (thr[j]);
2016-09-11 14:37:41 +02:00
void *ret = NULL;
pthread_join (thr[j], &ret);
2016-09-11 14:37:41 +02:00
if (ret != NULL) {
free (ret);
}
}
free (thr);
pubsubd_channels_del_all (&chans);
2016-06-04 20:33:44 +02:00
// the application will shut down, and remove the service named pipe
if (srv_close (&srv))
2016-06-07 13:49:23 +02:00
ohshit (1, "service_close error");
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
}