From f0dbecd67f2ad206710e390004dc72d5af7984aa Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Mon, 6 Jun 2016 02:25:28 +0200 Subject: [PATCH] lack the program core, most util functions here --- lib/pubsubd.c | 41 +++++++++++++++++++++++------------------ lib/pubsubd.h | 10 +++++++--- pubsub/pubsubd.c | 40 +++++++--------------------------------- 3 files changed, 37 insertions(+), 54 deletions(-) diff --git a/lib/pubsubd.c b/lib/pubsubd.c index 55d8db1..a1e3994 100644 --- a/lib/pubsubd.c +++ b/lib/pubsubd.c @@ -1,12 +1,6 @@ #include "pubsubd.h" #include -void -ohshit(int rvalue, const char* str) { - fprintf(stderr, "%s\n", str); - exit(rvalue); -} - // CHANNELS void pubsubd_channels_init (struct channels *chans) { LIST_INIT(chans); } @@ -27,7 +21,7 @@ pubsubd_channels_del (struct channels *chans, struct channel *c) struct channel *todel = pubsubd_channel_get (chans, c); if(todel != NULL) { LIST_REMOVE(todel, entries); - srv_process_free (todel); + pubsubd_channel_free (todel); free (todel); todel = NULL; } @@ -41,18 +35,22 @@ struct channel * pubsubd_channel_copy (struct channel *c) return copy; } +void pubsubd_channel_free (struct channel * c) +{ +} + struct channel * pubsubd_channel_get (struct channels *chans, struct channel *c) { struct channel * np = NULL; LIST_FOREACH(np, chans, entries) { - if (pubsubd_channels_eq (np, c)) + if (pubsubd_channel_eq (np, c)) return np; } return NULL; } int -pubsubd_channels_eq (const struct channel *c1, const struct channel *c2) +pubsubd_channel_eq (const struct channel *c1, const struct channel *c2) { return (strncmp (c1->chan, c2->chan, c1->chanlen) == 0); } @@ -61,7 +59,7 @@ pubsubd_channels_eq (const struct channel *c1, const struct channel *c2) void pubsubd_subscriber_init (struct app_list_head *chans) { LIST_INIT(chans); } -struct app_list_elm * pubsubd_app_list_elm_copy (struct app_list_elm *ale) +struct app_list_elm * pubsubd_app_list_elm_copy (const struct app_list_elm *ale) { if (ale == NULL) return NULL; @@ -74,8 +72,15 @@ struct app_list_elm * pubsubd_app_list_elm_copy (struct app_list_elm *ale) return n; } +int +pubsubd_subscriber_eq (const struct app_list_elm *ale1, const struct app_list_elm *ale2) +{ + return srv_process_eq (ale1->p, ale2->p); +} + + void -pubsubd_subscriber_add (struct app_list_head *alh, struct app_list_elm *ale) +pubsubd_subscriber_add (struct app_list_head *alh, const struct app_list_elm *ale) { if(!alh || !ale) return; @@ -89,7 +94,7 @@ pubsubd_subscriber_get (const struct app_list_head *chans, const struct app_list { struct app_list_elm *np, *res = NULL; LIST_FOREACH(np, chans, entries) { - if(srv_process_eq (np, p)) { + if(pubsubd_subscriber_eq (np, p)) { res = np; } } @@ -119,7 +124,7 @@ void pubsubd_app_list_elm_create (struct app_list_elm *ale, struct process *p) void pubsubd_app_list_elm_free (struct app_list_elm *todel) { if (todel == NULL) - return NULL; + return; srv_process_free (todel->p); } @@ -137,10 +142,10 @@ void pubsubd_msg_serialize (const struct pubsub_msg *msg, char **data, size_t *l size_t i = 0; data[0][i] = msg->type; i++; - memcpy (data[0][i], msg->chanlen, sizeof(size_t)); i += sizeof(size_t); - memcpy (data[0][i], msg->chan, msg->chanlen); i += msg->chanlen; - memcpy (data[0][i], msg->datalen, sizeof(size_t)); i += sizeof(size_t); - memcpy (data[0][i], msg->data, msg->datalen); i += msg->datalen; + memcpy (&data[0][i], &msg->chanlen, sizeof(size_t)); i += sizeof(size_t); + memcpy (&data[0][i], msg->chan, msg->chanlen); i += msg->chanlen; + memcpy (&data[0][i], &msg->datalen, sizeof(size_t)); i += sizeof(size_t); + memcpy (&data[0][i], msg->data, msg->datalen); i += msg->datalen; } void pubsubd_msg_unserialize (struct pubsub_msg *msg, const char *data, size_t len) @@ -149,7 +154,7 @@ void pubsubd_msg_unserialize (struct pubsub_msg *msg, const char *data, size_t l return; size_t i = 0; - msg->type = data[0][i]; i++; + msg->type = data[i]; i++; memcpy (&msg->chanlen, data + i, sizeof(size_t)); i += sizeof(size_t); msg->chan = malloc (msg->chanlen); memcpy (msg->chan, data + i, msg->chanlen); i += msg->chanlen; diff --git a/lib/pubsubd.h b/lib/pubsubd.h index 0f20e01..0a6e149 100644 --- a/lib/pubsubd.h +++ b/lib/pubsubd.h @@ -33,11 +33,12 @@ LIST_HEAD(channels, channel); // element of the list struct channel { - unsigned char *chan; + char *chan; size_t chanlen; LIST_ENTRY(channel) entries; }; +void pubsubd_channels_init (struct channels *chans); struct channel * pubsubd_channel_copy (struct channel *c); struct channel * pubsubd_channel_get (struct channels *chans, struct channel *c); @@ -55,13 +56,16 @@ struct app_list_elm { LIST_ENTRY(app_list_elm) entries; }; -void pubsubd_subscriber_add (const struct app_list_head * +int +pubsubd_subscriber_eq (const struct app_list_elm *, const struct app_list_elm *); + +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 * , const struct app_list_elm *); void pubsubd_subscriber_del (struct app_list_head *al, struct app_list_elm *p); -struct app_list_elm * pubsubd_app_list_elm_copy (struct app_list_elm *ale); +struct app_list_elm * pubsubd_app_list_elm_copy (const struct app_list_elm *ale); void pubsubd_app_list_elm_create (struct app_list_elm *ale, struct process *p); void pubsubd_app_list_elm_free (struct app_list_elm *todel); diff --git a/pubsub/pubsubd.c b/pubsub/pubsubd.c index 39e333d..a8e38a5 100644 --- a/pubsub/pubsubd.c +++ b/pubsub/pubsubd.c @@ -12,10 +12,10 @@ main(int argc, char* argv[]) { struct service srv; srv_init (&srv, PUBSUB_SERVICE_NAME); - printf ("Listening on %s.\n", srv->spath); + printf ("Listening on %s.\n", srv.spath); // creates the service named pipe, that listens to client applications - if (service_create (&srv)) + if (srv_create (&srv)) ohshit(1, "service_create error"); struct channels chans; @@ -23,45 +23,18 @@ main(int argc, char* argv[]) for (;;) { struct process proc; - int proc_count, i; - - service_get_new_process (&proc, &srv); - - printf("> %i proc\n", proc_count); - - for (i = 0; i < proc_count; i++) { - size_t message_size = BUFSIZ; - char buffer[BUFSIZ]; - - process_print(proc + i); - - if (process_read (&proc[i], &buffer, &message_size)) - ohshit(1, "process_read error"); - - printf(": %s\n", buffer); - - - } - - service_free_processes(&proc, proc_count); + srv_get_new_process (&proc, &srv); + process_print (&proc); } // the application will shut down, and remove the service named pipe - if (service_close (s_path)) + if (srv_close (&srv)) ohshit(1, "service_close error"); return EXIT_SUCCESS; } - -/* - * main loop - * - * opens the application pipes, - * reads then writes the same message, - * then closes the pipes - */ - +#if 0 void main_loop (const struct service *srv) { int ret; @@ -106,3 +79,4 @@ void main_loop (const struct service *srv) printf ("\033[32mStill \033[31m%d\033[32m applications to serve\n",cnt); } } +#endif