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/pubsub/lib/pubsub.c

102 lines
2.5 KiB
C
Raw Normal View History

2017-01-19 22:07:52 +01:00
#include <stdlib.h>
#include <string.h> // strndup
#include "pubsub.h"
#include "pubsubd.h"
#include "../../core/error.h"
#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"
char * pubsub_action_to_str (enum subscriber_action action)
{
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;
}
#if 0
// tell the service to stop
2018-10-04 00:30:47 +02:00
void pubsub_quit (struct ipc_service *srv)
2017-01-19 22:07:52 +01:00
{
// line fmt : 0 0 0 quit
char line[BUFSIZ];
snprintf (line, BUFSIZ, "0 0 0 quit\n");
2018-10-03 21:52:11 +02:00
ipc_application_server_connection (srv, line, strlen (line));
2017-01-19 22:07:52 +01:00
}
#endif
int pubsub_connection (int argc, char **argv, char **env
2018-10-04 00:30:47 +02:00
, struct ipc_service *srv)
2017-01-19 22:07:52 +01:00
{
2018-10-03 21:52:11 +02:00
int ret = ipc_application_connection (argc, argv, env
2017-01-19 22:07:52 +01:00
, srv, PUBSUBD_SERVICE_NAME, NULL, 0);
if (ret != 0) {
2018-10-03 21:24:20 +02:00
handle_err ("pubsub_connection", "application_connection != 0");
2017-01-19 22:07:52 +01:00
}
return ret;
}
2018-10-04 00:30:47 +02:00
int pubsub_disconnect (struct ipc_service *srv)
2017-01-19 22:07:52 +01:00
{
2018-10-03 21:52:11 +02:00
return ipc_application_close (srv);
2017-01-19 22:07:52 +01:00
}
2018-10-04 00:30:47 +02:00
int pubsub_message_send (struct ipc_service *srv, const struct pubsub_msg * m)
2017-01-19 22:07:52 +01:00
{
size_t msize = 0;
char * buf = NULL;
2018-10-03 21:52:11 +02:00
pubsub_message_serialize (m, &buf, &msize);
2017-01-19 22:07:52 +01:00
2018-10-03 22:02:37 +02:00
struct ipc_message m_data;
memset (&m_data, 0, sizeof (struct ipc_message));
2017-01-19 22:07:52 +01:00
// format the connection msg
2018-10-04 00:18:08 +02:00
if (ipc_message_format_data (&m_data, buf, msize) < 0) {
2018-10-03 21:52:11 +02:00
handle_err ("pubsub_message_send", "msg_format_data");
2017-01-19 22:07:52 +01:00
if (buf != NULL)
free (buf);
return -1;
}
2018-10-03 21:52:11 +02:00
ipc_application_write (srv, &m_data);
ipc_message_free (&m_data);
2017-01-19 22:07:52 +01:00
if (buf != NULL)
free(buf);
return 0;
}
2018-10-04 00:30:47 +02:00
int pubsub_message_recv (struct ipc_service *srv, struct pubsub_msg *m)
2017-01-19 22:07:52 +01:00
{
if (srv == NULL) {
2018-10-03 21:52:11 +02:00
handle_err ("pubsub_message_recv", "srv == NULL");
2017-01-19 22:07:52 +01:00
return -1;
}
if (m == NULL) {
2018-10-03 21:52:11 +02:00
handle_err ("pubsub_message_recv", "m == NULL");
2017-01-19 22:07:52 +01:00
return -1;
}
2018-10-03 22:02:37 +02:00
struct ipc_message m_recv;
memset (&m_recv, 0, sizeof (struct ipc_message));
2017-01-19 22:07:52 +01:00
2018-10-03 21:52:11 +02:00
ipc_application_read (srv, &m_recv);
2018-10-04 01:54:12 +02:00
pubsub_message_unserialize (m, m_recv.payload, m_recv.length);
2017-01-19 22:07:52 +01:00
2018-10-03 21:52:11 +02:00
ipc_message_free (&m_recv);
2017-01-19 22:07:52 +01:00
return 0;
}