diff options
author | Maximilian Szengel <gnunet@maxsz.de> | 2012-10-21 15:24:38 +0000 |
---|---|---|
committer | Maximilian Szengel <gnunet@maxsz.de> | 2012-10-21 15:24:38 +0000 |
commit | 4d1382eca0c1c211a3a73a5a158d09269b380530 (patch) | |
tree | 49e9f79e45475f85c5f602bc66ee85361fab9607 /src | |
parent | 1538babdc61ea7e25fa1b738db11d8567a52667d (diff) | |
download | gnunet-4d1382eca0c1c211a3a73a5a158d09269b380530.tar.gz gnunet-4d1382eca0c1c211a3a73a5a158d09269b380530.zip |
regex profiler: loading a set of search strings from a file
Diffstat (limited to 'src')
-rw-r--r-- | src/mesh/gnunet-regex-profiler.c | 217 |
1 files changed, 184 insertions, 33 deletions
diff --git a/src/mesh/gnunet-regex-profiler.c b/src/mesh/gnunet-regex-profiler.c index dd5de1785..1f5edf68f 100644 --- a/src/mesh/gnunet-regex-profiler.c +++ b/src/mesh/gnunet-regex-profiler.c | |||
@@ -232,6 +232,21 @@ static unsigned int num_hosts; | |||
232 | static unsigned int num_links; | 232 | static unsigned int num_links; |
233 | 233 | ||
234 | /** | 234 | /** |
235 | * Number of timeout failures to tolerate | ||
236 | */ | ||
237 | static unsigned int num_cont_fails; | ||
238 | |||
239 | /** | ||
240 | * Number of times we try overlay connect operations | ||
241 | */ | ||
242 | static unsigned int retry_links; | ||
243 | |||
244 | /** | ||
245 | * Continuous failures during overlay connect operations | ||
246 | */ | ||
247 | static unsigned int cont_fails; | ||
248 | |||
249 | /** | ||
235 | * Global testing status | 250 | * Global testing status |
236 | */ | 251 | */ |
237 | static int result; | 252 | static int result; |
@@ -241,16 +256,25 @@ static int result; | |||
241 | */ | 256 | */ |
242 | enum State state; | 257 | enum State state; |
243 | 258 | ||
244 | |||
245 | /** | 259 | /** |
246 | * Folder where policy files are stored. | 260 | * Folder where policy files are stored. |
247 | */ | 261 | */ |
248 | static char * policy_dir; | 262 | static char * policy_dir; |
249 | 263 | ||
250 | /** | 264 | /** |
251 | * Search string. | 265 | * Search strings. |
266 | */ | ||
267 | static char **search_strings; | ||
268 | |||
269 | /** | ||
270 | * Number of search strings. | ||
271 | */ | ||
272 | static int num_search_strings; | ||
273 | |||
274 | /** | ||
275 | * Number of peers found with search strings. | ||
252 | */ | 276 | */ |
253 | static char *search_string = "GNUNETVPN0001000IPEX4110010011001111001101000"; | 277 | static unsigned int peers_found; |
254 | 278 | ||
255 | /** | 279 | /** |
256 | * Search task identifier | 280 | * Search task identifier |
@@ -262,6 +286,16 @@ static GNUNET_SCHEDULER_TaskIdentifier search_task; | |||
262 | */ | 286 | */ |
263 | static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task; | 287 | static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task; |
264 | 288 | ||
289 | /** | ||
290 | * Search timeout in seconds. | ||
291 | */ | ||
292 | static long search_timeout_sec; | ||
293 | |||
294 | /** | ||
295 | * Search wait time in minutes. | ||
296 | */ | ||
297 | static long search_wait_min; | ||
298 | |||
265 | 299 | ||
266 | /** | 300 | /** |
267 | * Shutdown nicely | 301 | * Shutdown nicely |
@@ -274,8 +308,19 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
274 | { | 308 | { |
275 | struct DLLOperation *dll_op; | 309 | struct DLLOperation *dll_op; |
276 | unsigned int nhost; | 310 | unsigned int nhost; |
277 | int peer_cnt; | 311 | unsigned int peer_cnt; |
278 | 312 | unsigned int search_str_cnt; | |
313 | |||
314 | for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++) | ||
315 | { | ||
316 | if (NULL != peers[peer_cnt].op_handle) | ||
317 | GNUNET_TESTBED_operation_cancel (peers[peer_cnt].op_handle); | ||
318 | } | ||
319 | for (search_str_cnt = 0; search_str_cnt < num_search_strings; search_str_cnt++) | ||
320 | { | ||
321 | GNUNET_free (search_strings[search_str_cnt]); | ||
322 | } | ||
323 | GNUNET_free (search_strings); | ||
279 | if (GNUNET_SCHEDULER_NO_TASK != abort_task) | 324 | if (GNUNET_SCHEDULER_NO_TASK != abort_task) |
280 | GNUNET_SCHEDULER_cancel (abort_task); | 325 | GNUNET_SCHEDULER_cancel (abort_task); |
281 | if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task) | 326 | if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task) |
@@ -294,11 +339,6 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
294 | GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op); | 339 | GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op); |
295 | GNUNET_free (dll_op); | 340 | GNUNET_free (dll_op); |
296 | } | 341 | } |
297 | for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++) | ||
298 | { | ||
299 | if (NULL != peers[peer_cnt].op_handle) | ||
300 | GNUNET_TESTBED_operation_cancel (peers[peer_cnt].op_handle); | ||
301 | } | ||
302 | if (NULL != mc) | 342 | if (NULL != mc) |
303 | GNUNET_TESTBED_controller_disconnect (mc); | 343 | GNUNET_TESTBED_controller_disconnect (mc); |
304 | if (NULL != mc_proc) | 344 | if (NULL != mc_proc) |
@@ -404,12 +444,17 @@ mesh_peer_connect_handler (void *cls, | |||
404 | const struct GNUNET_ATS_Information * atsi) | 444 | const struct GNUNET_ATS_Information * atsi) |
405 | { | 445 | { |
406 | // struct Peer *peer = (struct Peer *)cls; | 446 | // struct Peer *peer = (struct Peer *)cls; |
407 | 447 | const char * search_str = (const char *)cls; | |
408 | GNUNET_SCHEDULER_cancel (search_timeout_task); | ||
409 | 448 | ||
410 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh peer connect handler.\n"); | 449 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh peer connect handler.\n"); |
411 | printf ("\nString successfully matched\n"); | 450 | printf ("String %s successfully matched\n", search_str); |
412 | GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); | 451 | |
452 | if (++peers_found == num_search_strings) | ||
453 | { | ||
454 | printf ("\nAll strings successfully matched!\n"); | ||
455 | GNUNET_SCHEDULER_cancel (search_timeout_task); | ||
456 | GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); | ||
457 | } | ||
413 | } | 458 | } |
414 | 459 | ||
415 | 460 | ||
@@ -425,7 +470,8 @@ do_connect_by_string_timeout (void *cls, | |||
425 | { | 470 | { |
426 | long sec = (long)cls; | 471 | long sec = (long)cls; |
427 | 472 | ||
428 | printf ("Searching for string did not succeed after %ld seconds\n", sec); | 473 | printf ("Searching for all strings did not succeed after %ld seconds\n", sec); |
474 | printf ("Found %i of %i strings\n", peers_found, num_search_strings); | ||
429 | 475 | ||
430 | GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); | 476 | GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); |
431 | } | 477 | } |
@@ -441,21 +487,28 @@ static void | |||
441 | do_connect_by_string (void *cls, | 487 | do_connect_by_string (void *cls, |
442 | const struct GNUNET_SCHEDULER_TaskContext * tc) | 488 | const struct GNUNET_SCHEDULER_TaskContext * tc) |
443 | { | 489 | { |
444 | printf ("Searching for string \"%s\"\n", search_string); | 490 | unsigned int search_cnt; |
491 | struct Peer *peer; | ||
445 | 492 | ||
446 | peers[0].mesh_tunnel_handle = GNUNET_MESH_tunnel_create (peers[0].mesh_handle, | 493 | for (search_cnt = 0; search_cnt < num_search_strings; search_cnt++) |
447 | NULL, | 494 | { |
448 | &mesh_peer_connect_handler, | 495 | peer = &peers[search_cnt % num_peers]; |
449 | &mesh_peer_disconnect_handler, | 496 | |
450 | &peers[0]); | 497 | printf ("Searching for string \"%s\"\n", search_strings[search_cnt]); |
451 | 498 | ||
452 | 499 | peer->mesh_tunnel_handle = GNUNET_MESH_tunnel_create (peer->mesh_handle, | |
453 | GNUNET_MESH_peer_request_connect_by_string (peers[0].mesh_tunnel_handle, | 500 | NULL, |
454 | search_string); | 501 | &mesh_peer_connect_handler, |
502 | &mesh_peer_disconnect_handler, | ||
503 | search_strings[search_cnt]); | ||
504 | GNUNET_MESH_peer_request_connect_by_string (peer->mesh_tunnel_handle, | ||
505 | search_strings[search_cnt]); | ||
506 | |||
507 | } | ||
455 | 508 | ||
456 | search_timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply | 509 | search_timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply |
457 | (GNUNET_TIME_UNIT_SECONDS, 30), | 510 | (GNUNET_TIME_UNIT_SECONDS, search_timeout_sec), |
458 | &do_connect_by_string_timeout, (void *)(long)30); | 511 | &do_connect_by_string_timeout, (void *)search_timeout_sec); |
459 | } | 512 | } |
460 | 513 | ||
461 | 514 | ||
@@ -535,10 +588,10 @@ mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op, | |||
535 | 588 | ||
536 | if (++connected_mesh_handles == num_peers) | 589 | if (++connected_mesh_handles == num_peers) |
537 | { | 590 | { |
538 | printf ("\nAll mesh handles connected.\n"); | 591 | printf ("\nAll mesh handles connected.\nWaiting to search.\n"); |
539 | 592 | ||
540 | search_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply | 593 | search_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply |
541 | (GNUNET_TIME_UNIT_SECONDS, 10), | 594 | (GNUNET_TIME_UNIT_MINUTES, search_wait_min), |
542 | &do_connect_by_string, NULL); | 595 | &do_connect_by_string, NULL); |
543 | } | 596 | } |
544 | } | 597 | } |
@@ -833,8 +886,13 @@ controller_event_cb (void *cls, | |||
833 | { | 886 | { |
834 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 887 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
835 | _("An operation has failed while linking\n")); | 888 | _("An operation has failed while linking\n")); |
836 | GNUNET_SCHEDULER_cancel (abort_task); | 889 | retry_links++; |
837 | abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL); | 890 | if (++cont_fails > num_cont_fails) |
891 | { | ||
892 | printf ("\nAborting due to very high failure rate"); | ||
893 | GNUNET_SCHEDULER_cancel (abort_task); | ||
894 | abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL); | ||
895 | } | ||
838 | } | 896 | } |
839 | break; | 897 | break; |
840 | case GNUNET_TESTBED_ET_CONNECT: | 898 | case GNUNET_TESTBED_ET_CONNECT: |
@@ -991,6 +1049,75 @@ status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int stat | |||
991 | &do_abort, NULL); | 1049 | &do_abort, NULL); |
992 | } | 1050 | } |
993 | 1051 | ||
1052 | /** | ||
1053 | * Load search strings from given filename. One search string per line. | ||
1054 | * | ||
1055 | * @param filename filename of the file containing the search strings. | ||
1056 | * @param strings set of strings loaded from file. Caller needs to free this | ||
1057 | * if number returned is greater than zero. | ||
1058 | * @return number of strings found in the file. GNUNET_SYSERR on error. | ||
1059 | */ | ||
1060 | static int | ||
1061 | load_search_strings (const char *filename, char ***strings) | ||
1062 | { | ||
1063 | char *data; | ||
1064 | char *buf; | ||
1065 | uint64_t filesize; | ||
1066 | unsigned int offset; | ||
1067 | int str_cnt; | ||
1068 | unsigned int i; | ||
1069 | |||
1070 | if (NULL == filename) | ||
1071 | { | ||
1072 | return GNUNET_SYSERR; | ||
1073 | } | ||
1074 | |||
1075 | if (GNUNET_YES != GNUNET_DISK_file_test (filename)) | ||
1076 | { | ||
1077 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1078 | "Could not find search strings file %s\n", filename); | ||
1079 | return GNUNET_SYSERR; | ||
1080 | } | ||
1081 | if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES)) | ||
1082 | filesize = 0; | ||
1083 | if (0 == filesize) | ||
1084 | { | ||
1085 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename); | ||
1086 | return GNUNET_SYSERR; | ||
1087 | } | ||
1088 | data = GNUNET_malloc (filesize); | ||
1089 | if (filesize != GNUNET_DISK_fn_read (filename, data, filesize)) | ||
1090 | { | ||
1091 | GNUNET_free (data); | ||
1092 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n", | ||
1093 | filename); | ||
1094 | return GNUNET_SYSERR; | ||
1095 | } | ||
1096 | buf = data; | ||
1097 | offset = 0; | ||
1098 | str_cnt = 0; | ||
1099 | while (offset < (filesize - 1)) | ||
1100 | { | ||
1101 | offset++; | ||
1102 | if (((data[offset] == '\n')) && (buf != &data[offset])) | ||
1103 | { | ||
1104 | data[offset] = '\0'; | ||
1105 | str_cnt++; | ||
1106 | buf = &data[offset + 1]; | ||
1107 | } | ||
1108 | else if ((data[offset] == '\n') || (data[offset] == '\0')) | ||
1109 | buf = &data[offset + 1]; | ||
1110 | } | ||
1111 | *strings = GNUNET_malloc (sizeof (char *) * str_cnt); | ||
1112 | offset = 0; | ||
1113 | for (i = 0; i < str_cnt; i++) | ||
1114 | { | ||
1115 | (*strings)[i] = GNUNET_strdup (&data[offset]); | ||
1116 | offset += strlen ((*strings)[i]) + 1; | ||
1117 | } | ||
1118 | free (data); | ||
1119 | return str_cnt; | ||
1120 | } | ||
994 | 1121 | ||
995 | /** | 1122 | /** |
996 | * Main function that will be run by the scheduler. | 1123 | * Main function that will be run by the scheduler. |
@@ -1050,7 +1177,22 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
1050 | return; | 1177 | return; |
1051 | } | 1178 | } |
1052 | policy_dir = args[1]; | 1179 | policy_dir = args[1]; |
1053 | 1180 | if (GNUNET_YES != GNUNET_DISK_file_test (args[2])) | |
1181 | { | ||
1182 | fprintf (stderr, _("No search strings file given. Exiting.\n")); | ||
1183 | GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); | ||
1184 | return; | ||
1185 | } | ||
1186 | num_search_strings = load_search_strings (args[2], &search_strings); | ||
1187 | if (0 >= num_search_strings || NULL == search_strings) | ||
1188 | { | ||
1189 | fprintf (stderr, _("Error loading search strings. Exiting.\n")); | ||
1190 | GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); | ||
1191 | return; | ||
1192 | } | ||
1193 | unsigned int i; | ||
1194 | for (i = 0; i < num_search_strings; i++) | ||
1195 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "search string: %s\n", search_strings[i]); | ||
1054 | cfg = GNUNET_CONFIGURATION_dup (config); | 1196 | cfg = GNUNET_CONFIGURATION_dup (config); |
1055 | mc_proc = | 1197 | mc_proc = |
1056 | GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname | 1198 | GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname |
@@ -1080,6 +1222,15 @@ main (int argc, char *const *argv) | |||
1080 | { 'n', "num-links", "COUNT", | 1222 | { 'n', "num-links", "COUNT", |
1081 | gettext_noop ("create COUNT number of random links"), | 1223 | gettext_noop ("create COUNT number of random links"), |
1082 | GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links }, | 1224 | GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links }, |
1225 | { 'e', "num-errors", "COUNT", | ||
1226 | gettext_noop ("tolerate COUNT number of continious timeout failures"), | ||
1227 | GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails }, | ||
1228 | { 't', "matching-timeout", "TIMEOUT", | ||
1229 | gettext_noop ("wait TIMEOUT seconds before considering a string match as failed"), | ||
1230 | GNUNET_YES, &GNUNET_GETOPT_set_uint, &search_timeout_sec }, | ||
1231 | { 's', "search-wait", "WAIT", | ||
1232 | gettext_noop ("wait WAIT minutes before starting string search"), | ||
1233 | GNUNET_YES, &GNUNET_GETOPT_set_uint, &search_wait_min }, | ||
1083 | GNUNET_GETOPT_OPTION_END | 1234 | GNUNET_GETOPT_OPTION_END |
1084 | }; | 1235 | }; |
1085 | int ret; | 1236 | int ret; |
@@ -1089,7 +1240,7 @@ main (int argc, char *const *argv) | |||
1089 | 1240 | ||
1090 | result = GNUNET_SYSERR; | 1241 | result = GNUNET_SYSERR; |
1091 | ret = | 1242 | ret = |
1092 | GNUNET_PROGRAM_run (argc, argv, "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir", | 1243 | GNUNET_PROGRAM_run (argc, argv, "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file", |
1093 | _("Profiler for regex/mesh"), | 1244 | _("Profiler for regex/mesh"), |
1094 | options, &run, NULL); | 1245 | options, &run, NULL); |
1095 | if (GNUNET_OK != ret) | 1246 | if (GNUNET_OK != ret) |