indentation, comment and style fixes, no semantic changes
[oweals/gnunet.git] / src / scalarproduct / scalarproduct_api.c
index 297b3505e1ccd2998a97a522cf80a893b7124baf..df9f8d196c52e69ad7808e9ab4953de4650ed8cb 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2013 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
 
      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
  * @brief API for the scalarproduct
  * @author Christian Fuchs
  * @author Gaurav Kukreja
- *
+ * @author Christian Grothoff
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
 
 #define LOG(kind,...) GNUNET_log_from (kind, "scalarproduct-api",__VA_ARGS__)
 
-/**************************************************************
- ***  Datatype Declarations                          **********
- **************************************************************/
 
 /**
- * the abstraction function for our internal callback
+ * The abstraction function for our internal callback
+ *
+ * @param h computation handle
+ * @param msg response we got, NULL on errors
+ * @param status processing status code
  */
-typedef void (*GNUNET_SCALARPRODUCT_ResponseMessageHandler) (void *cls,
-                                                             const struct GNUNET_MessageHeader *msg,
-                                                             enum GNUNET_SCALARPRODUCT_ResponseStatus status);
+typedef void
+(*GNUNET_SCALARPRODUCT_ResponseMessageHandler) (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
+                                                const struct ClientResponseMessage *msg,
+                                                enum GNUNET_SCALARPRODUCT_ResponseStatus status);
+
 
 /**
  * A handle returned for each computation
@@ -58,48 +60,8 @@ struct GNUNET_SCALARPRODUCT_ComputationHandle
   /**
    * Current connection to the scalarproduct service.
    */
-  struct GNUNET_CLIENT_Connection *client;
-
-  /**
-   * Handle for statistics.
-   */
-  struct GNUNET_STATISTICS_Handle *stats;
-
-  /**
-   * The shared session key identifying this computation
-   */
-  struct GNUNET_HashCode key;
-
-  /**
-   * Current transmit handle.
-   */
-  struct GNUNET_CLIENT_TransmitHandle *th;
-
-  /**
-   * count of all elements we offer for computation
-   */
-  uint32_t element_count_total;
+  struct GNUNET_MQ_Handle *mq;
 
-  /**
-   * count of the transfered elements we offer for computation
-   */
-  uint32_t element_count_transfered;
-  
-  /**
-   * the client's elements which 
-   */
-  struct GNUNET_SCALARPRODUCT_Element * elements;
-  
-  /**
-   * Message to be sent to the scalarproduct service
-   */
-  void * msg;
-
-  /**
-   * The client's msg handler callback
-   */
-  union
-  {
   /**
    * Function to call after transmission of the request (Bob).
    */
@@ -109,350 +71,299 @@ struct GNUNET_SCALARPRODUCT_ComputationHandle
    * Function to call after transmission of the request (Alice).
    */
   GNUNET_SCALARPRODUCT_DatumProcessor cont_datum;
-  };
 
   /**
-   * Closure for 'cont'.
+   * Closure for @e cont_status or @e cont_datum.
    */
   void *cont_cls;
 
   /**
-   * API internal callback for results and failures to be forwarded to the client
+   * API internal callback for results and failures to be forwarded to
+   * the client.
    */
   GNUNET_SCALARPRODUCT_ResponseMessageHandler response_proc;
-  
+
   /**
-   * 
+   * The shared session key identifying this computation
    */
-  GNUNET_SCHEDULER_TaskIdentifier cont_multipart;
-};
-
-/**************************************************************
- ***  Forward Function Declarations                          **********
- **************************************************************/
-
-void
-GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle * h);
+  struct GNUNET_HashCode key;
 
