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 <avahi-common/watch.h> 33#include <avahi-common/timeval.h> 34#include <avahi-common/gccmacro.h> 35 36#include "glib-watch.h" 37 38static const AvahiPoll *api = NULL; 39static GMainLoop *loop = NULL; 40 41static 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 57static void wakeup(AvahiTimeout *t, AVAHI_GCC_UNUSED void *userdata) { 58 struct timeval tv; 59 static int i = 0; 60 61 printf("Wakeup #%i\n", i++); 62 63 if (i > 10) 64 g_main_loop_quit(loop); 65 66 avahi_elapse_time(&tv, 1000, 0); 67 api->timeout_update(t, &tv); 68} 69 70int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) { 71 AvahiGLibPoll *g; 72 struct timeval tv; 73 74 g = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT); 75 assert(g); 76 77 api = avahi_glib_poll_get(g); 78 79 api->watch_new(api, 0, AVAHI_WATCH_IN, callback, NULL); 80 81 avahi_elapse_time(&tv, 1000, 0); 82 api->timeout_new(api, &tv, wakeup, NULL); 83 84 loop = g_main_loop_new(NULL, FALSE); 85 g_main_loop_run(loop); 86 g_main_loop_unref(loop); 87 88 avahi_glib_poll_free(g); 89 90 return 0; 91} 92