2016-06-05 20:48:13 +02:00
|
|
|
#include "pubsubd.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2016-06-12 12:38:43 +02:00
|
|
|
#include <string.h> // strndup
|
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
// CHANNELS
|
|
|
|
|
|
|
|
void pubsubd_channels_init (struct channels *chans) { LIST_INIT(chans); }
|
|
|
|
|
2016-06-07 17:44:18 +02:00
|
|
|
struct channel *
|
2016-06-05 20:48:13 +02:00
|
|
|
pubsubd_channels_add (struct channels *chans, struct channel *c)
|
|
|
|
{
|
|
|
|
if(!chans || !c)
|
2016-06-07 17:44:18 +02:00
|
|
|
return NULL;
|
2016-06-05 20:48:13 +02:00
|
|
|
|
|
|
|
struct channel *n = pubsubd_channel_copy (c);
|
|
|
|
LIST_INSERT_HEAD(chans, n, entries);
|
2016-06-07 17:44:18 +02:00
|
|
|
|
|
|
|
return n;
|
2016-06-05 20:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pubsubd_channels_del (struct channels *chans, struct channel *c)
|
|
|
|
{
|
|
|
|
struct channel *todel = pubsubd_channel_get (chans, c);
|
|
|
|
if(todel != NULL) {
|
2016-06-06 02:25:28 +02:00
|
|
|
pubsubd_channel_free (todel);
|
2016-06-07 17:44:18 +02:00
|
|
|
LIST_REMOVE(todel, entries);
|
2016-06-05 20:48:13 +02:00
|
|
|
free (todel);
|
|
|
|
todel = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 13:49:23 +02:00
|
|
|
void pubsubd_channels_del_all (struct channels *chans)
|
|
|
|
{
|
|
|
|
if (!chans)
|
|
|
|
return;
|
|
|
|
|
2016-06-10 01:21:04 +02:00
|
|
|
struct channel *c = NULL;
|
2016-06-07 13:49:23 +02:00
|
|
|
|
|
|
|
while (!LIST_EMPTY(chans)) {
|
|
|
|
c = LIST_FIRST(chans);
|
|
|
|
LIST_REMOVE(c, entries);
|
|
|
|
pubsubd_channel_free (c);
|
|
|
|
free (c);
|
|
|
|
c = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
struct channel * pubsubd_channel_copy (struct channel *c)
|
|
|
|
{
|
2016-06-07 17:44:18 +02:00
|
|
|
if (c == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2016-06-10 01:21:04 +02:00
|
|
|
struct channel *copy = NULL;
|
2016-06-05 20:48:13 +02:00
|
|
|
copy = malloc (sizeof(struct channel));
|
2016-06-10 20:02:58 +02:00
|
|
|
memset (copy, 0, sizeof (struct channel));
|
2016-06-07 17:44:18 +02:00
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
memcpy (copy, c, sizeof(struct channel));
|
2016-06-07 17:44:18 +02:00
|
|
|
|
|
|
|
if (c->chan != NULL) {
|
2016-09-07 23:46:00 +02:00
|
|
|
copy->chan = malloc (c->chanlen);
|
|
|
|
memset (copy->chan, 0, c->chanlen);
|
|
|
|
memcpy (copy->chan, c->chan, c->chanlen);
|
2016-06-07 17:44:18 +02:00
|
|
|
copy->chanlen = c->chanlen;
|
|
|
|
}
|
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2016-09-07 23:46:00 +02:00
|
|
|
int pubsubd_channel_new (struct channel *c, const char * name)
|
|
|
|
{
|
|
|
|
if (c == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-09 12:06:25 +02:00
|
|
|
size_t nlen = (strlen (name) > BUFSIZ) ? BUFSIZ : strlen (name);
|
2016-09-07 23:46:00 +02:00
|
|
|
|
|
|
|
printf ("NAME : %s, SIZE : %ld\n", name, nlen);
|
|
|
|
|
|
|
|
if (c->chan == NULL)
|
2016-09-09 12:06:25 +02:00
|
|
|
c->chan = malloc (nlen +1);
|
2016-09-07 23:46:00 +02:00
|
|
|
|
2016-09-09 12:06:25 +02:00
|
|
|
memset (c->chan, 0, nlen +1);
|
2016-09-07 23:46:00 +02:00
|
|
|
memcpy (c->chan, name, nlen);
|
|
|
|
c->chanlen = nlen;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-06 02:25:28 +02:00
|
|
|
void pubsubd_channel_free (struct channel * c)
|
|
|
|
{
|
2016-06-07 17:44:18 +02:00
|
|
|
if (c == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (c->chan != NULL) {
|
|
|
|
free (c->chan);
|
|
|
|
c->chan = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->alh != NULL) {
|
|
|
|
pubsubd_subscriber_del_all (c->alh);
|
|
|
|
free (c->alh);
|
|
|
|
}
|
2016-06-06 02:25:28 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-06-06 02:25:28 +02:00
|
|
|
if (pubsubd_channel_eq (np, c))
|
2016-06-05 20:48:13 +02:00
|
|
|
return np;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-06-06 02:25:28 +02:00
|
|
|
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
|
|
|
|
|
2016-06-07 17:44:18 +02:00
|
|
|
void pubsubd_subscriber_init (struct app_list_head **chans) {
|
|
|
|
if (chans == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (*chans == NULL)
|
|
|
|
*chans = malloc (sizeof(struct channels));
|
|
|
|
LIST_INIT(*chans);
|
|
|
|
}
|
2016-06-05 20:48:13 +02:00
|
|
|
|
2016-06-07 17:44:18 +02:00
|
|
|
void pubsubd_channels_print (const struct channels *chans)
|
2016-06-07 13:49:23 +02:00
|
|
|
{
|
2016-09-07 23:26:28 +02:00
|
|
|
printf ("\033[36mmchannels\033[00m\n");
|
2016-06-07 17:44:18 +02:00
|
|
|
|
|
|
|
if (chans == NULL)
|
|
|
|
return ;
|
2016-06-07 13:49:23 +02:00
|
|
|
|
2016-06-07 17:44:18 +02:00
|
|
|
struct channel *chan = NULL;
|
|
|
|
LIST_FOREACH(chan, chans, entries) {
|
|
|
|
pubsubd_channel_print (chan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pubsubd_channel_print (const struct channel *c)
|
|
|
|
{
|
|
|
|
if (c == NULL || c->chan == NULL)
|
|
|
|
return;
|
|
|
|
|
2016-09-07 23:26:28 +02:00
|
|
|
if (c->chan == NULL) {
|
|
|
|
printf ( "\033[32mchan name not available\033[00m\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf ( "\033[32mchan %s\033[00m\n", c->chan);
|
|
|
|
}
|
2016-06-07 17:44:18 +02:00
|
|
|
|
|
|
|
if (c->alh == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct app_list_elm *ale = NULL;
|
|
|
|
LIST_FOREACH(ale, c->alh, entries) {
|
2016-09-07 23:26:28 +02:00
|
|
|
printf ("\t");
|
2016-06-07 17:44:18 +02:00
|
|
|
srv_process_print (ale->p);
|
|
|
|
}
|
2016-06-07 13:49:23 +02:00
|
|
|
}
|
|
|
|
|
2016-06-06 02:25:28 +02:00
|
|
|
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;
|
|
|
|
|
2016-06-10 01:21:04 +02:00
|
|
|
struct app_list_elm * n = NULL;
|
2016-06-05 20:48:13 +02:00
|
|
|
n = malloc (sizeof (struct app_list_elm));
|
|
|
|
|
2016-06-07 17:44:18 +02:00
|
|
|
if (ale->p != NULL)
|
|
|
|
n->p = srv_process_copy (ale->p);
|
2016-06-05 20:48:13 +02:00
|
|
|
|
2016-06-08 17:57:05 +02:00
|
|
|
n->action = ale->action;
|
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2016-06-06 02:25:28 +02:00
|
|
|
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
|
2016-06-06 02:25:28 +02:00
|
|
|
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 *
|
2016-06-13 09:47:19 +02:00
|
|
|
pubsubd_subscriber_get (const struct app_list_head *alh, const struct app_list_elm *p)
|
2016-06-05 20:48:13 +02:00
|
|
|
{
|
2016-06-10 01:21:04 +02:00
|
|
|
struct app_list_elm *np = NULL, *res = NULL;
|
2016-06-13 09:47:19 +02:00
|
|
|
LIST_FOREACH(np, alh, entries) {
|
2016-06-06 02:25:28 +02:00
|
|
|
if(pubsubd_subscriber_eq (np, p)) {
|
2016-06-05 20:48:13 +02:00
|
|
|
res = np;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:47:19 +02:00
|
|
|
int
|
|
|
|
pubsubd_subscriber_del (struct app_list_head *alh, struct app_list_elm *p)
|
2016-06-05 20:48:13 +02:00
|
|
|
{
|
2016-06-13 09:47:19 +02:00
|
|
|
struct app_list_elm *todel = pubsubd_subscriber_get (alh, p);
|
2016-06-05 20:48:13 +02:00
|
|
|
if(todel != NULL) {
|
|
|
|
pubsubd_app_list_elm_free (todel);
|
2016-06-07 17:44:18 +02:00
|
|
|
LIST_REMOVE(todel, entries);
|
2016-06-05 20:48:13 +02:00
|
|
|
free (todel);
|
|
|
|
todel = NULL;
|
2016-06-13 09:47:19 +02:00
|
|
|
return 0;
|
2016-06-05 20:48:13 +02:00
|
|
|
}
|
2016-06-13 09:47:19 +02:00
|
|
|
|
|
|
|
return 1;
|
2016-06-05 20:48:13 +02:00
|
|
|
}
|
|
|
|
|
2016-06-07 17:44:18 +02:00
|
|
|
void pubsubd_subscriber_del_all (struct app_list_head *alh)
|
|
|
|
{
|
|
|
|
if (!alh)
|
|
|
|
return;
|
|
|
|
|
2016-06-10 01:21:04 +02:00
|
|
|
struct app_list_elm *ale = NULL;
|
2016-06-07 17:44:18 +02:00
|
|
|
|
|
|
|
while (!LIST_EMPTY(alh)) {
|
|
|
|
ale = LIST_FIRST(alh);
|
|
|
|
LIST_REMOVE(ale, entries);
|
|
|
|
pubsubd_app_list_elm_free (ale);
|
|
|
|
free (ale);
|
|
|
|
ale = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
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)
|
|
|
|
{
|
2016-06-07 17:44:18 +02:00
|
|
|
if (todel == NULL || todel->p == NULL)
|
2016-06-06 02:25:28 +02:00
|
|
|
return;
|
2016-09-09 12:06:25 +02:00
|
|
|
free (todel->p);
|
2016-06-05 20:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-06-13 09:47:19 +02:00
|
|
|
if (msg->type == PUBSUB_TYPE_DISCONNECT) {
|
|
|
|
*len = 1;
|
|
|
|
if (*data != NULL) {
|
|
|
|
free (*data);
|
|
|
|
*data = NULL;
|
|
|
|
}
|
|
|
|
*data = malloc(*len);
|
|
|
|
data[0][0] = msg->type;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// type + size chan + chan + size data + data
|
|
|
|
*len = 1 + 2 * sizeof(size_t) + msg->chanlen + msg->datalen;
|
|
|
|
}
|
2016-06-08 17:57:05 +02:00
|
|
|
|
|
|
|
if (*data != NULL) {
|
|
|
|
free (*data);
|
|
|
|
*data = NULL;
|
|
|
|
}
|
2016-06-05 20:48:13 +02:00
|
|
|
*data = malloc(*len);
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
2016-06-08 17:57:05 +02:00
|
|
|
data[0][i] = msg->type; i++;
|
2016-06-06 02:25:28 +02:00
|
|
|
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)
|
|
|
|
{
|
2016-06-08 17:57:05 +02:00
|
|
|
if (msg == NULL) {
|
|
|
|
fprintf (stderr
|
|
|
|
, "\033[31merr: pubsubd_msg_unserialize, msg NULL\033[00m\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data == NULL) {
|
|
|
|
fprintf (stderr
|
|
|
|
, "\033[31merr: pubsubd_msg_unserialize, data NULL\033[00m\n");
|
2016-06-05 20:48:13 +02:00
|
|
|
return;
|
2016-06-08 17:57:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len > BUFSIZ) {
|
|
|
|
fprintf (stderr
|
|
|
|
, "\033[31merr: pubsubd_msg_unserialize, len %ld\033[00m\n"
|
|
|
|
, len);
|
|
|
|
return;
|
|
|
|
}
|
2016-06-05 20:48:13 +02:00
|
|
|
|
|
|
|
size_t i = 0;
|
2016-06-08 17:57:05 +02:00
|
|
|
msg->type = data[i]; i++;
|
|
|
|
|
2016-06-13 09:47:19 +02:00
|
|
|
if (msg->type == PUBSUB_TYPE_DISCONNECT) {
|
|
|
|
msg->chanlen = 0;
|
|
|
|
msg->chan = NULL;
|
|
|
|
msg->datalen = 0;
|
|
|
|
msg->data = NULL;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
memcpy (&msg->chanlen, data + i, sizeof(size_t)); i += sizeof(size_t);
|
2016-06-08 17:57:05 +02:00
|
|
|
if (msg->chanlen > BUFSIZ) {
|
|
|
|
fprintf (stderr, "\033[31merr : msg->chanlen > BUFSIZ\033[00m\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-06-05 20:48:13 +02:00
|
|
|
msg->chan = malloc (msg->chanlen);
|
|
|
|
memcpy (msg->chan, data + i, msg->chanlen); i += msg->chanlen;
|
2016-06-08 17:57:05 +02:00
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
memcpy (&msg->datalen, data + i, sizeof(size_t)); i += sizeof(size_t);
|
2016-06-08 17:57:05 +02:00
|
|
|
if (msg->datalen > BUFSIZ) {
|
|
|
|
fprintf (stderr, "\033[31merr : msg->datalen > BUFSIZ\033[00m\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-06-05 20:48:13 +02:00
|
|
|
msg->data = malloc (msg->datalen);
|
|
|
|
memcpy (msg->data, data + i, msg->datalen); i += msg->datalen;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pubsubd_msg_free (struct pubsub_msg *msg)
|
|
|
|
{
|
2016-06-08 17:57:05 +02:00
|
|
|
if (msg == NULL) {
|
|
|
|
fprintf (stderr, "\033[31merr: pubsubd_msg_free, msg NULL\033[00m\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
if (msg->chan) {
|
|
|
|
free (msg->chan);
|
2016-06-08 17:57:05 +02:00
|
|
|
msg->chan = NULL;
|
2016-06-05 20:48:13 +02:00
|
|
|
}
|
|
|
|
if (msg->data) {
|
|
|
|
free (msg->data);
|
2016-06-08 17:57:05 +02:00
|
|
|
msg->data = NULL;
|
2016-06-05 20:48:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// COMMUNICATION
|
|
|
|
|
2016-06-07 17:44:18 +02:00
|
|
|
int pubsubd_get_new_process (struct service *srv, struct app_list_elm *ale
|
2016-06-08 17:57:05 +02:00
|
|
|
, struct channels *chans, struct channel **c)
|
2016-06-05 20:48:13 +02:00
|
|
|
{
|
2016-06-07 17:44:18 +02:00
|
|
|
if (srv == NULL || ale == NULL || chans == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2016-06-10 01:21:04 +02:00
|
|
|
char *buf = NULL;
|
|
|
|
size_t msize = 0;
|
2016-06-07 11:45:21 +02:00
|
|
|
srv_get_listen_raw (srv, &buf, &msize);
|
|
|
|
|
|
|
|
// parse pubsubd init msg (sent in TMPDIR/<service>)
|
|
|
|
//
|
2016-06-07 18:09:37 +02:00
|
|
|
// line fmt : pid index version action chan
|
2016-06-07 17:44:18 +02:00
|
|
|
// action : quit | pub | sub
|
2016-06-07 11:45:21 +02:00
|
|
|
|
2016-06-10 01:21:04 +02:00
|
|
|
size_t i = 0;
|
|
|
|
char *str = NULL, *token = NULL, *saveptr = NULL;
|
2016-06-07 11:45:21 +02:00
|
|
|
|
2016-06-10 01:21:04 +02:00
|
|
|
pid_t pid = 0;
|
|
|
|
int index = 0;
|
|
|
|
int version = 0;
|
2016-06-07 11:45:21 +02:00
|
|
|
|
2016-09-07 23:26:28 +02:00
|
|
|
// chan name
|
2016-06-07 17:44:18 +02:00
|
|
|
char chan[BUFSIZ];
|
2016-06-10 20:02:58 +02:00
|
|
|
memset (chan, 0, BUFSIZ);
|
2016-06-07 17:44:18 +02:00
|
|
|
|
2016-09-09 01:36:44 +02:00
|
|
|
printf ("INIT: %s\n", buf);
|
|
|
|
|
2016-06-07 11:45:21 +02:00
|
|
|
for (str = buf, i = 1; ; str = NULL, i++) {
|
|
|
|
token = strtok_r(str, " ", &saveptr);
|
|
|
|
if (token == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case 1 : pid = strtoul(token, NULL, 10); break;
|
|
|
|
case 2 : index = strtoul(token, NULL, 10); break;
|
|
|
|
case 3 : version = strtoul(token, NULL, 10); break;
|
2016-06-07 17:44:18 +02:00
|
|
|
case 4 : {
|
|
|
|
if (strncmp("both", token, 4) == 0) {
|
|
|
|
ale->action = PUBSUB_BOTH;
|
2016-06-07 11:45:21 +02:00
|
|
|
}
|
2016-06-07 17:44:18 +02:00
|
|
|
else if (strncmp("pub", token, 3) == 0) {
|
|
|
|
ale->action = PUBSUB_PUB;
|
|
|
|
}
|
2016-06-07 11:45:21 +02:00
|
|
|
else if (strncmp("sub", token, 3) == 0) {
|
2016-06-07 17:44:18 +02:00
|
|
|
ale->action = PUBSUB_SUB;
|
2016-06-07 13:49:23 +02:00
|
|
|
}
|
|
|
|
else { // everything else is about killing the service
|
2016-06-07 17:44:18 +02:00
|
|
|
ale->action = PUBSUB_QUIT;
|
2016-06-07 11:45:21 +02:00
|
|
|
}
|
2016-06-07 18:09:37 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 5 : {
|
|
|
|
if (ale->action != PUBSUB_QUIT)
|
|
|
|
memcpy (chan, token, (strlen (token) < BUFSIZ) ?
|
2016-09-09 01:36:44 +02:00
|
|
|
strlen (token) -1 : BUFSIZ);
|
2016-06-07 18:09:37 +02:00
|
|
|
break;
|
2016-06-07 11:45:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 17:44:18 +02:00
|
|
|
if (buf != NULL) {
|
|
|
|
free (buf);
|
|
|
|
buf = NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-08 17:57:05 +02:00
|
|
|
if (ale->action == PUBSUB_QUIT) {
|
|
|
|
return 0;
|
2016-06-07 17:44:18 +02:00
|
|
|
}
|
|
|
|
|
2016-06-07 18:09:37 +02:00
|
|
|
if (ale->p != NULL) {
|
|
|
|
free (ale->p);
|
|
|
|
ale->p = NULL;
|
|
|
|
}
|
2016-06-08 17:57:05 +02:00
|
|
|
|
|
|
|
ale->p = malloc (sizeof (struct process));
|
|
|
|
srv_process_gen (ale->p, pid, index, version);
|
|
|
|
|
|
|
|
if (*c == NULL) {
|
|
|
|
*c = malloc (sizeof (struct channel));
|
|
|
|
}
|
|
|
|
|
|
|
|
chan[BUFSIZ -1] = '\0';
|
2016-09-07 23:46:00 +02:00
|
|
|
pubsubd_channel_new (*c, chan);
|
|
|
|
|
2016-06-08 17:57:05 +02:00
|
|
|
|
2016-06-10 01:21:04 +02:00
|
|
|
struct channel *new_chan = NULL;
|
2016-06-08 17:57:05 +02:00
|
|
|
new_chan = pubsubd_channel_get (chans, *c);
|
|
|
|
if (new_chan == NULL) {
|
|
|
|
new_chan = pubsubd_channels_add (chans, *c);
|
|
|
|
pubsubd_subscriber_init (&new_chan->alh);
|
|
|
|
}
|
|
|
|
|
|
|
|
pubsubd_channel_free (*c);
|
|
|
|
*c = new_chan;
|
|
|
|
|
2016-06-07 17:44:18 +02:00
|
|
|
// add the subscriber
|
|
|
|
if (ale->action == PUBSUB_SUB || ale->action == PUBSUB_BOTH)
|
|
|
|
pubsubd_subscriber_add (new_chan->alh, ale);
|
2016-06-07 11:45:21 +02:00
|
|
|
|
|
|
|
return 0;
|
2016-06-05 20:48:13 +02:00
|
|
|
}
|
2016-06-07 11:45:21 +02:00
|
|
|
|
2016-06-08 17:57:05 +02:00
|
|
|
// alh from the channel, message to send
|
2016-06-07 11:45:21 +02:00
|
|
|
void pubsubd_msg_send (const struct app_list_head *alh, const struct pubsub_msg * m)
|
|
|
|
{
|
2016-06-08 17:57:05 +02:00
|
|
|
struct app_list_elm * ale = NULL;
|
|
|
|
|
2016-06-10 01:21:04 +02:00
|
|
|
char *buf = NULL;
|
|
|
|
size_t msize = 0;
|
2016-06-08 17:57:05 +02:00
|
|
|
pubsubd_msg_serialize (m, &buf, &msize);
|
|
|
|
|
|
|
|
LIST_FOREACH(ale, alh, entries) {
|
|
|
|
srv_write (ale->p, buf, msize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf != NULL) {
|
|
|
|
free (buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pubsubd_msg_print (const struct pubsub_msg *msg)
|
|
|
|
{
|
2016-09-09 01:36:44 +02:00
|
|
|
printf ("msg: type=%d chan=%s, data=%s\n"
|
|
|
|
, msg->type, msg->chan, msg->data);
|
2016-06-07 11:45:21 +02:00
|
|
|
}
|
2016-06-08 17:57:05 +02:00
|
|
|
|
2016-06-07 11:45:21 +02:00
|
|
|
void pubsubd_msg_recv (struct process *p, struct pubsub_msg *m)
|
|
|
|
{
|
|
|
|
// read the message from the process
|
2016-06-10 01:21:04 +02:00
|
|
|
size_t mlen = 0;
|
|
|
|
char *buf = NULL;
|
2016-09-09 01:36:44 +02:00
|
|
|
#if 0
|
2016-06-07 11:45:21 +02:00
|
|
|
srv_read_cb (p, &buf, &mlen, pubsubd_msg_read_cb);
|
2016-09-09 01:36:44 +02:00
|
|
|
#else
|
|
|
|
srv_read (p, &buf, &mlen);
|
|
|
|
#endif
|
2016-06-07 11:45:21 +02:00
|
|
|
|
|
|
|
pubsubd_msg_unserialize (m, buf, mlen);
|
2016-06-08 17:57:05 +02:00
|
|
|
|
|
|
|
if (buf != NULL) {
|
|
|
|
free (buf);
|
|
|
|
}
|
2016-06-05 20:48:13 +02:00
|
|
|
}
|
2016-06-07 11:45:21 +02:00
|
|
|
|
2016-06-10 01:21:04 +02:00
|
|
|
#define PUBSUB_SUBSCRIBER_ACTION_STR_PUB "pub"
|
|
|
|
#define PUBSUB_SUBSCRIBER_ACTION_STR_SUB "sub"
|
|
|
|
#define PUBSUB_SUBSCRIBER_ACTION_STR_BOTH "both"
|
|
|
|
#define PUBSUB_SUBSCRIBER_ACTION_STR_QUIT "quit"
|
|
|
|
|
|
|
|
// enum app_list_elm_action {PUBSUB_QUIT = 1, PUBSUB_PUB, PUBSUB_SUB, PUBSUB_BOTH};
|
|
|
|
|
|
|
|
char * pubsub_action_to_str (enum app_list_elm_action action)
|
2016-06-05 20:48:13 +02:00
|
|
|
{
|
2016-06-10 01:21:04 +02:00
|
|
|
switch (action) {
|
|
|
|
case PUBSUB_PUB : return strdup (PUBSUB_SUBSCRIBER_ACTION_STR_PUB);
|
|
|
|
case PUBSUB_SUB : return strdup (PUBSUB_SUBSCRIBER_ACTION_STR_SUB);
|
|
|
|
case PUBSUB_BOTH : return strdup (PUBSUB_SUBSCRIBER_ACTION_STR_BOTH);
|
|
|
|
case PUBSUB_QUIT : return strdup (PUBSUB_SUBSCRIBER_ACTION_STR_QUIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-06-08 17:57:05 +02:00
|
|
|
|
2016-06-10 01:21:04 +02:00
|
|
|
void pubsub_connection (struct service *srv, struct process *p, enum app_list_elm_action action, const char *channame)
|
|
|
|
{
|
|
|
|
char * straction = NULL;
|
|
|
|
straction = pubsub_action_to_str (action);
|
|
|
|
|
|
|
|
char line[BUFSIZ];
|
2016-06-10 20:02:58 +02:00
|
|
|
memset (line, 0, BUFSIZ);
|
2016-06-10 01:21:04 +02:00
|
|
|
|
|
|
|
// line fmt : pid index version action chan
|
2016-06-13 09:47:19 +02:00
|
|
|
// "quit" action is also possible (see pubsubd_quit)
|
2016-06-10 01:21:04 +02:00
|
|
|
snprintf (line, BUFSIZ, "%d %d %d %s %s\n"
|
|
|
|
, p->pid, p->index, p->version
|
|
|
|
, straction
|
|
|
|
, channame);
|
|
|
|
line[BUFSIZ -1] = '\0'; // to be sure
|
|
|
|
|
|
|
|
// send the connection line in the $TMP/<service> pipe
|
|
|
|
app_srv_connection (srv, line, strlen (line));
|
|
|
|
|
|
|
|
if (straction != NULL)
|
|
|
|
free (straction);
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:47:19 +02:00
|
|
|
void pubsub_disconnect (struct process *p)
|
|
|
|
{
|
|
|
|
struct pubsub_msg m;
|
|
|
|
memset (&m, 0, sizeof (struct pubsub_msg));
|
|
|
|
m.type = PUBSUB_TYPE_DISCONNECT;
|
|
|
|
|
|
|
|
char *buf = NULL;
|
|
|
|
size_t msize = 0;
|
|
|
|
pubsubd_msg_serialize (&m, &buf, &msize);
|
|
|
|
|
|
|
|
int ret = app_write (p, buf, msize);
|
2016-09-09 12:33:47 +02:00
|
|
|
if (ret != (int) msize) {
|
|
|
|
fprintf (stderr, "err: can't disconnect\n");
|
2016-06-13 09:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pubsubd_msg_free (&m);
|
|
|
|
if (buf != NULL) {
|
|
|
|
free (buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 14:41:25 +02:00
|
|
|
// tell the service to stop
|
2016-06-13 09:47:19 +02:00
|
|
|
void pubsubd_quit (struct service *srv)
|
2016-06-10 21:14:41 +02:00
|
|
|
{
|
2016-06-12 14:41:25 +02:00
|
|
|
// line fmt : 0 0 0 quit
|
2016-06-10 21:14:41 +02:00
|
|
|
char line[BUFSIZ];
|
2016-06-12 14:41:25 +02:00
|
|
|
snprintf (line, BUFSIZ, "0 0 0 quit\n");
|
|
|
|
app_srv_connection (srv, line, strlen (line));
|
2016-06-10 21:14:41 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 14:41:25 +02:00
|
|
|
void pubsub_msg_send (struct process *p, const struct pubsub_msg * m)
|
2016-06-10 01:21:04 +02:00
|
|
|
{
|
|
|
|
char *buf = NULL;
|
|
|
|
size_t msize = 0;
|
2016-06-08 17:57:05 +02:00
|
|
|
pubsubd_msg_serialize (m, &buf, &msize);
|
|
|
|
|
|
|
|
app_write (p, buf, msize);
|
|
|
|
|
|
|
|
if (buf != NULL) {
|
|
|
|
free (buf);
|
|
|
|
}
|
2016-06-05 20:48:13 +02:00
|
|
|
}
|
2016-06-08 17:57:05 +02:00
|
|
|
|
2016-06-12 14:41:25 +02:00
|
|
|
void pubsub_msg_recv (struct process *p, struct pubsub_msg * m)
|
2016-06-05 20:48:13 +02:00
|
|
|
{
|
2016-06-08 17:57:05 +02:00
|
|
|
// read the message from the process
|
2016-06-10 01:21:04 +02:00
|
|
|
size_t mlen = 0;
|
|
|
|
char *buf = NULL;
|
2016-09-09 12:06:25 +02:00
|
|
|
app_read (p, &buf, &mlen);
|
2016-06-08 17:57:05 +02:00
|
|
|
|
|
|
|
pubsubd_msg_unserialize (m, buf, mlen);
|
|
|
|
|
|
|
|
if (buf != NULL) {
|
|
|
|
free (buf);
|
|
|
|
}
|
2016-06-05 20:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SERVICE
|
|
|
|
|
|
|
|
void pubsubd_srv_init ();
|