get perfect sample of each sampler element
authorJulius Bünger <buenger@mytum.de>
Thu, 23 Jul 2015 18:21:49 +0000 (18:21 +0000)
committerJulius Bünger <buenger@mytum.de>
Thu, 23 Jul 2015 18:21:49 +0000 (18:21 +0000)
src/rps/Makefile.am
src/rps/gnunet-service-rps_sampler_elem.c
src/rps/gnunet-service-rps_sampler_elem.h
src/rps/rps-test_util.c
src/rps/rps-test_util.h
src/rps/test_rps.c

index fc0b9848d2a0284dbe6bd306f9f313f7ea519e2e..b1ebffee4392c33a6570c05d19b59afd2495c202 100644 (file)
@@ -81,34 +81,32 @@ ld_rps_test_lib = \
   $(top_builddir)/src/testbed/libgnunettestbed.la \
   -lm
 
+rps_test_src = \
+       test_rps.c \
+       rps-test_util.h rps-test_util.c \
+ gnunet-service-rps_sampler_elem.h gnunet-service-rps_sampler_elem.c
 
 if ENABLE_TEST_RUN
 AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;
 TESTS = $(check_PROGRAMS)
 endif
 
-test_rps_malicious_1_SOURCES = \
-       test_rps.c
+test_rps_malicious_1_SOURCES = $(rps_test_src)
 test_rps_malicious_1_LDADD = $(ld_rps_test_lib)
 
-test_rps_malicious_2_SOURCES = \
-       test_rps.c
+test_rps_malicious_2_SOURCES = $(rps_test_src)
 test_rps_malicious_2_LDADD = $(ld_rps_test_lib)
 
-test_rps_malicious_3_SOURCES = \
-       test_rps.c
+test_rps_malicious_3_SOURCES = $(rps_test_src)
 test_rps_malicious_3_LDADD = $(ld_rps_test_lib)
 
-test_rps_single_req_SOURCES = \
- test_rps.c
+test_rps_single_req_SOURCES = $(rps_test_src)
 test_rps_single_req_LDADD = $(ld_rps_test_lib)
 
-test_rps_seed_request_SOURCES = \
- test_rps.c
+test_rps_seed_request_SOURCES = $(rps_test_src)
 test_rps_seed_request_LDADD = $(ld_rps_test_lib)
 
-gnunet_rps_profiler_SOURCES = \
- test_rps.c
+gnunet_rps_profiler_SOURCES = $(rps_test_src)
 gnunet_rps_profiler_LDADD = $(ld_rps_test_lib)
 
 EXTRA_DIST = \
index 16b9cb39e524c2d3e05f710cbfd9caf452166e0e..5354c8081d547689b2b221650167ad5cb22f26d3 100644 (file)
@@ -166,4 +166,26 @@ RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
            GNUNET_i2s_full (&s_elem->peer_id));
 }
 
+/**
+ * Initialise the min-wise independent function of the given sampler element.
+ *
+ * @param s_elem the sampler element
+ * @param auth_key the key to use
+ */
+void
+RPS_sampler_elem_set (struct RPS_SamplerElement *s_elem,
+                      struct GNUNET_CRYPTO_AuthKey auth_key)
+{
+  s_elem->auth_key = auth_key;
+
+  #ifdef TO_FILE
+  /* Create a file(-name) to store internals to */
+  char *name_buf;
+  name_buf = auth_key_to_string (s_elem->auth_key);
+
+  s_elem->file_name = create_file (name_buf);
+  GNUNET_free (name_buf);
+  #endif /* TO_FILE */
+}
+
 /* end of gnunet-service-rps.c */
index cb9506d698b2ea8bb3838c9362af0023a5c4fa30..b7224331df624abd13a829cc3918c59897ff1894 100644 (file)
@@ -129,6 +129,16 @@ void
 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
                        const struct GNUNET_PeerIdentity *new_ID);
 
+/**
+ * Initialise the min-wise independent function of the given sampler element.
+ *
+ * @param s_elem the sampler element
+ * @param auth_key the key to use
+ */
+void
+RPS_sampler_elem_set (struct RPS_SamplerElement *s_elem,
+                      struct GNUNET_CRYPTO_AuthKey auth_key);
+
 
 #endif /* RPS_SAMPLER_ELEM_H */
 /* end of gnunet-service-rps.c */
index 7af2071b5a12276fa3d6df253ee7bb2e76950aa1..882f945c93e6ede3e8ac54772915ac1cdd8df753 100644 (file)
@@ -128,7 +128,7 @@ auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
     *end = '\0';
   }
 
-  size = GNUNET_snprintf (name_buf, name_buf_size, "sampler_el-%s-", buf);
+  size = GNUNET_snprintf (name_buf, name_buf_size, "sampler_el-%s", buf);
   if (0 > size)
     LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
 
@@ -137,6 +137,25 @@ auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
   return name_buf;
 }
 
+
+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)
 {
@@ -150,7 +169,7 @@ create_file (const char *name)
   name_buf_size = (strlen (prefix) + strlen (name) + 2) * sizeof (char);
   name_buf = GNUNET_malloc (name_buf_size);
 
-  size = GNUNET_snprintf (name_buf, name_buf_size, "%s%s-", prefix, name);
+  size = GNUNET_snprintf (name_buf, name_buf_size, "%s%s", prefix, name);
   if (0 > size)
     LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
 
@@ -161,12 +180,16 @@ create_file (const char *name)
          prefix);
   }
 
