]> git.meshlink.io Git - catta/commitdiff
implement new main loop abstraction layer
authorLennart Poettering <lennart@poettering.net>
Fri, 12 Aug 2005 19:09:50 +0000 (19:09 +0000)
committerLennart Poettering <lennart@poettering.net>
Fri, 12 Aug 2005 19:09:50 +0000 (19:09 +0000)
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@305 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

37 files changed:
Makefile.am
avahi-common/Makefile.am
avahi-common/simple-watch.c [new file with mode: 0644]
avahi-common/simple-watch.h [new file with mode: 0644]
avahi-common/timeval-test.c [new file with mode: 0644]
avahi-common/timeval.c [new file with mode: 0644]
avahi-common/timeval.h [new file with mode: 0644]
avahi-common/watch-test.c [new file with mode: 0644]
avahi-common/watch.h [new file with mode: 0644]
avahi-core/Makefile.am
avahi-core/announce.c
avahi-core/avahi-test.c
avahi-core/browse.c
avahi-core/cache.c
avahi-core/conformance-test.c
avahi-core/iface.h
avahi-core/probe-sched.c
avahi-core/probe-sched.h
avahi-core/query-sched.c
avahi-core/query-sched.h
avahi-core/resolve-address.c
avahi-core/resolve-host-name.c
avahi-core/resolve-service.c
avahi-core/response-sched.c
avahi-core/response-sched.h
avahi-core/server.c
avahi-core/timeeventq-test.c
avahi-core/timeeventq.c
avahi-core/timeval-test.c [deleted file]
avahi-core/timeval.c [deleted file]
avahi-core/timeval.h [deleted file]
avahi-glib/Makefile.am [new file with mode: 0644]
avahi-glib/glib-watch-test.c [new file with mode: 0644]
avahi-glib/glib-watch.c [new file with mode: 0644]
avahi-glib/glib-watch.h [new file with mode: 0644]
configure.ac
docs/TODO

index 1ce7f76cec35926079bd2688ab63968eb7a47933..628a0ac7d753ccb335b8143efa76ba09ae92df11 100644 (file)
@@ -43,6 +43,7 @@ SUBDIRS = \
        initscript \
        avahi-dnsconfd \
        avahi-utils \
+       avahi-glib \
        examples \
        man
 
