clean up for configs
[oweals/gnunet.git] / src / scalarproduct / gnunet-scalarproduct.c
index 83c907b6c4b4e88f657829593ec2d4c72b193b2f..012ce91b8c2d043d0d92c7dbf7904ac724842925 100644 (file)
@@ -36,6 +36,9 @@
 #define LOG(kind,...) GNUNET_log_from (kind, "gnunet-scalarproduct",__VA_ARGS__)
 #define INPUTSTRINGLENGTH       1024
 
+/**
+ * A primitive closure structure holding information about our session
+ */
 struct ScalarProductCallbackClosure
 {
   /**
@@ -52,22 +55,22 @@ struct ScalarProductCallbackClosure
 /**
  * Option -p: destination peer identity for checking message-ids with
  */
-static char *input_peer_id = NULL;
+static char *input_peer_id;
 
 /**
  * Option -p: destination peer identity for checking message-ids with
  */
-static char *input_key = NULL;
+static char *input_key;
 
 /**
  * Option -e: vector to calculate a scalarproduct with
  */
-static char *input_elements = NULL;
+static char *input_elements;
 
 /**
  * Option -m: message-ids to calculate a scalarproduct with
  */
-static char *input_mask = NULL;
+static char *input_mask;
 
 /**
  * Global return value
@@ -77,9 +80,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,
@@ -91,32 +94,41 @@ responder_callback (void *cls,
   {
   case GNUNET_SCALARPRODUCT_Status_Success:
     ret = 0;
-    LOG (GNUNET_ERROR_TYPE_INFO, "Session %s concluded.\n", GNUNET_h2s (&closure->key));
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         "Session %s concluded.\n",
+         GNUNET_h2s (&closure->key));
     break;
   case GNUNET_SCALARPRODUCT_Status_InvalidResponse:
-    LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: invalid response\n", GNUNET_h2s (&closure->key));
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         "Session %s failed: invalid response\n",
+         GNUNET_h2s (&closure->key));
     break;
   case GNUNET_SCALARPRODUCT_Status_Failure:
-    LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: service failure\n", GNUNET_h2s (&closure->key));
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         "Session %s failed: service failure\n",
+         GNUNET_h2s (&closure->key));
+    break;
   case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected:
-    LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: service disconnect!!\n", GNUNET_h2s (&closure->key));
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         "Session %s failed: service disconnect!\n",
+         GNUNET_h2s (&closure->key));
     break;
   default:
-    LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: return code %d\n", GNUNET_h2s (&closure->key), status);
+    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 size size of the msg returned
- * @param msg the response we got.
- * @param type of the message received 
+ * @param status if our job was successfully processed
+ * @param result the result in gnu/gcry MPI format
  */
 static void
 requester_callback (void *cls,
@@ -136,21 +148,39 @@ requester_callback (void *cls,
       printf ("%s", buf);
     }
     else
-      LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_aprint", rc);
+      LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
+                "gcry_mpi_aprint",
+                rc);
     break;
   case GNUNET_SCALARPRODUCT_Status_InvalidResponse:
-    LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: invalid response received\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         "Session %s with peer %s failed: invalid response received\n",
+         GNUNET_h2s (&closure->key),
+         GNUNET_i2s (&closure->peer));
     break;
   case GNUNET_SCALARPRODUCT_Status_Failure:
-    LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: API failure\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         "Session %s with peer %s failed: API failure\n",
+         GNUNET_h2s (&closure->key),
+         GNUNET_i2s (&closure->peer));
+    break;
   case GNUNET_SCALARPRODUCT_Status_ServiceDisconnected:
-    LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s was disconnected from service.\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         "Session %s with peer %s was disconnected from service.\n",
+         GNUNET_h2s (&closure->key),
+         GNUNET_i2s (&closure->peer));
     break;
   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);
+    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.
  *
@@ -164,6 +194,7 @@ shutdown_task (void *cls,
   GNUNET_SCALARPRODUCT_disconnect ();
 }
 
