From f26406f32ff6c91871d93df64948241781c1174b Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sun, 11 Sep 2016 01:47:25 +0200 Subject: [PATCH] pubsubd: print all structures --- lib/pubsubd.c | 81 ++++++++++++++++++++++------------ lib/pubsubd.h | 4 +- pubsub/test-chan-lists.c | 22 +++------ pubsub/test-gen-new-process.c | 2 +- pubsub/test-gen-new-process.sh | 2 +- 5 files changed, 62 insertions(+), 49 deletions(-) diff --git a/lib/pubsubd.c b/lib/pubsubd.c index 72a6e86..5d8afe5 100644 --- a/lib/pubsubd.c +++ b/lib/pubsubd.c @@ -8,12 +8,17 @@ void pubsubd_channels_init (struct channels *chans) { LIST_INIT(chans); } struct channel * -pubsubd_channels_add (struct channels *chans, struct channel *c) +pubsubd_channels_add (struct channels *chans, const char *chan) { - if(!chans || !c) + if(chans == NULL || chan == NULL) { + printf ("pubsubd_channels_add: chans == NULL or chan == NULL"); return NULL; + } + + struct channel *n = malloc (sizeof (struct channel));; + memset (n, 0, sizeof (struct channel)); + pubsubd_channel_new (n, chan); - struct channel *n = pubsubd_channel_copy (c); LIST_INSERT_HEAD(chans, n, entries); return n; @@ -64,6 +69,9 @@ struct channel * pubsubd_channel_copy (struct channel *c) memcpy (copy->chan, c->chan, c->chanlen); copy->chanlen = c->chanlen; } + else { + printf ("pubsubd_channel_copy: c->chan == NULL\n"); + } return copy; } @@ -102,6 +110,17 @@ void pubsubd_channel_free (struct channel * c) } } +struct channel * pubsubd_channel_search (struct channels *chans, char *chan) +{ + struct channel * np = NULL; + LIST_FOREACH(np, chans, entries) { + if (np->chanlen == strlen (chan) + 1 + && strncmp (np->chan, chan, np->chanlen)) + return np; + } + return NULL; +} + struct channel * pubsubd_channel_get (struct channels *chans, struct channel *c) { struct channel * np = NULL; @@ -136,8 +155,11 @@ void pubsubd_channels_print (const struct channels *chans) { printf ("\033[36mmchannels\033[00m\n"); - if (chans == NULL) + if (chans == NULL) { + // TODO debug + printf ("pubsubd_channels_print: chans == NULL\n"); return ; + } struct channel *chan = NULL; LIST_FOREACH(chan, chans, entries) { @@ -147,19 +169,19 @@ void pubsubd_channels_print (const struct channels *chans) void pubsubd_channel_print (const struct channel *c) { - if (c == NULL || c->chan == NULL) + if (c == NULL || c->chan == NULL) { + printf ("pubsubd_channel_print: c == NULL or c->chan == NULL\n"); return; + } printf ( "\033[32mchan %s\033[00m\n", c->chan); - if (c->alh == NULL) + if (c->alh == NULL) { + printf ("pubsubd_channel_print: c->alh == NULL\n"); return; - - struct app_list_elm *ale = NULL; - LIST_FOREACH(ale, c->alh, entries) { - printf ("\t"); - srv_process_print (ale->p); } + + pubsubd_subscriber_print (c->alh); } struct app_list_elm * pubsubd_app_list_elm_copy (const struct app_list_elm *ale) @@ -189,8 +211,10 @@ pubsubd_subscriber_eq (const struct app_list_elm *ale1, const struct app_list_el void pubsubd_subscriber_add (struct app_list_head *alh, const struct app_list_elm *ale) { - if(!alh || !ale) + if(alh == NULL || ale == NULL) { + fprintf (stderr, "err alh or ale is NULL\n"); return; + } struct app_list_elm *n = pubsubd_app_list_elm_copy (ale); LIST_INSERT_HEAD(alh, n, entries); @@ -208,6 +232,15 @@ pubsubd_subscriber_get (const struct app_list_head *alh, const struct app_list_e return res; } +void pubsubd_subscriber_print (struct app_list_head *alh) +{ + struct app_list_elm *np = NULL; + LIST_FOREACH(np, alh, entries) { + printf ("\t"); + srv_process_print (np->p); + } +} + int pubsubd_subscriber_del (struct app_list_head *alh, struct app_list_elm *p) { @@ -239,7 +272,6 @@ void pubsubd_subscriber_del_all (struct app_list_head *alh) } } - void pubsubd_app_list_elm_create (struct app_list_elm *ale, struct process *p) { if (ale == NULL) @@ -421,15 +453,12 @@ int pubsubd_get_new_process (const char *spath, struct app_list_elm *ale else { // everything else is about killing the service ale->action = PUBSUB_QUIT; } - - printf ("ACTION : %s\n", token); break; } case 5 : { // for the last element of the line // drop the following \n if (ale->action != PUBSUB_QUIT) { - printf ("REQUESTED CHAN : %s", token); memcpy (chan, token, (strlen (token) < BUFSIZ) ? strlen (token) -1 : BUFSIZ); } @@ -456,29 +485,23 @@ int pubsubd_get_new_process (const char *spath, struct app_list_elm *ale memset (ale->p, 0, sizeof (struct process)); srv_process_gen (ale->p, pid, index, version); - if (*c == NULL) { - *c = malloc (sizeof (struct channel)); - memset (*c, 0, sizeof (struct channel)); - } - chan[BUFSIZ -1] = '\0'; - printf ("AVANT\n"); - pubsubd_channel_new (*c, chan); - printf ("APRES\n"); + // not found = new struct channel *new_chan = NULL; - new_chan = pubsubd_channel_get (chans, *c); + new_chan = pubsubd_channel_search (chans, chan); if (new_chan == NULL) { - new_chan = pubsubd_channels_add (chans, *c); + new_chan = pubsubd_channels_add (chans, chan); pubsubd_subscriber_init (&new_chan->alh); } - pubsubd_channel_free (*c); *c = new_chan; // add the subscriber - if (ale->action == PUBSUB_SUB || ale->action == PUBSUB_BOTH) - pubsubd_subscriber_add (new_chan->alh, ale); + if (ale->action == PUBSUB_SUB || ale->action == PUBSUB_BOTH) { + printf ("new process in chan %s\n", chan); + pubsubd_subscriber_add ((*c)->alh, ale); + } return 0; } diff --git a/lib/pubsubd.h b/lib/pubsubd.h index 2dd3e63..99e1826 100644 --- a/lib/pubsubd.h +++ b/lib/pubsubd.h @@ -70,9 +70,10 @@ void pubsubd_channel_print (const struct channel *c); // list of channels void pubsubd_channels_init (struct channels *chans); void pubsubd_channels_print (const struct channels *chans); -struct channel * pubsubd_channels_add (struct channels *chans, struct channel *c); +struct channel * pubsubd_channels_add (struct channels *chans, const char *chan); void pubsubd_channels_del (struct channels *chans, struct channel *c); void pubsubd_channels_del_all (struct channels *chans); +struct channel * pubsubd_channel_search (struct channels *chans, char *chan); // remove an app_list_elm from the list (msg type DISCONNECT received) int pubsubd_channels_del_subscriber (struct channels *chans @@ -100,6 +101,7 @@ int pubsubd_subscriber_eq (const struct app_list_elm *, const struct app_list_elm *); void pubsubd_subscriber_init (struct app_list_head **chans); +void pubsubd_subscriber_print (struct app_list_head *alh); void pubsubd_subscriber_add (struct app_list_head * , const struct app_list_elm *); struct app_list_elm * pubsubd_subscriber_get (const struct app_list_head * diff --git a/pubsub/test-chan-lists.c b/pubsub/test-chan-lists.c index 826b64b..7a78173 100644 --- a/pubsub/test-chan-lists.c +++ b/pubsub/test-chan-lists.c @@ -30,26 +30,17 @@ main(int argc, char **argv, char **env) // struct app_list_elm ale1; // memset (&ale1, 0, sizeof (struct app_list_elm)); - // warning : this is a local structure, not exactly the same in the prog. - struct channel chan; - memset (&chan, 0, sizeof (struct channel)); - pubsubd_channel_new (&chan, "coucou"); - - // to emulate - // pubsubd_get_new_process (&srv, &ale1, &chans, &chan); - // FIRST CHAN TO BE ADDED // search for the chan in channels, add it if not found struct channel *new_chan = NULL; - new_chan = pubsubd_channel_get (&chans, &chan); + new_chan = pubsubd_channel_search (&chans, "coucou"); if (new_chan == NULL) { - new_chan = pubsubd_channels_add (&chans, &chan); + new_chan = pubsubd_channels_add (&chans, "coucou"); pubsubd_subscriber_init (&new_chan->alh); } else { ohshit (2, "error : new chan, can't be found in channels yet"); } - pubsubd_channel_free (&chan); printf ("print the channels, 1 chan\n"); printf ("--\n"); @@ -57,9 +48,8 @@ main(int argc, char **argv, char **env) printf ("--\n"); // SAME CHAN, SHOULD NOT BE ADDED - pubsubd_channel_new (&chan, "coucou"); // search for the chan in channels, add it if not found - new_chan = pubsubd_channel_get (&chans, &chan); + new_chan = pubsubd_channel_search (&chans, "coucou"); if (new_chan == NULL) { ohshit (3, "error : same chan, shouldn't be added in channels"); } @@ -73,17 +63,15 @@ main(int argc, char **argv, char **env) printf ("--\n"); // NEW CHAN, SHOULD BE ADDED - pubsubd_channel_new (&chan, "salut"); // search for the chan in channels, add it if not found - new_chan = pubsubd_channel_get (&chans, &chan); + new_chan = pubsubd_channel_search (&chans, "salut"); if (new_chan == NULL) { - new_chan = pubsubd_channels_add (&chans, &chan); + new_chan = pubsubd_channels_add (&chans, "salut"); pubsubd_subscriber_init (&new_chan->alh); } else { ohshit (4, "error : new chan, should be added in channels"); } - pubsubd_channel_free (&chan); printf ("print the channels, 2 chans\n"); printf ("--\n"); diff --git a/pubsub/test-gen-new-process.c b/pubsub/test-gen-new-process.c index 425e339..afb70df 100644 --- a/pubsub/test-gen-new-process.c +++ b/pubsub/test-gen-new-process.c @@ -37,7 +37,7 @@ main(int argc, char **argv) pubsubd_get_new_process (spath, &ale, &chans, &c); - printf ("print the channels, %d chan\n", i); + printf ("print the channels\n"); printf ("--\n"); pubsubd_channels_print (&chans); printf ("--\n"); diff --git a/pubsub/test-gen-new-process.sh b/pubsub/test-gen-new-process.sh index 0cd4f1c..0009bd5 100755 --- a/pubsub/test-gen-new-process.sh +++ b/pubsub/test-gen-new-process.sh @@ -2,6 +2,6 @@ for i in $(seq 1 10) do - echo "${i} 1 1 pub chan1" > /tmp/ipc/gen + echo "${i} 1 1 sub chan1" > /tmp/ipc/gen sleep 0.1 done