indentation, comment and style fixes, no semantic changes
[oweals/gnunet.git] / src / scalarproduct / scalarproduct_api.c
index cae045d62fc6e9b9f7317e42a928b2b6ae5f77ad..df9f8d196c52e69ad7808e9ab4953de4650ed8cb 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2013, 2014 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2013, 2014, 2016 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
  */
 /**
  * @file scalarproduct/scalarproduct_api.c
@@ -23,8 +23,6 @@
  * @author Christian Fuchs
  * @author Gaurav Kukreja
  * @author Christian Grothoff
- *
- * TODO: use MQ
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
@@ -62,22 +60,7 @@ struct GNUNET_SCALARPRODUCT_ComputationHandle
   /**
    * Current connection to the scalarproduct service.
    */
-  struct GNUNET_CLIENT_Connection *client;
-
-  /**
-   * Current transmit handle.
-   */
-  struct GNUNET_CLIENT_TransmitHandle *th;
-
-  /**
-   * the client's elements which
-   */
-  struct GNUNET_SCALARPRODUCT_Element *elements;
-
-  /**
-   * Message to be sent to the scalarproduct service
-   */
-  struct GNUNET_MessageHeader *msg;
+  struct GNUNET_MQ_Handle *mq;
 
   /**
    * Function to call after transmission of the request (Bob).
@@ -105,22 +88,50 @@ struct GNUNET_SCALARPRODUCT_ComputationHandle
    */
   struct GNUNET_HashCode key;
 
-  /**
-   * count of all @e elements we offer for computation
-   */
-  uint32_t element_count_total;
+};
 
-  /**
-   * count of the transfered @e elements we offer for computation
-   */
-  uint32_t element_count_transfered;
 
-  /**
-   * Type to use for the multipart messages.
-   */
-  uint16_t mp_type;
+/**
+ * Called when a response is received from the service. Perform basic
+ * check that the message is well-formed.
+ *
+ * @param cls Pointer to the Master Context
+ * @param message Pointer to the data received in response
+ * @return #GNUNET_OK if @a message is well-formed
+ */
+static int
+check_response (void *cls,
+                 const struct ClientResponseMessage *message)
+{
+  if (ntohs (message->header.size) !=
+      ntohl (message->product_length) + sizeof (struct ClientResponseMessage))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  return GNUNET_OK;
+}
 
-};
+
+/**
+ * Handles the STATUS received from the service for a response, does
+ * not contain a payload.  Called when we participate as "Bob" via
+ * #GNUNET_SCALARPRODUCT_accept_computation().
+ *
+ * @param h our Handle
+ * @param msg the response received
+ * @param status the condition the request was terminated with (eg: disconnect)
+ */
+static void
+process_status_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
+                        const struct ClientResponseMessage *msg,
+                        enum GNUNET_SCALARPRODUCT_ResponseStatus status)
+{
+  if (NULL != h->cont_status)
+    h->cont_status (h->cont_cls,
+                    status);
+  GNUNET_SCALARPRODUCT_cancel (h);
+}
 
 
 /**
@@ -132,40 +143,12 @@ struct GNUNET_SCALARPRODUCT_ComputationHandle
  * @param msg Pointer to the data received in response
  */
 static void
