]> git.meshlink.io Git - catta/blob - avahi-common/watch-test.c
improve strlst test
[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 #include "gccmacro.h"
36
37 static const AvahiPoll *api = NULL;
38 static AvahiSimplePoll *simple_poll = NULL;
39
40 static void callback(AvahiWatch *w, int fd, AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
41
42     if (event & AVAHI_WATCH_IN) {
43         ssize_t r;
44         char c;
45         
46         if ((r = read(fd, &c, 1)) <= 0) {
47             fprintf(stderr, "read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
48             api->watch_free(w);
49             return;
50         }
51
52         printf("Read: %c\n", c >= 32 && c < 127 ? c : '.');
53     }
54 }
55
56 static void wakeup(AvahiTimeout *t, AVAHI_GCC_UNUSED void *userdata) {
57     static int i = 0;
58     struct timeval tv;
59
60     printf("Wakeup #%i\n", i++);
61
62     if (i > 10)
63         avahi_simple_poll_quit(simple_poll);
64     else {
65         avahi_elapse_time(&tv, 1000, 0);
66         api->timeout_update(t, &tv);
67     }
68 }
69
70 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
71     struct timeval tv;
72     
73     simple_poll = avahi_simple_poll_new();
74
75     api = avahi_simple_poll_get(simple_poll);
76     api->watch_new(api, 0, AVAHI_WATCH_IN, callback, NULL);
77
78     avahi_elapse_time(&tv, 1000, 0);
79     api->timeout_new(api, &tv, wakeup, NULL);
80
81     /* Our main loop */
82     avahi_simple_poll_loop(simple_poll);
83
84
85     avahi_simple_poll_free(simple_poll);
86     
87     return 0;
88 }