refactor DHT for new service API
[oweals/gnunet.git] / src / cadet / cadet_api.c
index 660bcea3d0add7ce6182c46e3c9c9533b995aceb..d758155886c51312805bdd2dc636db01de2ecfbe 100644 (file)
@@ -423,6 +423,7 @@ create_channel (struct GNUNET_CADET_Handle *h, CADET_ChannelNumber chid)
  *
  * @return Handle to the required channel or NULL if not found.
  */
+// FIXME: simplify: call_cleaner is always #GNUNET_YES!!!
 static void
 destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
 {
@@ -484,6 +485,26 @@ add_to_queue (struct GNUNET_CADET_Handle *h,
 }
 
 
+/**
+ * Remove a transmit handle from the transmission queue, if present.
+ *
+ * Safe to call even if not queued.
+ *
+ * @param th handle to the packet to be unqueued.
+ */
+static void
+remove_from_queue (struct GNUNET_CADET_TransmitHandle *th)
+{
+  struct GNUNET_CADET_Handle *h = th->channel->cadet;
+
+  /* It might or might not have been queued (rarely not), but check anyway. */
+  if (NULL != th->next || h->th_tail == th)
+  {
+    GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
+  }
+}
+
+
 /**
  * Send an ack on the channel to confirm the processing of a message.
  *
@@ -527,16 +548,21 @@ request_data (void *cls)
   size_t osize;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting Data: %u bytes\n", th->size);
+
+  GNUNET_assert (GNUNET_YES == th->channel->allow_send);
+  th->channel->allow_send = GNUNET_NO;
   th->request_data_task = NULL;
   th->channel->packet_size = 0;
+  remove_from_queue (th);
+
   env = GNUNET_MQ_msg_extra (msg, th->size,
                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
   msg->id = htonl (th->channel->chid);
   osize = th->notify (th->notify_cls, th->size, &msg[1]);
   GNUNET_assert (osize == th->size);
-  th->channel->allow_send = GNUNET_NO;
   GNUNET_MQ_send (th->channel->cadet->mq, env);
-  GNUNET_CADET_notify_transmit_ready_cancel (th);
+
+  GNUNET_free (th);
 }
 
 
@@ -1213,43 +1239,43 @@ handle_get_tunnel (void *cls,
 static int
 do_reconnect (struct GNUNET_CADET_Handle *h)
 {
-  GNUNET_MQ_hd_fixed_size (channel_created,
-                           GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
-                           struct GNUNET_CADET_ChannelCreateMessage);
-  GNUNET_MQ_hd_fixed_size (channel_destroy,
-                           GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
-                           struct GNUNET_CADET_ChannelDestroyMessage);
-  GNUNET_MQ_hd_var_size (local_data,
-                         GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
-                         struct GNUNET_CADET_LocalData);
-  GNUNET_MQ_hd_fixed_size (local_ack,
-                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
-                           struct GNUNET_CADET_LocalAck);
-  GNUNET_MQ_hd_var_size (get_peers,
-                         GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
-                         struct GNUNET_CADET_LocalInfoPeer);
-  GNUNET_MQ_hd_var_size (get_peer,
-                         GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
-                         struct GNUNET_CADET_LocalInfoPeer);
-  GNUNET_MQ_hd_var_size (get_tunnels,
-                         GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
-                         struct GNUNET_CADET_LocalInfoTunnel);
-  GNUNET_MQ_hd_var_size (get_tunnel,
-                         GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
-                         struct GNUNET_CADET_LocalInfoTunnel);
+  struct GNUNET_MQ_MessageHandler handlers[] = {
+    GNUNET_MQ_hd_fixed_size (channel_created,
+                             GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
+                             struct GNUNET_CADET_ChannelCreateMessage,
+                             h),
+    GNUNET_MQ_hd_fixed_size (channel_destroy,
+                             GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
+                             struct GNUNET_CADET_ChannelDestroyMessage,
+                             h),
+    GNUNET_MQ_hd_var_size (local_data,
+                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
+                           struct GNUNET_CADET_LocalData,
+                           h),
+    GNUNET_MQ_hd_fixed_size (local_ack,
+                             GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
+                             struct GNUNET_CADET_LocalAck,
+                             h),
+    GNUNET_MQ_hd_var_size (get_peers,
+                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
+                           struct GNUNET_CADET_LocalInfoPeer,
+                           h),
+    GNUNET_MQ_hd_var_size (get_peer,
+                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
+                           struct GNUNET_CADET_LocalInfoPeer,
+                           h),
+    GNUNET_MQ_hd_var_size (get_tunnels,
+                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
+                           struct GNUNET_CADET_LocalInfoTunnel,
+                           h),
+    GNUNET_MQ_hd_var_size (get_tunnel,
+                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
+                           struct GNUNET_CADET_LocalInfoTunnel,
+                           h),
   // FIXME
 //   GNUNET_MQ_hd_fixed_Y       size (channel_destroyed,
 //                            GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK,
 //                            struct GNUNET_CADET_ChannelDestroyMessage);
-  struct GNUNET_MQ_MessageHandler handlers[] = {
-    make_channel_created_handler (h),
-    make_channel_destroy_handler (h),
-    make_local_data_handler (h),
-    make_local_ack_handler (h),
-    make_get_peers_handler (h),
-    make_get_peer_handler (h),
-    make_get_tunnels_handler (h),
-    make_get_tunnel_handler (h),
     GNUNET_MQ_handler_end ()
   };
 
@@ -1665,13 +1691,7 @@ GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *t
   }
   th->request_data_task = NULL;
 
-  /* It might or might not have been queued (rarely not), but check anyway. */
-  if (NULL != th->next)
-  {
-    struct GNUNET_CADET_Handle *h;
-    h = th->channel->cadet;
-    GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
-  }
+  remove_from_queue (th);
   GNUNET_free (th);
 }
 
@@ -1689,12 +1709,12 @@ send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
   struct GNUNET_MessageHeader *msg;
   struct GNUNET_MQ_Envelope *env;
 
-  env = GNUNET_MQ_msg (msg, type);
-  GNUNET_MQ_send (h->mq, env);
-
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       " Sending %s message to service\n",
+       " Sending %s monitor message to service\n",
        GC_m2s(type));
+
+  env = GNUNET_MQ_msg (msg, type);
+  GNUNET_MQ_send (h->mq, env);
 }