+
 /**
  * Main function that will be run by the scheduler.
  *
@@ -178,41 +209,47 @@ run (void *cls,
      const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
-  char * begin = input_elements;
-  char * end;
+  char *begin = input_elements;
+  char *end;
   int32_t element;
   int i;
-  int32_t * elements;
-  unsigned char * mask;
-  unsigned short mask_bytes;
-  unsigned short element_count = 0;
+  int32_t *elements;
+  unsigned char *mask;
+  uint32_t mask_bytes;
+  uint32_t element_count = 0;
   struct ScalarProductCallbackClosure * closure;
 
   if (NULL == input_elements)
   {
-    LOG (GNUNET_ERROR_TYPE_ERROR, _ ("You must specify at least one message ID to check!\n"));
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         _ ("You must specify at least one message ID to check!\n"));
     return;
   }
 
   if (NULL == input_key)
   {
-    LOG (GNUNET_ERROR_TYPE_ERROR, _ ("This program needs a session identifier for comparing vectors.\n"));
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         _ ("This program needs a session identifier for comparing vectors.\n"));
     return;
   }
 
   if (1 > strnlen (input_key, sizeof (struct GNUNET_HashCode)))
   {
-    LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Please give a session key for --input_key!\n"));
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         _ ("Please give a session key for --input_key!\n"));
     return;
   }
   closure = GNUNET_new (struct ScalarProductCallbackClosure);
   GNUNET_CRYPTO_hash (input_key, strlen (input_key), &closure->key);
 
-  if (input_peer_id && GNUNET_OK != GNUNET_CRYPTO_hash_from_string (input_peer_id,
-                                                                    (struct GNUNET_HashCode *) &closure->peer))
+  if (input_peer_id &&
+      (GNUNET_OK !=
+       GNUNET_CRYPTO_hash_from_string (input_peer_id,
+                                       (struct GNUNET_HashCode *) &closure->peer)))
   {
-    LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Tried to set initiator mode, as peer ID was given. "
-                                     "However, `%s' is not a valid peer identifier.\n"),
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         _ ("Tried to set initiator mode, as peer ID was given. "
+            "However, `%s' is not a valid peer identifier.\n"),
          input_peer_id);
     return;
   }
@@ -223,52 +260,55 @@ 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;
     }
     else
     {
-      LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Could not convert `%s' to int32_t.\n"), begin);
+      LOG (GNUNET_ERROR_TYPE_ERROR,
+           _ ("Could not convert `%s' to int32_t.\n"), begin);
       return;
     }
   }
   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;
     }
   }
   while (1);
 
-  mask_bytes = element_count / 8 + (element_count % 8 ? 1 : 0);
-  mask = GNUNET_malloc ((element_count / 8) + 2);
+  mask_bytes = element_count / 8 + ( (element_count % 8) ? 1 : 0);
+  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))
@@ -292,7 +332,8 @@ run (void *cls,
       }
       else
       {
-        LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Could not convert `%s' to int32_t.\n"), begin);
+        LOG (GNUNET_ERROR_TYPE_ERROR,
+             _ ("Could not convert `%s' to integer.\n"), begin);
         return;
       }
 
@@ -306,26 +347,37 @@ run (void *cls,
     for (i = 0; i <= mask_bytes; i++)
       mask[i] = UCHAR_MAX; // all 1's
 
-  if (input_peer_id && (NULL == GNUNET_SCALARPRODUCT_request (cfg,
-                                                              &closure->key,
-                                                              &closure->peer,
-                                                              elements, element_count,
-                                                              mask, mask_bytes,
-                                                              &requester_callback,
-                                                              (void *) &closure)))
+  if (input_peer_id &&
+      (NULL == GNUNET_SCALARPRODUCT_request (cfg,
+                                             &closure->key,
+                                             &closure->peer,
+                                             elements, element_count,
+                                             mask, mask_bytes,
+                                             &requester_callback,
+                                             (void *) &closure)))
+  {
+    GNUNET_free (elements);
+    GNUNET_free (mask);
     return;
+  }
 
-  if ((NULL == input_peer_id) && (NULL == GNUNET_SCALARPRODUCT_response (cfg,
-                                                                         &closure->key,
-                                                                         elements, element_count,
-                                                                         &responder_callback,
-                                                                         (void *) &closure)))
+  if ((NULL == input_peer_id) &&
+      (NULL == GNUNET_SCALARPRODUCT_response (cfg,
+                                              &closure->key,
+                                              elements, element_count,
+                                              &responder_callback,
+                                              (void *) &closure)))
+  {
+    GNUNET_free (elements);
+    GNUNET_free (mask);
     return;
-
+  }
+  GNUNET_free (elements);
+  GNUNET_free (mask);
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
                                 &shutdown_task,
                                 NULL);
-  
+
   ret = 0;
 }