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-09-14 00:20:04 +02:00
|
|
|
// head of the list
|
|
|
|
LIST_HEAD(workers, worker);
|
|
|
|
|
|
|
|
struct workers *my_workers;
|
|
|
|
|
|
|
|
// element of the list
|
|
|
|
// worker : process to handle (threaded)
|
|
|
|
struct worker {
|
2016-09-14 15:07:08 +02:00
|
|
|
pthread_t *thr;
|
2016-06-08 17:57:05 +02:00
|
|
|
struct channels *chans;
|
|
|
|
struct channel *chan;
|
|
|
|
struct app_list_elm *ale;
|
2016-09-14 00:20:04 +02:00
|
|
|
LIST_ENTRY(worker) entries;
|
2016-06-08 17:57:05 +02:00
|
|
|
};
|
|
|
|
|
2016-09-14 00:20:04 +02:00
|
|
|
void pubsubd_worker_free (struct worker * w);
|
|
|
|
struct worker * pubsubd_worker_get (struct workers *wrkrs, struct worker *w);
|
|
|
|
int pubsubd_worker_eq (const struct worker *w1, const struct worker *w2);
|
|
|
|
|
|
|
|
void pubsubd_workers_init (struct workers *wrkrs) { LIST_INIT(wrkrs); }
|
|
|
|
|
|
|
|
struct worker *
|
|
|
|
pubsubd_workers_add (struct workers *wrkrs, const struct worker *w)
|
|
|
|
{
|
|
|
|
if (wrkrs == NULL || w == NULL) {
|
|
|
|
printf ("pubsubd_workers_add: wrkrs == NULL or w == NULL");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct worker *n = malloc (sizeof (struct worker));
|
|
|
|
memset (n, 0, sizeof (struct worker));
|
|
|
|
memcpy (n, w, sizeof (struct worker));
|
|
|
|
if (w->ale != NULL)
|
|
|
|
n->ale = pubsubd_app_list_elm_copy (w->ale);
|
|
|
|
|
|
|
|
LIST_INSERT_HEAD(wrkrs, n, entries);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pubsubd_worker_del (struct workers *wrkrs, struct worker *w)
|
|
|
|
{
|
|
|
|
struct worker *todel = pubsubd_worker_get (wrkrs, w);
|
|
|
|
if(todel != NULL) {
|
|
|
|
LIST_REMOVE(todel, entries);
|
|
|
|
pubsubd_worker_free (todel);
|
|
|
|
free (todel);
|
|
|
|
todel = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 15:07:08 +02:00
|
|
|
// kill the threads
|
|
|
|
void pubsubd_workers_stop (struct workers *wrkrs)
|
|
|
|
{
|
|
|
|
if (!wrkrs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct worker *w = NULL;
|
|
|
|
struct worker *wtmp = NULL;
|
|
|
|
|
|
|
|
LIST_FOREACH_SAFE(w, wrkrs, entries, wtmp) {
|
|
|
|
if (w->thr == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pthread_cancel (*w->thr);
|
|
|
|
void *ret = NULL;
|
|
|
|
pthread_join (*w->thr, &ret);
|
|
|
|
if (ret != NULL) {
|
|
|
|
free (ret);
|
|
|
|
}
|
|
|
|
free (w->thr);
|
|
|
|
w->thr = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 00:20:04 +02:00
|
|
|
void pubsubd_workers_del_all (struct workers *wrkrs)
|
|
|
|
{
|
|
|
|
if (!wrkrs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct worker *w = NULL;
|
|
|
|
|
|
|
|
while (!LIST_EMPTY(wrkrs)) {
|
|
|
|
printf ("KILL THE WORKERS : %p\n", w);
|
|
|
|
w = LIST_FIRST(wrkrs);
|
|
|
|
LIST_REMOVE(w, entries);
|
|
|
|
pubsubd_worker_free (w);
|
|
|
|
free (w);
|
|
|
|
w = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pubsubd_worker_free (struct worker * w)
|
|
|
|
{
|
|
|
|
if (w == NULL)
|
|
|
|
return;
|
|
|
|
pubsubd_app_list_elm_free (w->ale);
|
|
|
|
free (w->ale);
|
|
|
|
w->ale = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct worker * pubsubd_worker_get (struct workers *wrkrs, struct worker *w)
|
|
|
|
{
|
|
|
|
struct worker * np = NULL;
|
|
|
|
LIST_FOREACH(np, wrkrs, entries) {
|
|
|
|
if (pubsubd_worker_eq (np, w))
|
|
|
|
return np;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pubsubd_worker_eq (const struct worker *w1, const struct worker *w2)
|
|
|
|
{
|
|
|
|
return w1 == w2; // if it's the same pointer
|
|
|
|
}
|
|
|
|
|
|
|
|
// a thread for each connected process
|
2016-06-08 17:57:05 +02:00
|
|
|
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-09-14 00:20:04 +02:00
|
|
|
struct worker *w = (struct worker *) params;
|
|
|
|
if (w == NULL) {
|
2016-09-08 13:23:07 +02:00
|
|
|
fprintf (stderr, "error pubsubd_worker_thread : params NULL\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-06-08 17:57:05 +02:00
|
|
|
|
2016-09-14 00:20:04 +02:00
|
|
|
struct channels *chans = w->chans;
|
|
|
|
struct channel *chan = w->chan;
|
|
|
|
struct app_list_elm *ale = w->ale;
|
2016-09-11 14:37:41 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
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) {
|
2016-09-11 16:55:02 +02:00
|
|
|
printf ("CHAN NOT FOUND\n");
|
2016-09-11 14:37:41 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-09-11 16:55:02 +02:00
|
|
|
printf ("what should be sent: ");
|
2016-09-11 14:37:41 +02:00
|
|
|
pubsubd_msg_print (&m);
|
2016-09-11 16:55:02 +02:00
|
|
|
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
|
|
|
}
|
2016-09-12 12:45:31 +02:00
|
|
|
pubsubd_msg_free (&m);
|
2016-09-09 01:36:44 +02:00
|
|
|
}
|
2016-06-08 17:57:05 +02:00
|
|
|
|
2016-09-11 14:37:41 +02:00
|
|
|
pubsubd_app_list_elm_free (ale);
|
2016-09-14 00:20:04 +02:00
|
|
|
free (w->ale);
|
|
|
|
w->ale = NULL;
|
|
|
|
|
2016-09-14 15:07:08 +02:00
|
|
|
free (w->thr);
|
|
|
|
w->thr = NULL;
|
|
|
|
|
2016-09-14 00:20:04 +02:00
|
|
|
pubsubd_worker_del (my_workers, w);
|
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;
|
2016-06-10 20:02:58 +02:00
|
|
|
memset (&srv, 0, sizeof (struct service));
|
2016-06-13 09:47:19 +02:00
|
|
|
srv_init (argc, argv, env, &srv, PUBSUB_SERVICE_NAME, NULL);
|
2016-06-06 02:25:28 +02:00
|
|
|
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
|
2016-06-06 02:25:28 +02:00
|
|
|
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;
|
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
|
|
|
|
2016-09-14 00:20:04 +02:00
|
|
|
my_workers = malloc (sizeof (struct workers));
|
|
|
|
memset (my_workers, 0, sizeof (struct workers));
|
|
|
|
pubsubd_workers_init (my_workers);
|
|
|
|
|
2016-09-11 16:55:02 +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;
|
2016-06-10 20:02:58 +02:00
|
|
|
memset (&ale, 0, sizeof (struct app_list_elm));
|
2016-06-10 01:21:04 +02:00
|
|
|
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) {
|
2016-09-12 12:45:31 +02:00
|
|
|
pubsubd_app_list_elm_free (&ale);
|
|
|
|
break;
|
2016-06-07 13:49:23 +02:00
|
|
|
}
|
|
|
|
|
2016-09-14 00:20:04 +02:00
|
|
|
// thread to handle multiple clients at a time
|
|
|
|
struct worker *w = NULL;
|
|
|
|
w = malloc (sizeof (struct worker));
|
2016-09-14 15:07:08 +02:00
|
|
|
w->thr = malloc (sizeof (pthread_t));
|
|
|
|
memset (w->thr, 0, sizeof (pthread_t));
|
2016-09-14 00:20:04 +02:00
|
|
|
w->ale = pubsubd_app_list_elm_copy (&ale);
|
|
|
|
w->chans = &chans;
|
|
|
|
w->chan = chan;
|
|
|
|
struct worker *wtmp = pubsubd_workers_add (my_workers, w);
|
|
|
|
pubsubd_worker_free (w);
|
|
|
|
free (w);
|
|
|
|
w = wtmp;
|
2016-06-07 13:49:23 +02:00
|
|
|
|
2016-09-14 15:07:08 +02:00
|
|
|
pthread_create (w->thr, NULL, pubsubd_worker_thread, w);
|
|
|
|
pthread_detach (*w->thr);
|
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
|
|
|
|
2016-09-12 12:45:31 +02:00
|
|
|
printf ("Quitting ...\n");
|
2016-09-11 14:37:41 +02:00
|
|
|
|
2016-09-14 15:07:08 +02:00
|
|
|
pubsubd_workers_stop (my_workers);
|
2016-09-11 14:37:41 +02:00
|
|
|
pubsubd_channels_del_all (&chans);
|
2016-09-14 00:20:04 +02:00
|
|
|
pubsubd_workers_del_all (my_workers);
|
|
|
|
|
|
|
|
free (my_workers);
|
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
|
2016-06-06 02:25:28 +02:00
|
|
|
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
|
|
|
}
|