aboutsummaryrefslogtreecommitdiff
path: root/src/rps/gnunet-service-rps_sampler.c
diff options
context:
space:
mode:
authorJulius Bünger <buenger@mytum.de>2015-06-05 23:03:04 +0000
committerJulius Bünger <buenger@mytum.de>2015-06-05 23:03:04 +0000
commit1e4ddb54ca92d3feb5c6382c97c08de7fb9cb7e8 (patch)
tree6f83ceec54d8be3f01930987d84266c3cd2b978a /src/rps/gnunet-service-rps_sampler.c
parent6c0a7bacca0e00bcaa72b8b0a279660d146649ad (diff)
downloadgnunet-1e4ddb54ca92d3feb5c6382c97c08de7fb9cb7e8.tar.gz
gnunet-1e4ddb54ca92d3feb5c6382c97c08de7fb9cb7e8.zip
-restructured the code to faciliate viewing the internals
Diffstat (limited to 'src/rps/gnunet-service-rps_sampler.c')
-rw-r--r--src/rps/gnunet-service-rps_sampler.c137
1 files changed, 19 insertions, 118 deletions
diff --git a/src/rps/gnunet-service-rps_sampler.c b/src/rps/gnunet-service-rps_sampler.c
index 07bd1e317..f382786a4 100644
--- a/src/rps/gnunet-service-rps_sampler.c
+++ b/src/rps/gnunet-service-rps_sampler.c
@@ -32,8 +32,11 @@
32#include <math.h> 32#include <math.h>
33#include <inttypes.h> 33#include <inttypes.h>
34 34
35#include "rps-test_util.h"
36
35#define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__) 37#define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
36 38
39
37// multiple 'clients'? 40// multiple 'clients'?
38 41
39// TODO check for overflows 42// TODO check for overflows
@@ -299,76 +302,6 @@ static size_t max_size;
299static uint32_t client_get_index; 302static uint32_t client_get_index;
300 303
301 304
302#ifdef TO_FILE
303/**
304 * This function is used to facilitate writing important information to disk
305 */
306#define to_file(file_name, ...) do {char tmp_buf[512];\
307 int size;\
308 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
309 if (0 > size)\
310 LOG (GNUNET_ERROR_TYPE_WARNING,\
311 "Failed to create tmp_buf\n");\
312 else\
313 to_file_(file_name,tmp_buf);\
314} while (0);
315
316static void
317to_file_ (char *file_name, char *line)
318{
319 struct GNUNET_DISK_FileHandle *f;
320 char output_buffer[512];
321 //size_t size;
322 int size;
323 size_t size2;
324
325
326 if (NULL == (f = GNUNET_DISK_file_open (file_name,
327 GNUNET_DISK_OPEN_APPEND |
328 GNUNET_DISK_OPEN_WRITE |
329 GNUNET_DISK_OPEN_CREATE,
330 GNUNET_DISK_PERM_USER_WRITE)))
331 {
332 LOG (GNUNET_ERROR_TYPE_WARNING,
333 "Not able to open file %s\n",
334 file_name);
335 return;
336 }
337 size = GNUNET_snprintf (output_buffer,
338 sizeof (output_buffer),
339 "%llu %s\n",
340 GNUNET_TIME_absolute_get ().abs_value_us,
341 line);
342 if (0 > size)
343 {
344 LOG (GNUNET_ERROR_TYPE_WARNING,
345 "Failed to write string to buffer (size: %i)\n",
346 size);
347 return;
348 }
349
350 size2 = GNUNET_DISK_file_write (f, output_buffer, size);
351 if (size != size2)
352 {
353 LOG (GNUNET_ERROR_TYPE_WARNING,
354 "Unable to write to file! (Size: %u, size2: %u)\n",
355 size,
356 size2);
357 return;
358 }
359
360 if (GNUNET_YES != GNUNET_DISK_file_close (f))
361 LOG (GNUNET_ERROR_TYPE_WARNING,
362 "Unable to close file\n");
363}
364#endif /* TO_FILE */
365
366
367/** FIXME document */
368static struct GetPeerCls *gpc_head;
369static struct GetPeerCls *gpc_tail;
370
371
372/** 305/**
373 * Callback to _get_rand_peer() used by _get_n_rand_peers(). 306 * Callback to _get_rand_peer() used by _get_n_rand_peers().
374 * 307 *
@@ -417,38 +350,11 @@ RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_el)
417 350
418 #ifdef TO_FILE 351 #ifdef TO_FILE
419 /* Create a file(-name) to store internals to */ 352 /* Create a file(-name) to store internals to */
420 int size; 353 char *name_buf;
421 char *end; 354 name_buf = auth_key_to_string (sampler_el->auth_key);
422 char *buf;
423 char name_buf[512];
424 size_t keylen = (sizeof (struct GNUNET_CRYPTO_AuthKey)) * 8;
425
426 if (keylen % 5 > 0)
427 keylen += 5 - keylen % 5;
428 keylen /= 5;
429 buf = GNUNET_malloc (keylen + 1);
430
431 end = GNUNET_STRINGS_data_to_string (&(sampler_el->auth_key.key),
432 sizeof (struct GNUNET_CRYPTO_AuthKey),
433 buf,
434 keylen);
435
436 if (NULL == end)
437 {
438 GNUNET_free (buf);
439 GNUNET_break (0);
440 }
441 else
442 {
443 *end = '\0';
444 }
445
446 size = GNUNET_snprintf (name_buf, sizeof (name_buf), "sampler_el-%s-", buf);
447 if (0 > size)
448 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
449 355
450 if (NULL == (sampler_el->file_name = GNUNET_DISK_mktemp (name_buf))) 356 sampler_el->file_name = create_file (name_buf);
451 LOG (GNUNET_ERROR_TYPE_WARNING, "Could not create file\n"); 357 GNUNET_free (name_buf);
452 #endif /* TO_FILE */ 358 #endif /* TO_FILE */
453 359
454 sampler_el->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS; 360 sampler_el->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
@@ -495,11 +401,9 @@ RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
495 401
496 s_elem->num_peers++; 402 s_elem->num_peers++;
497 403
498 #ifdef TO_FILE
499 to_file (s_elem->file_name, 404 to_file (s_elem->file_name,
500 "Got id %s", 405 "Got id %s",
501 GNUNET_i2s_full (other)); 406 GNUNET_i2s_full (other));
502 #endif /* TO_FILE */
503 407
504 if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id))) 408 if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)))
505 { 409 {
@@ -545,11 +449,10 @@ RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
545 } 449 }
546 } 450 }
547 s_elem->is_empty = NOT_EMPTY; 451 s_elem->is_empty = NOT_EMPTY;
548 #ifdef TO_FILE 452
549 to_file (s_elem->file_name, 453 to_file (s_elem->file_name,
550 "Now holding %s", 454 "Now holding %s",
551 GNUNET_i2s_full (&s_elem->peer_id)); 455 GNUNET_i2s_full (&s_elem->peer_id));
552 #endif /* TO_FILE */
553} 456}
554 457
555 458
@@ -590,7 +493,7 @@ sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
590 "Shrinking sampler %d -> %d\n", 493 "Shrinking sampler %d -> %d\n",
591 old_size, 494 old_size,
592 new_size); 495 new_size);
593 #ifdef TO_FILE 496
594 to_file (sampler->file_name, 497 to_file (sampler->file_name,
595 "Shrinking sampler %d -> %d", 498 "Shrinking sampler %d -> %d",
596 old_size, 499 old_size,
@@ -603,7 +506,7 @@ sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
603 i, 506 i,
604 sampler->sampler_elements[i]->file_name); 507 sampler->sampler_elements[i]->file_name);
605 } 508 }
606 #endif /* TO_FILE */ 509
607 GNUNET_array_grow (sampler->sampler_elements, 510 GNUNET_array_grow (sampler->sampler_elements,
608 sampler->sampler_size, 511 sampler->sampler_size,
609 new_size); 512 new_size);
@@ -618,12 +521,12 @@ sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
618 "Growing sampler %d -> %d\n", 521 "Growing sampler %d -> %d\n",
619 old_size, 522 old_size,
620 new_size); 523 new_size);
621 #ifdef TO_FILE 524
622 to_file (sampler->file_name, 525 to_file (sampler->file_name,
623 "Growing sampler %d -> %d", 526 "Growing sampler %d -> %d",
624 old_size, 527 old_size,
625 new_size); 528 new_size);
626 #endif /* TO_FILE */ 529
627 GNUNET_array_grow (sampler->sampler_elements, 530 GNUNET_array_grow (sampler->sampler_elements,
628 sampler->sampler_size, 531 sampler->sampler_size,
629 new_size); 532 new_size);
@@ -631,12 +534,11 @@ sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
631 for (i = old_size ; i < new_size ; i++) 534 for (i = old_size ; i < new_size ; i++)
632 { /* Add new sampler elements */ 535 { /* Add new sampler elements */
633 sampler->sampler_elements[i] = RPS_sampler_elem_create (); 536 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
634 #ifdef TO_FILE 537
635 to_file (sampler->file_name, 538 to_file (sampler->file_name,
636 "+%" PRIu32 ": %s", 539 "+%" PRIu32 ": %s",
637 i, 540 i,
638 sampler->sampler_elements[i]->file_name); 541 sampler->sampler_elements[i]->file_name);
639 #endif /* TO_FILE */
640 } 542 }
641 } 543 }
642 else 544 else
@@ -696,9 +598,8 @@ RPS_sampler_init (size_t init_size,
696 sampler = GNUNET_new (struct RPS_Sampler); 598 sampler = GNUNET_new (struct RPS_Sampler);
697 599
698 #ifdef TO_FILE 600 #ifdef TO_FILE
699 if (NULL == (sampler->file_name = GNUNET_DISK_mktemp ("sampler-"))) 601 sampler->file_name = create_file ("sampler-");
700 LOG (GNUNET_ERROR_TYPE_WARNING, 602
701 "Could not create file\n");
702 LOG (GNUNET_ERROR_TYPE_DEBUG, 603 LOG (GNUNET_ERROR_TYPE_DEBUG,
703 "Initialised sampler %s\n", 604 "Initialised sampler %s\n",
704 sampler->file_name); 605 sampler->file_name);
@@ -736,13 +637,11 @@ RPS_sampler_mod_init (size_t init_size,
736 sampler = RPS_sampler_init (init_size, max_round_interval); 637 sampler = RPS_sampler_init (init_size, max_round_interval);
737 sampler->get_peers = sampler_mod_get_rand_peer; 638 sampler->get_peers = sampler_mod_get_rand_peer;
738 639
739 #ifdef TO_FILE
740 LOG (GNUNET_ERROR_TYPE_DEBUG, 640 LOG (GNUNET_ERROR_TYPE_DEBUG,
741 "Initialised modified sampler %s\n", 641 "Initialised modified sampler %s\n",
742 sampler->file_name); 642 sampler->file_name);
743 to_file (sampler->file_name, 643 to_file (sampler->file_name,
744 "This is a modified sampler"); 644 "This is a modified sampler");
745 #endif /* TO_FILE */
746 645
747 return sampler; 646 return sampler;
748} 647}
@@ -760,11 +659,9 @@ RPS_sampler_update (struct RPS_Sampler *sampler,
760{ 659{
761 uint32_t i; 660 uint32_t i;
762 661
763 #ifdef TO_FILE
764 to_file (sampler->file_name, 662 to_file (sampler->file_name,
765 "Got %s", 663 "Got %s",
766 GNUNET_i2s_full (id)); 664 GNUNET_i2s_full (id));
767 #endif /* TO_FILE */
768 665
769 for (i = 0 ; i < sampler->sampler_size ; i++) 666 for (i = 0 ; i < sampler->sampler_size ; i++)
770 { 667 {
@@ -830,6 +727,10 @@ sampler_get_rand_peer (void *cls,
830 //LOG (GNUNET_ERROR_TYPE_DEBUG, 727 //LOG (GNUNET_ERROR_TYPE_DEBUG,
831 // "Not returning randomly selected, empty PeerID. - Rescheduling.\n"); 728 // "Not returning randomly selected, empty PeerID. - Rescheduling.\n");
832 729
730 /* FIXME no active wait - get notified, when new id arrives?
731 * Might also be a freshly emptied one. Others might still contain ids.
732 * Counter?
733 */
833 gpc->get_peer_task = 734 gpc->get_peer_task =
834 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply ( 735 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
835 GNUNET_TIME_UNIT_SECONDS, 0.1), 736 GNUNET_TIME_UNIT_SECONDS, 0.1),