X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-core%2Fcache.c;h=865950f65bb464240bf29f3db41c3b8d593f09d7;hb=dbd6c48a552238d33e80a703feb901ade0c1a2b8;hp=0307b8e58cf18f877af45d2440f046f2aba3d293;hpb=c58379bde376cb2298fca14f83a86626f1b76f2f;p=catta diff --git a/avahi-core/cache.c b/avahi-core/cache.c index 0307b8e..865950f 100644 --- a/avahi-core/cache.c +++ b/avahi-core/cache.c @@ -28,13 +28,15 @@ #include "util.h" #include "cache.h" +#define AVAHI_MAX_CACHE_ENTRIES 200 + static void remove_entry(AvahiCache *c, AvahiCacheEntry *e) { AvahiCacheEntry *t; g_assert(c); g_assert(e); -/* g_message("removing from cache: %p %p", c, e); */ +/* avahi_log_debug("removing from cache: %p %p", c, e); */ /* Remove from hash table */ t = g_hash_table_lookup(c->hash_table, e->record->key); @@ -50,11 +52,13 @@ static void remove_entry(AvahiCache *c, AvahiCacheEntry *e) { if (e->time_event) avahi_time_event_queue_remove(c->server->time_event_queue, e->time_event); - avahi_subscription_notify(c->server, c->interface, e->record, AVAHI_SUBSCRIPTION_REMOVE); + avahi_browser_notify(c->server, c->interface, e->record, AVAHI_BROWSER_REMOVE); avahi_record_unref(e->record); g_free(e); + + g_assert(c->n_entries-- >= 1); } AvahiCache *avahi_cache_new(AvahiServer *server, AvahiInterface *iface) { @@ -67,6 +71,7 @@ AvahiCache *avahi_cache_new(AvahiServer *server, AvahiInterface *iface) { c->hash_table = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal); AVAHI_LLIST_HEAD_INIT(AvahiCacheEntry, c->entries); + c->n_entries = 0; return c; } @@ -76,6 +81,7 @@ void avahi_cache_free(AvahiCache *c) { while (c->entries) remove_entry(c, c->entries); + g_assert(c->n_entries == 0); g_hash_table_destroy(c->hash_table); @@ -151,7 +157,7 @@ static void elapse_func(AvahiTimeEvent *t, void *userdata) { if (e->state == AVAHI_CACHE_FINAL) { remove_entry(e->cache, e); - g_message("Removing entry from cache due to expiration"); +/* avahi_log_debug("Removing entry from cache due to expiration"); */ } else { guint percent = 0; @@ -181,11 +187,11 @@ static void elapse_func(AvahiTimeEvent *t, void *userdata) { g_assert(percent > 0); - g_message("Requesting cache entry update at %i%%.", percent); - /* Request a cache update, if we are subscribed to this entry */ - if (avahi_is_subscribed(e->cache->server, e->record->key)) + if (avahi_is_subscribed(e->cache->server, e->cache->interface, e->record->key)) { +/* avahi_log_debug("Requesting cache entry update at %i%%.", percent); */ avahi_interface_post_query(e->cache->interface, e->record->key, TRUE); + } /* Check again later */ next_expiry(e->cache, e, percent); @@ -220,70 +226,84 @@ static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent) { update_time_event(c, e); } -void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean unique, const AvahiAddress *a) { - AvahiCacheEntry *e, *t; - gchar *txt; +static void expire_in_one_second(AvahiCache *c, AvahiCacheEntry *e) { + g_assert(c); + g_assert(e); + + e->state = AVAHI_CACHE_FINAL; + g_get_current_time(&e->expiry); + g_time_val_add(&e->expiry, 1000000); /* 1s */ + update_time_event(c, e); +} + +void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean cache_flush, const AvahiAddress *a) { +/* gchar *txt; */ g_assert(c); g_assert(r && r->ref >= 1); - g_message("cache update: %s", (txt = avahi_record_to_string(r))); - g_free(txt); +/* avahi_log_debug("cache update: %s", (txt = avahi_record_to_string(r))); */ +/* g_free(txt); */ if (r->ttl == 0) { - /* This is a goodbye request */ - if ((e = avahi_cache_lookup_record(c, r))) { + AvahiCacheEntry *e; - e->state = AVAHI_CACHE_FINAL; - g_get_current_time(&e->timestamp); - e->expiry = e->timestamp; - g_time_val_add(&e->expiry, 1000000); /* 1s */ - update_time_event(c, e); - } + if ((e = avahi_cache_lookup_record(c, r))) + expire_in_one_second(c, e); } else { + AvahiCacheEntry *e = NULL, *first; + GTimeVal now; + + g_get_current_time(&now); /* This is an update request */ - if ((t = e = avahi_cache_lookup_key(c, r->key))) { - - if (unique) { - - /* For unique records, remove all entries but one */ - while (e->by_key_next) - remove_entry(c, e->by_key_next); - - } else { - - /* For non-unique record, look for exactly the same entry */ - for (; e; e = e->by_key_next) - if (avahi_record_equal_no_ttl(e->record, r)) - break; + if ((first = avahi_cache_lookup_key(c, r->key))) { + + if (cache_flush) { + + /* For unique entries drop all entries older than one second */ + for (e = first; e; e = e->by_key_next) { + glong t; + + t = avahi_timeval_diff(&now, &e->timestamp); + + if (t > 1000000) + expire_in_one_second(c, e); + } } + + /* Look for exactly the same entry */ + for (e = first; e; e = e->by_key_next) + if (avahi_record_equal_no_ttl(e->record, r)) + break; } if (e) { -/* g_message("found matching cache entry"); */ - - /* We are the first in the linked list so let's replace the hash table key with the new one */ +/* avahi_log_debug("found matching cache entry"); */ + + /* We need to update the hash table key if we replace the + * record */ if (e->by_key_prev == NULL) g_hash_table_replace(c->hash_table, r->key, e); - /* Notify subscribers */ - if (!avahi_record_equal_no_ttl(e->record, r)) - avahi_subscription_notify(c->server, c->interface, r, AVAHI_SUBSCRIPTION_CHANGE); - /* Update the record */ avahi_record_unref(e->record); e->record = avahi_record_ref(r); - + } else { /* No entry found, therefore we create a new one */ -/* g_message("couldn't find matching cache entry"); */ +/* avahi_log_debug("couldn't find matching cache entry"); */ + + if (c->n_entries >= AVAHI_MAX_CACHE_ENTRIES) + return; + + c->n_entries++; e = g_new(AvahiCacheEntry, 1); e->cache = c; @@ -291,60 +311,36 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean unique, const Av e->record = avahi_record_ref(r); /* Append to hash table */ - AVAHI_LLIST_PREPEND(AvahiCacheEntry, by_key, t, e); - g_hash_table_replace(c->hash_table, e->record->key, t); + AVAHI_LLIST_PREPEND(AvahiCacheEntry, by_key, first, e); + g_hash_table_replace(c->hash_table, e->record->key, first); /* Append to linked list */ AVAHI_LLIST_PREPEND(AvahiCacheEntry, entry, c->entries, e); /* Notify subscribers */ - avahi_subscription_notify(c->server, c->interface, e->record, AVAHI_SUBSCRIPTION_NEW); + avahi_browser_notify(c->server, c->interface, e->record, AVAHI_BROWSER_NEW); } e->origin = *a; - g_get_current_time(&e->timestamp); + e->timestamp = now; next_expiry(c, e, 80); e->state = AVAHI_CACHE_VALID; + e->cache_flush = cache_flush; } } -static gpointer drop_key_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, gpointer userdata) { - g_assert(c); - g_assert(pattern); - g_assert(e); - - remove_entry(c, e); - return NULL; -} - -void avahi_cache_drop_key(AvahiCache *c, AvahiKey *k) { - g_assert(c); - g_assert(k); - - avahi_cache_walk(c, k, drop_key_callback, NULL); -} - -void avahi_cache_drop_record(AvahiCache *c, AvahiRecord *r) { - AvahiCacheEntry *e; - - g_assert(c); - g_assert(r); - - if ((e = avahi_cache_lookup_record(c, r))) - remove_entry(c, e); -} - -static void func(gpointer key, gpointer data, gpointer userdata) { +static void dump_callback(gpointer key, gpointer data, gpointer userdata) { AvahiCacheEntry *e = data; AvahiKey *k = key; - gchar *t; g_assert(k); g_assert(e); - - t = avahi_record_to_string(e->record); - fprintf((FILE*) userdata, "%s\n", t); - g_free(t); + + for (; e; e = e->by_key_next) { + gchar *t = avahi_record_to_string(e->record); + fprintf((FILE*) userdata, "%s\n", t); + g_free(t); + } } void avahi_cache_dump(AvahiCache *c, FILE *f) { @@ -352,7 +348,7 @@ void avahi_cache_dump(AvahiCache *c, FILE *f) { g_assert(f); fprintf(f, ";;; CACHE DUMP FOLLOWS ;;;\n"); - g_hash_table_foreach(c->hash_table, func, f); + g_hash_table_foreach(c->hash_table, dump_callback, f); } gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) { @@ -366,7 +362,14 @@ gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) { age = avahi_timeval_diff(&now, &e->timestamp)/1000000; - g_message("age: %u, ttl/2: %u", age, e->record->ttl); +/* avahi_log_debug("age: %u, ttl/2: %u", age, e->record->ttl); */ return age >= e->record->ttl/2; } + +void avahi_cache_flush(AvahiCache *c) { + g_assert(c); + + while (c->entries) + remove_entry(c, c->entries); +}