-  if (NULL == (file_name = GNUNET_DISK_mktemp (name_buf)))
-        LOG (GNUNET_ERROR_TYPE_WARNING, "Could not create file\n");
+  if (NULL == strstr (name, "sampler_el"))
+  {/* only append random string to sampler */
+    if (NULL == (file_name = GNUNET_DISK_mktemp (name_buf)))
+          LOG (GNUNET_ERROR_TYPE_WARNING, "Could not create file\n");
 
   GNUNET_free (name_buf);
-
   return file_name;
+  }
+
+  return name_buf;
 }
 
 #endif /* TO_FILE */
index 831ab64eeef0540cdfb2dd15631589c3ed6f238d..209152151c65bab4860f617ad038b5631d09c5f2 100644 (file)
@@ -39,6 +39,9 @@ to_file_ (char *file_name, char *line);
 char * 
 auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key);
 
+struct GNUNET_CRYPTO_AuthKey
+string_to_auth_key (const char *str);
+
 char * 
 create_file (const char *name);
 
index f6db147f9cfeabd2c045db81f8b78c82820044e5..47abe668af2b7672d31436b080c1dd59894144f4 100644 (file)
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_testbed_service.h"
+
 #include "gnunet_rps_service.h"
+#include "rps-test_util.h"
+#include "gnunet-service-rps_sampler_elem.h"
 
 #include <inttypes.h>
 
@@ -257,7 +260,7 @@ struct SingleTestRun
  * Append arguments to file
  */
 static void
-to_file_ (char *file_name, char *line)
+tofile_ (const char *file_name, char *line)
 {
   struct GNUNET_DISK_FileHandle *f;
   /* char output_buffer[512]; */
@@ -312,7 +315,7 @@ to_file_ (char *file_name, char *line)
 /**
  * This function is used to facilitate writing important information to disk
  */
-#define to_file(file_name, ...) do {\
+#define tofile(file_name, ...) do {\
   char tmp_buf[512];\
     int size;\
     size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
@@ -320,7 +323,7 @@ to_file_ (char *file_name, char *line)
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
                      "Failed to create tmp_buf\n");\
     else\
-      to_file_(file_name,tmp_buf);\
+      tofile_(file_name,tmp_buf);\
   } while (0);
 
 /**
@@ -386,7 +389,7 @@ make_oplist_entry ()
 
 
 /**
- * Callback to be called when NSE service is started or stopped at peers
+ * Callback to be called when RPS service is started or stopped at peers
  *
  * @param cls NULL
  * @param op the operation handle
@@ -521,7 +524,7 @@ info_cb (void *cb_cls,
   rps_peers[entry->index].rec_ids = NULL;
   rps_peers[entry->index].num_rec_ids = 0;
 
-  to_file ("/tmp/rps/peer_ids",
+  tofile ("/tmp/rps/peer_ids",
            "%u\t%s\n",
            entry->index,
            GNUNET_i2s_full (&rps_peer_ids[entry->index]));
@@ -972,7 +975,7 @@ profiler_reply_handle (void *cls,
                 GNUNET_i2s (&recv_peers[i]));
 
     /* GNUNET_array_append (rps_peer->rec_ids, rps_peer->num_rec_ids, recv_peers[i]); */
-    to_file (file_name,
+    tofile (file_name,
              "%s\n",
              GNUNET_i2s_full (&recv_peers[i]));
   }
@@ -1010,6 +1013,57 @@ profiler_cb (struct RPSPeer *rps_peer)
   }
 }
 
+/**
+ * Function called from #profiler_eval with a filename.
+ *
+ * @param cls closure
+ * @param filename complete filename (absolute path)
+ * @return #GNUNET_OK to continue to iterate,
+ *  #GNUNET_NO to stop iteration with no error,
+ *  #GNUNET_SYSERR to abort iteration with error!
+ */
+int
+file_name_cb (void *cls, const char *filename)
+{
+  if (NULL != strstr (filename, "sampler_el"))
+  {
+    struct RPS_SamplerElement *s_elem;
+    struct GNUNET_CRYPTO_AuthKey auth_key;
+    const char *key_char;
+    uint32_t i;
+
+    key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
+    tofile (filename, "--------------------------\n");
+
+    auth_key = string_to_auth_key (key_char);
+    s_elem = RPS_sampler_elem_create ();
+    RPS_sampler_elem_set (s_elem, auth_key);
+
+    for (i = 0; i < num_peers; i++)
+    {
+      RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
+    }
+  }
+  return GNUNET_OK;
+}
+
+/**
+ * This is run after the test finished.
+ *
+ * Compute all perfect samples.
+ */
+int
+profiler_eval (void)
+{
+  /* Compute perfect sample for each sampler element */
+  if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
+  }
+
+  return 0;
+}
+
 
 /***********************************************************************
  * /Definition of tests
@@ -1187,7 +1241,7 @@ main (int argc, char *argv[])
     cur_test_run.pre_test = profiler_pre;
     cur_test_run.main_test = profiler_cb;
     cur_test_run.reply_handle = profiler_reply_handle;
-    cur_test_run.eval_cb = no_eval;
+    cur_test_run.eval_cb = profiler_eval;
     cur_test_run.request_interval = 2;
     cur_test_run.num_requests = 50;