+/**
+ * @brief Computes the probability for each other peer to be selected by the
+ * sampling process based on the views of all peers
+ *
+ * @param peer_idx index of the peer that is about to sample
+ */
+static void compute_probabilities (uint32_t peer_idx)
+{
+ //double probs[num_peers] = { 0 };
+ double probs[num_peers];
+ size_t probs_as_str_size = (num_peers * 10 + 1) * sizeof (char);
+ char *probs_as_str = GNUNET_malloc (probs_as_str_size);
+ char *probs_as_str_cpy;
+ uint32_t i;
+ double prob_push;
+ double prob_pull;
+ uint32_t view_size;
+ uint32_t cont_views;
+ uint32_t number_of_being_in_pull_events;
+ int tmp;
+ uint32_t count_non_zero_prob = 0;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Computing probabilities for peer %" PRIu32 "\n", peer_idx);
+ /* Firstly without knowledge of old views */
+ for (i = 0; i < num_peers; i++)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "\tfor peer %" PRIu32 ":\n", i);
+ view_size = rps_peers[i].cur_view_count;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "\t\tview_size: %" PRIu32 "\n", view_size);
+ /* For peer i the probability of being sampled is
+ * evenly distributed among all possibly observed peers. */
+ /* We could have observed a peer in three cases:
+ * 1. peer sent a push
+ * 2. peer was contained in a pull reply
+ * 3. peer was in history (sampler) - ignored for now */
+ /* 1. Probability of having received a push from peer i */
+ if ((GNUNET_YES == is_in_view (i, peer_idx)) &&
+ (1 <= (0.45 * view_size)))
+ {
+ prob_push = 1.0 * binom (0.45 * view_size, 1)
+ /
+ binom (view_size, 0.45 * view_size);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "\t\t%" PRIu32 " is in %" PRIu32 "'s view, prob: %f\n",
+ peer_idx,
+ i,
+ prob_push);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "\t\tposs choices from view: %" PRIu32 ", containing i: %" PRIu32 "\n",
+ binom (view_size, 0.45 * view_size),
+ binom (0.45 * view_size, 1));
+ } else {
+ prob_push = 0;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "\t\t%" PRIu32 " is not in %" PRIu32 "'s view, prob: 0\n",
+ peer_idx,
+ i);
+ }
+ /* 2. Probability of peer i being contained in pulls */
+ view_size = rps_peers[peer_idx].cur_view_count;
+ cont_views = count_containing_views (peer_idx, i);
+ number_of_being_in_pull_events =
+ (binom (view_size, 0.45 * view_size) -
+ binom (view_size - cont_views, 0.45 * view_size));
+ if (0 != number_of_being_in_pull_events)
+ {
+ prob_pull = number_of_being_in_pull_events
+ /
+ (1.0 * binom (view_size, 0.45 * view_size));
+ } else
+ {
+ prob_pull = 0;
+ }
+ probs[i] = prob_push + prob_pull - (prob_push * prob_pull);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "\t\t%" PRIu32 " has %" PRIu32 " of %" PRIu32
+ " peers in its view who know %" PRIu32 " prob: %f\n",
+ peer_idx,
+ cont_views,
+ view_size,
+ i,
+ prob_pull);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "\t\tnumber of possible pull combinations: %" PRIu32 "\n",
+ binom (view_size, 0.45 * view_size));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "\t\tnumber of possible pull combinations without %" PRIu32
+ ": %" PRIu32 "\n",
+ i,
+ binom (view_size - cont_views, 0.45 * view_size));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "\t\tnumber of possible pull combinations with %" PRIu32
+ ": %" PRIu32 "\n",
+ i,
+ number_of_being_in_pull_events);
+
+ if (0 != probs[i]) count_non_zero_prob++;
+ }
+ /* normalize */
+ if (0 != count_non_zero_prob)
+ {
+ for (i = 0; i < num_peers; i++)
+ {
+ probs[i] = probs[i] * (1.0 / count_non_zero_prob);
+ }
+ } else {
+ for (i = 0; i < num_peers; i++)
+ {
+ probs[i] = 0;
+ }
+ }
+ /* str repr */
+ for (i = 0; i < num_peers; i++)
+ {
+ probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
+ tmp = GNUNET_snprintf (probs_as_str,
+ probs_as_str_size,
+ "%s %7.6f", probs_as_str_cpy, probs[i]);
+ GNUNET_free (probs_as_str_cpy);
+ GNUNET_assert (0 <= tmp);
+ }
+
+ to_file_w_len (rps_peers[peer_idx].file_name_probs,
+ probs_as_str_size,
+ probs_as_str);
+ GNUNET_free (probs_as_str);
+}
+
+/**
+ * @brief This counts the number of peers in which views a given peer occurs.
+ *
+ * It also stores this value in the rps peer.
+ *
+ * @param peer_idx the index of the peer to count the representation
+ *
+ * @return the number of occurrences
+ */
+static uint32_t count_peer_in_views_2 (uint32_t peer_idx)
+{
+ uint32_t i, j;
+ uint32_t count = 0;
+
+ for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
+ {
+ for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
+ {
+ if (0 == memcmp (rps_peers[peer_idx].peer_id,
+ &rps_peers[i].cur_view[j],
+ sizeof (struct GNUNET_PeerIdentity)))
+ {
+ count++;
+ break;
+ }
+ }
+ }
+ rps_peers[peer_idx].count_in_views = count;
+ return count;
+}
+
+static uint32_t cumulated_view_sizes ()
+{
+ uint32_t i;
+
+ view_sizes = 0;
+ for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
+ {
+ view_sizes += rps_peers[i].cur_view_count;
+ }
+ return view_sizes;
+}
+
+static void count_peer_in_views (uint32_t *count_peers)