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 "watch.h"
31 #include "timeval.h"
32 #include "gccmacro.h"
33
34 static const AvahiPoll *api = NULL;
35
36 #ifndef USE_THREAD
37 #include "simple-watch.h"
38 static AvahiSimplePoll *simple_poll = NULL;
39 #else
40 #include "thread-watch.h"
41 static AvahiThreadedPoll *threaded_poll = NULL;
42 #endif
43
callback(AvahiWatch * w,int fd,AvahiWatchEvent event,AVAHI_GCC_UNUSED void * userdata)44 static void callback(AvahiWatch *w, int fd, AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
45
46 if (event & AVAHI_WATCH_IN) {
47 ssize_t r;
48 char c;
49
50 if ((r = read(fd, &c, 1)) <= 0) {
51 fprintf(stderr, "read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
52 api->watch_free(w);
53 return;
54 }
55
56 printf("Read: %c\n", c >= 32 && c < 127 ? c : '.');
57 }
58 }
59
wakeup(AvahiTimeout * t,AVAHI_GCC_UNUSED void * userdata)60 static void wakeup(AvahiTimeout *t, AVAHI_GCC_UNUSED void *userdata) {
61 static int i = 0;
62 struct timeval tv;
63
64 printf("Wakeup #%i\n", i++);
65
66 if (i > 10) {
67 #ifndef USE_THREAD
68 avahi_simple_poll_quit(simple_poll);
69 #else
70 avahi_threaded_poll_quit(threaded_poll);
71 #endif
72 } else {
73 avahi_elapse_time(&tv, 1000, 0);
74 api->timeout_update(t, &tv);
75 }
76 }
77
main(AVAHI_GCC_UNUSED int argc,AVAHI_GCC_UNUSED char * argv[])78 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
79 struct timeval tv;
80
81 #ifndef USE_THREAD
82 simple_poll = avahi_simple_poll_new();
83 assert(simple_poll);
84 api = avahi_simple_poll_get(simple_poll);
85 assert(api);
86 #else
87 threaded_poll = avahi_threaded_poll_new();
88 assert(threaded_poll);
89 api = avahi_threaded_poll_get(threaded_poll);
90 assert(api);
91 #endif
92
93 api->watch_new(api, 0, AVAHI_WATCH_IN, callback, NULL);
94
95 avahi_elapse_time(&tv, 1000, 0);
96 api->timeout_new(api, &tv, wakeup, NULL);
97
98 #ifndef USE_THREAD
99 /* Our main loop */
100 avahi_simple_poll_loop(simple_poll);
101 avahi_simple_poll_free(simple_poll);
102
103 #else
104 avahi_threaded_poll_start(threaded_poll);
105
106 fprintf(stderr, "Now doing some stupid stuff ...\n");
107 sleep(20);
108 fprintf(stderr, "... stupid stuff is done.\n");
109
110 avahi_threaded_poll_free(threaded_poll);
111
112 #endif
113
114 return 0;
115 }
116