X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-core%2Fcache.c;h=9d97b6cd0a55ab01fbb609ba164529e848112cbf;hb=c7ecad8fa467b8be2b34064020e2c541c480645e;hp=5e87706947323d8832cd275f813ed56d8de41dfa;hpb=6efe2615e04c6ef664fa9d49b013e261ba1e6e66;p=catta diff --git a/avahi-core/cache.c b/avahi-core/cache.c index 5e87706..9d97b6c 100644 --- a/avahi-core/cache.c +++ b/avahi-core/cache.c @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -86,6 +87,8 @@ AvahiCache *avahi_cache_new(AvahiServer *server, AvahiInterface *iface) { AVAHI_LLIST_HEAD_INIT(AvahiCacheEntry, c->entries); c->n_entries = 0; + + c->last_rand_timestamp = 0; return c; } @@ -211,11 +214,9 @@ static void elapse_func(AvahiTimeEvent *t, void *userdata) { assert(percent > 0); - /* Request a cache update, if we are subscribed to this entry */ - if (avahi_querier_exists(e->cache->interface, e->record->key)) { -/* avahi_log_debug("Requesting cache entry update at %i%% for %s.", percent, txt); */ - avahi_interface_post_query(e->cache->interface, e->record->key, 1); - } + /* Request a cache update if we are subscribed to this entry */ + if (avahi_querier_shall_refresh_cache(e->cache->interface, e->record->key)) + avahi_interface_post_query(e->cache->interface, e->record->key, 0, NULL); /* Check again later */ next_expiry(e->cache, e, percent); @@ -237,6 +238,7 @@ static void update_time_event(AvahiCache *c, AvahiCacheEntry *e) { static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, unsigned percent) { AvahiUsec usec, left, right; + time_t now; assert(c); assert(e); @@ -247,7 +249,14 @@ static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, unsigned percent) { left = usec * percent; right = usec * (percent+2); /* 2% jitter */ - usec = left + (AvahiUsec) ((double) (right-left) * rand() / (RAND_MAX+1.0)); + now = time(NULL); + + if (now >= c->last_rand_timestamp + 10) { + c->last_rand = rand(); + c->last_rand_timestamp = now; + } + + usec = left + (AvahiUsec) ((double) (right-left) * c->last_rand / (RAND_MAX+1.0)); e->expiry = e->timestamp; avahi_timeval_add(&e->expiry, usec); @@ -435,12 +444,15 @@ void avahi_cache_flush(AvahiCache *c) { static void* start_poof_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void *userdata) { AvahiAddress *a = userdata; + struct timeval now; assert(c); assert(pattern); assert(e); assert(a); - + + gettimeofday(&now, NULL); + switch (e->state) { case AVAHI_CACHE_VALID: @@ -449,15 +461,23 @@ static void* start_poof_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEnt e->state = AVAHI_CACHE_POOF; e->poof_address = *a; - + e->poof_timestamp = now; + e->poof_num = 0; + break; case AVAHI_CACHE_POOF: + if (avahi_timeval_diff(&now, &e->poof_timestamp) < 1000000) + break; - /* This is the second time we got no response, so let's + e->poof_timestamp = now; + e->poof_address = *a; + e->poof_num ++; + + /* This is the 4th time we got no response, so let's * fucking remove this entry. */ - - expire_in_one_second(c, e, AVAHI_CACHE_POOF_FINAL); + if (e->poof_num > 3) + expire_in_one_second(c, e, AVAHI_CACHE_POOF_FINAL); break; default: @@ -471,7 +491,7 @@ void avahi_cache_start_poof(AvahiCache *c, AvahiKey *key, const AvahiAddress *a) assert(c); assert(key); - avahi_cache_walk(c, key, start_poof_callback, a); + avahi_cache_walk(c, key, start_poof_callback, (void*) a); } void avahi_cache_stop_poof(AvahiCache *c, AvahiRecord *record, const AvahiAddress *a) {