Decode JSON TRP messages, then send to main thread.
[trust_router.git] / common / tr_mq.c
index 4e43091..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;
@@ -79,7 +90,7 @@ 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_MSG *tr_mq_get_head(TR_MQ *mq)
@@ -133,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 */
@@ -144,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 */
   }