X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=examples%2Fpublish-service.c;h=37ecc02b8aab5e7338d6a88343abc6d91450115a;hb=280e0fa69dc03706a24bf7128422443cb37d08ab;hp=c9d0141d5949f3e6f5917df43f1695eac81024f6;hpb=3a625af272ee8c6f878ba4d44ed1c741a1582395;p=catta diff --git a/examples/publish-service.c b/examples/publish-service.c index c9d0141..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. */ @@ -121,7 +141,9 @@ static void server_callback(AvahiServer *s, AvahiServerState state, gpointer use int main(int argc, char*argv[]) { AvahiServerConfig config; AvahiServer *server = NULL; - + gint error; + int ret = 1; + srand(time(NULL)); name = g_strdup("MegaPrinter"); @@ -129,17 +151,28 @@ int main(int argc, char*argv[]) { /* Let's set the host name for this server. */ avahi_server_config_init(&config); config.host_name = g_strdup("gurkiman"); + config.publish_workstation = FALSE; /* Allocate a new server */ - server = avahi_server_new(NULL, &config, server_callback, NULL); + server = avahi_server_new(NULL, &config, server_callback, NULL, &error); /* Free the configuration data */ avahi_server_config_free(&config); + + /* Check wether creating the server object succeeded */ + if (!server) { + g_message("Failed to create server: %s", avahi_strerror(error)); + goto fail; + } /* Run the main loop */ main_loop = g_main_loop_new(NULL, FALSE); g_main_loop_run(main_loop); + ret = 0; + +fail: + /* Cleanup things */ if (group) avahi_entry_group_free(group); @@ -152,5 +185,5 @@ int main(int argc, char*argv[]) { g_free(name); - return 0; + return ret; }