Decode JSON TRP messages, then send to main thread.
[trust_router.git] / common / tr_mq.c
index b2e854c..5240c1a 100644 (file)
@@ -2,6 +2,7 @@
 #include <pthread.h>
 
 #include <tr_mq.h>
+#include <tr_debug.h>
 
 /* Messages */
 static int tr_mq_msg_destructor(void *object)
@@ -12,11 +13,16 @@ static int tr_mq_msg_destructor(void *object)
   return 0;
 }
 
-TR_MQ_MSG *tr_mq_msg_new(TALLOC_CTX *mem_ctx)
+TR_MQ_MSG *tr_mq_msg_new(TALLOC_CTX *mem_ctx, const char *message)
 {
   TR_MQ_MSG *msg=talloc(mem_ctx, TR_MQ_MSG);
   if (msg!=NULL) {
     msg->next=NULL;
+    msg->message=talloc_strdup(msg, message);
+    if (msg->message==NULL) {
+      talloc_free(msg);
+      return NULL;
+    }
     msg->p=NULL;
     talloc_set_destructor((void *)msg, tr_mq_msg_destructor);
   }
@@ -29,6 +35,11 @@ void tr_mq_msg_free(TR_MQ_MSG *msg)
     talloc_free(msg);
 }
 
+const char *tr_mq_msg_get_message(TR_MQ_MSG *msg)
+{
+  return msg->message;
+}
+
 void *tr_mq_msg_get_payload(TR_MQ_MSG *msg)
 {
   return msg->p;
@@ -52,18 +63,6 @@ static void tr_mq_msg_set_next(TR_MQ_MSG *msg, TR_MQ_MSG *next)
   msg->next=next;
 }
 
-static TR_MQ_MSG *tr_mq_msg_get_tail(TR_MQ_MSG *msg)
-{
-  while (msg!=NULL)
-    msg=tr_mq_msg_get_next(msg);
-  return msg;
-}
-
-static void tr_mq_msg_append(TR_MQ_MSG *msg, TR_MQ_MSG *new)
-{
-  tr_mq_msg_set_next(tr_mq_msg_get_tail(msg), new);
-}
-
 /* Message Queues */
 TR_MQ *tr_mq_new(TALLOC_CTX *mem_ctx)
 {
@@ -91,10 +90,10 @@ int tr_mq_lock(TR_MQ *mq)
 
 int tr_mq_unlock(TR_MQ *mq)
 {
-  return pthread_mutex_lock(&(mq->mutex));
+  return pthread_mutex_unlock(&(mq->mutex));
 }
 
-static TR_MQ_MST *tr_mq_get_head(TR_MQ *mq)
+static TR_MQ_MSG *tr_mq_get_head(TR_MQ *mq)
 {
   return mq->head;
 }
@@ -104,7 +103,7 @@ static void tr_mq_set_head(TR_MQ *mq, TR_MQ_MSG *msg)
   mq->head=msg;
 }
 
-static TR_MQ_MST *tr_mq_get_tail(TR_MQ *mq)
+static TR_MQ_MSG *tr_mq_get_tail(TR_MQ *mq)
 {
   return mq->tail;
 }
@@ -114,20 +113,27 @@ static void tr_mq_set_tail(TR_MQ *mq, TR_MQ_MSG *msg)
   mq->tail=msg;
 }
 
+void tr_mq_set_notify_cb(TR_MQ *mq, TR_MQ_NOTIFY_FN cb, void *arg)
+{
+  mq->notify_cb=cb;
+  mq->notify_cb_arg=arg;
+}
+
+
 /* puts msg in mq's talloc context */
 void tr_mq_append(TR_MQ *mq, TR_MQ_MSG *msg)
 {
-  int was_empty=FALSE;
+  int was_empty=0;
   TR_MQ_NOTIFY_FN notify_cb=NULL;
   void *notify_cb_arg=NULL;
 
   tr_mq_lock(mq);
   if (tr_mq_get_head(mq)==NULL) {
-    was_empty=TRUE;
+    was_empty=1;
     tr_mq_set_head(mq, msg);
     tr_mq_set_tail(mq, msg);
   } else {
-    tr_mq_msg_set_next(tr_mq_get_tail(), msg); /* add to list */
+    tr_mq_msg_set_next(tr_mq_get_tail(mq), msg); /* add to list */
     tr_mq_set_tail(mq, msg); /* update tail of list */
   }
   talloc_steal(mq, msg);
@@ -138,7 +144,7 @@ void tr_mq_append(TR_MQ *mq, TR_MQ_MSG *msg)
 
   /* see if we need to tell someone we became non-empty */
   if (was_empty && (notify_cb!=NULL))
-    mq->notify_cb(mq, notify_cb_arg);
+    notify_cb(mq, notify_cb_arg);
 }
 
 /* caller must free msg via tr_mq_msg_free */
@@ -149,7 +155,7 @@ TR_MQ_MSG *tr_mq_pop(TR_MQ *mq)
   tr_mq_lock(mq);
   if (tr_mq_get_head(mq)!=NULL) {
     popped=tr_mq_get_head(mq);
-    tr_mq_msg_set_next(popped, tr_mq_msg_get_next(popped)); /* popped is the old head */
+    tr_mq_set_head(mq, tr_mq_msg_get_next(popped)); /* popped is the old head */
     if (tr_mq_get_head(mq)==NULL)
       tr_mq_set_tail(mq, NULL); /* just popped the last element */
   }