-static size_t do_send_message (void *cls, size_t size, void *buf);
-/**************************************************************
- ***  Static Function Declarations                   **********
- **************************************************************/
+};
 
 
 /**
- * Handles the STATUS received from the service for a response, does not contain a payload
+ * Called when a response is received from the service. Perform basic
+ * check that the message is well-formed.
  *
- * @param cls our Handle
- * @param msg Pointer to the response received
- * @param status the condition the request was terminated with (eg: disconnect)
+ * @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 void
-process_status_message (void *cls,
-                        const struct GNUNET_MessageHeader *msg,
-                        enum GNUNET_SCALARPRODUCT_ResponseStatus status)
+static int
+check_response (void *cls,
+                 const struct ClientResponseMessage *message)
 {
-  struct GNUNET_SCALARPRODUCT_ComputationHandle *qe = cls;
-
-  qe->cont_status (qe->cont_cls, status);
+  if (ntohs (message->header.size) !=
+      ntohl (message->product_length) + sizeof (struct ClientResponseMessage))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  return GNUNET_OK;
 }
 
 
 /**
- * Handles the RESULT received from the service for a request, should contain a result MPI value
+ * 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 cls our Handle
- * @param msg Pointer to the response received
+ * @param h our Handle
+ * @param msg the response received
  * @param status the condition the request was terminated with (eg: disconnect)
  */
 static void
-process_result_message (void *cls,
-                        const struct GNUNET_MessageHeader *msg,
+process_status_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
+                        const struct ClientResponseMessage *msg,
                         enum GNUNET_SCALARPRODUCT_ResponseStatus status)
 {
-  struct GNUNET_SCALARPRODUCT_ComputationHandle *qe = cls;
-  const struct GNUNET_SCALARPRODUCT_client_response *message =
-          (const struct GNUNET_SCALARPRODUCT_client_response *) msg;
-  gcry_mpi_t result = NULL;
-  gcry_error_t rc;
-
-  if (GNUNET_SCALARPRODUCT_Status_Success == status)
-    {
-      size_t product_len = ntohl (message->product_length);
-      result = gcry_mpi_new (0);
-
-      if (0 < product_len)
-        {
-          gcry_mpi_t num;
-          size_t read = 0;
-
-          if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD, &message[1], product_len, &read)))
-            {
-              LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
-              gcry_mpi_release (result);
-              result = NULL;
-              status = GNUNET_SCALARPRODUCT_Status_InvalidResponse;
-            }
-          else
-            {
-              if (0 < message->range)
-                gcry_mpi_add (result, result, num);
-              else if (0 > message->range)
-                gcry_mpi_sub (result, result, num);
-              gcry_mpi_release (num);
-            }
-        }
-    }
-  qe->cont_datum (qe->cont_cls, status, result);
+  if (NULL != h->cont_status)
+    h->cont_status (h->cont_cls,
+                    status);
+  GNUNET_SCALARPRODUCT_cancel (h);
 }
 
 
 /**
- * Called when a response is received from the service. After basic check, the
- * handler in qe->response_proc is called. This functions handles the response
- * to the client which used the API.
+ * Called when a response is received from the service. After basic
+ * check, the handler in `h->response_proc` is called. This functions
+ * handles the response to the client which used the API.
  *
  * @param cls Pointer to the Master Context
  * @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 GNUNET_SCALARPRODUCT_client_response *message =
-          (const struct GNUNET_SCALARPRODUCT_client_response *) msg;
-  enum GNUNET_SCALARPRODUCT_ResponseStatus status = GNUNET_SCALARPRODUCT_Status_InvalidResponse;
+  enum GNUNET_SCALARPRODUCT_ResponseStatus status;
 
-  if (NULL == msg)
-    {
-      LOG (GNUNET_ERROR_TYPE_WARNING, "Disconnected by Service.\n");
-      status = GNUNET_SCALARPRODUCT_Status_ServiceDisconnected;
-    }
-  else if (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT != ntohs (msg->type))
-    {
-      LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid message type received\n");
-    }
-  else if (0 < ntohl (message->product_length) || (0 == message->range))
-    {
-      // response for the responder client, successful
-      GNUNET_STATISTICS_update (h->stats,
-                                gettext_noop ("# SUC responder result messages received"), 1,
-                                GNUNET_NO);
-
-      status = GNUNET_SCALARPRODUCT_Status_Success;
-    }
-
-  if (h->cont_status != NULL)
-    h->response_proc (h, msg, status);
-
-  GNUNET_free (h);
+  status = (enum GNUNET_SCALARPRODUCT_ResponseStatus) ntohl (message->status);
+  h->response_proc (h,
+                    message,
+                    status);
 }
 
 
-static void
-send_multipart (void * cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
+/**
+ * Check if the keys for all given elements are unique.
+ *
+ * @param elements elements to check
+ * @param element_count size of the @a elements array
+ * @return #GNUNET_OK if all keys are unique
+ */
+static int
+check_unique (const struct GNUNET_SCALARPRODUCT_Element *elements,
+              uint32_t element_count)
 {
-  struct GNUNET_SCALARPRODUCT_ComputationHandle *h = (struct GNUNET_SCALARPRODUCT_ComputationHandle *) cls;
-  struct GNUNET_SCALARPRODUCT_computation_message_multipart *msg;
-  uint32_t size;
-  uint32_t todo;
-
-  h->cont_multipart = GNUNET_SCHEDULER_NO_TASK;
-
-  todo = h->element_count_total - h->element_count_transfered;
-  size = sizeof (struct GNUNET_SCALARPRODUCT_computation_message_multipart) +todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
-  if (GNUNET_SERVER_MAX_MESSAGE_SIZE <= size) {
-    //create a multipart msg, first we calculate a new msg size for the head msg
-    todo = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct GNUNET_SCALARPRODUCT_computation_message_multipart)) / sizeof (struct GNUNET_SCALARPRODUCT_Element);
-    size = sizeof (struct GNUNET_SCALARPRODUCT_computation_message_multipart) +todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
-  }
-
-  msg = (struct GNUNET_SCALARPRODUCT_computation_message_multipart*) GNUNET_malloc (size);
-  h->msg = msg;
-  msg->header.size = htons (size);
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART);
-  msg->element_count_contained = htonl (todo);
-
-  memcpy (&msg[1], &h->elements[h->element_count_transfered], todo);
-  h->element_count_transfered += todo;
-
-  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);
-
-  if (!h->th) {
-    LOG (GNUNET_ERROR_TYPE_ERROR,
-         _ ("Failed to send a multipart message to the scalarproduct service\n"));
-    GNUNET_STATISTICS_update (h->stats,
-                              gettext_noop ("# transmission request failures"),
-                              1, GNUNET_NO);
-    GNUNET_STATISTICS_destroy (h->stats, GNUNET_YES);
-    GNUNET_CLIENT_disconnect (h->client);
-    GNUNET_free (h->msg);
-    h->msg = NULL;
-    if (h->cont_status != NULL)
-      h->response_proc (h, NULL, GNUNET_SCALARPRODUCT_Status_Failure);
-
-    GNUNET_SCALARPRODUCT_cancel (cls);
-  }
+  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;
 }
 
