From ff073fe2f2c6a0fe0532061533c570acfba60e7d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Julius=20B=C3=BCnger?= Date: Thu, 23 Jul 2015 18:21:49 +0000 Subject: [PATCH] get perfect sample of each sampler element --- src/rps/Makefile.am | 22 ++++---- src/rps/gnunet-service-rps_sampler_elem.c | 22 ++++++++ src/rps/gnunet-service-rps_sampler_elem.h | 10 ++++ src/rps/rps-test_util.c | 33 +++++++++-- src/rps/rps-test_util.h | 3 + src/rps/test_rps.c | 68 ++++++++++++++++++++--- 6 files changed, 134 insertions(+), 24 deletions(-) diff --git a/src/rps/Makefile.am b/src/rps/Makefile.am index fc0b9848d..b1ebffee4 100644 --- a/src/rps/Makefile.am +++ b/src/rps/Makefile.am @@ -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 = \ diff --git a/src/rps/gnunet-service-rps_sampler_elem.c b/src/rps/gnunet-service-rps_sampler_elem.c index 16b9cb39e..5354c8081 100644 --- a/src/rps/gnunet-service-rps_sampler_elem.c +++ b/src/rps/gnunet-service-rps_sampler_elem.c @@ -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 */ diff --git a/src/rps/gnunet-service-rps_sampler_elem.h b/src/rps/gnunet-service-rps_sampler_elem.h index cb9506d69..b7224331d 100644 --- a/src/rps/gnunet-service-rps_sampler_elem.h +++ b/src/rps/gnunet-service-rps_sampler_elem.h @@ -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 */ diff --git a/src/rps/rps-test_util.c b/src/rps/rps-test_util.c index 7af2071b5..882f945c9 100644 --- a/src/rps/rps-test_util.c +++ b/src/rps/rps-test_util.c @@ -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 */ diff --git a/src/rps/rps-test_util.h b/src/rps/rps-test_util.h index 831ab64ee..209152151 100644 --- a/src/rps/rps-test_util.h +++ b/src/rps/rps-test_util.h @@ -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); diff --git a/src/rps/test_rps.c b/src/rps/test_rps.c index f6db147f9..47abe668a 100644 --- a/src/rps/test_rps.c +++ b/src/rps/test_rps.c @@ -27,7 +27,10 @@ #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 @@ -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; -- 2.25.1