migrate peerstore to new service MQ API
[oweals/gnunet.git] / src / include / gnunet_mq_lib.h
index 8f0c6ee3fb1c5a6c411d33ff1bce7c2520b27201..08434698e053935d22892eeea7ad26462a8f72b5 100644 (file)
@@ -42,7 +42,8 @@
  * The allocated message will already have the type and size field set.
  *
  * @param mvar variable to store the allocated message in;
- *             must have a header field
+ *             must have a header field;
+ *             can be NULL
  * @param esize extra space to allocate after the message
  * @param type type of the message
  * @return the MQ message
@@ -54,7 +55,8 @@
  * The contained message will already have the type and size field set.
  *
  * @param mvar variable to store the allocated message in;
- *             must have a header field
+ *             must have a header field;
+ *             can be NULL
  * @param type type of the message
  * @return the allocated envelope
  */
  * @param mh message header to extract nested message header from
  * @param base_size size of the message before the nested message's header appears
  * @return pointer to the nested message, does not copy the message
+ *         OR NULL in case of a malformed message.
  */
 const struct GNUNET_MessageHeader *
 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
@@ -171,7 +174,13 @@ enum GNUNET_MQ_Error
    * We received a message that was malformed and thus
    * could not be passed to its handler.
    */
-  GNUNET_MQ_ERROR_MALFORMED = 8
+  GNUNET_MQ_ERROR_MALFORMED = 8,
+
+  /**
+   * We received a message for which we have no matching
+   * handler.
+   */
+  GNUNET_MQ_ERROR_NO_MATCH = 16
 };
 
 
@@ -323,27 +332,26 @@ struct GNUNET_MQ_MessageHandler
  *                      const struct GNUNET_MessageTest *msg)
  * { ... }
  *
- * GNUNET_MQ_hd_fixed_size(test_message,
- *                         GNUNET_MESSAGE_TYPE_TEST,
- *                         struct GNUNET_MessageTest);
  * struct GNUNET_MQ_MessageHandler handlers[] = {
- *   make_test_message_handler (NULL),
+ *   GNUNET_MQ_hd_fixed_size(test_message,
+ *                           GNUNET_MESSAGE_TYPE_TEST,
+ *                           struct GNUNET_MessageTest,
+ *                           "context"),
  *   GNUNET_MQ_handler_end()
  * };
  *
  * @param name unique basename for the functions
  * @param code message type constant
  * @param str type of the message (a struct)
+ * @param ctx context for the callbacks
  */
-#define GNUNET_MQ_hd_fixed_size(name,code,str)   \
-  struct GNUNET_MQ_MessageHandler                           \
-  make_##name##_handler (void *cls) {                        \
-    void (*cb)(void *cls, const str *msg) = &handle_##name;  \
-    struct GNUNET_MQ_MessageHandler mh = {                  \
-      NULL, (GNUNET_MQ_MessageCallback) cb,                  \
-      cls, code, sizeof (str) };                             \
-    return mh;                                               \
-  }
+#define GNUNET_MQ_hd_fixed_size(name,code,str,ctx)           \
+  ({                                                         \
+    void (*_cb)(void *cls, const str *msg) = &handle_##name; \
+    ((struct GNUNET_MQ_MessageHandler) {                     \
+      NULL, (GNUNET_MQ_MessageCallback) _cb,                 \
+      (ctx), (code), sizeof (str) });                        \
+  })
 
 
 /**
@@ -356,9 +364,6 @@ struct GNUNET_MQ_MessageHandler
  * The macro is to be used as follows:
  * <code>
  * struct GNUNET_MessageTest { ... }; // can be variable size
- * GNUNET_MQ_hd_var_size(test_message,
- *                       GNUNET_MESSAGE_TYPE_TEST,
- *                       struct GNUNET_MessageTest);
  * static int
  * check_test (void *cls,
  *             const struct GNUNET_MessageTest *msg)
@@ -377,25 +382,27 @@ struct GNUNET_MQ_MessageHandler
  * }
  *
  * struct GNUNET_MQ_MessageHandler handlers[] = {
- *   make_test_message_handler ("context"),
+ *   GNUNET_MQ_hd_var_size(test_message,
+ *                         GNUNET_MESSAGE_TYPE_TEST,
+ *                         struct GNUNET_MessageTest,
+ *                         "context"),
  *   GNUNET_MQ_handler_end()
  * };
  *
  * @param name unique basename for the functions
  * @param code message type constant
  * @param str type of the message (a struct)
+ * @param ctx context for the callbacks
  */
