Archived
3
0

pubsubd: print all structures

This commit is contained in:
Philippe PITTOLI 2016-09-11 01:47:25 +02:00
parent 80c5201b04
commit f26406f32f
5 changed files with 62 additions and 49 deletions

View File

@ -8,12 +8,17 @@
void pubsubd_channels_init (struct channels *chans) { LIST_INIT(chans); } void pubsubd_channels_init (struct channels *chans) { LIST_INIT(chans); }
struct channel * 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; 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); LIST_INSERT_HEAD(chans, n, entries);
return n; return n;
@ -64,6 +69,9 @@ struct channel * pubsubd_channel_copy (struct channel *c)
memcpy (copy->chan, c->chan, c->chanlen); memcpy (copy->chan, c->chan, c->chanlen);
copy->chanlen = c->chanlen; copy->chanlen = c->chanlen;
} }
else {
printf ("pubsubd_channel_copy: c->chan == NULL\n");
}
return copy; 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 * pubsubd_channel_get (struct channels *chans, struct channel *c)
{ {
struct channel * np = NULL; struct channel * np = NULL;
@ -136,8 +155,11 @@ void pubsubd_channels_print (const struct channels *chans)
{ {
printf ("\033[36mmchannels\033[00m\n"); printf ("\033[36mmchannels\033[00m\n");
if (chans == NULL) if (chans == NULL) {
// TODO debug
printf ("pubsubd_channels_print: chans == NULL\n");
return ; return ;
}
struct channel *chan = NULL; struct channel *chan = NULL;
LIST_FOREACH(chan, chans, entries) { 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) 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; return;
}
printf ( "\033[32mchan %s\033[00m\n", c->chan); 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; 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) 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 void
pubsubd_subscriber_add (struct app_list_head *alh, const struct app_list_elm *ale) 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; return;
}
struct app_list_elm *n = pubsubd_app_list_elm_copy (ale); struct app_list_elm *n = pubsubd_app_list_elm_copy (ale);
LIST_INSERT_HEAD(alh, n, entries); 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; 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 int
pubsubd_subscriber_del (struct app_list_head *alh, struct app_list_elm *p) 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) void pubsubd_app_list_elm_create (struct app_list_elm *ale, struct process *p)
{ {
if (ale == NULL) 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 else { // everything else is about killing the service
ale->action = PUBSUB_QUIT; ale->action = PUBSUB_QUIT;
} }
printf ("ACTION : %s\n", token);
break; break;
} }
case 5 : { case 5 : {
// for the last element of the line // for the last element of the line
// drop the following \n // drop the following \n
if (ale->action != PUBSUB_QUIT) { if (ale->action != PUBSUB_QUIT) {
printf ("REQUESTED CHAN : %s", token);
memcpy (chan, token, (strlen (token) < BUFSIZ) ? memcpy (chan, token, (strlen (token) < BUFSIZ) ?
strlen (token) -1 : 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)); memset (ale->p, 0, sizeof (struct process));
srv_process_gen (ale->p, pid, index, version); 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'; chan[BUFSIZ -1] = '\0';
printf ("AVANT\n");
pubsubd_channel_new (*c, chan);
printf ("APRES\n");
// not found = new
struct channel *new_chan = NULL; struct channel *new_chan = NULL;
new_chan = pubsubd_channel_get (chans, *c); new_chan = pubsubd_channel_search (chans, chan);
if (new_chan == NULL) { 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_subscriber_init (&new_chan->alh);
} }
pubsubd_channel_free (*c);
*c = new_chan; *c = new_chan;
// add the subscriber // add the subscriber
if (ale->action == PUBSUB_SUB || ale->action == PUBSUB_BOTH) if (ale->action == PUBSUB_SUB || ale->action == PUBSUB_BOTH) {
pubsubd_subscriber_add (new_chan->alh, ale); printf ("new process in chan %s\n", chan);
pubsubd_subscriber_add ((*c)->alh, ale);
}
return 0; return 0;
} }

View File

@ -70,9 +70,10 @@ void pubsubd_channel_print (const struct channel *c);
// list of channels // list of channels
void pubsubd_channels_init (struct channels *chans); void pubsubd_channels_init (struct channels *chans);
void pubsubd_channels_print (const 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 (struct channels *chans, struct channel *c);
void pubsubd_channels_del_all (struct channels *chans); 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) // remove an app_list_elm from the list (msg type DISCONNECT received)
int pubsubd_channels_del_subscriber (struct channels *chans 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 *); 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_init (struct app_list_head **chans);
void pubsubd_subscriber_print (struct app_list_head *alh);
void pubsubd_subscriber_add (struct app_list_head * void pubsubd_subscriber_add (struct app_list_head *
, const struct app_list_elm *); , const struct app_list_elm *);
struct app_list_elm * pubsubd_subscriber_get (const struct app_list_head * struct app_list_elm * pubsubd_subscriber_get (const struct app_list_head *

View File

@ -30,26 +30,17 @@ main(int argc, char **argv, char **env)
// struct app_list_elm ale1; // struct app_list_elm ale1;
// memset (&ale1, 0, sizeof (struct app_list_elm)); // 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 // FIRST CHAN TO BE ADDED
// search for the chan in channels, add it if not found // search for the chan in channels, add it if not found
struct channel *new_chan = NULL; struct channel *new_chan = NULL;
new_chan = pubsubd_channel_get (&chans, &chan); new_chan = pubsubd_channel_search (&chans, "coucou");
if (new_chan == NULL) { if (new_chan == NULL) {
new_chan = pubsubd_channels_add (&chans, &chan); new_chan = pubsubd_channels_add (&chans, "coucou");
pubsubd_subscriber_init (&new_chan->alh); pubsubd_subscriber_init (&new_chan->alh);
} }
else { else {
ohshit (2, "error : new chan, can't be found in channels yet"); ohshit (2, "error : new chan, can't be found in channels yet");
} }
pubsubd_channel_free (&chan);
printf ("print the channels, 1 chan\n"); printf ("print the channels, 1 chan\n");
printf ("--\n"); printf ("--\n");
@ -57,9 +48,8 @@ main(int argc, char **argv, char **env)
printf ("--\n"); printf ("--\n");
// SAME CHAN, SHOULD NOT BE ADDED // SAME CHAN, SHOULD NOT BE ADDED
pubsubd_channel_new (&chan, "coucou");
// search for the chan in channels, add it if not found // 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) { if (new_chan == NULL) {
ohshit (3, "error : same chan, shouldn't be added in channels"); ohshit (3, "error : same chan, shouldn't be added in channels");
} }
@ -73,17 +63,15 @@ main(int argc, char **argv, char **env)
printf ("--\n"); printf ("--\n");
// NEW CHAN, SHOULD BE ADDED // NEW CHAN, SHOULD BE ADDED
pubsubd_channel_new (&chan, "salut");
// search for the chan in channels, add it if not found // 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) { if (new_chan == NULL) {
new_chan = pubsubd_channels_add (&chans, &chan); new_chan = pubsubd_channels_add (&chans, "salut");
pubsubd_subscriber_init (&new_chan->alh); pubsubd_subscriber_init (&new_chan->alh);
} }
else { else {
ohshit (4, "error : new chan, should be added in channels"); ohshit (4, "error : new chan, should be added in channels");
} }
pubsubd_channel_free (&chan);
printf ("print the channels, 2 chans\n"); printf ("print the channels, 2 chans\n");
printf ("--\n"); printf ("--\n");

View File

@ -37,7 +37,7 @@ main(int argc, char **argv)
pubsubd_get_new_process (spath, &ale, &chans, &c); pubsubd_get_new_process (spath, &ale, &chans, &c);
printf ("print the channels, %d chan\n", i); printf ("print the channels\n");
printf ("--\n"); printf ("--\n");
pubsubd_channels_print (&chans); pubsubd_channels_print (&chans);
printf ("--\n"); printf ("--\n");

View File

@ -2,6 +2,6 @@
for i in $(seq 1 10) for i in $(seq 1 10)
do do
echo "${i} 1 1 pub chan1" > /tmp/ipc/gen echo "${i} 1 1 sub chan1" > /tmp/ipc/gen
sleep 0.1 sleep 0.1
done done