Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / rps / test_rps.c
index 63eb8417a7e2379824db5ea423ee198b55c381b8..1ce174454dc8e909675442775057ad12e81e3793 100644 (file)
@@ -38,7 +38,7 @@
 /**
  * How many peers do we start?
  */
-uint32_t num_peers;
+static uint32_t num_peers;
 
 /**
  * How long do we run the test?
@@ -256,18 +256,11 @@ static unsigned int num_peers_online;
  */
 static int ok;
 
-
 /**
  * Identifier for the churn task that runs periodically
  */
 static struct GNUNET_SCHEDULER_Task *churn_task;
 
-/**
- * Identifier for the churn task that runs periodically
- */
-static struct GNUNET_SCHEDULER_Task *shutdown_task;
-
-
 /**
  * Called to initialise the given RPSPeer
  */
@@ -362,7 +355,7 @@ static int in_shutdown;
  * Append arguments to file
  */
 static void
-tofile_ (const char *file_name, char *line)
+tofile_ (const char *file_name, const char *line)
 {
   struct GNUNET_DISK_FileHandle *f;
   /* char output_buffer[512]; */
@@ -403,15 +396,22 @@ tofile_ (const char *file_name, char *line)
   if (size != size2)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "Unable to write to file! (Size: %u, size2: %u)\n",
+                "Unable to write to file! (Size: %lu, size2: %lu)\n",
                 size,
                 size2);
+    if (GNUNET_YES != GNUNET_DISK_file_close (f))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  "Unable to close file\n");
+    }
     return;
   }
 
   if (GNUNET_YES != GNUNET_DISK_file_close (f))
+  {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                 "Unable to close file\n");
+  }
 }
 
 /**
@@ -430,7 +430,7 @@ tofile_ (const char *file_name, char *line)
 
 
 /**
- * Write the ids and their according index in the given array to a file 
+ * Write the ids and their according index in the given array to a file
  * Unused
  */
 /* static void
@@ -493,7 +493,7 @@ make_oplist_entry ()
  * Task run on timeout to shut everything down.
  */
 static void
-shutdown_op (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_op (void *cls)
 {
   unsigned int i;
 
@@ -513,11 +513,11 @@ shutdown_op (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 /**
  * Seed peers.
  */
-  void
-seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+static void
+seed_peers (void *cls)
 {
+  struct RPSPeer *peer = cls;
   unsigned int amount;
-  struct RPSPeer *peer = (struct RPSPeer *) cls;
   unsigned int i;
 
   // TODO if malicious don't seed mal peers
@@ -532,13 +532,14 @@ seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
 }
 
+
 /**
  * Seed peers.
  */
-  void
-seed_peers_big (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+static void
+seed_peers_big (void *cls)
 {
-  struct RPSPeer *peer = (struct RPSPeer *) cls;
+  struct RPSPeer *peer = cls;
   unsigned int seed_msg_size;
   uint32_t num_peers_max;
   unsigned int amount;
@@ -592,10 +593,11 @@ info_cb (void *cb_cls,
   rps_peer_ids[entry->index] = *(pinfo->result.id);
   rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
 
-  GNUNET_CONTAINER_multipeermap_put (peer_map,
-      &rps_peer_ids[entry->index],
-      &rps_peers[entry->index],
-      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+  GNUNET_assert (GNUNET_OK ==
+      GNUNET_CONTAINER_multipeermap_put (peer_map,
+        &rps_peer_ids[entry->index],
+        &rps_peers[entry->index],
+        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
   tofile ("/tmp/rps/peer_ids",
            "%u\t%s\n",
            entry->index,
@@ -740,7 +742,7 @@ default_reply_handle (void *cls,
               "[%s] got %" PRIu64 " peers:\n",
               GNUNET_i2s (rps_peer->peer_id),
               n);
-  
+
   for (i = 0; i < n; i++)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -753,8 +755,7 @@ default_reply_handle (void *cls,
 
   if (0 == evaluate ())
   {
-    GNUNET_SCHEDULER_cancel (shutdown_task);
-    shutdown_task = GNUNET_SCHEDULER_add_now (&shutdown_op, NULL);
+    GNUNET_SCHEDULER_shutdown ();
   }
 }
 
@@ -762,11 +763,10 @@ default_reply_handle (void *cls,
  * Request random peers.
  */
 static void
-request_peers (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+request_peers (void *cls)
 {
+  struct PendingRequest *pending_req = cls;
   struct RPSPeer *rps_peer;
-  struct PendingRequest *pending_req = (struct PendingRequest *) cls;
   struct PendingReply *pending_rep;
 
   if (GNUNET_YES == in_shutdown)
@@ -827,11 +827,10 @@ cancel_request (struct PendingReply *pending_rep)
  * Cancel a request.
  */
 static void
-cancel_request_cb (void *cls,
-                const struct GNUNET_SCHEDULER_TaskContext *tc)
+cancel_request_cb (void *cls)
 {
+  struct RPSPeer *rps_peer = cls;
   struct PendingReply *pending_rep;
-  struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
 
   if (GNUNET_YES == in_shutdown)
     return;
@@ -1137,8 +1136,9 @@ manage_service_wrapper (unsigned int i, unsigned int j, int delta,
   }
 }
 
+
 static void
-churn (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+churn (void *cls)
 {
   unsigned int i;
   unsigned int j;
@@ -1243,6 +1243,7 @@ profiler_reply_handle (void *cls,
              "%s\n",
              GNUNET_i2s_full (&recv_peers[i]));
     rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
+    GNUNET_assert (NULL != rcv_rps_peer);
     tofile (file_name_dh,
              "%" PRIu32 "\n",
              (uint32_t) rcv_rps_peer->index);
@@ -1300,6 +1301,7 @@ file_name_cb (void *cls, const char *filename)
     {
       RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
     }
+    RPS_sampler_elem_destroy (s_elem);
   }
   return GNUNET_OK;
 }
@@ -1388,7 +1390,7 @@ run (void *cls,
 
   if (NULL != churn_task)
     GNUNET_SCHEDULER_cancel (churn_task);
-  shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
+  GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
 }