+
 /**
- * Transmits the request to the VectorProduct Service
+ * We encountered an error communicating with the set service while
+ * performing a set operation. Report to the application.
  *
- * @param cls Closure
- * @param size Size of the buffer
- * @param buf Pointer to the buffer
- *
- * @return Size of the message sent
+ * @param cls the `struct GNUNET_SCALARPRODUCT_ComputationHandle`
+ * @param error error code
  */
-static size_t
-do_send_message (void *cls, size_t size,
-                 void *buf)
+static void
+mq_error_handler (void *cls,
+                  enum GNUNET_MQ_Error error)
 {
   struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
 
-  if (NULL == buf) {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to transmit request to SCALARPRODUCT.\n");
-    GNUNET_STATISTICS_update (h->stats,
-                              gettext_noop ("# transmission request failures"),
-                              1, GNUNET_NO);
-
-    // notify caller about the error, done here.
-    if (h->cont_status != NULL)
-      h->response_proc (h, NULL, GNUNET_SCALARPRODUCT_Status_Failure);
-
-    GNUNET_SCALARPRODUCT_cancel (cls);
-    return 0;
-  }
-  memcpy (buf, h->msg, size);
-
-  GNUNET_free (h->msg);
-  h->msg = NULL;
-  h->th = NULL;
-
-#if INSANE_STATISTICS
-  GNUNET_STATISTICS_update (h->stats,
-                            gettext_noop ("# bytes sent to scalarproduct"), 1,
-                            GNUNET_NO);
-#endif
-
-  /* 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 size;
-  }
-  
-  h->cont_multipart = GNUNET_SCHEDULER_add_now (&send_multipart, h);
-  
-  return size;
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "Disconnected from SCALARPRODUCT service.\n");
+  h->response_proc (h,
+                    NULL,
+                    GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED);
 }
 
 
-/**************************************************************
- ***  API                                            **********
- **************************************************************/
-
-
 /**
  * Used by Bob's client to cooperate with Alice,
  *
  * @param cfg the gnunet configuration handle
  * @param key Session key unique to the requesting client
  * @param elements Array of elements of the vector
- * @param element_count Number of elements in the vector
+ * @param element_count Number of elements in the @a elements vector
  * @param cont Callback function
- * @param cont_cls Closure for the callback function
- *
+ * @param cont_cls Closure for @a cont
  * @return a new handle for this computation
  */
 struct GNUNET_SCALARPRODUCT_ComputationHandle *
-GNUNET_SCALARPRODUCT_accept_computation (const struct GNUNET_CONFIGURATION_Handle * cfg,
-                               const struct GNUNET_HashCode * session_key,
-                               const struct GNUNET_SCALARPRODUCT_Element * elements,
-                               uint32_t element_count,
-                               GNUNET_SCALARPRODUCT_ContinuationWithStatus cont,
-                               void * cont_cls)
+GNUNET_SCALARPRODUCT_accept_computation (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                                         const struct GNUNET_HashCode *session_key,
+                                         const struct GNUNET_SCALARPRODUCT_Element *elements,
+                                         uint32_t element_count,
+                                         GNUNET_SCALARPRODUCT_ContinuationWithStatus cont,
+                                         void *cont_cls)
 {
-  struct GNUNET_SCALARPRODUCT_ComputationHandle *h;
-  struct GNUNET_SCALARPRODUCT_computation_message *msg;
+  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;
 
-  GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= sizeof (struct GNUNET_SCALARPRODUCT_computation_message)
-                 + element_count * sizeof (int32_t));
-  h = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
-  h->client = GNUNET_CLIENT_connect ("scalarproduct", cfg);
-  if (!h->client)
-    {
-      LOG (GNUNET_ERROR_TYPE_ERROR,
-           _ ("Failed to connect to the scalarproduct service\n"));
-      GNUNET_free (h);
-      return NULL;
-    }
-  h->stats = GNUNET_STATISTICS_create ("scalarproduct-api", cfg);
-  if (!h->stats)
-    {
-      LOG (GNUNET_ERROR_TYPE_ERROR,
-           _ ("Failed to send a message to the statistics service\n"));
-      GNUNET_CLIENT_disconnect (h->client);
-      GNUNET_free (h);
-      return NULL;
-    }
-
-  h->element_count_total = element_count;
-  size = sizeof (struct GNUNET_SCALARPRODUCT_computation_message) + 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 GNUNET_SCALARPRODUCT_computation_message)) / sizeof (struct GNUNET_SCALARPRODUCT_Element);
-    h->element_count_transfered = possible;
-    size = sizeof (struct GNUNET_SCALARPRODUCT_computation_message) + possible*sizeof (struct GNUNET_SCALARPRODUCT_Element);
-    h->elements = (struct GNUNET_SCALARPRODUCT_Element*) 
-            GNUNET_malloc (sizeof(struct GNUNET_SCALARPRODUCT_Element) * element_count);
-    memcpy (h->elements, elements, sizeof (struct GNUNET_SCALARPRODUCT_Element)*element_count);
-  }
 