-receive_cb (void *cls,
-            const struct GNUNET_MessageHeader *msg)
+handle_response (void *cls,
+                 const struct ClientResponseMessage *message)
 {
   struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
-  const struct ClientResponseMessage *message;
   enum GNUNET_SCALARPRODUCT_ResponseStatus status;
 
-  if (NULL == msg)
-  {
-    LOG (GNUNET_ERROR_TYPE_INFO,
-         "Disconnected from SCALARPRODUCT service.\n");
-    h->response_proc (h,
-                      NULL,
-                      GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED);
-    return;
-  }
-  if (ntohs (msg->size) < sizeof (struct ClientResponseMessage))
-  {
-    GNUNET_break (0);
-    h->response_proc (h,
-                      NULL,
-                      GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE);
-    return;
-  }
-  message = (const struct ClientResponseMessage *) msg;
-  if (ntohs (msg->size) !=
-      ntohl (message->product_length) + sizeof (struct ClientResponseMessage))
-  {
-    GNUNET_break (0);
-    h->response_proc (h,
-                      NULL,
-                      GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE);
-    return;
-  }
   status = (enum GNUNET_SCALARPRODUCT_ResponseStatus) ntohl (message->status);
   h->response_proc (h,
                     message,
@@ -174,96 +157,57 @@ receive_cb (void *cls,
 
 
 /**
- * Transmits the request to the SCALARPRODUCT service
+ * Check if the keys for all given elements are unique.
  *
- * @param cls Closure with the `struct GNUNET_SCALARPRODUCT_ComputationHandle`
- * @param size Size of the buffer @a buf
- * @param buf Pointer to the buffer
- * @return Size of the message sent
+ * @param elements elements to check
+ * @param element_count size of the @a elements array
+ * @return #GNUNET_OK if all keys are unique
  */
-static size_t
-do_send_message (void *cls,
-                 size_t size,
-                 void *buf)
+static int
+check_unique (const struct GNUNET_SCALARPRODUCT_Element *elements,
+              uint32_t element_count)
 {
-  struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
-  struct ComputationBobCryptodataMultipartMessage *msg;
-  size_t ret;
-  uint32_t nsize;
-  uint32_t todo;
-
-  h->th = NULL;
-  if (NULL == buf)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Failed to transmit request to SCALARPRODUCT.\n");
-    /* notify caller about the error, done here */
-    h->response_proc (h, NULL,
-                      GNUNET_SCALARPRODUCT_STATUS_FAILURE);
-    return 0;
-  }
-  ret = ntohs (h->msg->size);
-  memcpy (buf, h->msg, ret);
-  GNUNET_free (h->msg);
-  h->msg = NULL;
-
-  /* done sending? */
-  if (h->element_count_total == h->element_count_transfered)
-  {
-    GNUNET_CLIENT_receive (h->client,
-                           &receive_cb, h,
-                           GNUNET_TIME_UNIT_FOREVER_REL);
-    return ret;
-  }
-
-  todo = h->element_count_total - h->element_count_transfered;
-  nsize = sizeof (struct ComputationBobCryptodataMultipartMessage)
-    + todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
-  if (GNUNET_SERVER_MAX_MESSAGE_SIZE <= size)
-  {
-    /* cannot do all of them, limit to what is possible in one message */
-    todo = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct ComputationBobCryptodataMultipartMessage))
-      / sizeof (struct GNUNET_SCALARPRODUCT_Element);
-    nsize = sizeof (struct ComputationBobCryptodataMultipartMessage)
-      + todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
-  }
-
-  msg = GNUNET_malloc (nsize);
-  h->msg = &msg->header;
-  msg->header.size = htons (nsize);
-  msg->header.type = htons (h->mp_type);
-  msg->element_count_contained = htonl (todo);
-  memcpy (&msg[1],
-          &h->elements[h->element_count_transfered],
-          todo * sizeof (struct GNUNET_SCALARPRODUCT_Element));
-  h->element_count_transfered += todo;
-  h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, nsize,
-                                               GNUNET_TIME_UNIT_FOREVER_REL,
-                                               GNUNET_NO,
-                                               &do_send_message, h);
-  GNUNET_assert (NULL != h->th);
-  return ret;
+  struct GNUNET_CONTAINER_MultiHashMap *map;
+  uint32_t i;
+  int ok;
+
+  ok = GNUNET_OK;
+  map = GNUNET_CONTAINER_multihashmap_create (2 * element_count,
+                                              GNUNET_YES);
+  for (i=0;i<element_count;i++)
+    if (GNUNET_OK !=
+        GNUNET_CONTAINER_multihashmap_put (map,
+                                           &elements[i].key,
+                                           map,
+                                           GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  _("Keys given to SCALARPRODUCT not unique!\n"));
+      ok = GNUNET_SYSERR;
+    }
+  GNUNET_CONTAINER_multihashmap_destroy (map);
+  return ok;
 }
 
 
 /**
- * Handles the STATUS received from the service for a response, does
- * not contain a payload.  Called when we participate as "Bob" via
- * #GNUNET_SCALARPRODUCT_accept_computation().
+ * We encountered an error communicating with the set service while
+ * performing a set operation. Report to the application.
  *
- * @param h our Handle
- * @param msg the response received
- * @param status the condition the request was terminated with (eg: disconnect)
+ * @param cls the `struct GNUNET_SCALARPRODUCT_ComputationHandle`
+ * @param error error code
  */
 static void
