#include <string.h>
-#include "util.h"
+#include "timeval.h"
#include "cache.h"
#include "log.h"
if (e->state == AVAHI_CACHE_FINAL) {
remove_entry(e->cache, e);
-/* avahi_log_debug("Removing entry from cache due to expiration (%s)", txt); */
+/* avahi_log_debug("Removing entry from cache due to expiration (%s)", txt); */
} else {
guint percent = 0;
/* Request a cache update, if we are subscribed to this entry */
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_log_debug("Requesting cache entry update at %i%% for %s.", percent, txt); */
avahi_interface_post_query(e->cache->interface, e->record->key, TRUE);
}
g_assert(c);
g_assert(e);
g_assert(percent > 0 && percent <= 100);
- gdouble usec;
+ AvahiUsec usec;
/* gchar *txt; */
-
- usec = (gdouble) e->record->ttl * 10000;
+ usec = ((AvahiUsec) e->record->ttl) * 10000;
/* 2% jitter */
- usec = g_random_double_range(usec*percent, usec*(percent+2));
-/* g_message("next expiry: %g (%s)", usec / 1000000.0, txt = avahi_record_to_string(e->record)); */
+ 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;
- g_time_val_add(&e->expiry, (glong) usec);
-
+ avahi_timeval_add(&e->expiry, usec);
+
/* g_message("wake up in +%lu seconds", e->expiry.tv_sec - e->timestamp.tv_sec); */
update_time_event(c, 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);
}
g_assert(c);
g_assert(r && r->ref >= 1);
-/* avahi_log_debug("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 */
} else {
AvahiCacheEntry *e = NULL, *first;
- GTimeVal now;
+ struct timeval now;
- g_get_current_time(&now);
+ gettimeofday(&now, NULL);
/* This is an update request */
/* 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);
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 */
-/* avahi_log_debug("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;
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;
-/* avahi_log_debug("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;
}