-#define GNUNET_MQ_hd_var_size(name,code,str)               \
-  struct GNUNET_MQ_MessageHandler                         \
-  make_##name##_handler (void *ctx) {                     \
-    int (*mv)(void *cls, const str *msg) = &check_##name;  \
-    void (*cb)(void *cls, const str *msg) = &handle_##name;\
-    struct GNUNET_MQ_MessageHandler mh =                   \
-      { (GNUNET_MQ_MessageValidationCallback) mv,          \
-       (GNUNET_MQ_MessageCallback) cb,                    \
-       ctx, code, sizeof (str) };                         \
-    return mh;                                             \
-  }
+#define GNUNET_MQ_hd_var_size(name,code,str,ctx)             \
+  ({                                                         \
+    int (*_mv)(void *cls, const str *msg) = &check_##name;   \
+    void (*_cb)(void *cls, const str *msg) = &handle_##name; \
+    ((struct GNUNET_MQ_MessageHandler)                       \
+      { (GNUNET_MQ_MessageValidationCallback) _mv,           \
+        (GNUNET_MQ_MessageCallback) _cb,                     \
+        (ctx), (code), sizeof (str) });                      \
+  })
 
 
 /**
@@ -434,8 +441,8 @@ GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
 
 
 /**
- * Function to obtain the current envelope from
- * within #GNUNET_MQ_SendImpl implementations.
+ * Function to obtain the current envelope
+ * from within #GNUNET_MQ_SendImpl implementations.
  *
  * @param mq message queue to interrogate
  * @return the current envelope
@@ -444,6 +451,16 @@ struct GNUNET_MQ_Envelope *
 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq);
 
 
+/**
+ * Function to obtain the last envelope in the queue.
+ *
+ * @param mq message queue to interrogate
+ * @return the last envelope in the queue
+ */
+struct GNUNET_MQ_Envelope *
+GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq);
+
+
 /**
  * Set application-specific options for this envelope.
  * Overrides the options set for the queue with
@@ -605,12 +622,12 @@ GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
  *
  * @param ev message to call the notify callback for
  * @param cb the notify callback
- * @param cls closure for the callback
+ * @param cb_cls closure for the callback
  */
 void
 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
                        GNUNET_MQ_NotifyCallback cb,
-                       void *cls);
+                       void *cb_cls);
 
 
 /**
@@ -622,6 +639,36 @@ void
 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
 
 
+/**
+ * Handle we return for callbacks registered to be
+ * notified when #GNUNET_MQ_destroy() is called on a queue.
+ */
+struct GNUNET_MQ_DestroyNotificationHandle;
+
+
+/**
+ * Register function to be called whenever @a mq is being
+ * destroyed.
+ *
+ * @param mq message queue to watch
+ * @param cb function to call on @a mq destruction
+ * @param cb_cls closure for @a cb
+ * @return handle for #GNUNET_MQ_destroy_notify_cancel().
+ */
+struct GNUNET_MQ_DestroyNotificationHandle *
+GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
+                         GNUNET_SCHEDULER_TaskCallback cb,
+                         void *cb_cls);
+
+/**
+ * Cancel registration from #GNUNET_MQ_destroy_notify().
+ *
+ * @param dnh handle for registration to cancel
+ */
+void
+GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh);
+
+
 /**
  * Call the message message handler that was registered
  * for the type of the given message in the given message queue.