Start changing architecture of rps service/client
authorJulius Bünger <buenger@mytum.de>
Wed, 19 Sep 2018 23:15:53 +0000 (01:15 +0200)
committerJulius Bünger <buenger@mytum.de>
Wed, 19 Sep 2018 23:15:53 +0000 (01:15 +0200)
src/rps/Makefile.am
src/rps/rps-test_util.c
src/rps/rps-test_util.h
src/rps/rps_api.c

index d477ade929e3045fa2355b2670ce30146ef0d48e..2ed93ef7c2b38c2a387079255d3e9472a2dd8980 100644 (file)
@@ -19,7 +19,11 @@ pkgcfg_DATA = \
 
 bin_PROGRAMS = gnunet-rps
 
-gnunet_rps_SOURCES = gnunet-rps.c
+gnunet_rps_SOURCES = \
+  gnunet-service-rps_sampler_elem.h gnunet-service-rps_sampler_elem.c \
+  gnunet-service-rps_sampler.h gnunet-service-rps_sampler.c \
+  gnunet-rps.c
+
 gnunet_rps_LDADD = \
   libgnunetrps.la \
   $(top_builddir)/src/util/libgnunetutil.la \
@@ -143,6 +147,7 @@ test_rps_churn_LDADD = $(ld_rps_test_lib)
 
 gnunet_rps_profiler_SOURCES = \
  gnunet-service-rps_sampler_elem.h gnunet-service-rps_sampler_elem.c \
+ gnunet-service-rps_sampler.h gnunet-service-rps_sampler.c \
  rps-test_util.h rps-test_util.c \
  gnunet-rps-profiler.c
 
index ef39638bf1a1a3f8d117c9f8ba72f73c401bc588..271c96648ac3bcc4986f1b7a5e2410fb1fa8e514 100644 (file)
@@ -379,23 +379,6 @@ auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
 }
 
 
-struct GNUNET_CRYPTO_AuthKey
-string_to_auth_key (const char *str)
-{
-  struct GNUNET_CRYPTO_AuthKey auth_key;
-
-  if (GNUNET_OK !=
-      GNUNET_STRINGS_string_to_data (str,
-                                     strlen (str),
-                                     &auth_key.key,
-                                     sizeof (struct GNUNET_CRYPTO_AuthKey)))
-  {
-    LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to data\n");
-  }
-
-  return auth_key;
-}
-
 
 char *
 create_file (const char *name)
@@ -435,6 +418,25 @@ create_file (const char *name)
 
 #endif /* TO_FILE */
 
+
+struct GNUNET_CRYPTO_AuthKey
+string_to_auth_key (const char *str)
+{
+  struct GNUNET_CRYPTO_AuthKey auth_key;
+
+  if (GNUNET_OK !=
+      GNUNET_STRINGS_string_to_data (str,
+                                     strlen (str),
+                                     &auth_key.key,
+                                     sizeof (struct GNUNET_CRYPTO_AuthKey)))
+  {
+    LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to data\n");
+  }
+
+  return auth_key;
+}
+
+
 /**
  * @brief Try to ensure that `/tmp/rps` exists.
  *
index bb9b397137909f42e0b0d7b0c7c7aa90a47a9b62..def52a0acf6a186e5d6979b8f535ed75a2ed89b0 100644 (file)
@@ -36,7 +36,7 @@ auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key);
 struct GNUNET_CRYPTO_AuthKey
 string_to_auth_key (const char *str);
 
-char * 
+char *
 create_file (const char *name);
 
 /**
index 7afe21d3adb7e9419d0e43e9b7375863d9109545..a558c8a35e59081107180723a40e2428de8e68d1 100644 (file)
@@ -25,6 +25,7 @@
 #include "gnunet_util_lib.h"
 #include "rps.h"
 #include "gnunet_rps_service.h"
+#include "gnunet-service-rps_sampler.h"
 
 #include <inttypes.h>
 
@@ -523,6 +524,39 @@ GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
 }
 
 
+/**
+ * @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,
+                    struct RPS_Sampler *sampler,
+                    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->id = rps_handle->current_request_id++;
+  rh->num_requests = num_requests;
+  rh->sampler = sampler;
+  rh->ready_cb = ready_cb;
+  rh->ready_cb_cls = cls;
+  GNUNET_CONTAINER_multihashmap32_put (rps_handle->req_handlers, rh->id, rh,
+      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
+
+  return rh;
+}
+
+
 /**
  * Request n random peers.
  *
@@ -533,33 +567,111 @@ GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
  * @return a handle to cancel this request
  */
 struct GNUNET_RPS_Request_Handle *
-GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
+GNUNET_RPS_request_peers_2 (struct GNUNET_RPS_Handle *rps_handle,
                           uint32_t num_req_peers,
                           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->id = rps_handle->current_request_id++;
-  rh->num_peers = num_req_peers;
-  rh->ready_cb = ready_cb;
-  rh->ready_cb_cls = cls;
+  rh = new_request_handle (rps_handle,
+                           num_req_peers,
+                           NULL, /* no sampler needed */
+                           ready_cb,
+                           cls);
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Requesting %" PRIu32 " peers with id %" PRIu32 "\n",
        num_req_peers,
        rh->id);
 
-  GNUNET_CONTAINER_multihashmap32_put (rps_handle->req_handlers, rh->id, rh,
-      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
-
   send_request (rps_handle, rh->id, num_req_peers);
   return rh;
 }
 
 
+/**
+ * @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 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.
+}
+
+/**
+ * Request n random peers.
+ *
+ * @param rps_handle handle to the rps service
+ * @param num_req_peers number of peers we want to receive
+ * @param ready_cb the callback called when the peers are available
+ * @param cls closure given to the callback
+ * @return a handle to cancel this request
+ */
+struct GNUNET_RPS_Request_Handle *
+GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
+                            uint32_t num_req_peers,
+                            GNUNET_RPS_NotifyReadyCB ready_cb,
+                            void *cls)
+{
+  struct GNUNET_RPS_Request_Handle *rh;
+
+  rh = new_request_handle (rps_handle,
+                           num_req_peers,
+                           RPS_sampler_mod_init (num_req_peers,
+                                                 GNUNET_TIME_UNIT_SECONDS), // TODO remove this time-stuff
+                           ready_cb,
+                           cls);
+  RPS_sampler_get_n_rand_peers (rh->sampler,
+                                num_req_peers,
+                                peers_ready_cb,
+                                rh);
+
+  GNUNET_RPS_stream_request (rps_handle,
+                             0, /* infinite updates */
+                             collect_peers_cb,
+                             rh); /* cls */
+
+  return rh;
+}
+
+
 /**
  * Seed rps service with peerIDs.
  *