static void pow_cb (void *cls, struct GNUNET_SENSOR_crypto_pow_block *block) { struct GNUNET_TIME_Absolute end_time; struct GNUNET_TIME_Relative duration; pow_task = NULL; end_time = GNUNET_TIME_absolute_get(); duration = GNUNET_TIME_absolute_get_difference (block->timestamp, end_time); printf("."); performed_iterations++; total_duration = GNUNET_TIME_relative_add (total_duration, duration); if (ITERATIONS == performed_iterations) { total_duration = GNUNET_TIME_relative_divide (total_duration, ITERATIONS); printf ("Matching bits %d: %s\n", current_matching_bits, GNUNET_STRINGS_relative_time_to_string(total_duration, GNUNET_NO)); total_duration = GNUNET_TIME_UNIT_ZERO; performed_iterations = 0; if (MATCHING_BITS_END == current_matching_bits) { ok = 0; GNUNET_SCHEDULER_cancel (shutdown_task); GNUNET_SCHEDULER_add_now (do_shutdown, NULL); return; } current_matching_bits ++; } GNUNET_SCHEDULER_add_now (&pow_start, NULL); }
static void timer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { static int percentage; timer_task = GNUNET_SCHEDULER_NO_TASK; if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) return; percentage += 10; time_running = GNUNET_TIME_relative_add (time_running, GNUNET_TIME_relative_divide (WAIT, 10)); if (time_running.rel_value == GNUNET_TIME_relative_max (time_running, WAIT).rel_value) { FPRINTF (stderr, "%s", "100%%\n"); shutdown_flag = GNUNET_YES; GNUNET_SCHEDULER_add_now (&end, NULL); } else { FPRINTF (stderr, "%u%%..", percentage); timer_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide (WAIT, 10), &timer, NULL); } }
/** * Do address validation again to keep address valid. * * @param cls the 'struct ValidationEntry' * @param tc scheduler context (unused) */ static void revalidate_address (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct ValidationEntry *ve = cls; struct GNUNET_TIME_Relative canonical_delay; struct GNUNET_TIME_Relative delay; struct GST_BlacklistCheck *bc; uint32_t rdelay; ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK; delay = GNUNET_TIME_absolute_get_remaining (ve->revalidation_block); /* How long until we can possibly permit the next PING? */ canonical_delay = (ve->in_use == GNUNET_YES) ? CONNECTED_PING_FREQUENCY : ((GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value > 0) ? VALIDATED_PING_FREQUENCY : UNVALIDATED_PING_KEEPALIVE); if (delay.rel_value > canonical_delay.rel_value * 2) { /* situation changed, recalculate delay */ delay = canonical_delay; ve->revalidation_block = GNUNET_TIME_relative_to_absolute (delay); } if (delay.rel_value > 0) { /* should wait a bit longer */ ve->revalidation_task = GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve); return; } ve->revalidation_block = GNUNET_TIME_relative_to_absolute (canonical_delay); /* schedule next PINGing with some extra random delay to avoid synchronous re-validations */ rdelay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, canonical_delay.rel_value); delay = GNUNET_TIME_relative_add (canonical_delay, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rdelay)); ve->revalidation_task = GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve); /* start PINGing by checking blacklist */ GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# address revalidations started"), 1, GNUNET_NO); bc = GST_blacklist_test_allowed (&ve->pid, ve->address->transport_name, &transmit_ping_if_allowed, ve); if (NULL != bc) ve->bc = bc; /* only set 'bc' if 'transmit_ping_if_allowed' was not already * called... */ }
/** * @brief This function initiates the transmission of the next message. * * \latexonly \\ \\ \endlatexonly * \em Detailed \em description \n * This function initiates the transmission of the next message. In order to do that it dequeues the message from the * output queue and calls the appropriate GNUnet function for the transmission of the message. The function is implemented as a GNUnet task; this is done in order to decouple it from the * transmit_ready() function call (see above). * * @param cls the GNUnet closure (not used) * @param tc the GNUnet task context (not used) */ static void gnunet_search_flooding_transmit_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { if(!queue_get_length(gnunet_search_flooding_message_queue)) return; struct gnunet_search_flooding_queued_message *msg = (struct gnunet_search_flooding_queued_message*) queue_dequeue( gnunet_search_flooding_message_queue); struct GNUNET_TIME_Relative max_delay = GNUNET_TIME_relative_get_minute_(); struct GNUNET_TIME_Relative gct = GNUNET_TIME_relative_add(max_delay, GNUNET_TIME_relative_get_second_()); GNUNET_CORE_notify_transmit_ready(gnunet_search_flooding_core_handle, 0, 0, max_delay, msg->peer, msg->size, &gnunet_search_flooding_notify_transmit_ready, msg->buffer); /* * Todo: Save and free... */ GNUNET_SCHEDULER_add_delayed(gct, &gnunet_search_flooding_queued_message_free_task, msg); }
int main (int argc, char *argv[]) { struct GNUNET_TIME_Absolute now; struct GNUNET_TIME_AbsoluteNBO nown; struct GNUNET_TIME_Absolute future; struct GNUNET_TIME_Absolute past; struct GNUNET_TIME_Absolute last; struct GNUNET_TIME_Absolute forever; struct GNUNET_TIME_Absolute zero; struct GNUNET_TIME_Relative rel; struct GNUNET_TIME_Relative relForever; struct GNUNET_TIME_Relative relUnit; struct GNUNET_TIME_RelativeNBO reln; unsigned int i; GNUNET_log_setup ("test-time", "WARNING", NULL); forever = GNUNET_TIME_UNIT_FOREVER_ABS; relForever = GNUNET_TIME_UNIT_FOREVER_REL; relUnit = GNUNET_TIME_UNIT_MILLISECONDS; zero.abs_value_us = 0; last = now = GNUNET_TIME_absolute_get (); while (now.abs_value_us == last.abs_value_us) now = GNUNET_TIME_absolute_get (); GNUNET_assert (now.abs_value_us > last.abs_value_us); /* test overflow checking in multiply */ rel = GNUNET_TIME_UNIT_MILLISECONDS; GNUNET_log_skip (1, GNUNET_NO); for (i = 0; i < 55; i++) rel = GNUNET_TIME_relative_multiply (rel, 2); GNUNET_log_skip (0, GNUNET_NO); GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us); /*check zero */ rel.rel_value_us = (UINT64_MAX) - 1024; GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us == GNUNET_TIME_relative_multiply (rel, 0).rel_value_us); /* test infinity-check for relative to absolute */ GNUNET_log_skip (1, GNUNET_NO); last = GNUNET_TIME_relative_to_absolute (rel); GNUNET_assert (last.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us); GNUNET_log_skip (0, GNUNET_YES); /* check relative to absolute */ rel.rel_value_us = 1000000; GNUNET_assert (GNUNET_TIME_absolute_get ().abs_value_us < GNUNET_TIME_relative_to_absolute (rel).abs_value_us); /*check forever */ rel.rel_value_us = UINT64_MAX; GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == GNUNET_TIME_relative_to_absolute (rel).abs_value_us); /* check overflow for r2a */ rel.rel_value_us = (UINT64_MAX) - 1024; GNUNET_log_skip (1, GNUNET_NO); last = GNUNET_TIME_relative_to_absolute (rel); GNUNET_log_skip (0, GNUNET_NO); GNUNET_assert (last.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us); /* check overflow for relative add */ GNUNET_log_skip (1, GNUNET_NO); rel = GNUNET_TIME_relative_add (rel, rel); GNUNET_log_skip (0, GNUNET_NO); GNUNET_assert (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us); GNUNET_log_skip (1, GNUNET_NO); rel = GNUNET_TIME_relative_add (relForever, relForever); GNUNET_log_skip (0, GNUNET_NO); GNUNET_assert (rel.rel_value_us == relForever.rel_value_us); GNUNET_log_skip (1, GNUNET_NO); rel = GNUNET_TIME_relative_add (relUnit, relUnit); GNUNET_assert (rel.rel_value_us == 2 * relUnit.rel_value_us); /* check relation check in get_duration */ future.abs_value_us = now.abs_value_us + 1000000; GNUNET_assert (GNUNET_TIME_absolute_get_difference (now, future).rel_value_us == 1000000); GNUNET_assert (GNUNET_TIME_absolute_get_difference (future, now).rel_value_us == 0); GNUNET_assert (GNUNET_TIME_absolute_get_difference (zero, forever).rel_value_us == forever.abs_value_us); past.abs_value_us = now.abs_value_us - 1000000; rel = GNUNET_TIME_absolute_get_duration (future); GNUNET_assert (rel.rel_value_us == 0); rel = GNUNET_TIME_absolute_get_duration (past); GNUNET_assert (rel.rel_value_us >= 1000000); /* check get remaining */ rel = GNUNET_TIME_absolute_get_remaining (now); GNUNET_assert (rel.rel_value_us == 0); rel = GNUNET_TIME_absolute_get_remaining (past); GNUNET_assert (rel.rel_value_us == 0); rel = GNUNET_TIME_absolute_get_remaining (future); GNUNET_assert (rel.rel_value_us > 0); GNUNET_assert (rel.rel_value_us <= 1000000); forever = GNUNET_TIME_UNIT_FOREVER_ABS; GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == GNUNET_TIME_absolute_get_remaining (forever).rel_value_us); /* check endianess */ reln = GNUNET_TIME_relative_hton (rel); GNUNET_assert (rel.rel_value_us == GNUNET_TIME_relative_ntoh (reln).rel_value_us); nown = GNUNET_TIME_absolute_hton (now); GNUNET_assert (now.abs_value_us == GNUNET_TIME_absolute_ntoh (nown).abs_value_us); /* check absolute addition */ future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_SECONDS); GNUNET_assert (future.abs_value_us == now.abs_value_us + 1000 * 1000LL); future = GNUNET_TIME_absolute_add (forever, GNUNET_TIME_UNIT_ZERO); GNUNET_assert (future.abs_value_us == forever.abs_value_us); rel.rel_value_us = (UINT64_MAX) - 1024; now.abs_value_us = rel.rel_value_us; future = GNUNET_TIME_absolute_add (now, rel); GNUNET_assert (future.abs_value_us == forever.abs_value_us); /* check zero */ future = GNUNET_TIME_absolute_add (now, GNUNET_TIME_UNIT_ZERO); GNUNET_assert (future.abs_value_us == now.abs_value_us); GNUNET_assert (forever.abs_value_us == GNUNET_TIME_absolute_subtract (forever, GNUNET_TIME_UNIT_MINUTES).abs_value_us); /*check absolute subtract */ now.abs_value_us = 50000; rel.rel_value_us = 100000; GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == (GNUNET_TIME_absolute_subtract (now, rel)).abs_value_us); rel.rel_value_us = 10000; GNUNET_assert (40000 == (GNUNET_TIME_absolute_subtract (now, rel)).abs_value_us); /*check relative divide */ GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == (GNUNET_TIME_relative_divide (rel, 0)).rel_value_us); rel = GNUNET_TIME_UNIT_FOREVER_REL; GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == (GNUNET_TIME_relative_divide (rel, 2)).rel_value_us); rel = GNUNET_TIME_relative_divide (relUnit, 2); GNUNET_assert (rel.rel_value_us == relUnit.rel_value_us / 2); /* check Return absolute time of 0ms */ zero = GNUNET_TIME_UNIT_ZERO_ABS; /* check GNUNET_TIME_calculate_eta */ last.abs_value_us = GNUNET_TIME_absolute_get ().abs_value_us - 1024; forever = GNUNET_TIME_UNIT_FOREVER_ABS; forever.abs_value_us = forever.abs_value_us - 1024; GNUNET_assert (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == GNUNET_TIME_calculate_eta (forever, 50000, 100000).rel_value_us); /* check zero */ GNUNET_log_skip (1, GNUNET_NO); GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us == (GNUNET_TIME_calculate_eta (last, 60000, 50000)).rel_value_us); GNUNET_log_skip (0, GNUNET_YES); /*check forever */ GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == (GNUNET_TIME_calculate_eta (last, 0, 50000)).rel_value_us); /*check relative subtract */ now = GNUNET_TIME_absolute_get (); rel.rel_value_us = now.abs_value_us; relForever.rel_value_us = rel.rel_value_us + 1024; GNUNET_assert (1024 == GNUNET_TIME_relative_subtract (relForever, rel).rel_value_us); /*check zero */ GNUNET_assert (GNUNET_TIME_UNIT_ZERO.rel_value_us == GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us); /*check forever */ rel.rel_value_us = UINT64_MAX; GNUNET_assert (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == GNUNET_TIME_relative_subtract (rel, relForever).rel_value_us); /*check GNUNET_TIME_relative_min */ now = GNUNET_TIME_absolute_get (); rel.rel_value_us = now.abs_value_us; relForever.rel_value_us = rel.rel_value_us - 1024; GNUNET_assert (relForever.rel_value_us == GNUNET_TIME_relative_min (rel, relForever).rel_value_us); /*check GNUNET_TIME_relative_max */ GNUNET_assert (rel.rel_value_us == GNUNET_TIME_relative_max (rel, relForever).rel_value_us); /*check GNUNET_TIME_absolute_min */ now = GNUNET_TIME_absolute_get (); last.abs_value_us = now.abs_value_us - 1024; GNUNET_assert (last.abs_value_us == GNUNET_TIME_absolute_min (now, last).abs_value_us); /*check GNUNET_TIME_absolute_max */ GNUNET_assert (now.abs_value_us == GNUNET_TIME_absolute_max (now, last).abs_value_us); return 0; }
/** * Find a "good" address to use for a peer. If we already have an existing * address, we stick to it. Otherwise, we pick by lowest distance and then * by lowest latency. * * @param cls the 'struct ATS_Address**' where we store the result * @param key unused * @param value another 'struct ATS_Address*' to consider using * @return GNUNET_OK (continue to iterate) */ static int find_address_it (void *cls, const GNUNET_HashCode * key, void *value) { struct ATS_Address **ap = cls; struct ATS_Address *aa = (struct ATS_Address *) value; struct ATS_Address *ab = *ap; struct GNUNET_TIME_Absolute now; now = GNUNET_TIME_absolute_get(); if (aa->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, aa->blocked_until).abs_value) { /* This address is blocked for suggestion */ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Address %p blocked for suggestion for %llu ms \n", aa, GNUNET_TIME_absolute_get_difference(now, aa->blocked_until).rel_value); return GNUNET_OK; } aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA); aa->blocked_until = GNUNET_TIME_absolute_add (now, aa->block_interval); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Address %p ready for suggestion, block interval now %llu \n", aa, aa->block_interval); /* FIXME this is a hack */ if (NULL != ab) { if ((0 == strcmp (ab->plugin, "tcp")) && (0 == strcmp (aa->plugin, "tcp"))) { if ((0 != ab->addr_len) && (0 == aa->addr_len)) { /* saved address was an outbound address, but we have an inbound address */ *ap = aa; return GNUNET_OK; } if (0 == ab->addr_len) { /* saved address was an inbound address, so do not overwrite */ return GNUNET_OK; } } } /* FIXME end of hack */ if (NULL == ab) { *ap = aa; return GNUNET_OK; } if ((ntohl (ab->assigned_bw_in.value__) == 0) && (ntohl (aa->assigned_bw_in.value__) > 0)) { /* stick to existing connection */ *ap = aa; return GNUNET_OK; } if (ab->atsp_distance > aa->atsp_distance) { /* user shorter distance */ *ap = aa; return GNUNET_OK; } if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value) { /* user lower latency */ *ap = aa; return GNUNET_OK; } /* don't care */ return GNUNET_OK; }
static void topology_setup_done (void *cls, struct BenchmarkPeer *masters, struct BenchmarkPeer *slaves) { GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Topology setup complete!\n"); masters_p = masters; slaves_p = slaves; l = GNUNET_ATS_TEST_logging_start (e->log_freq, e->name, masters_p, e->num_masters, e->num_slaves, opt_verbose); GNUNET_ATS_TEST_experimentation_run (e, &episode_done_cb, &experiment_done_cb); /* GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0], GNUNET_ATS_TEST_TG_CONSTANT, 1, 1, GNUNET_TIME_UNIT_SECONDS, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_ATS_PREFERENCE_BANDWIDTH); */ /* GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0], GNUNET_ATS_TEST_TG_LINEAR, 1, 50, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2), GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_ATS_PREFERENCE_BANDWIDTH); */ /* GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0], GNUNET_ATS_TEST_TG_RANDOM, 1, 50, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2), GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_ATS_PREFERENCE_BANDWIDTH); */ /* GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0], GNUNET_ATS_TEST_TG_SINUS, 10, 5, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5), GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_ATS_PREFERENCE_BANDWIDTH); */ #if 0 int c_m; int c_s; for (c_m = 0; c_m < e->num_masters; c_m++) { for (c_s = 0; c_s < e->num_slaves; c_s++) { /* Generate maximum traffic to all peers */ /* Example: Generate traffic with constant 10,000 Bytes/s */ GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m], &masters[c_m].partners[c_s], GNUNET_ATS_TEST_TG_CONSTANT, 10000, GNUNET_TIME_UNIT_FOREVER_REL); /* Example: Generate traffic with an increasing rate from 1000 to 2000 * Bytes/s with in a minute */ GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m], &masters[c_m].partners[c_s], GNUNET_ATS_TEST_TG_LINEAR, 1000, 2000, GNUNET_TIME_UNIT_MINUTES, GNUNET_TIME_UNIT_FOREVER_REL); /* Example: Generate traffic with a random rate between 1000 to 2000 * Bytes/s */ GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m], &masters[c_m].partners[c_s], GNUNET_ATS_TEST_TG_RANDOM, 1000, 2000, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TIME_UNIT_FOREVER_REL); /* Example: Generate traffic with a sinus form, a base rate of * 1000 Bytes/s, an amplitude of (max-base), and a period of 1 minute */ GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m], &masters[c_m].partners[c_s], GNUNET_ATS_TEST_TG_SINUS, 1000, 2000, GNUNET_TIME_UNIT_MINUTES, GNUNET_TIME_UNIT_FOREVER_REL); } } #endif timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add (GNUNET_TIME_UNIT_MINUTES, e->max_duration), &do_shutdown, NULL); }