]> git.meshlink.io Git - catta/blob - tests/querier-test.c
move tests into their own directory
[catta] / tests / querier-test.c
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 <stdlib.h>
25 #include <assert.h>
26
27 #include <avahi/malloc.h>
28 #include <avahi/simple-watch.h>
29 #include <avahi/alternative.h>
30 #include <avahi/timeval.h>
31
32 #include <avahi/core.h>
33 #include <avahi/log.h>
34 #include <avahi/publish.h>
35 #include <avahi/lookup.h>
36
37 #define DOMAIN NULL
38 #define SERVICE_TYPE "_http._tcp"
39
40 static AvahiSServiceBrowser *service_browser1 = NULL, *service_browser2 = NULL;
41 static const AvahiPoll * poll_api = NULL;
42 static AvahiServer *server = NULL;
43 static AvahiSimplePoll *simple_poll;
44
45 static const char *browser_event_to_string(AvahiBrowserEvent event) {
46     switch (event) {
47         case AVAHI_BROWSER_NEW : return "NEW";
48         case AVAHI_BROWSER_REMOVE : return "REMOVE";
49         case AVAHI_BROWSER_CACHE_EXHAUSTED : return "CACHE_EXHAUSTED";
50         case AVAHI_BROWSER_ALL_FOR_NOW : return "ALL_FOR_NOW";
51         case AVAHI_BROWSER_FAILURE : return "FAILURE";
52     }
53
54     abort();
55 }
56
57 static void sb_callback(
58     AvahiSServiceBrowser *b,
59     AvahiIfIndex iface,
60     AvahiProtocol protocol,
61     AvahiBrowserEvent event,
62     const char *name,
63     const char *service_type,
64     const char *domain,
65     AvahiLookupResultFlags flags,
66     AVAHI_GCC_UNUSED void* userdata) {
67     avahi_log_debug("SB%i: (%i.%s) <%s> as <%s> in <%s> [%s] cached=%i", b == service_browser1 ? 1 : 2, iface, avahi_proto_to_string(protocol), name, service_type, domain, browser_event_to_string(event), !!(flags & AVAHI_LOOKUP_RESULT_CACHED));
68 }
69
70 static void create_second_service_browser(AvahiTimeout *timeout, AVAHI_GCC_UNUSED void* userdata) {
71
72     service_browser2 = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, SERVICE_TYPE, DOMAIN, 0, sb_callback, NULL);
73     assert(service_browser2);
74
75     poll_api->timeout_free(timeout);
76 }
77
78 static void quit(AVAHI_GCC_UNUSED AvahiTimeout *timeout, AVAHI_GCC_UNUSED void *userdata) {
79     avahi_simple_poll_quit(simple_poll);
80 }
81
82 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
83     struct timeval tv;
84     AvahiServerConfig config;
85
86     simple_poll = avahi_simple_poll_new();
87     assert(simple_poll);
88
89     poll_api = avahi_simple_poll_get(simple_poll);
90     assert(poll_api);
91
92     avahi_server_config_init(&config);
93     config.publish_hinfo = 0;
94     config.publish_addresses = 0;
95     config.publish_workstation = 0;
96     config.publish_domain = 0;
97
98     avahi_address_parse("192.168.50.1", AVAHI_PROTO_UNSPEC, &config.wide_area_servers[0]);
99     config.n_wide_area_servers = 1;
100     config.enable_wide_area = 1;
101
102     server = avahi_server_new(poll_api, &config, NULL, NULL, NULL);
103     assert(server);
104     avahi_server_config_free(&config);
105
106     service_browser1 = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, SERVICE_TYPE, DOMAIN, 0, sb_callback, NULL);
107     assert(service_browser1);
108
109     poll_api->timeout_new(poll_api, avahi_elapse_time(&tv, 10000, 0), create_second_service_browser, NULL);
110
111     poll_api->timeout_new(poll_api, avahi_elapse_time(&tv, 60000, 0), quit, NULL);
112
113
114     for (;;)
115         if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
116             break;
117
118     avahi_server_free(server);
119     avahi_simple_poll_free(simple_poll);
120
121     return 0;
122 }