tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / rps / test_rps.c
index cbd3ba8450d7bb38ac80784798d3ac0d65032098..7fc91743b35d762395c855cd54e37f8e34637a25 100644 (file)
@@ -14,6 +14,8 @@
     
      You should have received a copy of the GNU Affero General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 /**
  * @file rps/test_rps.c
@@ -189,6 +191,11 @@ struct RPSPeer
    */
   struct GNUNET_RPS_Handle *rps_handle;
 
+  /**
+   * Handle to stream requests
+   */
+  struct GNUNET_RPS_StreamRequestHandle *rps_srh;
+
   /**
    * ID of the peer.
    */
@@ -1116,6 +1123,11 @@ rps_disconnect_adapter (void *cls,
   struct RPSPeer *peer = cls;
   struct GNUNET_RPS_Handle *h = op_result;
 
+  if (NULL != peer->rps_srh)
+  {
+    GNUNET_RPS_stream_cancel (peer->rps_srh);
+    peer->rps_srh = NULL;
+  }
   GNUNET_assert (NULL != peer);
   GNUNET_RPS_disconnect (h);
   peer->rps_handle = NULL;
@@ -1345,7 +1357,7 @@ static void mal_init_peer (struct RPSPeer *rps_peer)
 static void
 mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
 {
-  #ifdef ENABLE_MALICIOUS
+  #if ENABLE_MALICIOUS
   uint32_t num_mal_peers;
 
   GNUNET_assert ( (1 >= portion) &&
@@ -1376,7 +1388,7 @@ mal_cb (struct RPSPeer *rps_peer)
     return;
   }
 
-  #ifdef ENABLE_MALICIOUS
+  #if ENABLE_MALICIOUS
   GNUNET_assert ( (1 >= portion) &&
                   (0 <  portion) );
   num_mal_peers = round (portion * num_peers);
@@ -1547,25 +1559,57 @@ churn_test_cb (struct RPSPeer *rps_peer)
  * SUB
 ***********************************/
 
-void sub_post (struct RPSPeer *rps_peer)
+static void
+got_stream_peer_cb (void *cls,
+                    uint64_t num_peers,
+                    const struct GNUNET_PeerIdentity *peers)
 {
-  GNUNET_RPS_sub_stop (rps_peer->rps_handle, "test");
+  const struct RPSPeer *rps_peer = cls;
+
+  for (uint64_t i = 0; i < num_peers; i++)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Peer %" PRIu32 " received [%s] from stream.\n",
+                rps_peer->index,
+                GNUNET_i2s (&peers[i]));
+    if (0 != rps_peer->index &&
+        0 == memcmp (&peers[i],
+                     &rps_peers[0].peer_id,
+                     sizeof (struct GNUNET_PeerIdentity)))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Received a peer id outside sub\n");
+      ok = 1;
+    }
+    else if (0 == rps_peer->index &&
+             0 != memcmp (&peers[i],
+                          &rps_peers[0].peer_id,
+                          sizeof (struct GNUNET_PeerIdentity)))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Received a peer id outside sub (lonely)\n");
+      ok = 1;
+    }
+  }
 }
 
+
 static void
-sub_stop_op (void *cls)
+sub_post (struct RPSPeer *rps_peer)
 {
-  struct GNUNET_RPS_Handle *h = cls;
-
-  GNUNET_RPS_sub_stop (h, "test");
+  if (0 != rps_peer->index) GNUNET_RPS_sub_stop (rps_peer->rps_handle, "test");
+  else GNUNET_RPS_sub_stop (rps_peer->rps_handle, "lonely");
 }
 
+
 static void
 sub_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
 {
   (void) rps_peer;
 
-  GNUNET_RPS_sub_start (h, "test");
+  if (0 != rps_peer->index) GNUNET_RPS_sub_start (h, "test");
+  else GNUNET_RPS_sub_start (h, "lonely"); /* have a group of one */
+  rps_peer->rps_srh = GNUNET_RPS_stream_request (h,
+                                                 &got_stream_peer_cb,
+                                                 rps_peer);
 }
 
 /***********************************
@@ -1920,26 +1964,6 @@ profiler_eval (void)
   return evaluate ();
 }
 
-static uint32_t fac (uint32_t x)
-{
-  if (1 >= x)
-  {
-    return x;
-  }
-  return x * fac (x - 1);
-}
-
-static uint32_t binom (uint32_t n, uint32_t k)
-{
-  //GNUNET_assert (n >= k);
-  if (k > n) return 0;
-  if (0 > n) return 0;
-  if (0 > k) return 0;
-  if (0 == k) return 1;
-  return fac (n)
-    /
-    fac(k) * fac(n - k);
-}
 
 /**
  * @brief is b in view of a?
@@ -2764,8 +2788,6 @@ run (void *cls,
   timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
       (timeout_s * 1.2) + 0.1 * num_peers);
   shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
-  shutdown_task = GNUNET_SCHEDULER_add_shutdown (shutdown_op, NULL);
-
 }
 
 
@@ -2986,7 +3008,7 @@ main (int argc, char *argv[])
   }
 
   ret_value = cur_test_run.eval_cb();
-  
+
   if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
   {
     GNUNET_array_grow (rps_peers->cur_view,
@@ -2999,4 +3021,5 @@ main (int argc, char *argv[])
   return ret_value;
 }
 
+
 /* end of test_rps.c */