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