]> git.meshlink.io Git - catta/blob - avahi-glib/glib-watch-test.c
53c668329859449f148eecade32ba88be6e5b453
[catta] / avahi-glib / glib-watch-test.c
1 /***
2   This file is part of avahi.
3
4   avahi is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8
9   avahi is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with avahi; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include <string.h>
29
30 #include <avahi-common/watch.h>
31 #include <avahi-common/timeval.h>
32 #include <avahi-common/gccmacro.h>
33
34 #include "glib-watch.h"
35
36 static const AvahiPoll *api = NULL;
37 static GMainLoop *loop = NULL;
38
39 static void callback(AvahiWatch *w, int fd, AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
40
41     if (event & AVAHI_WATCH_IN) {
42         ssize_t r;
43         char c;
44
45         if ((r = read(fd, &c, 1)) <= 0) {
46             fprintf(stderr, "read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
47             api->watch_free(w);
48             return;
49         }
50
51         printf("Read: %c\n", c >= 32 && c < 127 ? c : '.');
52     }
53 }
54
55 static void wakeup(AvahiTimeout *t, AVAHI_GCC_UNUSED void *userdata) {
56     struct timeval tv;
57     static int i = 0;
58
59     printf("Wakeup #%i\n", i++);
60
61     if (i > 10)
62         g_main_loop_quit(loop);
63
64     avahi_elapse_time(&tv, 1000, 0);
65     api->timeout_update(t, &tv);
66 }
67
68 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
69     AvahiGLibPoll *g;
70     struct timeval tv;
71
72     g = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT);
73     assert(g);
74
75     api = avahi_glib_poll_get(g);
76
77     api->watch_new(api, 0, AVAHI_WATCH_IN, callback, NULL);
78
79     avahi_elapse_time(&tv, 1000, 0);
80     api->timeout_new(api, &tv, wakeup, NULL);
81
82     loop = g_main_loop_new(NULL, FALSE);
83     g_main_loop_run(loop);
84     g_main_loop_unref(loop);
85
86     avahi_glib_poll_free(g);
87
88     return 0;
89 }