official_browse_example.cpp
Go to the documentation of this file.
00001 /***
00002   This file is part of avahi.
00003 
00004   avahi is free software; you can redistribute it and/or modify it
00005   under the terms of the GNU Lesser General Public License as
00006   published by the Free Software Foundation; either version 2.1 of the
00007   License, or (at your option) any later version.
00008 
00009   avahi is distributed in the hope that it will be useful, but WITHOUT
00010   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00011   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
00012   Public License for more details.
00013 
00014   You should have received a copy of the GNU Lesser General Public
00015   License along with avahi; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00017   USA.
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     /* Called whenever a service has been resolved successfully or timed out */
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 //    avahi_service_resolver_free(r);
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     /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
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             /* We ignore the returned resolver object. In the callback
00122                function we free it. If the server is terminated before
00123                the callback function is called the server will free
00124                the resolver for us. */
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     /* Called whenever the client or server state changes */
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     /* Allocate main loop object */
00160     if (!(simple_poll = avahi_simple_poll_new())) {
00161         fprintf(stderr, "Failed to create simple poll object.\n");
00162         goto fail;
00163     }
00164 
00165     /* Allocate a new client */
00166     client = avahi_client_new(avahi_simple_poll_get(simple_poll), static_cast<AvahiClientFlags>(0), client_callback, NULL, &error);
00167 
00168     /* Check wether creating the client object succeeded */
00169     if (!client) {
00170         fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
00171         goto fail;
00172     }
00173 
00174     /* Create the service browser */
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     /* Run the main loop */
00181     avahi_simple_poll_loop(simple_poll);
00182 
00183     ret = 0;
00184 
00185 fail:
00186 
00187     /* Cleanup things */
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 }


zeroconf_avahi_demos
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 20:53:04