-process_status_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
-                        const struct ClientResponseMessage *msg,
-                        enum GNUNET_SCALARPRODUCT_ResponseStatus status)
+mq_error_handler (void *cls,
+                  enum GNUNET_MQ_Error error)
 {
-  if (NULL != h->cont_status)
-    h->cont_status (h->cont_cls,
-                    status);
-  GNUNET_SCALARPRODUCT_cancel (h);
+  struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
+
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "Disconnected from SCALARPRODUCT service.\n");
+  h->response_proc (h,
+                    NULL,
+                    GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED);
 }
 
 
@@ -286,63 +230,79 @@ GNUNET_SCALARPRODUCT_accept_computation (const struct GNUNET_CONFIGURATION_Handl
                                          GNUNET_SCALARPRODUCT_ContinuationWithStatus cont,
                                          void *cont_cls)
 {
-  struct GNUNET_SCALARPRODUCT_ComputationHandle *h;
+  struct GNUNET_SCALARPRODUCT_ComputationHandle *h
+    = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
+  struct GNUNET_MQ_MessageHandler handlers[] = {
+    GNUNET_MQ_hd_var_size (response,
+                           GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT,
+                           struct ClientResponseMessage,
+                           h),
+    GNUNET_MQ_handler_end ()
+  };
+  struct GNUNET_MQ_Envelope *env;
   struct BobComputationMessage *msg;
+  struct ComputationBobCryptodataMultipartMessage *mmsg;
   uint32_t size;
   uint16_t possible;
+  uint16_t todo;
+  uint32_t element_count_transfered;
+
 
-  h = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
+  if (GNUNET_SYSERR == check_unique (elements,
+                                     element_count))
+    return NULL;
   h->cont_status = cont;
   h->cont_cls = cont_cls;
   h->response_proc = &process_status_message;
   h->cfg = cfg;
   h->key = *session_key;
-  h->client = GNUNET_CLIENT_connect ("scalarproduct-bob", cfg);
-  h->element_count_total = element_count;
-  h->mp_type = GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART_BOB;
-  if (NULL == h->client)
+  h->mq = GNUNET_CLIENT_connect (cfg,
+                                 "scalarproduct-bob",
+                                 handlers,
+                                 &mq_error_handler,
+                                 h);
+  if (NULL == h->mq)
   {
     /* scalarproduct configuration error */
     GNUNET_break (0);
     GNUNET_free (h);
     return NULL;
   }
-  size = sizeof (struct BobComputationMessage)
-    + element_count * sizeof (struct GNUNET_SCALARPRODUCT_Element);
-  if (GNUNET_SERVER_MAX_MESSAGE_SIZE > size)
-  {
-    possible = element_count;
-    h->element_count_transfered = element_count;
-  }
-  else
-  {
-    /* create a multipart msg, first we calculate a new msg size for the head msg */
-    possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct BobComputationMessage))
-      / sizeof (struct GNUNET_SCALARPRODUCT_Element);
-    h->element_count_transfered = possible;
-    size = sizeof (struct BobComputationMessage)
-      + possible * sizeof (struct GNUNET_SCALARPRODUCT_Element);
-    h->elements = GNUNET_malloc (sizeof(struct GNUNET_SCALARPRODUCT_Element) * element_count);
-    memcpy (h->elements,
-            elements,
-            sizeof (struct GNUNET_SCALARPRODUCT_Element) * element_count);
-  }
-
-  msg = GNUNET_malloc (size);
-  h->msg = &msg->header;
-  msg->header.size = htons (size);
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB);
+  possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct BobComputationMessage))
+    / sizeof (struct GNUNET_SCALARPRODUCT_Element);
+  todo = GNUNET_MIN (possible,
+                     element_count);
+  size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
+  env = GNUNET_MQ_msg_extra (msg,
+                             size,
+                             GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB);
   msg->element_count_total = htonl (element_count);
