2 This file is part of catta.
4 catta 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.
9 catta 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.
14 You should have received a copy of the GNU Lesser General Public
15 License along with catta; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <catta/malloc.h>
28 #include <catta/simple-watch.h>
29 #include <catta/alternative.h>
30 #include <catta/timeval.h>
32 #include <catta/core.h>
33 #include <catta/log.h>
34 #include <catta/publish.h>
35 #include <catta/lookup.h>
38 #define SERVICE_TYPE "_http._tcp"
40 static CattaSServiceBrowser *service_browser1 = NULL, *service_browser2 = NULL;
41 static const CattaPoll * poll_api = NULL;
42 static CattaServer *server = NULL;
43 static CattaSimplePoll *simple_poll;
45 static const char *browser_event_to_string(CattaBrowserEvent event) {
47 case CATTA_BROWSER_NEW : return "NEW";
48 case CATTA_BROWSER_REMOVE : return "REMOVE";
49 case CATTA_BROWSER_CACHE_EXHAUSTED : return "CACHE_EXHAUSTED";
50 case CATTA_BROWSER_ALL_FOR_NOW : return "ALL_FOR_NOW";
51 case CATTA_BROWSER_FAILURE : return "FAILURE";
57 static void sb_callback(
58 CattaSServiceBrowser *b,
60 CattaProtocol protocol,
61 CattaBrowserEvent event,
63 const char *service_type,
65 CattaLookupResultFlags flags,
66 CATTA_GCC_UNUSED void* userdata) {
67 catta_log_debug("SB%i: (%i.%s) <%s> as <%s> in <%s> [%s] cached=%i", b == service_browser1 ? 1 : 2, iface, catta_proto_to_string(protocol), name, service_type, domain, browser_event_to_string(event), !!(flags & CATTA_LOOKUP_RESULT_CACHED));
70 static void create_second_service_browser(CattaTimeout *timeout, CATTA_GCC_UNUSED void* userdata) {
72 service_browser2 = catta_s_service_browser_new(server, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, SERVICE_TYPE, DOMAIN, 0, sb_callback, NULL);
73 assert(service_browser2);
75 poll_api->timeout_free(timeout);
78 static void quit(CATTA_GCC_UNUSED CattaTimeout *timeout, CATTA_GCC_UNUSED void *userdata) {
79 catta_simple_poll_quit(simple_poll);
82 int main(CATTA_GCC_UNUSED int argc, CATTA_GCC_UNUSED char *argv[]) {
84 CattaServerConfig config;
86 simple_poll = catta_simple_poll_new();
89 poll_api = catta_simple_poll_get(simple_poll);
92 catta_server_config_init(&config);
93 config.publish_hinfo = 0;
94 config.publish_addresses = 0;
95 config.publish_workstation = 0;
96 config.publish_domain = 0;
98 catta_address_parse("192.168.50.1", CATTA_PROTO_UNSPEC, &config.wide_area_servers[0]);
99 config.n_wide_area_servers = 1;
100 config.enable_wide_area = 1;
102 server = catta_server_new(poll_api, &config, NULL, NULL, NULL);
104 catta_server_config_free(&config);
106 service_browser1 = catta_s_service_browser_new(server, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, SERVICE_TYPE, DOMAIN, 0, sb_callback, NULL);
107 assert(service_browser1);
109 poll_api->timeout_new(poll_api, catta_elapse_time(&tv, 10000, 0), create_second_service_browser, NULL);
111 poll_api->timeout_new(poll_api, catta_elapse_time(&tv, 60000, 0), quit, NULL);
115 if (catta_simple_poll_iterate(simple_poll, -1) != 0)
118 catta_server_free(server);
119 catta_simple_poll_free(simple_poll);