convert fs publish to MQ
[oweals/gnunet.git] / src / core / core_api.c
index 7699f8b74abbd7e0fc64949cffa11ac09815ce6f..86c9d38a250eac210636b6131f9c3342d3bcbfa5 100644 (file)
@@ -332,6 +332,9 @@ handle_mq_error (void *cls,
 {
   struct GNUNET_CORE_Handle *h = cls;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "MQ ERROR: %d\n",
+              error);
   reconnect_later (h);
 }
 
@@ -408,7 +411,7 @@ handle_init_reply (void *cls,
  */
 static void
 handle_connect_notify (void *cls,
-                       const struct ConnectNotifyMessage * cnm)
+                       const struct ConnectNotifyMessage *cnm)
 {
   struct GNUNET_CORE_Handle *h = cls;
   struct PeerRecord *pr;
@@ -707,12 +710,25 @@ handle_send_ready (void *cls,
   sm->peer = pr->peer;
   sm->cork = htonl ((uint32_t) th->cork);
   sm->reserved = htonl (0);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Calling get_message with buffer of %u bytes\n",
+              (unsigned int) th->msize);
+  /* FIXME: this is ugly and a bit brutal, but "get_message"
+     may call GNUNET_CORE_notify_transmit_ready() which
+     may call GNUNET_MQ_send() as well, and we MUST get this
+     message out before the next SEND_REQUEST.  So we queue
+     it (even though incomplete) and then---relying on MQ being
+     nice and not actually touching 'env' until much later---
+     fill it afterwards.  This is horrible style, and once
+     the core_api abandons GNUNET_CORE_notify_transmit_ready
+     in favor of an MQ-style API, this hack should no longer
+     be required */
+  GNUNET_MQ_send (h->mq,
+                  env);
   ret = th->get_message (th->get_message_cls,
                          th->msize,
                          &sm[1]);
   sm->header.size = htons (ret + sizeof (struct SendMessage));
-  th->msize = ret;
-  // GNUNET_assert (ret == th->msize); /* NOTE: API change! */
   delay = GNUNET_TIME_absolute_get_duration (th->request_time);
   overdue = GNUNET_TIME_absolute_get_duration (th->deadline);
   if (overdue.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
@@ -733,8 +749,6 @@ handle_send_ready (void *cls,
          GNUNET_STRINGS_relative_time_to_string (delay,
                                                  GNUNET_YES),
          (th->cork) ? " (corked)" : "");
-  GNUNET_MQ_send (h->mq,
-                  env);
 }
 
 
@@ -834,13 +848,13 @@ reconnect (struct GNUNET_CORE_Handle *h)
  * @param inbound_notify function to call for all inbound messages, can be NULL
  * @param inbound_hdr_only set to #GNUNET_YES if inbound_notify will only read the
  *                GNUNET_MessageHeader and hence we do not need to give it the full message;
- *                can be used to improve efficiency, ignored if @a inbound_notify is NULLL
+ *                can be used to improve efficiency, ignored if @a inbound_notify is NULL
  * @param outbound_notify function to call for all outbound messages, can be NULL
  * @param outbound_hdr_only set to #GNUNET_YES if outbound_notify will only read the
  *                GNUNET_MessageHeader and hence we do not need to give it the full message
- *                can be used to improve efficiency, ignored if @a outbound_notify is NULLL
+ *                can be used to improve efficiency, ignored if @a outbound_notify is NULL
  * @param handlers callbacks for messages we care about, NULL-terminated
- * @return handle to the core service (only useful for disconnect until 'init' is called);
+ * @return handle to the core service (only useful for disconnect until @a init is called);
  *                NULL on error (in this case, init is never called)
  */
 struct GNUNET_CORE_Handle *
@@ -887,6 +901,11 @@ GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Connecting to CORE service\n");
   reconnect (h);
+  if (NULL == h->mq)
+  {
+    GNUNET_CORE_disconnect (h);
+    return NULL;
+  }
   return h;
 }
 
@@ -963,6 +982,11 @@ GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
   struct SendMessageRequest *smr;
   struct GNUNET_MQ_Envelope *env;
 
+  if (NULL == handle->mq)
+  {
+    GNUNET_break (0); /* SEE #4588: do not call NTR from disconnect notification! */
+    return NULL;
+  }
   GNUNET_assert (NULL != notify);
   if ( (notify_size > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE) ||
        (notify_size + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) )
@@ -1003,6 +1027,8 @@ GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
   th->priority = priority;
   th->msize = notify_size;
   th->cork = cork;
+  if (NULL == handle->mq)
+    return th; /* see #4588 (hack until we transition core fully to MQ) */
   env = GNUNET_MQ_msg (smr,
                        GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
   smr->priority = htonl ((uint32_t) th->priority);