+  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;
-  memcpy (&h->key, session_key, sizeof (struct GNUNET_HashCode));
-
-  msg = (struct GNUNET_SCALARPRODUCT_computation_message*) GNUNET_malloc (size);
-  h->msg = msg;
-  msg->header.size = htons (size);
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB);
+  h->key = *session_key;
+  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;
+  }
+  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;
+  GNUNET_memcpy (&msg[1],
+          elements,
+          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;
+}
 
-  memcpy (&msg->session_key, session_key, sizeof (struct GNUNET_HashCode));
-  memcpy (&msg[1], elements, 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);
-  if (!h->th)
+/**
+ * Handles the RESULT received from the service for a request, should
+ * contain a result MPI value.  Called when we participate as "Alice" via
+ * #GNUNET_SCALARPRODUCT_start_computation().
+ *
+ * @param h our Handle
+ * @param msg Pointer to the response received
+ * @param status the condition the request was terminated with (eg: disconnect)
+ */
+static void
+process_result_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
+                        const struct ClientResponseMessage *msg,
+                        enum GNUNET_SCALARPRODUCT_ResponseStatus status)
+{
+  uint32_t product_len;
+  gcry_mpi_t result = NULL;
+  gcry_error_t rc;
+  gcry_mpi_t num;
+  size_t rsize;
+
+  if (GNUNET_SCALARPRODUCT_STATUS_SUCCESS == status)
+  {
+    result = gcry_mpi_new (0);
+
+    product_len = ntohl (msg->product_length);
+    if (0 < product_len)
     {
-      LOG (GNUNET_ERROR_TYPE_ERROR,
-           _ ("Failed to send a message to the scalarproduct service\n"));
-      GNUNET_STATISTICS_update (h->stats,
-                              gettext_noop ("# transmission request failures"),
-                              1, GNUNET_NO);
-      GNUNET_STATISTICS_destroy (h->stats, GNUNET_YES);
-      GNUNET_CLIENT_disconnect (h->client);
-      GNUNET_free (h->msg);
-      GNUNET_free_non_null (h->elements);
-      GNUNET_free (h);
-      return NULL;
+      rsize = 0;
+      if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD,
+                                    &msg[1],
+                                    product_len,
+                                    &rsize)))
+      {
+        LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
+                  "gcry_mpi_scan",
+                  rc);
+        gcry_mpi_release (result);
+        result = NULL;
+        status = GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE;
+      }
+      else
+      {
+        if (0 < (int32_t) ntohl (msg->range))
+          gcry_mpi_add (result, result, num);
+        else
+          gcry_mpi_sub (result, result, num);
+        gcry_mpi_release (num);
+      }
     }