@@ -55,4 +56,3 @@ homepage:
        scp avahi-daemon/*.introspect avahi-daemon/introspect.dtd avahi-daemon/introspect.xsl\
            man/*.xml man/xmltoman.dtd man/xmltoman.xsl \
            fdo:public_html/
-           
index d91343b317eb798798c7c9df8ebdfdd7acb4b8c0..c78e0541df6690e4d3feb217ac4c98a8510e3a3b 100644 (file)
 
 AM_CFLAGS=-I$(top_srcdir)
 
-# GLIB 2.0
-AM_CFLAGS+=$(GLIB20_CFLAGS)
-AM_LDADD=$(GLIB20_LIBS)
-
 # This cool debug trap works on i386/gcc only
 AM_CFLAGS+='-DDEBUG_TRAP=__asm__("int $$3")'
 
@@ -36,6 +32,9 @@ avahi_commoninclude_HEADERS = \
        cdecl.h \
        defs.h \
        malloc.h
+       watch.h \
+       timeval.h \
+       simple-watch.h
 
 noinst_HEADERS = llist.h
 
@@ -43,11 +42,12 @@ if ENABLE_DBUS
 noinst_HEADERS += dbus.h
 endif
 
-
 noinst_PROGRAMS = \
        strlst-test \
        domain-test \
-       alternative-test
+       alternative-test \
+       timeval-test \
+       watch-test
 
 lib_LTLIBRARIES = \
        libavahi-common.la 
@@ -58,7 +58,10 @@ libavahi_common_la_SOURCES = \
        alternative.c alternative.h \
        error.c error.h \
        strlst.c strlst.h \
-       domain.c domain.h
+       domain.c domain.h \
+       timeval.c timeval.h \
+       simple-watch.c simple-watch.h \
+       watch.h
 libavahi_common_la_CFLAGS = $(AM_CFLAGS)
 libavahi_common_la_LIBADD = $(AM_LDADD)
 
@@ -84,3 +87,19 @@ domain_test_SOURCES = \
 domain_test_CFLAGS = $(AM_CFLAGS)
 domain_test_LDADD = $(AM_LDADD)
 
+watch_test_SOURCES = \
+       timeval.c timeval.h \
+       simple-watch.c simple-watch.h \
+       watch.h \
+       malloc.c malloc.h \
+       watch-test.c
+watch_test_CFLAGS = $(AM_CFLAGS)
+watch_test_LDADD = $(AM_LDADD)
+
+timeval_test_SOURCES = \
+       timeval.c timeval.h \
+       timeval-test.c
+timeval_test_CFLAGS = $(AM_CFLAGS)
+timeval_test_LDADD = $(AM_LDADD)
+
+
diff --git a/avahi-common/simple-watch.c b/avahi-common/simple-watch.c
new file mode 100644 (file)
index 0000000..acc0923
--- /dev/null
@@ -0,0 +1,324 @@
+/* $Id$ */
+
+/***
+  This file is part of avahi.
+  avahi is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as
+  published by the Free Software Foundation; either version 2.1 of the
+  License, or (at your option) any later version.
+  avahi is distributed in the hope that it will be useful, but WITHOUT
+  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+  Public License for more details.
+  You should have received a copy of the GNU Lesser General Public
+  License along with avahi; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <sys/poll.h>
+#include <assert.h>
+#include <string.h>
+#include <errno.h>
+
+#include <avahi-common/llist.h>
+#include <avahi-common/malloc.h>
+
+#include "simple-watch.h"
+
+struct AvahiWatch {
+    AvahiSimplePoll *simple_poll;
+    int dead;
+    int idx;
+    struct pollfd pollfd;
+    AvahiWatchCallback callback;
+    void *userdata;
+
+    AVAHI_LLIST_FIELDS(AvahiWatch, watches);
+};
+
+struct AvahiSimplePoll {
+    AvahiPoll api;
+
+    struct pollfd* pollfds;
+    int n_pollfds, max_pollfds, rebuild_pollfds;
+
+    struct timeval wakeup;
+    int use_wakeup;
+
+    int req_cleanup;
+    
+    int quit;
+
+    int n_watches;
+    AVAHI_LLIST_HEAD(AvahiWatch, watches);
+};
+
+static AvahiWatch* watch_new(AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
+    AvahiWatch *w;
+    AvahiSimplePoll *s;
+    
+    assert(api);
+    assert(fd >= 0);
+    assert(callback);
+
+    s = api->userdata;
+    assert(s);
+
+    if (!(w = avahi_new(AvahiWatch, 1)))
+        return NULL;
+    
+    w->simple_poll = s;
+    w->pollfd.fd = fd;
+    w->pollfd.events = event;
+    w->callback = callback;
+    w->userdata = userdata;
+    w->dead = 0;
+
+    if (s->n_pollfds < s->max_pollfds) {
+        /* If there's space for this pollfd, go on and allocate it */
+        w->idx = s->n_pollfds++;
+        s->pollfds[w->idx] = w->pollfd;
+        
+    } else {
+        /* Unfortunately there's no place for this pollfd, so request a rebuild of the array */
+        w->idx = -1;
+        s->rebuild_pollfds = 1;
+    }
+
+    AVAHI_LLIST_PREPEND(AvahiWatch, watches, s->watches, w);
+    s->n_watches++;
+
+    return w;
+}
+
+static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
+    assert(w);
+    assert(!w->dead);
+
+    w->pollfd.events = events;
+
+    if (w->idx != -1) {
+        assert(w->simple_poll);
+        w->simple_poll->pollfds[w->idx] = w->pollfd;
+    } else
+        w->simple_poll->rebuild_pollfds = 1;
+}
+
+static void remove_pollfd(AvahiWatch *w) {
+    assert(w);
+
+    if (w->idx == -1)
+        return;
+    
+    if (w->idx == w->simple_poll->n_pollfds-1) {
+
+        /* This pollfd is at the end of the array, so we can easily cut it */
+
+        assert(w->simple_poll->n_pollfds > 0);
+        w->simple_poll->n_pollfds -= 1;
+    } else
+
+        /* Unfortunately this pollfd is in the middle of the array, so request a rebuild of it */
+        w->simple_poll->rebuild_pollfds = 1;
+}
+
+static void watch_free(AvahiWatch *w) {
+    assert(w);
+    assert(!w->dead);
+
+    remove_pollfd(w);
+    
+    w->dead = 1;
+    w->simple_poll->n_watches --;
+    w->simple_poll->req_cleanup = 1;
+}
+
+static void set_wakeup_time(AvahiPoll *api, const struct timeval *tv) {
+    AvahiSimplePoll *s;
+
+    assert(api);
+    s = api->userdata;
+
+    if (tv) {
+        s->wakeup = *tv;
+        s->use_wakeup = 1;
+    } else
+        s->use_wakeup = 0;
+}
+
+static void destroy_watch(AvahiWatch *w) {
+    assert(w);
+
+    remove_pollfd(w);
+    AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->simple_poll->watches, w);
+
+    if (!w->dead)
+        w->simple_poll->n_watches --;
+    
+    avahi_free(w);
+}
+
+static void cleanup(AvahiSimplePoll *s, int all) {
+    AvahiWatch *w, *next;
+    assert(s);
+
+    for (w = s->watches; w; w = next) {
+        next = w->watches_next;
+
+        if (all || w->dead)
+            destroy_watch(w);
+    }
+
+    s->req_cleanup = 0;
+}
+
+AvahiSimplePoll *avahi_simple_poll_new(void) {
+    AvahiSimplePoll *s;
+
+    if (!(s = avahi_new(AvahiSimplePoll, 1)))
+        return NULL;
+    
+    s->api.userdata = s;
+    s->api.watch_new = watch_new;
+    s->api.watch_free = watch_free;
+    s->api.watch_update = watch_update;
+    s->api.set_wakeup_time = set_wakeup_time;
+    s->pollfds = NULL;
+    s->max_pollfds = s->n_pollfds = 0;
+    s->use_wakeup = 0;
+    s->rebuild_pollfds = 0;
+    s->quit = 0;
+    s->n_watches = 0;
+    s->req_cleanup = 0;
+
+    AVAHI_LLIST_HEAD_INIT(AvahiWatch, s->watches);
+
+    return s;
+}
+
+void avahi_simple_poll_free(AvahiSimplePoll *s) {
+    assert(s);
+
+    cleanup(s, 1);
+    
+    assert(s->n_watches == 0);
+    
+    avahi_free(s->pollfds);
+    avahi_free(s);
+}
+
+static int rebuild(AvahiSimplePoll *s) {
+    AvahiWatch *w;
+    int idx;
+    
+    assert(s);
+
+    if (s->n_watches > s->max_pollfds) {
+        struct pollfd *n;
+
+        s->max_pollfds = s->n_watches + 10;
+        
+        if (!(n = avahi_realloc(s->pollfds, sizeof(struct pollfd) * s->max_pollfds)))
+            return -1;
+
+        s->pollfds = n;
+    }
+
+    for (idx = 0, w = s->watches; w; w = w->watches_next) {
+
+        if(w->dead)
+            continue;
+
+        assert(w->idx < s->max_pollfds);
+        s->pollfds[w->idx = idx++] = w->pollfd;
+    }
+
+    s->n_pollfds = idx;
+    
+    s->rebuild_pollfds = 0;
+
+    return 0;
+}
+
+int avahi_simple_poll_iterate(AvahiSimplePoll *s, int block) {
+    int timeout, r, ret = 0;
+    assert(s);
+
+    if (s->quit)
+        return 1;
+
+    if (s->req_cleanup)
+        cleanup(s, 0);
+    
+    if (s->rebuild_pollfds)
+        if (rebuild(s) < 0)
+            return -1;
+
+    if (block) {
+        if (s->use_wakeup) {
+            struct timeval now;
+            AvahiUsec usec;
+
+            gettimeofday(&now, NULL);
+
+            usec = avahi_timeval_diff(&s->wakeup, &now);
+            
+            timeout = usec <= 0 ? 0 : (int) (usec / 1000);
+        } else
+            timeout = -1;
+    } else
+        timeout = 0;
+
+    if ((r = poll(s->pollfds, s->n_pollfds, timeout)) < 0)
+        return -1;
+
+    else if (r > 0) {
+        AvahiWatch *w;
+
+        for (w = s->watches; w; w = w->watches_next) {
+
+            if (w->dead)
+                continue;
+
+            assert(w->idx >= 0);
+            assert(w->idx < s->n_pollfds);
+
+            if (s->pollfds[w->idx].revents > 0)
+                w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata);
+
+            if (s->quit) {
+                ret = 1;
+                goto finish;
+            }
+        }
+    }
+
+    ret = 0;
+
+finish:
+
+    if (s->req_cleanup)
+        cleanup(s, 0);
+    
+    return ret;
+}
+
+void avahi_simple_poll_quit(AvahiSimplePoll *w) {
+    assert(w);
+
+    w->quit = 1;
+}
+
+AvahiPoll* avahi_simple_poll_get(AvahiSimplePoll *s) {
+    assert(s);
+    
+    return &s->api;
+}
diff --git a/avahi-common/simple-watch.h b/avahi-common/simple-watch.h
new file mode 100644 (file)
index 0000000..600e5ce
--- /dev/null
@@ -0,0 +1,44 @@
+#ifndef foosimplewatchhfoo
+#define foosimplewatchhfoo
+
+/* $Id$ */
+
+/***
+  This file is part of avahi.
+  avahi is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as
+  published by the Free Software Foundation; either version 2.1 of the
+  License, or (at your option) any later version.
+  avahi is distributed in the hope that it will be useful, but WITHOUT
+  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+  Public License for more details.
+  You should have received a copy of the GNU Lesser General Public
+  License along with avahi; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#include <avahi-common/cdecl.h>
+
+#include "watch.h"
+
+AVAHI_C_DECL_BEGIN
+
+typedef struct AvahiSimplePoll AvahiSimplePoll;
+
+AvahiSimplePoll *avahi_simple_poll_new(void);
+void avahi_simple_poll_free(AvahiSimplePoll *s);
+
+AvahiPoll* avahi_simple_poll_get(AvahiSimplePoll *s);
+
+int avahi_simple_poll_iterate(AvahiSimplePoll *s, int block);
+
+void avahi_simple_poll_quit(AvahiSimplePoll *s);
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/timeval-test.c b/avahi-common/timeval-test.c
new file mode 100644 (file)
index 0000000..7dedf41
--- /dev/null
@@ -0,0 +1,41 @@
+/* $Id$ */
+
+/***
+  This file is part of avahi.
+  avahi is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as
+  published by the Free Software Foundation; either version 2.1 of the
+  License, or (at your option) any later version.
+  avahi is distributed in the hope that it will be useful, but WITHOUT
+  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+  Public License for more details.
+  You should have received a copy of the GNU Lesser General Public
+  License along with avahi; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include "timeval.h"
+
+int main(int argc, char *argv[]) {
+
+    struct timeval a = { 5, 5 }, b;
+
+    b = a;
+
+    printf("%li.%li\n", a.tv_sec, a.tv_usec);
+    avahi_timeval_add(&a, -50);
+
+    printf("%li.%li\n", a.tv_sec, a.tv_usec);
+
+    printf("%lli\n", avahi_timeval_diff(&a, &b));
+}
diff --git a/avahi-common/timeval.c b/avahi-common/timeval.c
new file mode 100644 (file)
index 0000000..e5732cd
--- /dev/null
@@ -0,0 +1,101 @@
+/* $Id$ */
+
+/***
+  This file is part of avahi.
+  avahi is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as
+  published by the Free Software Foundation; either version 2.1 of the
+  License, or (at your option) any later version.
+  avahi is distributed in the hope that it will be useful, but WITHOUT
+  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+  Public License for more details.
+  You should have received a copy of the GNU Lesser General Public
+  License along with avahi; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <assert.h>
+
+#include "timeval.h"
+
+int avahi_timeval_compare(const struct timeval *a, const struct timeval *b) {
+    assert(a);
+    assert(b);
+
+    if (a->tv_sec < b->tv_sec)
+        return -1;
+
+    if (a->tv_sec > b->tv_sec)
+        return 1;
+
+    if (a->tv_usec < b->tv_usec)
+        return -1;
+
+    if (a->tv_usec > b->tv_usec)
+        return 1;
+
+    return 0;
+}
+
+AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b) {
+    assert(a);
+    assert(b);
+
+    if (avahi_timeval_compare(a, b) < 0)
+        return - avahi_timeval_diff(b, a);
+
+    return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
+}
+
+struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec) {
+    AvahiUsec u;
+    assert(a);
+
+    u = usec + a->tv_usec;
+
+    if (u < 0) {
+        a->tv_usec = (long) (1000000 + (u % 1000000));
+        a->tv_sec += (long) (-1 + (u / 1000000));
+    } else {
+        a->tv_usec = (long) (u % 1000000);
+        a->tv_sec += (long) (u / 1000000);
+    }
+
+    return a;
+}
+
+AvahiUsec avahi_age(const struct timeval *a) {
+    struct timeval now;
+    
+    assert(a);
+
+    gettimeofday(&now, NULL);
+
+    return avahi_timeval_diff(&now, a);
+}
+
+
+struct timeval *avahi_elapse_time(struct timeval *tv, unsigned msec, unsigned jitter) {
+    assert(tv);
+
+    gettimeofday(tv, NULL);
+
+    if (msec)
+        avahi_timeval_add(tv, (AvahiUsec) msec*1000);
+
+    if (jitter)
+        avahi_timeval_add(tv, (AvahiUsec) (jitter*1000.0*rand()/(RAND_MAX+1.0)));
+        
+    return tv;
+}
+
diff --git a/avahi-common/timeval.h b/avahi-common/timeval.h
new file mode 100644 (file)
index 0000000..c046a19
--- /dev/null
@@ -0,0 +1,43 @@
+#ifndef footimevalhfoo
+#define footimevalhfoo
+
+/* $Id$ */
+
+/***
+  This file is part of avahi.
+  avahi is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as
+  published by the Free Software Foundation; either version 2.1 of the
+  License, or (at your option) any later version.
+  avahi is distributed in the hope that it will be useful, but WITHOUT
+  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+  Public License for more details.
+  You should have received a copy of the GNU Lesser General Public
+  License along with avahi; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#include <inttypes.h>
+#include <sys/time.h>
+
+#include <avahi-common/cdecl.h>
+
+AVAHI_C_DECL_BEGIN
+
+typedef int64_t AvahiUsec;
+
+int avahi_timeval_compare(const struct timeval *a, const struct timeval *b);
+AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b);
+struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec);
+
+AvahiUsec avahi_age(const struct timeval *a);
+struct timeval *avahi_elapse_time(struct timeval *tv, unsigned msec, unsigned jitter);
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/watch-test.c b/avahi-common/watch-test.c
new file mode 100644 (file)
index 0000000..f87a303
--- /dev/null
@@ -0,0 +1,81 @@
+/* $Id$ */
+
+/***
+  This file is part of avahi.
+  avahi is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as
+  published by the Free Software Foundation; either version 2.1 of the
+  License, or (at your option) any later version.
+  avahi is distributed in the hope that it will be useful, but WITHOUT
+  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+  Public License for more details.
+  You should have received a copy of the GNU Lesser General Public
+  License along with avahi; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <unistd.h>
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+
+#include "watch.h"
+#include "simple-watch.h"
+#include "timeval.h"
+
+static AvahiPoll *api = NULL;
+
+static void callback(AvahiWatch *w, int fd, AvahiWatchEvent event, void *userdata) {
+
+    if (event & AVAHI_WATCH_IN) {
+        ssize_t r;
+        char c;
+        
+        if ((r = read(fd, &c, 1)) <= 0) {
+            fprintf(stderr, "read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
+            api->watch_free(w);
+            return;
+        }
+
+        printf("Read: %c\n", c >= 32 && c < 127 ? c : '.');
+    }
+}
+
+int main(int argc, char *argv[]) {
+    int i = 0;
+    AvahiSimplePoll *s;
+    
+    s = avahi_simple_poll_new();
+    assert(s);
+
+    api = avahi_simple_poll_get(s);
+
+    api->watch_new(api, 0, AVAHI_WATCH_IN, callback, NULL);
+
+    for (;;) {
+        struct timeval tv;
+        printf("Iteration %i\n", i++);
+
+        if (i > 100)
+            avahi_simple_poll_quit(s);
+
+        avahi_elapse_time(&tv, 1000, 0);
+        
+        api->set_wakeup_time(api, &tv);
+        
+        if (avahi_simple_poll_iterate(s, 1) != 0)
+            break;
+    }
+    
+    return 0;
+}
diff --git a/avahi-common/watch.h b/avahi-common/watch.h
new file mode 100644 (file)
index 0000000..b26c2ba
--- /dev/null
@@ -0,0 +1,57 @@
+#ifndef foowatchhfoo
+#define foowatchhfoo
+
+/* $Id$ */
+
+/***
+  This file is part of avahi.
+  avahi is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as
+  published by the Free Software Foundation; either version 2.1 of the
+  License, or (at your option) any later version.
+  avahi is distributed in the hope that it will be useful, but WITHOUT
+  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+  Public License for more details.
+  You should have received a copy of the GNU Lesser General Public
+  License along with avahi; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#include <sys/poll.h>
+#include <avahi-common/cdecl.h>
+
+#include "timeval.h"
+
+AVAHI_C_DECL_BEGIN
+
+typedef struct AvahiWatch AvahiWatch;
+typedef struct AvahiPoll AvahiPoll;
+
+typedef enum {
+    AVAHI_WATCH_IN = POLLIN,
+    AVAHI_WATCH_OUT = POLLOUT,
+    AVAHI_WATCH_ERR = POLLERR,
+    AVAHI_WATCH_HUP = POLLHUP
+} AvahiWatchEvent;
+
+typedef void (*AvahiWatchCallback)(AvahiWatch *w, int fd, AvahiWatchEvent event, void *userdata);
+
+struct AvahiPoll {
+    void* userdata;
+    
+    AvahiWatch* (*watch_new)(AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata);
+    void (*watch_update)(AvahiWatch *w, AvahiWatchEvent event);
+    void (*watch_free)(AvahiWatch *w);
+    
+    void (*set_wakeup_time)(AvahiPoll *api, const struct timeval *tv);
+};
+
+AVAHI_C_DECL_END
+
+#endif
+
index e15f295cadad8a35bad8837ebbc2f2b858855f49..c71d6edde8cee3328d1cafcf08dc35f09dda3dd8 100644 (file)
@@ -24,7 +24,6 @@ AM_CFLAGS+=$(GLIB20_CFLAGS)
 AM_LDADD=$(GLIB20_LIBS)
 
 # Import stuff from avahi-common
-AM_CFLAGS+=-I$(top_srcdir)/avahi-common 
 COMMON_LDADD=../avahi-common/libavahi-common.la
 
 # This cool debug trap works on i386/gcc only
@@ -46,8 +45,7 @@ noinst_PROGRAMS = \
        conformance-test \
        avahi-reflector \
        dns-test \
-       timeeventq-test \
-       timeval-test
+       timeeventq-test
 
 libavahi_core_la_SOURCES = \
        timeeventq.c timeeventq.h\
@@ -75,8 +73,8 @@ libavahi_core_la_SOURCES = \
        log.c log.h \
        browse-dns-server.c \
        fdutil.h fdutil.c \
-       timeval.h timeval.c \
        util.c util.h
+
 libavahi_core_la_CFLAGS = $(AM_CFLAGS)
 libavahi_core_la_LIBADD = $(AM_LDADD) $(COMMON_LDADD)
 
@@ -113,17 +111,10 @@ dns_test_LDADD = $(AM_LDADD) $(COMMON_LDADD)
 timeeventq_test_SOURCES = \
        timeeventq-test.c \
        timeeventq.h timeeventq.c \
-       timeval.c timeval.h \
        prioq.h prioq.c
 timeeventq_test_CFLAGS = $(AM_CFLAGS)
 timeeventq_test_LDADD = $(AM_LDADD) $(COMMON_LDADD)
 
-timeval_test_SOURCES = \
-       timeval.c timeval.h \
-       timeval-test.c
-timeval_test_CFLAGS = $(AM_CFLAGS)
-timeval_test_LDADD = $(AM_LDADD)
-
 valgrind: avahi-test
        libtool --mode=execute valgrind ./avahi-test
 
index 3787f8fe18f089cdb0103bdf93e081b8a876a1cb..9a6d505c43f7fb4860b6b93dd4cb6818be14eee9 100644 (file)
@@ -23,8 +23,8 @@
 #include <config.h>
 #endif
 
+#include <avahi-common/timeval.h>
 #include "announce.h"
-#include "timeval.h"
 
 #define AVAHI_ANNOUNCEMENT_JITTER_MSEC 250
 #define AVAHI_PROBE_JITTER_MSEC 250
index 5db2473d327bf16af5cb5df1175a6c7183debec7..7156d06ebf2fe807a176b3a6d3e600c51a451344 100644 (file)
@@ -152,7 +152,7 @@ fail:
     group = NULL;
 }
 
-static void hnr_callback(AvahiHostNameResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *hostname, const AvahiAddress *a, gpointer userdata) {
+static void hnr_callback(AvahiHostNameResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *hostname, const AvahiAddress *a, gpointer userdata) {
     gchar t[64];
 
     if (a)
@@ -161,7 +161,7 @@ static void hnr_callback(AvahiHostNameResolver *r, AvahiIfIndex iface, AvahiProt
     avahi_log_debug("HNR: (%i.%i) <%s> -> %s [%s]", iface, protocol, hostname, a ? t : "n/a", event == AVAHI_RESOLVER_FOUND ? "found" : "timeout");
 }
 
-static void ar_callback(AvahiAddressResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const AvahiAddress *a, const gchar *hostname, gpointer userdata) {
+static void ar_callback(AvahiAddressResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *a, const gchar *hostname, gpointer userdata) {
     gchar t[64];
 
     avahi_address_snprint(t, sizeof(t), a);
@@ -183,7 +183,7 @@ static void sb_callback(AvahiServiceBrowser *b, AvahiIfIndex iface, AvahiProtoco
    avahi_log_debug("SB: (%i.%i) <%s> as %s in <%s> [%s]", iface, protocol, name, service_type, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove");
 }
 
-static void sr_callback(AvahiServiceResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *name, const gchar*service_type, const gchar*domain_name, const gchar*hostname, const AvahiAddress *a, guint16 port, AvahiStringList *txt, gpointer userdata) {
+static void sr_callback(AvahiServiceResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *name, const gchar*service_type, const gchar*domain_name, const gchar*hostname, const AvahiAddress *a, guint16 port, AvahiStringList *txt, gpointer userdata) {
 
     if (event == AVAHI_RESOLVER_TIMEOUT)
         avahi_log_debug("SR: (%i.%i) <%s> as %s in <%s> [timeout]", iface, protocol, name, service_type, domain_name);
index e2d996e2372d366103efa3391a45133d2bd1d1e7..6357095d9edf246b008da910e398bf61ce09862a 100644 (file)
@@ -23,8 +23,8 @@
 #include <config.h>
 #endif
 
+#include <avahi-common/timeval.h>
 #include "browse.h"
-#include "timeval.h"
 #include "log.h"
 
 struct AvahiRecordBrowser {
index dddd5eea8b1eae0f9036bcb8e2cd35c5767e8a82..ebe78fc149cd29b74b94cc8cdddf745b501d4509 100644 (file)
@@ -25,7 +25,7 @@
 
 #include <string.h>
 
-#include "timeval.h"
+#include <avahi-common/timeval.h>
 #include "cache.h"
 #include "log.h"
 
@@ -215,10 +215,10 @@ static void update_time_event(AvahiCache *c, AvahiCacheEntry *e) {
 }
 
 static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent) {
+    AvahiUsec usec;
     g_assert(c);
     g_assert(e);
     g_assert(percent > 0 && percent <= 100);
-    AvahiUsec usec;
 /*     gchar *txt; */
 
     usec = ((AvahiUsec) e->record->ttl) * 10000;
