]> git.meshlink.io Git - catta/blobdiff - avahi-core/cache.c
* drop glib from avahi-common
[catta] / avahi-core / cache.c
index ec4ee6088e55e1a57a410c3b7f24eaf8f9eab74f..2f3296f618524733fceef4111a0e329650eab550 100644 (file)
@@ -27,6 +27,9 @@
 
 #include "util.h"
 #include "cache.h"
+#include "log.h"
+
+#define AVAHI_MAX_CACHE_ENTRIES 200
 
 static void remove_entry(AvahiCache *c, AvahiCacheEntry *e) {
     AvahiCacheEntry *t;
@@ -34,7 +37,7 @@ static void remove_entry(AvahiCache *c, AvahiCacheEntry *e) {
     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 +53,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_resolver_notify(c->server, c->interface, e->record, AVAHI_BROWSER_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 +72,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 +82,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);
     
@@ -145,13 +152,16 @@ static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent);
 
 static void elapse_func(AvahiTimeEvent *t, void *userdata) {
     AvahiCacheEntry *e = userdata;
+/*     gchar *txt; */
     
     g_assert(t);
     g_assert(e);
 
+/*     txt = avahi_record_to_string(e->record); */
+
     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 (%s)", txt); */
     } else {
         guint percent = 0;
     
@@ -182,14 +192,16 @@ static void elapse_func(AvahiTimeEvent *t, void *userdata) {
         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%% for %s.", percent, txt);   */
             avahi_interface_post_query(e->cache->interface, e->record->key, TRUE);
         }
 
         /* Check again later */
         next_expiry(e->cache, e, percent);
     }
+
+/*     g_free(txt); */
 }
 
 static void update_time_event(AvahiCache *c, AvahiCacheEntry *e) {
@@ -203,20 +215,24 @@ static void update_time_event(AvahiCache *c, AvahiCacheEntry *e) {
 }
 
 static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent) {
-    gulong usec;
-
     g_assert(c);
     g_assert(e);
     g_assert(percent > 0 && percent <= 100);
+    AvahiUsec usec;
+/*     gchar *txt; */
 
-    e->expiry = e->timestamp;
-
-    usec = e->record->ttl * 10000;
+    usec = ((AvahiUsec) e->record->ttl) * 10000;
 
     /* 2% jitter */
-    usec = g_random_int_range(usec*percent, usec*(percent+2));
+    usec = (AvahiUsec) g_random_double_range((gdouble) (usec*percent), (gdouble) (usec*(percent+2)));
+/*     g_message("next expiry: %lli (%s)", usec / 1000000, txt = avahi_record_to_string(e->record)); */
+/*     g_free(txt); */
+    
+    e->expiry = e->timestamp;
+    avahi_timeval_add(&e->expiry, usec);
+    
+/*     g_message("wake up in +%lu seconds", e->expiry.tv_sec - e->timestamp.tv_sec); */
     
-    g_time_val_add(&e->expiry, usec);
     update_time_event(c, e);
 }
 
@@ -225,19 +241,18 @@ static void expire_in_one_second(AvahiCache *c, AvahiCacheEntry *e) {
     g_assert(e);
     
     e->state = AVAHI_CACHE_FINAL;
-    g_get_current_time(&e->expiry);
-    g_time_val_add(&e->expiry, 1000000); /* 1s */
+    gettimeofday(&e->expiry, NULL);
+    avahi_timeval_add(&e->expiry, 1000000); /* 1s */
     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))); */
-/*     g_free(txt); */
+/*     txt = avahi_record_to_string(r); */
 
     if (r->ttl == 0) {
         /* This is a goodbye request */
@@ -249,19 +264,19 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean unique, const Av
 
     } else {
         AvahiCacheEntry *e = NULL, *first;
-        GTimeVal now;
+        struct timeval now;
 
-        g_get_current_time(&now);
+        gettimeofday(&now, NULL);
 
         /* This is an update request */
 
         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) {
-                    glong t;
+                    AvahiUsec t;
 
                     t = avahi_timeval_diff(&now, &e->timestamp);
 
@@ -278,7 +293,7 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean unique, const Av
     
         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 */
@@ -288,11 +303,18 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean unique, const Av
             /* Update the record */
             avahi_record_unref(e->record);
             e->record = avahi_record_ref(r);
+
+/*             avahi_log_debug("cache: updating %s", txt);   */
             
         } else {
             /* No entry found, therefore we create a new one */
             
-/*             g_message("couldn't find matching cache entry");  */
+/*             avahi_log_debug("cache: couldn't find matching cache entry for %s", txt);   */
+
+            if (c->n_entries >= AVAHI_MAX_CACHE_ENTRIES)
+                return;
+
+            c->n_entries++;
             
             e = g_new(AvahiCacheEntry, 1);
             e->cache = c;
@@ -307,50 +329,66 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean unique, const Av
             AVAHI_LLIST_PREPEND(AvahiCacheEntry, entry, c->entries, e);
 
             /* Notify subscribers */
-            avahi_resolver_notify(c->server, c->interface, e->record, AVAHI_BROWSER_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;
     }
+
+/*     g_free(txt);  */
 }
 
+struct dump_data {
+    AvahiDumpCallback callback;
+    gpointer userdata;
+};
+
 static void dump_callback(gpointer key, gpointer data, gpointer userdata) {
     AvahiCacheEntry *e = data;
     AvahiKey *k = key;
+    struct dump_data *dump_data = userdata;
 
     g_assert(k);
     g_assert(e);
+    g_assert(data);
 
     for (; e; e = e->by_key_next) {
         gchar *t = avahi_record_to_string(e->record);
-        fprintf((FILE*) userdata, "%s\n", t);
+        dump_data->callback(t, dump_data->userdata);
         g_free(t);
     }
 }
 
-void avahi_cache_dump(AvahiCache *c, FILE *f) {
+void avahi_cache_dump(AvahiCache *c, AvahiDumpCallback callback, gpointer userdata) {
+    struct dump_data data;
+
     g_assert(c);
-    g_assert(f);
+    g_assert(callback);
+
+    callback(";;; CACHE DUMP FOLLOWS ;;;", userdata);
+
+    data.callback = callback;
+    data.userdata = userdata;
 
-    fprintf(f, ";;; CACHE DUMP FOLLOWS ;;;\n");
-    g_hash_table_foreach(c->hash_table, dump_callback, f);
+    g_hash_table_foreach(c->hash_table, dump_callback, &data);
 }
 
 gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) {
-    GTimeVal now;
-    guint age;
+    struct timeval now;
+    AvahiUsec age;
     
     g_assert(c);
     g_assert(e);
 
-    g_get_current_time(&now);
+    gettimeofday(&now, NULL);
 
     age = avahi_timeval_diff(&now, &e->timestamp)/1000000;
 
-/*     g_message("age: %u, ttl/2: %u", age, e->record->ttl); */
+/*     avahi_log_debug("age: %lli, ttl/2: %u", age, e->record->ttl);  */
     
     return age >= e->record->ttl/2;
 }