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 <event2/event.h>
31 
32 #include <avahi-common/watch.h>
33 #include <avahi-common/timeval.h>
34 #include <avahi-common/gccmacro.h>
35 
36 #include "libevent-watch.h"
37 
38 static const AvahiPoll *api = NULL;
39 static struct event_base *base = NULL;
40 
callback(AvahiWatch * w,int fd,AvahiWatchEvent event,AVAHI_GCC_UNUSED void * userdata)41 static void callback(AvahiWatch *w, int fd, AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata)
42 {
43     if (event & AVAHI_WATCH_IN) {
44         ssize_t r;
45         char c;
46 
47         if ((r = read(fd, &c, 1)) <= 0) {
48             fprintf(stderr, "read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
49             api->watch_free(w);
50             return;
51         }
52 
53         printf("Read: %c\n", c >= 32 && c < 127 ? c : '.');
54     }
55 }
56 
wakeup(AvahiTimeout * t,AVAHI_GCC_UNUSED void * userdata)57 static void wakeup(AvahiTimeout *t, AVAHI_GCC_UNUSED void *userdata)
58 {
59     struct timeval tv;
60     static unsigned i = 0;
61 
62     printf("Wakeup #%u\n", i++);
63 
64     if (i > 10)
65 	event_base_loopbreak(base);
66 
67     avahi_elapse_time(&tv, 1000, 0);
68     api->timeout_update(t, &tv);
69 }
70 
main(AVAHI_GCC_UNUSED int argc,AVAHI_GCC_UNUSED char * argv[])71 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[])
72 {
73     AvahiLibeventPoll *ep;
74     struct timeval tv;
75 
76     base = event_base_new();
77     assert(base);
78 
79     ep = avahi_libevent_poll_new(base);
80     assert(ep);
81 
82     api = avahi_libevent_poll_get(ep);
83 
84     api->watch_new(api, 0, AVAHI_WATCH_IN, callback, NULL);
85 
86     avahi_elapse_time(&tv, 1000, 0);
87     api->timeout_new(api, &tv, wakeup, NULL);
88 
89     event_base_dispatch(base);
90 
91     avahi_libevent_poll_free(ep);
92 
93     event_base_free(base);
94 
95     return 0;
96 }
97