Archived
3
0

pubsubd: channel copy improved

This commit is contained in:
Philippe PITTOLI 2016-09-07 23:46:00 +02:00
parent 14fd9b84b9
commit e9169a633d
3 changed files with 28 additions and 29 deletions

View File

@ -59,15 +59,34 @@ struct channel * pubsubd_channel_copy (struct channel *c)
memcpy (copy, c, sizeof(struct channel));
if (c->chan != NULL) {
// copy->chan = strndup (c->chan, c->chanlen);
copy->chan = malloc (BUFSIZ);
memcpy (copy->chan, c->chan, BUFSIZ);
copy->chan = malloc (c->chanlen);
memset (copy->chan, 0, c->chanlen);
memcpy (copy->chan, c->chan, c->chanlen);
copy->chanlen = c->chanlen;
}
return copy;
}
int pubsubd_channel_new (struct channel *c, const char * name)
{
if (c == NULL) {
return 1;
}
size_t nlen = (strlen (name) > BUFSIZ) ? BUFSIZ : strlen (name) + 1;
printf ("NAME : %s, SIZE : %ld\n", name, nlen);
if (c->chan == NULL)
c->chan = malloc (nlen);
memset (c->chan, 0, nlen);
memcpy (c->chan, name, nlen);
c->chanlen = nlen;
return 0;
}
void pubsubd_channel_free (struct channel * c)
{
// TODO
@ -425,16 +444,9 @@ int pubsubd_get_new_process (struct service *srv, struct app_list_elm *ale
*c = malloc (sizeof (struct channel));
}
if (c[0]->chan != NULL) {
free (c[0]->chan);
c[0]->chan = NULL;
}
chan[BUFSIZ -1] = '\0';
// c[0]->chan = strndup (chan, BUFSIZ);
c[0]->chan = malloc (BUFSIZ);
memcpy(c[0]->chan, chan, BUFSIZ);
c[0]->chanlen = strlen (chan);
pubsubd_channel_new (*c, chan);
struct channel *new_chan = NULL;
new_chan = pubsubd_channel_get (chans, *c);

View File

@ -60,6 +60,7 @@ struct channel {
};
// simple channel
int pubsubd_channel_new (struct channel *c, const char *name);
struct channel * pubsubd_channel_copy (struct channel *c);
struct channel * pubsubd_channel_get (struct channels *chans, struct channel *c);
void pubsubd_channel_free (struct channel *c);

View File

@ -3,20 +3,6 @@
#define TEST_NAME "test-chan-lists"
int
create_chan (struct channel *c, const char * name) {
if (c == NULL) {
return 1;
}
if (c->chan == NULL)
c->chan = malloc (BUFSIZ);
memset (c->chan, 0, BUFSIZ);
memcpy (c->chan, name, strlen (name));
c->chanlen = strlen (name);
return 0;
}
void
ohshit(int rvalue, const char* str) {
fprintf(stderr, "%s\n", str);
@ -47,7 +33,7 @@ main(int argc, char **argv, char **env)
// warning : this is a local structure, not exactly the same in the prog.
struct channel chan;
memset (&chan, 0, sizeof (struct channel));
create_chan (&chan, "coucou");
pubsubd_channel_new (&chan, "coucou");
// to emulate
// pubsubd_get_new_process (&srv, &ale1, &chans, &chan);
@ -71,7 +57,7 @@ main(int argc, char **argv, char **env)
printf ("--\n");
// SAME CHAN, SHOULD NOT BE ADDED
create_chan (&chan, "coucou");
pubsubd_channel_new (&chan, "coucou");
// search for the chan in channels, add it if not found
new_chan = pubsubd_channel_get (&chans, &chan);
if (new_chan == NULL) {
@ -87,7 +73,7 @@ main(int argc, char **argv, char **env)
printf ("--\n");
// NEW CHAN, SHOULD BE ADDED
create_chan (&chan, "salut");
pubsubd_channel_new (&chan, "salut");
// search for the chan in channels, add it if not found
new_chan = pubsubd_channel_get (&chans, &chan);
if (new_chan == NULL) {