removing remenants of abstract unix domain socket handling, this finishes addressing...
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed_barriers.c
index 5668d03cfd506c09e2ca6028a66df3b9d20949b9..634f0a07a89136f943b57b7fbd3499fa276452d3 100644 (file)
@@ -21,7 +21,7 @@
 /**
  * @file testbed/gnunet-service-testbed_barriers.c
  * @brief barrier handling at the testbed controller
- * @author Sree Harsha Totakura <sreeharsha@totakura.in> 
+ * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  */
 
 #include "gnunet-service-testbed.h"
@@ -200,12 +200,12 @@ struct Barrier
    * Identifier for the timeout task
    */
   GNUNET_SCHEDULER_TaskIdentifier tout_task;
-  
+
   /**
    * The status of this barrier
    */
   enum GNUNET_TESTBED_BarrierStatus status;
-  
+
   /**
    * Number of barriers wrapped in the above DLL
    */
@@ -235,7 +235,7 @@ struct Barrier
    * Quorum percentage to be reached
    */
   uint8_t quorum;
-  
+
 };
 
 
@@ -261,7 +261,7 @@ static struct GNUNET_SERVICE_Context *ctx;
  * @param buf where the callee should write the message
  * @return number of bytes written to buf
  */
-static size_t 
+static size_t
 transmit_ready_cb (void *cls, size_t size, void *buf)
 {
   struct ClientCtx *ctx = cls;
@@ -276,7 +276,7 @@ transmit_ready_cb (void *cls, size_t size, void *buf)
   {
     GNUNET_assert (NULL != ctx->client);
     GNUNET_SERVER_client_drop (ctx->client);
-    ctx->client = NULL;    
+    ctx->client = NULL;
     return 0;
   }
   mq = ctx->mq_head;
@@ -306,7 +306,7 @@ queue_message (struct ClientCtx *ctx, struct GNUNET_MessageHeader *msg)
 {
   struct MessageQueue *mq;
   struct GNUNET_SERVER_Client *client = ctx->client;
-  
+
   mq = GNUNET_malloc (sizeof (struct MessageQueue));
   mq->msg = msg;
   LOG_DEBUG ("Queueing message of type %u, size %u for sending\n",
@@ -328,9 +328,12 @@ static void
 cleanup_clientctx (struct ClientCtx *ctx)
 {
   struct MessageQueue *mq;
-  
+
   if (NULL != ctx->client)
+  {
+    GNUNET_SERVER_client_set_user_context_ (ctx->client, NULL, 0);
     GNUNET_SERVER_client_drop (ctx->client);
+  }
   if (NULL != ctx->tx)
     GNUNET_SERVER_notify_transmit_ready_cancel (ctx->tx);
   if (NULL != (mq = ctx->mq_head))
@@ -353,7 +356,7 @@ static void
 remove_barrier (struct Barrier *barrier)
 {
   struct ClientCtx *ctx;
-  
+
   GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (barrier_map,
                                                                     &barrier->hash,
                                                                     barrier));
@@ -394,7 +397,7 @@ cancel_wrappers (struct Barrier *barrier)
  * @param name the barrier name
  * @param status the status of the barrier
  * @param emsg the error message; should be non-NULL for
- *   status=BARRIER_STATUS_ERROR 
+ *   status=GNUNET_TESTBED_BARRIERSTATUS_ERROR
  */
 static void
 send_client_status_msg (struct GNUNET_SERVER_Client *client,
@@ -406,7 +409,7 @@ send_client_status_msg (struct GNUNET_SERVER_Client *client,
   size_t name_len;
   uint16_t msize;
 
-  GNUNET_assert ((NULL == emsg) || (BARRIER_STATUS_ERROR == status));
+  GNUNET_assert ((NULL == emsg) || (GNUNET_TESTBED_BARRIERSTATUS_ERROR == status));
   name_len = strlen (name);
   msize = sizeof (struct GNUNET_TESTBED_BarrierStatusMsg)
       + (name_len + 1)
@@ -428,7 +431,7 @@ send_client_status_msg (struct GNUNET_SERVER_Client *client,
  *
  * @param barrier the corresponding barrier
  * @param emsg the error message; should be non-NULL for
- *   status=BARRIER_STATUS_ERROR 
+ *   status=GNUNET_TESTBED_BARRIERSTATUS_ERROR
  */
 static void
 send_barrier_status_msg (struct Barrier *barrier, const char *emsg)
@@ -461,7 +464,7 @@ handle_barrier_wait (void *cls, struct GNUNET_SERVER_Client *client,
   struct GNUNET_HashCode key;
   size_t name_len;
   uint16_t msize;
-  
+
   msize = ntohs (message->size);
   if (msize <= sizeof (struct GNUNET_TESTBED_BarrierWait))
   {
@@ -482,11 +485,11 @@ handle_barrier_wait (void *cls, struct GNUNET_SERVER_Client *client,
   (void) memcpy (name, msg->name, name_len);
   LOG_DEBUG ("Received BARRIER_WAIT for barrier `%s'\n", name);
   GNUNET_CRYPTO_hash (name, name_len, &key);
+  GNUNET_free (name);
   if (NULL == (barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &key)))
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    GNUNET_free (name);
     return;
   }
   client_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientCtx);
@@ -497,13 +500,13 @@ handle_barrier_wait (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_SERVER_client_keep (client);
     client_ctx->barrier = barrier;
     GNUNET_CONTAINER_DLL_insert_tail (barrier->head, barrier->tail, client_ctx);
-    GNUNET_SERVER_client_set_user_context (client, client_ctx); 
+    GNUNET_SERVER_client_set_user_context (client, client_ctx);
   }
   barrier->nreached++;
   if ((barrier->num_wbarriers_reached == barrier->num_wbarriers)
         && (LOCAL_QUORUM_REACHED (barrier)))
   {
-    barrier->status = BARRIER_STATUS_CROSSED;
+    barrier->status = GNUNET_TESTBED_BARRIERSTATUS_CROSSED;
     send_barrier_status_msg (barrier, NULL);
   }
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -522,7 +525,7 @@ static void
 disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
 {
   struct ClientCtx *client_ctx;
-  
+
   if (NULL == client)
     return;
   client_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientCtx);
@@ -551,7 +554,31 @@ GST_barriers_init (struct GNUNET_CONFIGURATION_Handle *cfg)
                               GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN);
   srv = GNUNET_SERVICE_get_server (ctx);
   GNUNET_SERVER_add_handlers (srv, message_handlers);
-  GNUNET_SERVER_disconnect_notify (srv, &disconnect_cb, NULL);  
+  GNUNET_SERVER_disconnect_notify (srv, &disconnect_cb, NULL);
+}
+
+
+/**
+ * Iterator over hash map entries.
+ *
+ * @param cls closure
+ * @param key current key code
+ * @param value value in the hash map
+ * @return #GNUNET_YES if we should continue to
+ *         iterate,
+ *         #GNUNET_NO if not.
+ */
+static int
+barrier_destroy_iterator (void *cls,
+                          const struct GNUNET_HashCode *key,
+                          void *value)
+{
+  struct Barrier *barrier = value;
+
+  GNUNET_assert (NULL != barrier);
+  cancel_wrappers (barrier);
+  remove_barrier (barrier);
+  return GNUNET_YES;
 }
 
 
@@ -562,6 +589,10 @@ void
 GST_barriers_destroy ()
 {
   GNUNET_assert (NULL != barrier_map);
+  GNUNET_assert (GNUNET_SYSERR !=
+                 GNUNET_CONTAINER_multihashmap_iterate (barrier_map,
+                                                        &barrier_destroy_iterator,
+                                                        NULL));
   GNUNET_CONTAINER_multihashmap_destroy (barrier_map);
   GNUNET_assert (NULL != ctx);
   GNUNET_SERVICE_stop (ctx);
@@ -581,7 +612,7 @@ GST_barriers_destroy ()
  * @param emsg if the status were to be GNUNET_SYSERR, this parameter has the
  *   error messsage
  */
-static void 
+static void
 wbarrier_status_cb (void *cls, const char *name,
                     struct GNUNET_TESTBED_Barrier *b_,
                     enum GNUNET_TESTBED_BarrierStatus status,
@@ -596,18 +627,18 @@ wbarrier_status_cb (void *cls, const char *name,
   GNUNET_free (wrapper);
   switch (status)
   {
-  case BARRIER_STATUS_ERROR:
+  case GNUNET_TESTBED_BARRIERSTATUS_ERROR:
     LOG (GNUNET_ERROR_TYPE_ERROR,
          "Initialising barrier `%s' failed at a sub-controller: %s\n",
          barrier->name, (NULL != emsg) ? emsg : "NULL");
     cancel_wrappers (barrier);
     if (NULL == emsg)
       emsg = "Initialisation failed at a sub-controller";
-    barrier->status = BARRIER_STATUS_ERROR;
+    barrier->status = GNUNET_TESTBED_BARRIERSTATUS_ERROR;
     send_barrier_status_msg (barrier, emsg);
     return;
-  case BARRIER_STATUS_CROSSED:
-    if (BARRIER_STATUS_INITIALISED != barrier->status)
+  case GNUNET_TESTBED_BARRIERSTATUS_CROSSED:
+    if (GNUNET_TESTBED_BARRIERSTATUS_INITIALISED != barrier->status)
     {
       GNUNET_break_op (0);
       return;
@@ -616,11 +647,11 @@ wbarrier_status_cb (void *cls, const char *name,
     if ((barrier->num_wbarriers_reached == barrier->num_wbarriers)
         && (LOCAL_QUORUM_REACHED (barrier)))
     {
-      barrier->status = BARRIER_STATUS_CROSSED;
+      barrier->status = GNUNET_TESTBED_BARRIERSTATUS_CROSSED;
       send_barrier_status_msg (barrier, NULL);
     }
     return;
-  case BARRIER_STATUS_INITIALISED:
+  case GNUNET_TESTBED_BARRIERSTATUS_INITIALISED:
     if (0 != barrier->status)
     {
       GNUNET_break_op (0);
@@ -629,7 +660,7 @@ wbarrier_status_cb (void *cls, const char *name,
     barrier->num_wbarriers_inited++;
     if (barrier->num_wbarriers_inited == barrier->num_wbarriers)
     {
-      barrier->status = BARRIER_STATUS_INITIALISED;
+      barrier->status = GNUNET_TESTBED_BARRIERSTATUS_INITIALISED;
       send_barrier_status_msg (barrier, NULL);
     }
     return;
@@ -648,9 +679,9 @@ static void
 fwd_tout_barrier_init (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct Barrier *barrier = cls;
-  
+
   cancel_wrappers (barrier);
-  barrier->status = BARRIER_STATUS_ERROR;
+  barrier->status = GNUNET_TESTBED_BARRIERSTATUS_ERROR;
   send_barrier_status_msg (barrier,
                            "Timedout while propagating barrier initialisation\n");
   remove_barrier (barrier);
@@ -682,7 +713,7 @@ GST_handle_barrier_init (void *cls, struct GNUNET_SERVER_Client *client,
   size_t name_len;
   unsigned int cnt;
   uint16_t msize;
-  
+
   if (NULL == GST_context)
   {
     GNUNET_break_op (0);
@@ -710,8 +741,8 @@ GST_handle_barrier_init (void *cls, struct GNUNET_SERVER_Client *client,
   LOG_DEBUG ("Received BARRIER_INIT for barrier `%s'\n", name);
   if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (barrier_map, &hash))
   {
-    
-    send_client_status_msg (client, name, BARRIER_STATUS_ERROR,
+
+    send_client_status_msg (client, name, GNUNET_TESTBED_BARRIERSTATUS_ERROR,
                             "A barrier with the same name already exists");
     GNUNET_free (name);
     GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -751,8 +782,8 @@ GST_handle_barrier_init (void *cls, struct GNUNET_SERVER_Client *client,
   }
   if (NULL == barrier->whead)   /* No further propagation */
   {
-    barrier->status = BARRIER_STATUS_INITIALISED;
-    LOG_DEBUG ("Sending BARRIER_STATUS_INITIALISED for barrier `%s'\n",
+    barrier->status = GNUNET_TESTBED_BARRIERSTATUS_INITIALISED;
+    LOG_DEBUG ("Sending GNUNET_TESTBED_BARRIERSTATUS_INITIALISED for barrier `%s'\n",
                barrier->name);
     send_barrier_status_msg (barrier, NULL);
   }else
@@ -790,7 +821,7 @@ GST_handle_barrier_cancel (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_break_op (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
-  }  
+  }
   if (client != GST_context->client)
   {
     GNUNET_break_op (0);
@@ -819,7 +850,7 @@ GST_handle_barrier_cancel (void *cls, struct GNUNET_SERVER_Client *client,
   GNUNET_assert (NULL != barrier);
   cancel_wrappers (barrier);
   remove_barrier (barrier);
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);  
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
 
@@ -837,20 +868,20 @@ GST_handle_barrier_status (void *cls, struct GNUNET_SERVER_Client *client,
                            const struct GNUNET_MessageHeader *message)
 {
   const struct GNUNET_TESTBED_BarrierStatusMsg *msg;
-  struct Barrier *barrier; 
+  struct Barrier *barrier;
   struct ClientCtx *client_ctx;
   const char *name;
   struct GNUNET_HashCode key;
   enum GNUNET_TESTBED_BarrierStatus status;
   uint16_t msize;
   uint16_t name_len;
-  
+
   if (NULL == GST_context)
   {
     GNUNET_break_op (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
-  }  
+  }
   if (client != GST_context->client)
   {
     GNUNET_break_op (0);
@@ -866,7 +897,7 @@ GST_handle_barrier_status (void *cls, struct GNUNET_SERVER_Client *client,
   }
   msg = (const struct GNUNET_TESTBED_BarrierStatusMsg *) message;
   status = ntohs (msg->status);
-  if (BARRIER_STATUS_CROSSED != status)
+  if (GNUNET_TESTBED_BARRIERSTATUS_CROSSED != status)
   {
     GNUNET_break_op (0);        /* current we only expect BARRIER_CROSSED
                                    status message this way */