00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifdef HAVE_CONFIG_H
00021 #include <config.h>
00022 #endif
00023
00024 #include <stdio.h>
00025 #include <assert.h>
00026 #include <stdlib.h>
00027 #include <time.h>
00028
00029 #include <avahi-client/client.h>
00030 #include <avahi-client/lookup.h>
00031
00032 #include <avahi-common/simple-watch.h>
00033 #include <avahi-common/malloc.h>
00034 #include <avahi-common/error.h>
00035
00036 static AvahiSimplePoll *simple_poll = NULL;
00037
00038 static void resolve_callback(
00039 AvahiServiceResolver *r,
00040 AVAHI_GCC_UNUSED AvahiIfIndex interface,
00041 AVAHI_GCC_UNUSED AvahiProtocol protocol,
00042 AvahiResolverEvent event,
00043 const char *name,
00044 const char *type,
00045 const char *domain,
00046 const char *host_name,
00047 const AvahiAddress *address,
00048 uint16_t port,
00049 AvahiStringList *txt,
00050 AvahiLookupResultFlags flags,
00051 AVAHI_GCC_UNUSED void* userdata) {
00052
00053 assert(r);
00054
00055
00056
00057 switch (event) {
00058 case AVAHI_RESOLVER_FAILURE:
00059 fprintf(stderr, "(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(avahi_service_resolver_get_client(r))));
00060 break;
00061
00062 case AVAHI_RESOLVER_FOUND: {
00063 char a[AVAHI_ADDRESS_STR_MAX], *t;
00064
00065 fprintf(stderr, "Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
00066
00067 avahi_address_snprint(a, sizeof(a), address);
00068 t = avahi_string_list_to_string(txt);
00069 fprintf(stderr,
00070 "\t%s:%u (%s)\n"
00071 "\tTXT=%s\n"
00072 "\tcookie is %u\n"
00073 "\tis_local: %i\n"
00074 "\tour_own: %i\n"
00075 "\twide_area: %i\n"
00076 "\tmulticast: %i\n"
00077 "\tcached: %i\n",
00078 host_name, port, a,
00079 t,
00080 avahi_string_list_get_service_cookie(txt),
00081 !!(flags & AVAHI_LOOKUP_RESULT_LOCAL),
00082 !!(flags & AVAHI_LOOKUP_RESULT_OUR_OWN),
00083 !!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA),
00084 !!(flags & AVAHI_LOOKUP_RESULT_MULTICAST),
00085 !!(flags & AVAHI_LOOKUP_RESULT_CACHED));
00086
00087 avahi_free(t);
00088 break;
00089 }
00090 }
00091
00092
00093 }
00094
00095 static void browse_callback(
00096 AvahiServiceBrowser *b,
00097 AvahiIfIndex interface,
00098 AvahiProtocol protocol,
00099 AvahiBrowserEvent event,
00100 const char *name,
00101 const char *type,
00102 const char *domain,
00103 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
00104 void* userdata) {
00105
00106 AvahiClient *c = static_cast<AvahiClient *>(userdata);
00107 assert(b);
00108
00109
00110
00111 switch (event) {
00112 case AVAHI_BROWSER_FAILURE:
00113
00114 fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
00115 avahi_simple_poll_quit(simple_poll);
00116 return;
00117
00118 case AVAHI_BROWSER_NEW:
00119 fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
00120
00121
00122
00123
00124
00125
00126 if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, static_cast<AvahiLookupFlags>(0), resolve_callback, c)))
00127 fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c)));
00128
00129 break;
00130
00131 case AVAHI_BROWSER_REMOVE:
00132 fprintf(stderr, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
00133 break;
00134
00135 case AVAHI_BROWSER_ALL_FOR_NOW:
00136 case AVAHI_BROWSER_CACHE_EXHAUSTED:
00137 fprintf(stderr, "(Browser) %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
00138 break;
00139 }
00140 }
00141
00142 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
00143 assert(c);
00144
00145
00146
00147 if (state == AVAHI_CLIENT_FAILURE) {
00148 fprintf(stderr, "Server connection failure: %s\n", avahi_strerror(avahi_client_errno(c)));
00149 avahi_simple_poll_quit(simple_poll);
00150 }
00151 }
00152
00153 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
00154 AvahiClient *client = NULL;
00155 AvahiServiceBrowser *sb = NULL;
00156 int error;
00157 int ret = 1;
00158
00159
00160 if (!(simple_poll = avahi_simple_poll_new())) {
00161 fprintf(stderr, "Failed to create simple poll object.\n");
00162 goto fail;
00163 }
00164
00165
00166 client = avahi_client_new(avahi_simple_poll_get(simple_poll), static_cast<AvahiClientFlags>(0), client_callback, NULL, &error);
00167
00168
00169 if (!client) {
00170 fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
00171 goto fail;
00172 }
00173
00174
00175 if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_ros-master._tcp", NULL, static_cast<AvahiLookupFlags>(0), browse_callback, client))) {
00176 fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client)));
00177 goto fail;
00178 }
00179
00180
00181 avahi_simple_poll_loop(simple_poll);
00182
00183 ret = 0;
00184
00185 fail:
00186
00187
00188 if (sb)
00189 avahi_service_browser_free(sb);
00190
00191 if (client)
00192 avahi_client_free(client);
00193
00194 if (simple_poll)
00195 avahi_simple_poll_free(simple_poll);
00196
00197 return ret;
00198 }