index 7a4c601fbd65abd51412834d92fd5c73d6cc2e29..c14a761ba7c6df734b9107e599c711f66c422994 100644 (file)
@@ -30,8 +30,8 @@
 #include <unistd.h>
 #include <stdio.h>
 
+#include <avahi-common/alternative.h>
 #include "core.h"
-#include "alternative.h"
 #include "log.h"
 
 static gchar *name = NULL;
index cb763591fe6630367bc9f0815748d2b3f041b262..47c54bad5f54285f006b836b54b6d2f4a35e4807 100644 (file)
@@ -30,7 +30,7 @@ typedef struct AvahiInterface AvahiInterface;
 typedef struct AvahiHwInterface AvahiHwInterface;
 
 #include <avahi-common/llist.h>
-#include "address.h"
+#include <avahi-common/address.h>
 #include "server.h"
 #include "netlink.h"
 #include "cache.h"
index d7f9ca0de36be947d7635aee8696a26f4b415486..a375c7beeb217c5a3052ca30958bf1e7513f3adf 100644 (file)
@@ -24,9 +24,9 @@
 #endif
 
 #include <avahi-common/domain.h>
+#include <avahi-common/timeval.h>
 
 #include "probe-sched.h"
-#include "timeval.h"
 #include "log.h"
 
 #define AVAHI_PROBE_HISTORY_MSEC 150
