From: Lennart Poettering Date: Fri, 5 Aug 2005 19:41:30 +0000 (+0000) Subject: Improve examples to do error checking X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=c011557b07abda8145d7a711095acdd4284fe367;p=catta Improve examples to do error checking git-svn-id: file:///home/lennart/svn/public/avahi/trunk@240 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/examples/browse-services.c b/examples/browse-services.c index c58d230..f2707fd 100644 --- a/examples/browse-services.c +++ b/examples/browse-services.c @@ -70,7 +70,8 @@ static void browse_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, Avah we free it. If the server is terminated before the callback function is called the server will free the resolver for us. */ - avahi_service_resolver_new(s, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, resolve_callback, s); + if (!(avahi_service_resolver_new(s, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, resolve_callback, s))) + g_message("Failed to resolve service '%s': %s", name, avahi_strerror(avahi_server_errno(s))); } int main(int argc, char*argv[]) { @@ -100,7 +101,10 @@ int main(int argc, char*argv[]) { } /* Create the service browser */ - sb = avahi_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, browse_callback, server); + if (!(sb = avahi_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, browse_callback, server))) { + g_message("Failed to create service browser: %s", avahi_strerror(avahi_server_errno(server))); + goto fail; + } /* Run the main loop */ main_loop = g_main_loop_new(NULL, FALSE); diff --git a/examples/publish-service.c b/examples/publish-service.c index a936147..37ecc02 100644 --- a/examples/publish-service.c +++ b/examples/publish-service.c @@ -62,33 +62,45 @@ static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryG static void create_services(AvahiServer *s) { gchar r[128]; + gint ret; g_assert(s); /* If this is the first time we're called, let's create a new entry group */ - if (!group) - group = avahi_entry_group_new(s, entry_group_callback, NULL); - + if (!group) { + if (!(group = avahi_entry_group_new(s, entry_group_callback, NULL))) { + g_message("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_server_errno(s))); + goto fail; + } + } + g_message("Adding service '%s'", name); /* Create some random TXT data */ snprintf(r, sizeof(r), "random=%i", rand()); /* Add the service for IPP */ - if (avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL) < 0) { - g_message("Failed to add _ipp._tcp service."); - g_main_loop_quit(main_loop); - return; + if ((ret = avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL)) < 0) { + g_message("Failed to add _ipp._tcp service: %s", avahi_strerror(ret)); + goto fail; } /* Add the same service for BSD LPR */ - if (avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_printer._tcp", NULL, NULL, 515, NULL) < 0) { - g_message("Failed to add _printer._tcp service."); - g_main_loop_quit(main_loop); - return; + if ((ret = avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) { + g_message("Failed to add _printer._tcp service: %s", avahi_strerror(ret)); + goto fail; } /* Tell the server to register the service */ - avahi_entry_group_commit(group); + if ((ret = avahi_entry_group_commit(group)) < 0) { + g_message("Failed to commit entry_group: %s", avahi_strerror(ret)); + goto fail; + } + + return; + +fail: + g_main_loop_quit(main_loop); + return; } static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) { @@ -103,13 +115,21 @@ static void server_callback(AvahiServer *s, AvahiServerState state, gpointer use else if (state == AVAHI_SERVER_COLLISION) { gchar *n; + gint r; /* A host name collision happened. Let's pick a new name for the server */ n = avahi_alternative_host_name(avahi_server_get_host_name(s)); g_message("Host name collision, retrying with '%s'", n); - avahi_server_set_host_name(s, n); + r = avahi_server_set_host_name(s, n); g_free(n); + if (r < 0) { + g_message("Failed to set new host name: %s", avahi_strerror(r)); + + g_main_loop_quit(main_loop); + return; + } + /* Let's drop our registered services. When the server is back * in AVAHI_SERVER_RUNNING state we will register them * again with the new host name. */