-  msg->element_count_contained = htonl (possible);
+  msg->element_count_contained = htonl (todo);
   msg->session_key = *session_key;
-  memcpy (&msg[1],
+  GNUNET_memcpy (&msg[1],
           elements,
-          possible * sizeof (struct GNUNET_SCALARPRODUCT_Element));
-  h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, size,
-                                               GNUNET_TIME_UNIT_FOREVER_REL,
-                                               GNUNET_YES, /* retry is OK in the initial stage */
-                                               &do_send_message, h);
-  GNUNET_assert (NULL != h->th);
+          size);
+  element_count_transfered = todo;
+  GNUNET_MQ_send (h->mq,
+                  env);
+  possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (*mmsg))
+    / sizeof (struct GNUNET_SCALARPRODUCT_Element);
+  while (element_count_transfered < element_count)
+  {
+    todo = GNUNET_MIN (possible,
+                       element_count - element_count_transfered);
+    size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
+    env = GNUNET_MQ_msg_extra (mmsg,
+                               size,
+                               GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB);
+    mmsg->element_count_contained = htonl (todo);
+    GNUNET_memcpy (&mmsg[1],
+            &elements[element_count_transfered],
+            size);
+    element_count_transfered += todo;
+    GNUNET_MQ_send (h->mq,
+                    env);
+  }
   return h;
 }
 
@@ -389,9 +349,9 @@ process_result_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
       }
       else
       {
-        if (0 < ntohl (msg->range))
+        if (0 < (int32_t) ntohl (msg->range))
           gcry_mpi_add (result, result, num);
-        else if (0 > ntohl (msg->range))
+        else
           gcry_mpi_sub (result, result, num);
         gcry_mpi_release (num);
       }
@@ -428,65 +388,81 @@ GNUNET_SCALARPRODUCT_start_computation (const struct GNUNET_CONFIGURATION_Handle
                                         GNUNET_SCALARPRODUCT_DatumProcessor cont,
                                         void *cont_cls)
 {
-  struct GNUNET_SCALARPRODUCT_ComputationHandle *h;
+  struct GNUNET_SCALARPRODUCT_ComputationHandle *h
+    = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
+  struct GNUNET_MQ_MessageHandler handlers[] = {
+    GNUNET_MQ_hd_var_size (response,
+                           GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT,
+                           struct ClientResponseMessage,
+                           h),
+    GNUNET_MQ_handler_end ()
+  };
+  struct GNUNET_MQ_Envelope *env;
   struct AliceComputationMessage *msg;
+  struct ComputationBobCryptodataMultipartMessage *mmsg;
   uint32_t size;
-  uint32_t possible;
+  uint16_t possible;
+  uint16_t todo;
+  uint32_t element_count_transfered;
 
-  h = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
-  h->client = GNUNET_CLIENT_connect ("scalarproduct-alice", cfg);
-  if (NULL == h->client)
+  if (GNUNET_SYSERR == check_unique (elements,
+                                     element_count))
+    return NULL;
+  h->mq = GNUNET_CLIENT_connect (cfg,
+                                 "scalarproduct-alice",
+                                 handlers,
+                                 &mq_error_handler,
+                                 h);
+  if (NULL == h->mq)
   {
     /* missconfigured scalarproduct service */
     GNUNET_break (0);
     GNUNET_free (h);
     return NULL;
   }
-  h->element_count_total = element_count;
   h->cont_datum = cont;
   h->cont_cls = cont_cls;
   h->response_proc = &process_result_message;
   h->cfg = cfg;
   h->key = *session_key;
-  h->mp_type = GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART_ALICE;
-  size = sizeof (struct AliceComputationMessage)
-    + element_count * sizeof (struct GNUNET_SCALARPRODUCT_Element);
-  if (GNUNET_SERVER_MAX_MESSAGE_SIZE > size)
-  {
-    possible = element_count;
-    h->element_count_transfered = element_count;
-  }
-  else
-  {
-    /* create a multipart msg, first we calculate a new msg size for the head msg */
-    possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct AliceComputationMessage))
-      / sizeof (struct GNUNET_SCALARPRODUCT_Element);
-    h->element_count_transfered = possible;
-    size = sizeof (struct AliceComputationMessage)
-      + possible * sizeof (struct GNUNET_SCALARPRODUCT_Element);
-    h->elements = GNUNET_malloc (sizeof(struct GNUNET_SCALARPRODUCT_Element) * element_count);
-    memcpy (h->elements,
-            elements,
-            sizeof (struct GNUNET_SCALARPRODUCT_Element) * element_count);
-  }
 
