From ae7a6c404f9842e67e6822eacf465fb4b482d631 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Tue, 9 Oct 2018 16:38:27 +0200
Subject: [PATCH] message.[ch] from pubsub
---
pubsub/lib/message.c | 132 +++++++++++++++++++++++++++++++++++++++++++
pubsub/lib/message.h | 21 +++++++
2 files changed, 153 insertions(+)
create mode 100644 pubsub/lib/message.c
create mode 100644 pubsub/lib/message.h
diff --git a/pubsub/lib/message.c b/pubsub/lib/message.c
new file mode 100644
index 0000000..5e1c78a
--- /dev/null
+++ b/pubsub/lib/message.c
@@ -0,0 +1,132 @@
+#include
+#include
+#include
+
+#include "message.h"
+#include "../../core/error.h"
+
+void pubsub_message_serialize (const struct pubsub_msg *msg, char **data, size_t *len)
+{
+ if (msg == NULL) {
+ handle_err ("pubsub_message_serialize", "msg == NULL");
+ return;
+ }
+
+ if (data == NULL) {
+ handle_err ("pubsub_message_serialize", "data == NULL");
+ return;
+ }
+
+ if (*data != NULL) {
+ handle_err ("pubsub_message_serialize", "*data != NULL");
+ return;
+ }
+
+ if (len == NULL) {
+ handle_err ("pubsub_message_serialize", "len == NULL");
+ return;
+ }
+
+ // buflen = pubsub msg type (1) + 2* size_t (16) + chan+data
+ size_t buflen = 1 + 2 * sizeof (size_t) + msg->chanlen + msg->datalen;
+
+ if (buflen > BUFSIZ) {
+ handle_err ("pubsub_message_serialize", "chanlen + datalen too high");
+ return;
+ }
+
+ char *buf = malloc (buflen);
+ memset (buf, 0, buflen);
+
+ size_t offset = 0;
+
+ // msg type
+ buf[offset++] = msg->type;
+
+ // chan
+ memcpy (buf + offset, &msg->chanlen, sizeof (size_t));
+ offset += sizeof (size_t);
+ memcpy (buf + offset, msg->chan, msg->chanlen);
+ offset += msg->chanlen;
+
+ // data
+ memcpy (buf + offset, &msg->datalen, sizeof (size_t));
+ offset += sizeof (size_t);
+ memcpy (buf + offset, msg->data, msg->datalen);
+ offset += msg->datalen;
+
+ *data = buf;
+ *len = buflen;
+}
+
+void pubsub_message_unserialize (struct pubsub_msg *msg, const char *buf, size_t mlen)
+{
+ if (msg == NULL) {
+ handle_err ("pubsub_message_unserialize", "msg == NULL");
+ return;
+ }
+
+ pubsub_message_free (msg);
+
+ if (mlen > BUFSIZ) {
+ handle_err ("pubsub_message_unserialize", "mlen > BUFSIZ");
+ return;
+ }
+
+ size_t offset = 0;
+
+ // msg type
+ msg->type = buf[offset++];
+
+ // chan
+ memcpy (&msg->chanlen, buf + offset, sizeof (size_t));
+ if (msg->chanlen > BUFSIZ) {
+ handle_err ("pubsub_message_unserialize", "chanlen > BUFSIZ");
+ return;
+ }
+ msg->chan = malloc (msg->chanlen);
+ memset (msg->chan, 0, msg->chanlen);
+ offset += sizeof (size_t);
+ memcpy (msg->chan, buf + offset, msg->chanlen);
+ offset += msg->chanlen;
+
+ // data
+ memcpy (&msg->datalen, buf + offset, sizeof (size_t));
+ if (msg->datalen > BUFSIZ) {
+ handle_err ("pubsub_message_unserialize", "datalen > BUFSIZ");
+ return;
+ }
+ msg->data = malloc (msg->datalen);
+ memset (msg->data, 0, msg->datalen);
+ offset += sizeof (size_t);
+ memcpy (msg->data, buf + offset, msg->datalen);
+ offset += msg->datalen;
+}
+
+void pubsub_message_free (struct pubsub_msg *msg)
+{
+ if (msg == NULL) {
+ handle_err ("pubsub_message_free", "msg == NULL");
+ return;
+ }
+
+ if (msg->chan) {
+ free (msg->chan);
+ msg->chan = NULL;
+ }
+ if (msg->data) {
+ free (msg->data);
+ msg->data = NULL;
+ }
+}
+
+void pubsub_message_print (const struct pubsub_msg *msg)
+{
+ if (msg == NULL) {
+ handle_err ("pubsub_message_print", "msg == NULL");
+ return;
+ }
+
+ printf ("msg: type=%d chan=%s, data=%s\n"
+ , msg->type, msg->chan, msg->data);
+}
diff --git a/pubsub/lib/message.h b/pubsub/lib/message.h
new file mode 100644
index 0000000..8154fff
--- /dev/null
+++ b/pubsub/lib/message.h
@@ -0,0 +1,21 @@
+#ifndef __PUBSUB_MSG_H__
+#define __PUBSUB_MSG_H__
+
+#define PUBSUB_MSG_TYPE_SUB 1
+#define PUBSUB_MSG_TYPE_UNSUB 2
+#define PUBSUB_MSG_TYPE_PUB 3
+
+struct pubsub_msg {
+ unsigned char type; // message type : alert, notification, …
+ char *chan;
+ size_t chanlen;
+ char *data;
+ size_t datalen;
+};
+
+void pubsub_message_serialize (const struct pubsub_msg *msg, char **data, size_t *len);
+void pubsub_message_unserialize (struct pubsub_msg *msg, const char *data, size_t len);
+void pubsub_message_free (struct pubsub_msg *msg);
+void pubsub_message_print (const struct pubsub_msg *msg);
+
+#endif