-  return h;
+  }
+  if (NULL != h->cont_datum)
+    h->cont_datum (h->cont_cls,
+                   status,
+                   result);
+  if (NULL != result)
+    gcry_mpi_release (result);
+  GNUNET_SCALARPRODUCT_cancel (h);
 }
 
 
@@ -463,99 +374,99 @@ GNUNET_SCALARPRODUCT_accept_computation (const struct GNUNET_CONFIGURATION_Handl
  * @param session_key Session key should be unique to the requesting client
  * @param peer PeerID of the other peer
  * @param elements Array of elements of the vector
- * @param element_count Number of elements in the vector
+ * @param element_count Number of elements in the @a elements vector
  * @param cont Callback function
- * @param cont_cls Closure for the callback function
- *
+ * @param cont_cls Closure for @a cont
  * @return a new handle for this computation
  */
 struct GNUNET_SCALARPRODUCT_ComputationHandle *
-GNUNET_SCALARPRODUCT_start_computation (const struct GNUNET_CONFIGURATION_Handle * cfg,
-                              const struct GNUNET_HashCode * session_key,
-                              const struct GNUNET_PeerIdentity *peer,
-                              const struct GNUNET_SCALARPRODUCT_Element * elements,
-                              uint32_t element_count,
-                              GNUNET_SCALARPRODUCT_DatumProcessor cont,
-                              void * cont_cls)
+GNUNET_SCALARPRODUCT_start_computation (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                                        const struct GNUNET_HashCode *session_key,
+                                        const struct GNUNET_PeerIdentity *peer,
+                                        const struct GNUNET_SCALARPRODUCT_Element *elements,
+                                        uint32_t element_count,
+                                        GNUNET_SCALARPRODUCT_DatumProcessor cont,
+                                        void *cont_cls)
 {
-  struct GNUNET_SCALARPRODUCT_ComputationHandle *h;
-  struct GNUNET_SCALARPRODUCT_computation_message *msg;
+  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;
   uint16_t possible;
+  uint16_t todo;
+  uint32_t element_count_transfered;
 
-  h = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
-  h->client = GNUNET_CLIENT_connect ("scalarproduct", cfg);
-  if (!h->client)
-    {
-      LOG (GNUNET_ERROR_TYPE_ERROR,
-           _ ("Failed to connect to the scalarproduct service\n"));
-      GNUNET_free (h);
-      return NULL;
-    }
-  h->stats = GNUNET_STATISTICS_create ("scalarproduct-api", cfg);
-  if (!h->stats)
-    {
-      LOG (GNUNET_ERROR_TYPE_ERROR,
-           _ ("Failed to send a message to the statistics service\n"));
-      GNUNET_CLIENT_disconnect (h->client);
-      GNUNET_free (h);
-      return NULL;
-    }
-
-  h->element_count_total = element_count;
-  size = sizeof (struct GNUNET_SCALARPRODUCT_computation_message) + 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 GNUNET_SCALARPRODUCT_computation_message)) / sizeof (struct GNUNET_SCALARPRODUCT_Element);
-    h->element_count_transfered = possible;
-    size = sizeof (struct GNUNET_SCALARPRODUCT_computation_message) + possible*sizeof (struct GNUNET_SCALARPRODUCT_Element);
-    h->elements = (struct GNUNET_SCALARPRODUCT_Element*) 
-            GNUNET_malloc (sizeof(struct GNUNET_SCALARPRODUCT_Element) * element_count);
-    memcpy (h->elements, elements, sizeof (struct GNUNET_SCALARPRODUCT_Element)*element_count);
+  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->cont_datum = cont;
   h->cont_cls = cont_cls;
   h->response_proc = &process_result_message;
   h->cfg = cfg;
