noinst_PROGRAMS = \
strlst-test \
domain-test \
- alternative-test
+ alternative-test \
+ timeval-test
lib_LTLIBRARIES = \
libavahi-common.la
domain_test_CFLAGS = $(AM_CFLAGS)
domain_test_LDADD = $(AM_LDADD)
+timeval_test_SOURCES = \
+ util.c util.h \
+ timeval-test.c
+timeval_test_CFLAGS = $(AM_CFLAGS)
+timeval_test_LDADD = $(AM_LDADD)
+
--- /dev/null
+#include "util.h"
+
+int main(int argc, char *argv[]) {
+
+ GTimeVal a = { 5, 5 }, b;
+
+ b = a;
+
+ g_message("%li.%li", a.tv_sec, a.tv_usec);
+ avahi_timeval_add(&a, -50);
+
+ g_message("%li.%li", a.tv_sec, a.tv_usec);
+
+ g_message("%lli", avahi_timeval_diff(&a, &b));
+}
return 0;
}
-glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
+AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
g_assert(a);
g_assert(b);
if (avahi_timeval_compare(a, b) < 0)
- return avahi_timeval_diff(b, a);
+ return - avahi_timeval_diff(b, a);
- return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
+ return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
}
+GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec) {
+ AvahiUsec u;
+ g_assert(a);
+
+ u = usec + a->tv_usec;
+
+ if (u < 0) {
+ a->tv_usec = (glong) (1000000 + (u % 1000000));
+ a->tv_sec += (glong) (-1 + (u / 1000000));
+ } else {
+ a->tv_usec = (glong) (u % 1000000);
+ a->tv_sec += (glong) (u / 1000000);
+ }
+
+ return a;
+}
+
+AvahiUsec avahi_age(const GTimeVal *a) {
+ GTimeVal now;
+
+ g_assert(a);
+
+ g_get_current_time(&now);
+
+ return avahi_timeval_diff(&now, a);
+}
+
+GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
+ g_assert(tv);
+
+ g_get_current_time(tv);
+
+ if (msec)
+ avahi_timeval_add(tv, (AvahiUsec) msec*1000);
+
+ if (jitter)
+ avahi_timeval_add(tv, (AvahiUsec) g_random_int_range(0, jitter) * 1000);
+
+ return tv;
+}
gint avahi_set_cloexec(gint fd) {
gint n;
return 0;
}
-GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
- g_assert(tv);
-
- g_get_current_time(tv);
-
- if (msec)
- g_time_val_add(tv, msec*1000);
-
- if (jitter)
- g_time_val_add(tv, g_random_int_range(0, jitter) * 1000);
-
- return tv;
-}
-
-glong avahi_age(const GTimeVal *a) {
- GTimeVal now;
-
- g_assert(a);
-
- g_get_current_time(&now);
-
- return avahi_timeval_diff(&now, a);
-}
-
/* Read the first label from string *name, unescape "\" and write it to dest */
gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size) {
guint i = 0;
AVAHI_C_DECL_BEGIN
+typedef gint64 AvahiUsec;
+
gchar *avahi_normalize_name(const gchar *s); /* g_free() the result! */
gchar *avahi_get_host_name(void); /* g_free() the result! */
gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b);
-glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b);
+AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b);
+GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec);
+
+AvahiUsec avahi_age(const GTimeVal *a);
+GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter);
gint avahi_set_cloexec(gint fd);
gint avahi_set_nonblock(gint fd);
gint avahi_wait_for_write(gint fd);
-GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter);
-
-glong avahi_age(const GTimeVal *a);
-
gboolean avahi_domain_equal(const gchar *a, const gchar *b);
gint avahi_binary_domain_cmp(const gchar *a, const gchar *b);
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(usec*percent, 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);
e->state = AVAHI_CACHE_FINAL;
g_get_current_time(&e->expiry);
- g_time_val_add(&e->expiry, 1000000); /* 1s */
+ 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 */
/* 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); */
}
static void dump_callback(gpointer key, gpointer data, gpointer userdata) {
gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) {
GTimeVal now;
- guint age;
+ AvahiUsec age;
g_assert(c);
g_assert(e);
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;
}
addr->prefix_len = ifaddrmsg->ifa_prefixlen;
update_address_rr(m, addr, FALSE);
- check_interface_relevant(m, i);
} else {
AvahiInterfaceAddress *addr;
update_address_rr(m, addr, TRUE);
free_address(m, addr);
-
- check_interface_relevant(m, i);
}
-
+
+ check_interface_relevant(m, i);
+
} else if (n->nlmsg_type == NLMSG_DONE) {
if (m->list == LIST_IFACE) {