made the service more resilient against out of order and simply incorrect messages
[oweals/gnunet.git] / src / scalarproduct / gnunet-scalarproduct.c
index f56fd7625456a636ca2f20d366a8f6ba2c638bc7..32ed952685c92b84c2d0a28e0d33056be36e6417 100644 (file)
@@ -77,9 +77,9 @@ static int ret = -1;
 
 /**
  * Callback called if we are initiating a new computation session
- * 
+ *
  * @param cls unused
- * @param status if our job was successfully processed 
+ * @param status if our job was successfully processed
  */
 static void
 responder_callback (void *cls,
@@ -104,19 +104,20 @@ responder_callback (void *cls,
   default:
     LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: return code %d\n", GNUNET_h2s (&closure->key), status);
   }
+  GNUNET_SCHEDULER_shutdown();
 }
 
 
 /**
  * Callback called if we are initiating a new computation session
- * 
+ *
  * @param cls unused
  * @param key unused
  * @param peer unused
- * @param status if our job was successfully processed 
+ * @param status if our job was successfully processed
  * @param size size of the msg returned
  * @param msg the response we got.
- * @param type of the message received 
+ * @param type of the message received
  */
 static void
 requester_callback (void *cls,
@@ -149,8 +150,21 @@ requester_callback (void *cls,
   default:
     LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: return code %d\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer), status);
   }
+  GNUNET_SCHEDULER_shutdown();
 }
 
+/**
+ * Task run during shutdown.
+ *
+ * @param cls unused
+ * @param tc unused
+ */
+static void
+shutdown_task (void *cls,
+               const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  GNUNET_SCALARPRODUCT_disconnect ();
+}
 
 /**
  * Main function that will be run by the scheduler.
@@ -172,8 +186,8 @@ run (void *cls,
   int i;
   int32_t * elements;
   unsigned char * mask;
-  unsigned short mask_bytes;
-  unsigned short element_count = 0;
+  uint32_t mask_bytes;
+  uint32_t element_count = 0;
   struct ScalarProductCallbackClosure * closure;
 
   if (NULL == input_elements)
@@ -211,13 +225,13 @@ run (void *cls,
     // get the length of the current element and replace , with null
     for (end = begin; *end && *end != ','; end++);
 
-    if (1 == sscanf (begin, "%" SCNd32",", &element))
+    if (1 == sscanf (begin, "%" SCNd32 ",", &element))
     {
       //element in the middle
       element_count++;
       begin = end + 1;
     }
-    else if (*begin == 0)
+    else if (0 == *begin)
     {
       break;
     }
@@ -230,25 +244,26 @@ run (void *cls,
   while (1);
   if (0 == element_count)
   {
-
+    LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Need elements to compute the vectorproduct, got none.\n"));
     return;
   }
 
   begin = input_elements;
   elements = GNUNET_malloc (sizeof (int32_t) * element_count);
+  element_count = 0;
   /* Read input_elements_peer1, and put in elements_peer1 array */
   do
   {
     // get the length of the current element and replace , with null
     for (end = begin; *end && *end != ','; end++);
 
-    if (1 == sscanf (begin, "%" SCNd32",", &element))
+    if (1 == sscanf (begin, "%" SCNd32 ",", &elements[element_count]))
     {
       //element in the middle
       element_count++;
       begin = end + 1;
     }
-    else if (*begin == 0)
+    else if (0 == *begin)
     {
       break;
     }
@@ -256,7 +271,7 @@ run (void *cls,
   while (1);
 
   mask_bytes = element_count / 8 + (element_count % 8 ? 1 : 0);
-  mask = GNUNET_malloc ((element_count / 8) + 2);
+  mask = GNUNET_malloc ((element_count / 8) + 1);
 
   /* Read input_mask_peer1 and read in mask_peer1 array */
   if ((NULL != input_peer_id) && (NULL != input_mask))
@@ -310,6 +325,10 @@ run (void *cls,
                                                                          (void *) &closure)))
     return;
 
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
+                                &shutdown_task,
+                                NULL);
+
   ret = 0;
 }