Obsolete
/
libipc-old
Archived
3
0
Fork 0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues/pull-requests.
libipc-old/lib/pubsubd.c

196 lines
4.7 KiB
C
Raw Normal View History

2016-06-05 20:48:13 +02:00
#include "pubsubd.h"
#include <stdlib.h>
// CHANNELS
void pubsubd_channels_init (struct channels *chans) { LIST_INIT(chans); }
void
pubsubd_channels_add (struct channels *chans, struct channel *c)
{
if(!chans || !c)
return;
struct channel *n = pubsubd_channel_copy (c);
LIST_INSERT_HEAD(chans, n, entries);
}
void
pubsubd_channels_del (struct channels *chans, struct channel *c)
{
struct channel *todel = pubsubd_channel_get (chans, c);
if(todel != NULL) {
LIST_REMOVE(todel, entries);
pubsubd_channel_free (todel);
2016-06-05 20:48:13 +02:00
free (todel);
todel = NULL;
}
}
struct channel * pubsubd_channel_copy (struct channel *c)
{
struct channel *copy;
copy = malloc (sizeof(struct channel));
memcpy (copy, c, sizeof(struct channel));
return copy;
}
void pubsubd_channel_free (struct channel * c)
{
}
2016-06-05 20:48:13 +02:00
struct channel * pubsubd_channel_get (struct channels *chans, struct channel *c)
{
struct channel * np = NULL;
LIST_FOREACH(np, chans, entries) {
if (pubsubd_channel_eq (np, c))
2016-06-05 20:48:13 +02:00
return np;
}
return NULL;
}
int
pubsubd_channel_eq (const struct channel *c1, const struct channel *c2)
2016-06-05 20:48:13 +02:00
{
return (strncmp (c1->chan, c2->chan, c1->chanlen) == 0);
}
// SUBSCRIBER
void pubsubd_subscriber_init (struct app_list_head *chans) { LIST_INIT(chans); }
struct app_list_elm * pubsubd_app_list_elm_copy (const struct app_list_elm *ale)
2016-06-05 20:48:13 +02:00
{
if (ale == NULL)
return NULL;
struct app_list_elm * n;
n = malloc (sizeof (struct app_list_elm));
n->p = srv_process_copy(ale->p);
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);
}
2016-06-05 20:48:13 +02:00
void
pubsubd_subscriber_add (struct app_list_head *alh, const struct app_list_elm *ale)
2016-06-05 20:48:13 +02:00
{
if(!alh || !ale)
return;
struct app_list_elm *n = pubsubd_app_list_elm_copy (ale);
LIST_INSERT_HEAD(alh, n, entries);
}
struct app_list_elm *
pubsubd_subscriber_get (const struct app_list_head *chans, const struct app_list_elm *p)
{
struct app_list_elm *np, *res = NULL;
LIST_FOREACH(np, chans, entries) {
if(pubsubd_subscriber_eq (np, p)) {
2016-06-05 20:48:13 +02:00
res = np;
}
}
return res;
}
void
pubsubd_subscriber_del (struct app_list_head *chans, struct app_list_elm *p)
{
struct app_list_elm *todel = pubsubd_subscriber_get (chans, p);
if(todel != NULL) {
LIST_REMOVE(todel, entries);
pubsubd_app_list_elm_free (todel);
free (todel);
todel = NULL;
}
}
void pubsubd_app_list_elm_create (struct app_list_elm *ale, struct process *p)
{
if (ale == NULL)
return;
ale->p = srv_process_copy (p);
}
void pubsubd_app_list_elm_free (struct app_list_elm *todel)
{
if (todel == NULL)
return;
2016-06-05 20:48:13 +02:00
srv_process_free (todel->p);
}
// MESSAGE, TODO CBOR
void pubsubd_msg_serialize (const struct pubsub_msg *msg, char **data, size_t *len)
{
if (msg == NULL || data == NULL || len == NULL)
return;
// msg: "type(1) chanlen(8) chan datalen(8) data
*len = 1 + sizeof(size_t) + msg->chanlen + sizeof(size_t) + msg->datalen;
*data = malloc(*len);
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;
2016-06-05 20:48:13 +02:00
}
void pubsubd_msg_unserialize (struct pubsub_msg *msg, const char *data, size_t len)
{
if (msg == NULL || data == NULL)
return;
size_t i = 0;
msg->type = data[i]; i++;
2016-06-05 20:48:13 +02:00
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;
memcpy (&msg->datalen, data + i, sizeof(size_t)); i += sizeof(size_t);
msg->data = malloc (msg->datalen);
memcpy (msg->data, data + i, msg->datalen); i += msg->datalen;
}
void pubsubd_msg_free (struct pubsub_msg *msg)
{
if (msg->chan) {
free (msg->chan);
msg->chan = 0;
}
if (msg->data) {
free (msg->data);
msg->data = 0;
}
}
// COMMUNICATION
void pubsubd_msg_send (struct service *s, struct pubsub_msg * m, struct process *p)
{
}
void pubsubd_msg_recv (struct service *s, struct pubsub_msg * m, struct process *p)
{
}
void pubsub_msg_send (struct service *s, struct pubsub_msg * m)
{
}
void pubsub_msg_recv (struct service *s, struct pubsub_msg * m)
{
}
// SERVICE
void pubsubd_srv_init ();