#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);
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) {
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;
}
while (c->entries)
remove_entry(c, c->entries);
+ g_assert(c->n_entries == 0);
g_hash_table_destroy(c->hash_table);
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;
g_assert(percent > 0);
/* Request a cache update, if we are subscribed to this entry */
- if (avahi_is_subscribed(e->cache->server, e->record->key)) {
-/* g_message("Requesting cache entry update at %i%%.", percent); */
+ 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);
}
update_time_event(c, e);
}
-void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean unique, const AvahiAddress *a) {
+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))); */
+/* avahi_log_debug("cache update: %s", (txt = avahi_record_to_string(r))); */
/* g_free(txt); */
if (r->ttl == 0) {
if ((first = avahi_cache_lookup_key(c, r->key))) {
- if (unique) {
+ if (cache_flush) {
/* For unique entries drop all entries older than one second */
for (e = first; e; e = e->by_key_next) {
if (e) {
-/* g_message("found matching cache entry"); */
+/* avahi_log_debug("found matching cache entry"); */
/* We need to update the hash table key if we replace the
* record */
/* 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;
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;
e->timestamp = now;
next_expiry(c, e, 80);
e->state = AVAHI_CACHE_VALID;
+ e->cache_flush = cache_flush;
}
}
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;
}