-  memcpy (&h->key, session_key, sizeof (struct GNUNET_HashCode));
-
-  msg = (struct GNUNET_SCALARPRODUCT_computation_message*) GNUNET_malloc (size);
-  h->msg = msg;
-  msg->header.size = htons (size);
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
+  h->key = *session_key;
+
+  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);
-
-  memcpy (&msg->peer, peer, sizeof (struct GNUNET_PeerIdentity));
-  memcpy (&msg->session_key, session_key, sizeof (struct GNUNET_HashCode));
-  memcpy (&msg[1], elements, 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);
-  if (!h->th)
-    {
-      LOG (GNUNET_ERROR_TYPE_ERROR,
-           _ ("Failed to send a message to the scalarproduct service\n"));
-      GNUNET_STATISTICS_update (h->stats,
-                              gettext_noop ("# transmission request failures"),
-                              1, GNUNET_NO);
-      GNUNET_STATISTICS_destroy (h->stats, GNUNET_YES);
-      GNUNET_CLIENT_disconnect (h->client);
-      GNUNET_free (h->msg);
-      GNUNET_free_non_null (h->elements);
-      GNUNET_free (h);
-      return NULL;
-    }
+  msg->element_count_contained = htonl (todo);
+  msg->reserved = htonl (0);
+  msg->peer = *peer;
+  msg->session_key = *session_key;
+  GNUNET_memcpy (&msg[1],
+          elements,
+          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;
 }
 
+
 /**
  * Cancel an ongoing computation or revoke our collaboration offer.
  * Closes the connection to the service
@@ -563,16 +474,13 @@ GNUNET_SCALARPRODUCT_start_computation (const struct GNUNET_CONFIGURATION_Handle
  * @param h computation handle to terminate
  */
 void
-GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle * h)
+GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
 {
-  if (NULL != h->th)
-    GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
-  if (GNUNET_SCHEDULER_NO_TASK != h->cont_multipart)
-    GNUNET_SCHEDULER_cancel (h->cont_multipart);
-  GNUNET_free_non_null (h->elements);
-  GNUNET_free_non_null (h->msg);
-  GNUNET_CLIENT_disconnect (h->client);
-  GNUNET_STATISTICS_destroy (h->stats, GNUNET_YES);
+  if (NULL != h->mq)
+  {
+    GNUNET_MQ_destroy (h->mq);
+    h->mq = NULL;
+  }
   GNUNET_free (h);
 }