index 07933a7619857ef46fa4892c90c701cfb26715de..aae0f3a72017eb5152f18f9f12ba027a2bf13ea2 100644 (file)
@@ -24,8 +24,8 @@
 
 typedef struct AvahiProbeScheduler AvahiProbeScheduler;
 
+#include <avahi-common/address.h>
 #include "iface.h"
-#include "address.h"
 
 AvahiProbeScheduler *avahi_probe_scheduler_new(AvahiInterface *i);
 void avahi_probe_scheduler_free(AvahiProbeScheduler *s);
index 6e9234fa4dd19ac5ea53456e3c9af8271b03a07c..9a26dba09879ad9e7e699e105362bff370ffd3d4 100644 (file)
@@ -23,8 +23,8 @@
 #include <config.h>
 #endif
 
+#include <avahi-common/timeval.h>
 #include "query-sched.h"
-#include "timeval.h"
 
 #define AVAHI_QUERY_HISTORY_MSEC 100
 #define AVAHI_QUERY_DEFER_MSEC 100
index b77363c11567bb0c6ec012af43950a48466d9322..2c373b1d27ff59b2aea8d3e185c8933ca77980f7 100644 (file)
@@ -24,8 +24,8 @@
 
 typedef struct AvahiQueryScheduler AvahiQueryScheduler;
 
