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