-  msg = GNUNET_malloc (size);
-  h->msg = &msg->header;
-  msg->header.size = htons (size);
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
+  possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct AliceComputationMessage))
+      / sizeof (struct GNUNET_SCALARPRODUCT_Element);
+  todo = GNUNET_MIN (possible,
+                     element_count);
+  size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
+  env = GNUNET_MQ_msg_extra (msg,
+                             size,
+                             GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
   msg->element_count_total = htonl (element_count);
-  msg->element_count_contained = htonl (possible);
+  msg->element_count_contained = htonl (todo);
   msg->reserved = htonl (0);
   msg->peer = *peer;
   msg->session_key = *session_key;
-  memcpy (&msg[1],
+  GNUNET_memcpy (&msg[1],
           elements,
-          sizeof (struct GNUNET_SCALARPRODUCT_Element) * possible);
-  h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, size,
-                                               GNUNET_TIME_UNIT_FOREVER_REL,
-                                               GNUNET_YES, /* retry is OK in the initial stage */
-                                               &do_send_message, h);
-  GNUNET_assert (NULL != h->th);
+          size);
+  GNUNET_MQ_send (h->mq,
+                  env);
+  element_count_transfered = todo;
+  possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (*mmsg))
+    / sizeof (struct GNUNET_SCALARPRODUCT_Element);
+  while (element_count_transfered < element_count)
+  {
+    todo = GNUNET_MIN (possible,
+                       element_count - element_count_transfered);
+    size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
+    env = GNUNET_MQ_msg_extra (mmsg,
+                               size,
+                               GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE);
+    mmsg->element_count_contained = htonl (todo);
+    GNUNET_memcpy (&mmsg[1],
+            &elements[element_count_transfered],
+            size);
+    element_count_transfered += todo;
+    GNUNET_MQ_send (h->mq,
+                    env);
+  }
   return h;
 }
 
@@ -500,17 +476,10 @@ GNUNET_SCALARPRODUCT_start_computation (const struct GNUNET_CONFIGURATION_Handle
 void
 GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
 {
-  if (NULL != h->th)
-  {
-    GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
-    h->th = NULL;
-  }
-  GNUNET_free_non_null (h->elements);
-  GNUNET_free_non_null (h->msg);
-  if (NULL != h->client)
+  if (NULL != h->mq)
   {
-    GNUNET_CLIENT_disconnect (h->client);
-    h->client = NULL;
+    GNUNET_MQ_destroy (h->mq);
+    h->mq = NULL;
   }
   GNUNET_free (h);
 }