+ * @brief Remove the given stream request from the list of requests and memory
+ *
+ * @param srh The request to be removed
+ * @param srh_head Head of the DLL to remove request from
+ * @param srh_tail Tail of the DLL to remove request from
+ */
+static void
+remove_stream_request (struct GNUNET_RPS_StreamRequestHandle *srh,
+ struct GNUNET_RPS_StreamRequestHandle *srh_head,
+ struct GNUNET_RPS_StreamRequestHandle *srh_tail)
+{
+ GNUNET_CONTAINER_DLL_remove (srh_head,
+ srh_tail,
+ srh);
+
+ GNUNET_free (srh);
+}
+
+
+/**
+ * @brief Called once the sampler has collected all requested peers.
+ *
+ * Calls the callback provided by the client with the corresponding cls.
+ *
+ * @param peers The array of @a num_peers that has been returned.
+ * @param num_peers The number of peers that have been returned
+ * @param cls The #GNUNET_RPS_Request_Handle
+ */
+void
+peers_ready_cb (const struct GNUNET_PeerIdentity *peers,
+ uint32_t num_peers,
+ void *cls)
+{
+ struct GNUNET_RPS_Request_Handle *rh = cls;
+
+ rh->ready_cb (rh->ready_cb_cls,
+ num_peers,
+ peers);
+ // TODO cleanup, sampler, rh, cancel stuff
+ // TODO screw this function. We can give the cb,cls directly to the sampler.
+}
+
+
+/**
+ * @brief Callback to collect the peers from the biased stream and put those
+ * into the sampler.
+ *
+ * @param cls The #GNUNET_RPS_Request_Handle
+ * @param num_peers The number of peer that have been returned
+ * @param peers The array of @a num_peers that have been returned
+ */
+void
+collect_peers_cb (void *cls,
+ uint64_t num_peers,
+ const struct GNUNET_PeerIdentity *peers)
+{
+ struct GNUNET_RPS_Request_Handle *rh = cls;
+
+ for (uint64_t i = 0; i < num_peers; i++)
+ {
+ RPS_sampler_update (rh->sampler, &peers[i]);
+ }
+}
+
+
+/**
+ * @brief Create new request handle
+ *
+ * @param rps_handle Handle to the service
+ * @param num_requests Number of requests
+ * @param ready_cb Callback
+ * @param cls Closure
+ *
+ * @return The newly created request handle
+ */
+static struct GNUNET_RPS_Request_Handle *
+new_request_handle (struct GNUNET_RPS_Handle *rps_handle,
+ uint64_t num_requests,
+ GNUNET_RPS_NotifyReadyCB ready_cb,
+ void *cls)
+{
+ struct GNUNET_RPS_Request_Handle *rh;
+
+ rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
+ rh->rps_handle = rps_handle;
+ rh->num_requests = num_requests;
+ rh->sampler = RPS_sampler_mod_init (num_requests,
+ GNUNET_TIME_UNIT_SECONDS); // TODO remove this time-stuff
+ rh->sampler_rh = RPS_sampler_get_n_rand_peers (rh->sampler,
+ num_requests,
+ peers_ready_cb,
+ rh);
+ rh->srh = GNUNET_RPS_stream_request (rps_handle,
+ 0, /* infinite updates */
+ collect_peers_cb,
+ rh); /* cls */
+ rh->ready_cb = ready_cb;
+ rh->ready_cb_cls = cls;
+
+ return rh;
+}
+
+
+/* Get internals for debugging/profiling purposes */
+
+/**
+ * Request updates of view
+ *
+ * @param rps_handle handle to the rps service
+ * @param num_req_peers number of peers we want to receive
+ * (0 for infinite updates)
+ * @param cls a closure that will be given to the callback
+ * @param ready_cb the callback called when the peers are available
+ */
+void
+GNUNET_RPS_view_request (struct GNUNET_RPS_Handle *rps_handle,
+ uint32_t num_updates,
+ GNUNET_RPS_NotifyReadyCB view_update_cb,
+ void *cls)
+{
+ struct GNUNET_MQ_Envelope *ev;
+ struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Client requests %" PRIu32 " view updates\n",
+ num_updates);
+ rps_handle->view_update_cb = view_update_cb;
+ rps_handle->view_update_cls = cls;
+
+ ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST);
+ msg->num_updates = htonl (num_updates);
+ GNUNET_MQ_send (rps_handle->mq, ev);
+}
+
+
+void
+GNUNET_RPS_view_request_cancel (struct GNUNET_RPS_Handle *rps_handle)
+{
+ struct GNUNET_MQ_Envelope *ev;
+
+ GNUNET_assert (NULL != rps_handle->view_update_cb);
+
+ rps_handle->view_update_cb = NULL;
+
+ ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_CANCEL);
+ GNUNET_MQ_send (rps_handle->mq, ev);
+}
+
+
+/**
+ * Request biased stream of peers that are being put into the sampler
+ *
+ * @param rps_handle handle to the rps service
+ * @param num_req_peers number of peers we want to receive
+ * (0 for infinite updates)
+ * @param cls a closure that will be given to the callback
+ * @param ready_cb the callback called when the peers are available
+ */
+struct GNUNET_RPS_StreamRequestHandle *
+GNUNET_RPS_stream_request (struct GNUNET_RPS_Handle *rps_handle,
+ uint32_t num_peers,
+ GNUNET_RPS_NotifyReadyCB stream_input_cb,
+ void *cls)
+{
+ struct GNUNET_RPS_StreamRequestHandle *srh;
+ struct GNUNET_MQ_Envelope *ev;
+ struct GNUNET_RPS_CS_DEBUG_StreamRequest *msg;
+
+ srh = new_stream_request (rps_handle,
+ num_peers, /* num requests */
+ stream_input_cb,
+ cls);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Client requests %" PRIu32 " biased stream updates\n",
+ num_peers);
+
+ ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REQUEST);
+ GNUNET_MQ_send (rps_handle->mq, ev);
+ return srh;
+}
+
+
+/**
+ * This function is called, when the service updates the view.
+ * It verifies that @a msg is well-formed.