+#include <avahi-common/address.h>
 #include "iface.h"
-#include "address.h"
 
 AvahiQueryScheduler *avahi_query_scheduler_new(AvahiInterface *i);
 void avahi_query_scheduler_free(AvahiQueryScheduler *s);
index fa50c39742f72d547bac3a896c1915d03e47203c..6bd7cf2780b96606cb486f4378c477c9c3ce153e 100644 (file)
@@ -23,8 +23,8 @@
 #include <config.h>
 #endif
 
+#include <avahi-common/timeval.h>
 #include "browse.h"
-#include "timeval.h"
 
 struct AvahiAddressResolver {
     AvahiServer *server;
index f50ddb4b154b51ab918a89d9995ed8104bdeaea5..50fbcf838f4eb0adbca0712a0b9a55ce93e764b0 100644 (file)
@@ -24,7 +24,7 @@
 #endif
 
 #include <avahi-common/domain.h>
-#include "timeval.h"
+#include <avahi-common/timeval.h>
 #include "browse.h"
 
 struct AvahiHostNameResolver {
index bf4f7b94e4f7f06556955124c56a06927d715cec..0e4e119fbc01ab2def3e5b9487a21b4a1dc1fda4 100644 (file)
@@ -26,8 +26,8 @@
 #include <string.h>
 
 #include <avahi-common/domain.h>
+#include <avahi-common/timeval.h>
 #include "browse.h"
-#include "timeval.h"
 
 struct AvahiServiceResolver {
     AvahiServer *server;
index 6027287dae19f68eaa93133c5af966dde337d681..efb667763966421ddb251f8a1bf37f6641329343 100644 (file)
@@ -23,8 +23,8 @@
 #include <config.h>
 #endif
 
+#include <avahi-common/timeval.h>
 #include "response-sched.h"
-#include "timeval.h"
 #include "log.h"
 
 #define AVAHI_RESPONSE_HISTORY_MSEC 500
index 10fd821cd7b1ced7db3bb58f3549cae635a34e46..3760a442d4cb6af723d95610c303897b397d6ef8 100644 (file)
@@ -24,8 +24,8 @@
 
 typedef struct AvahiResponseScheduler AvahiResponseScheduler;
 
+#include <avahi-common/address.h>
 #include "iface.h"
-#include "address.h"
 
 AvahiResponseScheduler *avahi_response_scheduler_new(AvahiInterface *i);
 void avahi_response_scheduler_free(AvahiResponseScheduler *s);
index d9137385dd91fcd48b593fb7c8faf56eed8b31f4..241932fbd869be29714469916914f0d8008d336c 100644 (file)
@@ -32,9 +32,9 @@
 #include <stdio.h>
 
 #include <avahi-common/domain.h>
+#include <avahi-common/timeval.h>
 
 #include "server.h"
-#include "timeval.h"
 #include "iface.h"
 #include "socket.h"
 #include "browse.h"
@@ -459,9 +459,9 @@ void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsP
                         reply = avahi_dns_packet_new_reply(p, size, FALSE, TRUE);
 
                         if (!avahi_dns_packet_append_record(reply, r, flush_cache, 0)) {
+                            gchar *t;
                             avahi_dns_packet_free(reply);
-                            
-                            gchar *t = avahi_record_to_string(r);
+                            t = avahi_record_to_string(r);
                             avahi_log_warn("Record [%s] too large, doesn't fit in any packet!", t);
                             g_free(t);
                             break;
index 6ac55b8b1e9572b710073d290d1d5d1cd6a44d6b..613d4382f549209a69ca2bd763faa0e24dd4b3f6 100644 (file)
@@ -25,8 +25,8 @@
 
 #include <glib.h>
 
+#include <avahi-common/timeval.h>
 #include "timeeventq.h"
-#include "timeval.h"
 
 static AvahiTimeEventQueue *q = NULL;
 
index d466cd43f4eeb5e3e989a39a7d5499632e957fd7..88e17792e09436f8cf43d3c7fcb2e7a1d80e3dbb 100644 (file)
@@ -23,8 +23,8 @@
 #include <config.h>
 #endif
 
+#include <avahi-common/timeval.h>
 #include "timeeventq.h"
-#include "timeval.h"
 
 static gint compare(gconstpointer _a, gconstpointer _b) {
     const AvahiTimeEvent *a = _a,  *b = _b;
diff --git a/avahi-core/timeval-test.c b/avahi-core/timeval-test.c
deleted file mode 100644 (file)
index 7dedf41..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-/* $Id$ */
-
-/***
-  This file is part of avahi.
-  avahi is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as
-  published by the Free Software Foundation; either version 2.1 of the
-  License, or (at your option) any later version.
-  avahi is distributed in the hope that it will be useful, but WITHOUT
-  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
-  Public License for more details.
-  You should have received a copy of the GNU Lesser General Public
-  License along with avahi; if not, write to the Free Software
-  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
-  USA.
-***/
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <stdio.h>
-#include "timeval.h"
-
-int main(int argc, char *argv[]) {
-
-    struct timeval a = { 5, 5 }, b;
-
-    b = a;
-
-    printf("%li.%li\n", a.tv_sec, a.tv_usec);
-    avahi_timeval_add(&a, -50);
-
-    printf("%li.%li\n", a.tv_sec, a.tv_usec);
-
-    printf("%lli\n", avahi_timeval_diff(&a, &b));
-}
diff --git a/avahi-core/timeval.c b/avahi-core/timeval.c
deleted file mode 100644 (file)
index e5732cd..0000000
+++ /dev/null
@@ -1,101 +0,0 @@
-/* $Id$ */
-
-/***
-  This file is part of avahi.
-  avahi is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as
-  published by the Free Software Foundation; either version 2.1 of the
-  License, or (at your option) any later version.
-  avahi is distributed in the hope that it will be useful, but WITHOUT
-  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
-  Public License for more details.
-  You should have received a copy of the GNU Lesser General Public
-  License along with avahi; if not, write to the Free Software
-  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
-  USA.
-***/
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <stdlib.h>
-#include <assert.h>
-
-#include "timeval.h"
-
-int avahi_timeval_compare(const struct timeval *a, const struct timeval *b) {
-    assert(a);
-    assert(b);
-
-    if (a->tv_sec < b->tv_sec)
-        return -1;
-
-    if (a->tv_sec > b->tv_sec)
-        return 1;
-
-    if (a->tv_usec < b->tv_usec)
-        return -1;
-
-    if (a->tv_usec > b->tv_usec)
-        return 1;
-
-    return 0;
-}
-
-AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b) {
-    assert(a);
-    assert(b);
-
-    if (avahi_timeval_compare(a, b) < 0)
-        return - avahi_timeval_diff(b, a);
-
-    return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
-}
-
-struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec) {
-    AvahiUsec u;
-    assert(a);
-
-    u = usec + a->tv_usec;
-
-    if (u < 0) {
-        a->tv_usec = (long) (1000000 + (u % 1000000));
-        a->tv_sec += (long) (-1 + (u / 1000000));
-    } else {
-        a->tv_usec = (long) (u % 1000000);
-        a->tv_sec += (long) (u / 1000000);
-    }
-
-    return a;
-}
-
-AvahiUsec avahi_age(const struct timeval *a) {
-    struct timeval now;
-    
-    assert(a);
-
-    gettimeofday(&now, NULL);
-
-    return avahi_timeval_diff(&now, a);
-}
-
-
-struct timeval *avahi_elapse_time(struct timeval *tv, unsigned msec, unsigned jitter) {
-    assert(tv);
-
-    gettimeofday(tv, NULL);
-
-    if (msec)
-        avahi_timeval_add(tv, (AvahiUsec) msec*1000);
-
-    if (jitter)
-        avahi_timeval_add(tv, (AvahiUsec) (jitter*1000.0*rand()/(RAND_MAX+1.0)));
-        
-    return tv;
-}
-
diff --git a/avahi-core/timeval.h b/avahi-core/timeval.h
deleted file mode 100644 (file)
index c046a19..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-#ifndef footimevalhfoo
-#define footimevalhfoo
-
-/* $Id$ */
-
-/***
-  This file is part of avahi.
-  avahi is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as
-  published by the Free Software Foundation; either version 2.1 of the
-  License, or (at your option) any later version.
-  avahi is distributed in the hope that it will be useful, but WITHOUT
-  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
-  Public License for more details.
-  You should have received a copy of the GNU Lesser General Public
-  License along with avahi; if not, write to the Free Software
-  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
-  USA.
-***/
-
-#include <inttypes.h>
-#include <sys/time.h>
-
-#include <avahi-common/cdecl.h>
-
-AVAHI_C_DECL_BEGIN
-
-typedef int64_t AvahiUsec;
-
-int avahi_timeval_compare(const struct timeval *a, const struct timeval *b);
-AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b);
-struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec);
-
-AvahiUsec avahi_age(const struct timeval *a);
-struct timeval *avahi_elapse_time(struct timeval *tv, unsigned msec, unsigned jitter);
-
-AVAHI_C_DECL_END
-
-#endif
diff --git a/avahi-glib/Makefile.am b/avahi-glib/Makefile.am
new file mode 100644 (file)
index 0000000..6cfc46e
--- /dev/null
@@ -0,0 +1,57 @@
+# $Id$
+#
+# This file is part of avahi.
+# 
+# avahi is free software; you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
+#
+# avahi is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+# License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with avahi; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+# USA.
+
+AM_CFLAGS=-I$(top_srcdir)
+
+# GLIB 2.0
+AM_CFLAGS+=$(GLIB20_CFLAGS)
+AM_LDADD=$(GLIB20_LIBS)
+
+# Import stuff from avahi-common
+COMMON_LDADD=../avahi-common/libavahi-common.la
+
+# This cool debug trap works on i386/gcc only
+AM_CFLAGS+='-DDEBUG_TRAP=__asm__("int $$3")'
+
+avahiincludedir=$(includedir)/avahi-glib
+
+avahiinclude_HEADERS = \
+       glib-watch.h
+
+lib_LTLIBRARIES = \
+       libavahi-glib.la 
+
+noinst_PROGRAMS = \
+       glib-watch-test
+
+libavahi_glib_la_SOURCES = \
+       glib-watch.c glib-watch.h
+libavahi_glib_la_CFLAGS = $(AM_CFLAGS)
+libavahi_glib_la_LIBADD = $(AM_LDADD) $(COMMON_LDADD)
+
+glib_watch_test_SOURCES = \
+       glib-watch.c glib-watch.h \
+       glib-watch-test.c
+glib_watch_test_CFLAGS = $(AM_CFLAGS)
+glib_watch_test_LDADD = $(AM_LDADD) $(COMMON_LDADD)
+
+
+
+
+
diff --git a/avahi-glib/glib-watch-test.c b/avahi-glib/glib-watch-test.c
new file mode 100644 (file)
index 0000000..5072321
--- /dev/null
@@ -0,0 +1,87 @@
+/* $Id$ */
+
+/***
+  This file is part of avahi.
+  avahi is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as
+  published by the Free Software Foundation; either version 2.1 of the
+  License, or (at your option) any later version.
+  avahi is distributed in the hope that it will be useful, but WITHOUT
+  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+  Public License for more details.
+  You should have received a copy of the GNU Lesser General Public
+  License along with avahi; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <unistd.h>
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+
+#include <avahi-common/watch.h>
+#include <avahi-common/timeval.h>
+#include "glib-watch.h"
+
+static AvahiPoll *api = NULL;
+static GMainLoop *loop = NULL;
+
+static void callback(AvahiWatch *w, int fd, AvahiWatchEvent event, void *userdata) {
+
+    if (event & AVAHI_WATCH_IN) {
+        ssize_t r;
+        char c;
+        
+        if ((r = read(fd, &c, 1)) <= 0) {
+            fprintf(stderr, "read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
+            api->watch_free(w);
+            return;
+        }
+
+        printf("Read: %c\n", c >= 32 && c < 127 ? c : '.');
+    }
+}
+
+static void iteration(AvahiGLibPoll *p, void *userdata) {
+    struct timeval tv;
+    static int i = 0;
+
+    printf("Iteration %i\n", i++);
+
+    if (i > 100)
+        g_main_loop_quit(loop);
+
+    avahi_elapse_time(&tv, 1000, 0);
+    api->set_wakeup_time(api, &tv);
+}
+
+int main(int argc, char *argv[]) {
+    AvahiGLibPoll *s;
+    struct timeval tv;
+    
+    s = avahi_glib_poll_new(NULL, iteration, NULL);
+    assert(s);
+
+    api = avahi_glib_poll_get(s);
+
+    api->watch_new(api, 0, AVAHI_WATCH_IN, callback, NULL);
+
+    avahi_elapse_time(&tv, 1000, 0);
+    api->set_wakeup_time(api, &tv);
+
+    loop = g_main_loop_new(NULL, FALSE);
+    g_main_loop_run(loop);
+    g_main_loop_unref(loop);
+    
+    return 0;
+}
diff --git a/avahi-glib/glib-watch.c b/avahi-glib/glib-watch.c
new file mode 100644 (file)
index 0000000..7eab84b
--- /dev/null
@@ -0,0 +1,261 @@
+/* $Id$ */
+
+/***
+  This file is part of avahi.
+  avahi is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as
+  published by the Free Software Foundation; either version 2.1 of the
+  License, or (at your option) any later version.
+  avahi is distributed in the hope that it will be useful, but WITHOUT
+  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+  Public License for more details.
+  You should have received a copy of the GNU Lesser General Public
+  License along with avahi; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <avahi-common/llist.h>
+#include <avahi-common/malloc.h>
+
+#include "glib-watch.h"
+
+struct AvahiWatch {
+    AvahiGLibPoll *glib_poll;
+    int dead;
+    GPollFD pollfd;
+    int pollfd_added;
+    AvahiWatchCallback callback;
+    void *userdata;
+
+    AVAHI_LLIST_FIELDS(AvahiWatch, watches);
+};
+
+struct AvahiGLibPoll {
+    GSource source;
+    AvahiPoll api;
+    GMainContext *context;
+
+    struct timeval wakeup;
+    gboolean use_wakeup;
+    int req_cleanup;
+    
+    AvahiGLibProcessCallback process_callback;
+    void *userdata;
+    
+    AVAHI_LLIST_HEAD(AvahiWatch, watches);
+};
+
+static void destroy_watch(AvahiWatch *w) {
+    assert(w);
+
+    if (w->pollfd_added)
+        g_source_remove_poll(&w->glib_poll->source, &w->pollfd);
+
+    AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->glib_poll->watches, w);
+
+    avahi_free(w);
+}
+
+static void cleanup(AvahiGLibPoll *g, int all) {
+    AvahiWatch *w, *next;
+    assert(g);
+
+    for (w = g->watches; w; w = next) {
+        next = w->watches_next;
+
+        if (all || w->dead)
+            destroy_watch(w);
+    }
+
+    g->req_cleanup = 0;
+}
+
+static AvahiWatch* watch_new(AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
+    AvahiWatch *w;
+    AvahiGLibPoll *g;
+    
+    assert(api);
+    assert(fd >= 0);
+    assert(callback);
+
+    g = api->userdata;
+    assert(g);
+
+    if (!(w = avahi_new(AvahiWatch, 1)))
+        return NULL;
+    
+    w->glib_poll = g;
+    w->pollfd.fd = fd;
+    w->pollfd.events =
+        (event & AVAHI_WATCH_IN ? G_IO_IN : 0) |
+        (event & AVAHI_WATCH_OUT ? G_IO_OUT : 0) |
+        (event & AVAHI_WATCH_ERR ? G_IO_ERR : 0) |
+        (event & AVAHI_WATCH_HUP ? G_IO_HUP : 0);
+        ;
+    w->callback = callback;
+    w->userdata = userdata;
+    w->dead = 0;
+
+    g_source_add_poll(&g->source, &w->pollfd);
+    w->pollfd_added = 1;
+
+    AVAHI_LLIST_PREPEND(AvahiWatch, watches, g->watches, w);
+
+    return w;
+}
+
+static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
+    assert(w);
+    assert(!w->dead);
+
+    w->pollfd.events = events;
+}
+
+static void watch_free(AvahiWatch *w) {
+    assert(w);
+    assert(!w->dead);
+
+    if (w->pollfd_added) {
+        g_source_remove_poll(&w->glib_poll->source, &w->pollfd);
+        w->pollfd_added = 0;
+    }
+    
+    w->dead = 1;
+    w->glib_poll->req_cleanup = 1;
+}
+
+static void set_wakeup_time(AvahiPoll *api, const struct timeval *tv) {
+    AvahiGLibPoll *g;
+
+    assert(api);
+    g = api->userdata;
+
+    if (tv) {
+        g->wakeup = *tv;
+        g->use_wakeup = 1;
+    } else
+        g->use_wakeup = 0;
+}
+
+static gboolean prepare_func(GSource *source, gint *timeout) {
+    AvahiGLibPoll *g = (AvahiGLibPoll*) source;
+
+    g_assert(g);
+    g_assert(timeout);
+
+    if (g->use_wakeup) {
+        GTimeVal now;
+        struct timeval tvnow;
+        AvahiUsec usec;
+
+        g_source_get_current_time(source, &now);
+        tvnow.tv_sec = now.tv_sec;
+        tvnow.tv_usec = now.tv_usec;
+    
+        usec = avahi_timeval_diff(&g->wakeup, &tvnow);
+
+        if (usec <= 0)
+            return TRUE;
+
+        *timeout = (gint) (usec / 1000);
+    }
+        
+    return FALSE;
+}
+
+static gboolean check_func(GSource *source) {
+    AvahiGLibPoll *g = (AvahiGLibPoll*) source;
+    AvahiWatch *w;
+
+    g_assert(g);
+
+    for (w = g->watches; w; w = w->watches_next)
+        if (w->pollfd.revents > 0)
+            return TRUE;
+    
+    return FALSE;
+}
+
+static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer userdata) {
+    AvahiGLibPoll* g = (AvahiGLibPoll*) source;
+    AvahiWatch *w;
+    
+    g_assert(g);
+
+    if (g->req_cleanup)
+        cleanup(g, 0);
+    
+    if (g->process_callback)
+        g->process_callback(g, g->userdata);
+    
+    for (w = g->watches; w; w = w->watches_next)
+        if (w->pollfd.revents > 0) {
+            assert(w->callback);
+            w->callback(w, w->pollfd.fd, w->pollfd.revents, w->userdata);
+            w->pollfd.revents = 0;
+        }
+
+    if (g->req_cleanup)
+        cleanup(g, 0);
+    
+    return TRUE;
+}
+
+AvahiGLibPoll *avahi_glib_poll_new(GMainContext *context, AvahiGLibProcessCallback callback, void *userdata) {
+    AvahiGLibPoll *g;
+    
+    static GSourceFuncs source_funcs = {
+        prepare_func,
+        check_func,
+        dispatch_func,
+        NULL,
+        NULL,
+        NULL
+    };
+
+    g = (AvahiGLibPoll*) g_source_new(&source_funcs, sizeof(AvahiGLibPoll));
+    g_main_context_ref(g->context = context ? context : g_main_context_default());
+
+    g->api.userdata = g;
+    g->api.watch_new = watch_new;
+    g->api.watch_free = watch_free;
+    g->api.watch_update = watch_update;
+    g->api.set_wakeup_time = set_wakeup_time;
+
+    g->use_wakeup = 0;
+    g->process_callback = callback;
+    g->userdata = userdata;
+    g->req_cleanup = 0;
+    
+    AVAHI_LLIST_HEAD_INIT(AvahiWatch, g->watches);
+    
+    g_source_attach(&g->source, g->context);
+
+    return g;
+}
+
+void avahi_glib_poll_free(AvahiGLibPoll *g) {
+    GSource *s = &g->source;
+    assert(g);
+
+    cleanup(g, 1);
+    
+    g_main_context_unref(g->context);
+    g_source_destroy(s);
+    g_source_unref(s);
+}
+
+AvahiPoll* avahi_glib_poll_get(AvahiGLibPoll *g) {
+    assert(g);
+
+    return &g->api;
+}
diff --git a/avahi-glib/glib-watch.h b/avahi-glib/glib-watch.h
new file mode 100644 (file)
index 0000000..9df58b5
--- /dev/null
@@ -0,0 +1,43 @@
+#ifndef fooglibwatchhfoo
+#define fooglibwatchhfoo
+
+/* $Id$ */
+
+/***
+  This file is part of avahi.
+  avahi is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as
+  published by the Free Software Foundation; either version 2.1 of the
+  License, or (at your option) any later version.
+  avahi is distributed in the hope that it will be useful, but WITHOUT
+  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+  Public License for more details.
+  You should have received a copy of the GNU Lesser General Public
+  License along with avahi; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#include <avahi-common/cdecl.h>
+
+#include <glib.h>
+#include <avahi-common/watch.h>
+
+AVAHI_C_DECL_BEGIN
+
+typedef struct AvahiGLibPoll AvahiGLibPoll;
+
+typedef void (*AvahiGLibProcessCallback)(AvahiGLibPoll *g, void *userdata);
+
+AvahiGLibPoll *avahi_glib_poll_new(GMainContext *context, AvahiGLibProcessCallback callback, void *userdata);
+void avahi_glib_poll_free(AvahiGLibPoll *g);
+
+AvahiPoll* avahi_glib_poll_get(AvahiGLibPoll *g);
+
+AVAHI_C_DECL_END
+
+#endif
index f27a959a1fcaa60e9b2a9d31b9ac3e14173f9845..6278af64c0e1a5b50a34720a253e33d27c02e6c2 100644 (file)
@@ -279,6 +279,7 @@ Makefile
 avahi-core.pc 
 avahi-common/Makefile 
 avahi-core/Makefile 
+avahi-glib/Makefile 
 avahi-daemon/Makefile 
 avahi-daemon/avahi-dbus.conf
 avahi-discover-standalone/Makefile 
index 5ce60ce5f90cbe070e2c2141db5abbdec27aa131..6f7446a1f71ee3b7982414b77fd659f1457f529b 100644 (file)
--- a/docs/TODO
+++ b/docs/TODO
@@ -1,4 +1,7 @@
 todo:
+* drop glib
+* allow srv port == 0
+* deal with no local interface
 * release!
 
 later: