]> git.meshlink.io Git - catta/commitdiff
* add new entry group state AVAHI_ENTRY_GROUP_FAILURE
authorLennart Poettering <lennart@poettering.net>
Mon, 24 Oct 2005 22:41:39 +0000 (22:41 +0000)
committerLennart Poettering <lennart@poettering.net>
Mon, 24 Oct 2005 22:41:39 +0000 (22:41 +0000)
* pass error code alongside entry gorup state changes over DBUS
* add new server state AVAHI_SERVER_FAILURE
* pass error code alongside server state changes over DBUS
* fix client-publish-service
* rename error code AVAHI_ERR_LOCAL_COLLISION to AVAHI_ERR_COLLISION
* update TODO

git-svn-id: file:///home/lennart/svn/public/avahi/trunk@854 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

17 files changed:
avahi-client/client.c
avahi-client/client.h
avahi-common/dbus.c
avahi-common/dbus.h
avahi-common/defs.h
avahi-common/error.h
avahi-compat-howl/compat.c
avahi-compat-libdns_sd/compat.c
avahi-core/entry.c
avahi-daemon/EntryGroup.introspect
avahi-daemon/Server.introspect
avahi-daemon/dbus-protocol.c
avahi-daemon/main.c
avahi-daemon/main.h
docs/TODO
examples/client-publish-service.c
examples/core-publish-service.c

index 8bc515ee5aa42ff38f99fc5dccca25cda6c920f5..915ba31961b319a41c862092e883292ae64a27d5 100644 (file)
@@ -67,11 +67,12 @@ static void client_set_state (AvahiClient *client, AvahiServerState state) {
                 dbus_connection_unref(client->bus);
                 client->bus = NULL;
             }
-
+            
             /* Fall through */
-
+            
         case AVAHI_CLIENT_S_COLLISION:
         case AVAHI_CLIENT_S_REGISTERING:
+        case AVAHI_CLIENT_S_FAILURE:
 
             /* Clear cached strings */
             avahi_free(client->host_name);
@@ -139,15 +140,21 @@ static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message,
 
     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged")) {
         int32_t state;
+        char *e;
+        int c;
         
         if (!(dbus_message_get_args(
                   message, &error,
                   DBUS_TYPE_INT32, &state,
+                  DBUS_TYPE_STRING, &e,
                   DBUS_TYPE_INVALID) || dbus_error_is_set (&error))) {
             fprintf(stderr, "WARNING: Failed to parse Server.StateChanged signal: %s\n", error.message);
             goto fail;
         }
-            
+
+        if ((c = avahi_error_dbus_to_number(e)) != AVAHI_OK)
+            avahi_client_set_errno(client, c);
+        
         client_set_state(client, (AvahiClientState) state);
 
     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged")) {
@@ -161,11 +168,21 @@ static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message,
         
         if (g) {
             int32_t state;
-            if (!(dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID)) ||
+            char *e;
+            int c;
+            
+            if (!(dbus_message_get_args(
+                      message, &error,
+                      DBUS_TYPE_INT32, &state,
+                      DBUS_TYPE_STRING, &e,
+                      DBUS_TYPE_INVALID)) ||
                 dbus_error_is_set(&error)) {
                 fprintf(stderr, "WARNING: Failed to parse EntryGroup.StateChanged signal: %s\n", error.message);
                 goto fail;
             }
+
+            if ((c = avahi_error_dbus_to_number(e)) != AVAHI_OK)
+                avahi_client_set_errno(client, c);
             
             avahi_entry_group_set_state(g, state);
         }
index d00cc8b39f25acee2d07b1ab32ef0df7f96554f0..ffcc8d747ad5c71088f00fbbc5ff3acd0e97f43f 100644 (file)
@@ -49,6 +49,7 @@ typedef enum {
     AVAHI_CLIENT_S_REGISTERING = AVAHI_SERVER_REGISTERING,
     AVAHI_CLIENT_S_RUNNING = AVAHI_SERVER_RUNNING,
     AVAHI_CLIENT_S_COLLISION = AVAHI_SERVER_COLLISION,
+    AVAHI_CLIENT_S_FAILURE = AVAHI_SERVER_FAILURE,
     AVAHI_CLIENT_DISCONNECTED = 100 /**< Lost DBUS connection to the Avahi daemon */
 } AvahiClientState;
 
index 179b1eda9b8cb645366d24d11f266cde7d80af4f..058b8190950dc4ec09cde5390dcebf2e8b3bf88e 100644 (file)
@@ -31,7 +31,7 @@
 #include <avahi-common/dbus.h>
 
 static const char * const table[- AVAHI_ERR_MAX] = {
-    NULL, /* OK */
+    AVAHI_DBUS_ERR_OK,
     AVAHI_DBUS_ERR_FAILURE,
     AVAHI_DBUS_ERR_BAD_STATE,
     AVAHI_DBUS_ERR_INVALID_HOST_NAME,
@@ -39,7 +39,7 @@ static const char * const table[- AVAHI_ERR_MAX] = {
     AVAHI_DBUS_ERR_NO_NETWORK,
     AVAHI_DBUS_ERR_INVALID_TTL,
     AVAHI_DBUS_ERR_IS_PATTERN,
-    AVAHI_DBUS_ERR_LOCAL_COLLISION,
+    AVAHI_DBUS_ERR_COLLISION,
     AVAHI_DBUS_ERR_INVALID_RECORD,
     AVAHI_DBUS_ERR_INVALID_SERVICE_NAME,
     AVAHI_DBUS_ERR_INVALID_SERVICE_TYPE,
index 9806f587b2352b5f115385670ae125e26cf78cf2..9cafbde2bda61f69b9a6d99a0305fbbcee1effd2 100644 (file)
@@ -42,6 +42,7 @@ AVAHI_C_DECL_BEGIN
 #define AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER AVAHI_DBUS_NAME".HostNameResolver"
 #define AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER AVAHI_DBUS_NAME".ServiceResolver"
 
+#define AVAHI_DBUS_ERR_OK "org.freedesktop.Avahi.Success"
 #define AVAHI_DBUS_ERR_FAILURE "org.freedesktop.Avahi.Failure"
 #define AVAHI_DBUS_ERR_BAD_STATE "org.freedesktop.Avahi.BadStateError"
 #define AVAHI_DBUS_ERR_INVALID_HOST_NAME "org.freedesktop.Avahi.InvalidHostNameError"
@@ -49,7 +50,7 @@ AVAHI_C_DECL_BEGIN
 #define AVAHI_DBUS_ERR_NO_NETWORK "org.freedesktop.Avahi.NoNetworkError"
 #define AVAHI_DBUS_ERR_INVALID_TTL "org.freedesktop.Avahi.InvalidTTLError"
 #define AVAHI_DBUS_ERR_IS_PATTERN "org.freedesktop.Avahi.IsPatternError"
-#define AVAHI_DBUS_ERR_LOCAL_COLLISION "org.freedesktop.Avahi.LocalCollisionError"
+#define AVAHI_DBUS_ERR_COLLISION "org.freedesktop.Avahi.CollisionError"
 #define AVAHI_DBUS_ERR_INVALID_RECORD "org.freedesktop.Avahi.InvalidRecordError"
 #define AVAHI_DBUS_ERR_INVALID_SERVICE_NAME "org.freedesktop.Avahi.InvalidServiceNameError"
 #define AVAHI_DBUS_ERR_INVALID_SERVICE_TYPE "org.freedesktop.Avahi.InvalidServiceTypeError"
index 3b8c2a527098e581c04c346ae3b12b62eb3388c7..2dcc829d27d6a6754d6a0019042ddd811141273b 100644 (file)
@@ -139,7 +139,8 @@ typedef enum {
     AVAHI_ENTRY_GROUP_UNCOMMITED,    /**< The group has not yet been commited, the user must still call avahi_entry_group_commit() */
     AVAHI_ENTRY_GROUP_REGISTERING,   /**< The entries of the group are currently being registered */
     AVAHI_ENTRY_GROUP_ESTABLISHED,   /**< The entries have successfully been established */
-    AVAHI_ENTRY_GROUP_COLLISION      /**< A name collision for one of the entries in the group has been detected, the entries have been withdrawn */
+    AVAHI_ENTRY_GROUP_COLLISION,     /**< A name collision for one of the entries in the group has been detected, the entries have been withdrawn */
+    AVAHI_ENTRY_GROUP_FAILURE        /**< Some kind of failure happened, the entries have been withdrawn */
 } AvahiEntryGroupState;
 
 /** The type of domain to browse for */
@@ -200,7 +201,8 @@ typedef enum {
     AVAHI_SERVER_INVALID,          /**< Invalid state (initial) */ 
     AVAHI_SERVER_REGISTERING,      /**< Host RRs are being registered */
     AVAHI_SERVER_RUNNING,          /**< All host RRs have been established */
-    AVAHI_SERVER_COLLISION         /**< There is a collision with a host RR. All host RRs have been withdrawn, the user should set a new host name via avahi_server_set_host_name() */
+    AVAHI_SERVER_COLLISION,        /**< There is a collision with a host RR. All host RRs have been withdrawn, the user should set a new host name via avahi_server_set_host_name() */
+    AVAHI_SERVER_FAILURE           /**< Some fatal failure happened, the server is unable to proceed */
 } AvahiServerState;
 
 /** For every service a special TXT item is implicitly added, which
index 55c388bfb707ce47924dc50e71e41cfc3da44c7e..270aecef52e556ed96c180e2559ceea4f435538b 100644 (file)
@@ -40,7 +40,7 @@ enum {
     AVAHI_ERR_NO_NETWORK = -5,               /**< No suitable network protocol available */
     AVAHI_ERR_INVALID_TTL = -6,              /**< Invalid DNS TTL */
     AVAHI_ERR_IS_PATTERN = -7,               /**< RR key is pattern */
-    AVAHI_ERR_LOCAL_COLLISION = -8,          /**< Local name collision */
+    AVAHI_ERR_COLLISION = -8,                /**< Name collision */
     AVAHI_ERR_INVALID_RECORD = -9,           /**< Invalid RR */
 
     AVAHI_ERR_INVALID_SERVICE_NAME = -10,    /**< Invalid service name */
index 1663af021924104d033cb4e7ba618068a7acf657..8e8dd06f1819ab7b3f672506136d361e9967a6fd 100644 (file)
@@ -694,6 +694,7 @@ static void reg_client_callback(oid_data *data, AvahiClientState state) {
         return;
     
     switch (state) {
+        case AVAHI_CLIENT_S_FAILURE:
         case AVAHI_CLIENT_DISCONNECTED:
             reg_report_status(data, SW_DISCOVERY_PUBLISH_INVALID);
             break;
@@ -745,6 +746,11 @@ static void reg_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState st
         case AVAHI_ENTRY_GROUP_UNCOMMITED:
             /* Ignore */
             break;
+
+        case AVAHI_ENTRY_GROUP_FAILURE:
+            reg_report_status(data, SW_DISCOVERY_PUBLISH_INVALID);
+            break;
+            
     }
 }
 
index 9fbe731c70468cdf8dadc688bf510d483adda923..a06ca6826c627da13d9fdba991366e97076aee7d 100644 (file)
@@ -108,7 +108,7 @@ static DNSServiceErrorType map_error(int error) {
             return kDNSServiceErr_BadParam;
 
 
-        case AVAHI_ERR_LOCAL_COLLISION:
+        case AVAHI_ERR_COLLISION:
             return kDNSServiceErr_NameConflict;
 
         case AVAHI_ERR_TOO_MANY_CLIENTS:
@@ -469,23 +469,29 @@ static void service_browser_callback(
 
 static void generic_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) {
     DNSServiceRef sdref = userdata;
-
+    int error = kDNSServiceErr_Unknown;
+        
     assert(s);
     assert(sdref);
     assert(sdref->n_ref >= 1);
 
     switch (state) {
-        case AVAHI_CLIENT_DISCONNECTED: {
+        case AVAHI_CLIENT_S_FAILURE:
+
+            error = map_error(avahi_client_errno(s));
+
+            /* Fall through */
+            
+        case AVAHI_CLIENT_DISCONNECTED:
 
             if (sdref->service_browser_callback)
-                sdref->service_browser_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, NULL, NULL, sdref->context);
+                sdref->service_browser_callback(sdref, 0, 0, error, NULL, NULL, NULL, sdref->context);
             else if (sdref->service_resolver_callback)
-                sdref->service_resolver_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, NULL, 0, 0, NULL, sdref->context);
+                sdref->service_resolver_callback(sdref, 0, 0, error, NULL, NULL, 0, 0, NULL, sdref->context);
             else if (sdref->domain_browser_callback)
-                sdref->domain_browser_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, sdref->context);
+                sdref->domain_browser_callback(sdref, 0, 0, error, NULL, sdref->context);
 
             break;
-        }
 
         case AVAHI_CLIENT_S_RUNNING:
         case AVAHI_CLIENT_S_COLLISION:
@@ -857,9 +863,12 @@ static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* us
         return;
     
     switch (state) {
-        case AVAHI_CLIENT_DISCONNECTED: 
+        case AVAHI_CLIENT_DISCONNECTED:
+            reg_report_error(sdref, kDNSServiceErr_Unknown);
+            break;
 
-            reg_report_error(sdref, kDNSServiceErr_NoError);
+        case AVAHI_CLIENT_S_FAILURE:
+            reg_report_error(sdref, map_error(avahi_client_errno(s)));
             break;
         
         case AVAHI_CLIENT_S_RUNNING: {
@@ -950,6 +959,12 @@ static void reg_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState st
         case AVAHI_ENTRY_GROUP_UNCOMMITED:
             /* Ignore */
             break;
+
+        case AVAHI_ENTRY_GROUP_FAILURE:
+            /* Inform the user */
+            reg_report_error(sdref, map_error(avahi_client_errno(sdref->client)));
+            break;
+            
     }
 }
 
index 5ef2a6747258d3e4707cfb14321bac73d097f8fb..89f313eb5f6741fa24000a18a1a968afde7ad0e9 100644 (file)
@@ -224,7 +224,7 @@ static AvahiEntry * server_add_internal(
         /* Add a new record */
     
         if (check_record_conflict(s, interface, protocol, r, flags) < 0) {
-            avahi_server_set_errno(s, AVAHI_ERR_LOCAL_COLLISION);
+            avahi_server_set_errno(s, AVAHI_ERR_COLLISION);
             return NULL;
         }
 
index b5c2382ebbd2d6cd61e8e670c720a0fe1b6a932a..53fca44e230f2a863a2da58149a359b6004956b2 100644 (file)
     <method name="GetState">
       <arg name="state" type="i" direction="out"/>
     </method>
+
     <signal name="StateChanged">
       <arg name="state" type="i"/>
+      <arg name="error" type="s"/>
     </signal>
 
     <method name="IsEmpty">
index 2e36a21301289e02073354def776404406a89804..cfced9d7d8764ac651b0a2b7b30197888857b62f 100644 (file)
     <method name="GetState">
       <arg name="state" type="i" direction="out"/>
     </method>
+
     <signal name="StateChanged">
       <arg name="state" type="i"/>
+      <arg name="error" type="s"/>
     </signal>
 
     <method name="GetLocalServiceCookie">
index 287f62c235e637af2e2d23105dbb51681b6a2d10..b6a4e5d22473b00df66a2a723084d631037ec9af 100644 (file)
@@ -686,14 +686,27 @@ static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntry
     EntryGroupInfo *i = userdata;
     DBusMessage *m;
     int32_t t;
+    const char *e;
     
     assert(s);
     assert(g);
     assert(i);
 
     m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
+    
     t = (int32_t) state;
-    dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
+    if (state == AVAHI_ENTRY_GROUP_FAILURE)
+        e = avahi_error_number_to_dbus(avahi_server_errno(s));
+    else if (state == AVAHI_ENTRY_GROUP_COLLISION)
+        e = AVAHI_DBUS_ERR_COLLISION;
+    else
+        e = AVAHI_DBUS_ERR_OK;
+        
+    dbus_message_append_args(
+        m,
+        DBUS_TYPE_INT32, &t,
+        DBUS_TYPE_STRING, &e,
+        DBUS_TYPE_INVALID);
     dbus_message_set_destination(m, i->client->name);  
     dbus_connection_send(server->bus, m, NULL);
     dbus_message_unref(m);
@@ -2411,12 +2424,21 @@ fail:
 void dbus_protocol_server_state_changed(AvahiServerState state) {
     DBusMessage *m;
     int32_t t;
+    const char *e;
     
     if (!server)
         return;
 
     m = dbus_message_new_signal(AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged");
     t = (int32_t) state;
+
+    if (state == AVAHI_SERVER_COLLISION)
+        e = AVAHI_DBUS_ERR_COLLISION;
+    else if (state == AVAHI_SERVER_FAILURE)
+        e = avahi_error_number_to_dbus(avahi_server_errno(avahi_server));
+    else
+        e = AVAHI_DBUS_ERR_OK;
+    
     dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
     dbus_connection_send(server->bus, m, NULL);
     dbus_message_unref(m);
index 7bae4596ae65119bfdde22ccd848691d8e7761b4..f64524a730e85bcaf1c4289135197b52cd5f2033 100644 (file)
@@ -65,6 +65,7 @@
 #endif
 
 AvahiServer *avahi_server = NULL;
+AvahiSimplePoll *simple_poll_api = NULL;
 
 typedef enum {
     DAEMON_RUN,
@@ -214,7 +215,6 @@ static void update_wide_area_servers(void) {
     }
 
     avahi_server_set_wide_area_servers(avahi_server, a, n);
-    
 }
 
 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
@@ -223,7 +223,7 @@ static void server_callback(AvahiServer *s, AvahiServerState state, void *userda
     assert(s);
     assert(c);
 
-    /** This function is possibly called before the global variable
+    /* This function is possibly called before the global variable
      * avahi_server has been set, therefore we do it explicitly */
 
     avahi_server = s;
@@ -233,31 +233,46 @@ static void server_callback(AvahiServer *s, AvahiServerState state, void *userda
         dbus_protocol_server_state_changed(state);
 #endif
 
-    if (state == AVAHI_SERVER_RUNNING) {
-        avahi_log_info("Server startup complete. Host name is %s. Local service cookie is %u.", avahi_server_get_host_name_fqdn(s), avahi_server_get_local_service_cookie(s));
-        static_service_add_to_server();
-
-        remove_dns_server_entry_groups();
-
-        if (c->publish_resolv_conf && resolv_conf && resolv_conf[0])
-            resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf);
-
-        if (c->publish_dns_servers && c->publish_dns_servers[0])
-            dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
-
-        simple_protocol_restart_queries();
-        
-    } else if (state == AVAHI_SERVER_COLLISION) {
-        char *n;
+    switch (state) {
+        case AVAHI_SERVER_RUNNING:
+            avahi_log_info("Server startup complete. Host name is %s. Local service cookie is %u.", avahi_server_get_host_name_fqdn(s), avahi_server_get_local_service_cookie(s));
+            static_service_add_to_server();
+            
+            remove_dns_server_entry_groups();
+            
+            if (c->publish_resolv_conf && resolv_conf && resolv_conf[0])
+                resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf);
+            
+            if (c->publish_dns_servers && c->publish_dns_servers[0])
+                dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
+            
+            simple_protocol_restart_queries();
+            break;
+            
+        case AVAHI_SERVER_COLLISION: {
+            char *n;
+            
+            static_service_remove_from_server();
+            
+            remove_dns_server_entry_groups();
+            
+            n = avahi_alternative_host_name(avahi_server_get_host_name(s));
+            avahi_log_warn("Host name conflict, retrying with <%s>", n);
+            avahi_server_set_host_name(s, n);
+            avahi_free(n);
+            break;
+        }
 
-        static_service_remove_from_server();
+        case AVAHI_SERVER_FAILURE:
 
-        remove_dns_server_entry_groups();
+            avahi_log_error("Server error: %s", avahi_strerror(avahi_server_errno(s)));
+            avahi_simple_poll_quit(simple_poll_api);
+            break;
 
-        n = avahi_alternative_host_name(avahi_server_get_host_name(s));
-        avahi_log_warn("Host name conflict, retrying with <%s>", n);
-        avahi_server_set_host_name(s, n);
-        avahi_free(n);
+        case AVAHI_SERVER_REGISTERING:
+        case AVAHI_SERVER_INVALID:
+            break;
+            
     }
 }
 
@@ -546,7 +561,6 @@ static void dump(const char *text, void* userdata) {
 
 static void signal_callback(AvahiWatch *watch, int fd, AvahiWatchEvent event, void *userdata) {
     int sig;
-    AvahiSimplePoll *simple_poll_api = userdata;
     const AvahiPoll *poll_api;
     
     assert(watch);
@@ -602,7 +616,6 @@ static void signal_callback(AvahiWatch *watch, int fd, AvahiWatchEvent event, vo
 static int run_server(DaemonConfig *c) {
     int r = -1;
     int error;
-    AvahiSimplePoll *simple_poll_api;
     const AvahiPoll *poll_api;
     AvahiWatch *sig_watch;
 
@@ -684,16 +697,20 @@ finish:
         dbus_protocol_shutdown();
 #endif
 
-    if (avahi_server)
+    if (avahi_server) {
         avahi_server_free(avahi_server);
+        avahi_server = NULL;
+    }
 
     daemon_signal_done();
 
     if (sig_watch)
         poll_api->watch_free(sig_watch);
 
-    if (simple_poll_api)
+    if (simple_poll_api) {
         avahi_simple_poll_free(simple_poll_api);
+        simple_poll_api = NULL;
+    }
 
     if (r != 0 && c->daemonize)
         daemon_retval_send(1);
index 8db60e492966419a4c3ea43d012f73a95f3ceb07..0c933379233b687507eb3b2f5f95281760a6d2fd 100644 (file)
 ***/
 
 #include <avahi-core/core.h>
+#include <avahi-common/simple-watch.h>
 
 extern AvahiServer *avahi_server;
+extern AvahiSimplePoll *simple_poll_api;
+
 
 #endif
index a851d33f09a5edeb3ffb2123f0b0622db406424b..aaea9a9abb18320540b44cc21c0095aa5da5378b 100644 (file)
--- a/docs/TODO
+++ b/docs/TODO
@@ -6,6 +6,8 @@ for 0.6:
 * add support for subtypes in static services
 * Add static host configuration like static services [lathiat]
 * wrap avahi_server_add_record() via DBUS and in avahi-client [lathiat]
+* make sure that all limit definitions end with _MAX
+* unify argument oder of functions returning a string in a user supplied buffer 
 
 later:
 * add simplification routine for adding services 
index bb895ce66cc9c6ba10ed56a4f125b9f460d89fad..d99c46b351cb9060f602bbd92e735d9628c3ecc2 100644 (file)
@@ -47,22 +47,36 @@ static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state,
 
     /* Called whenever the entry group state changes */
 
-    if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
-        /* The entry group has been established successfully */
-        fprintf(stderr, "Service '%s' successfully established.\n", name);
-    
-    else if (state == AVAHI_ENTRY_GROUP_COLLISION) {
-        char *n;
+    switch (state) {
+        case AVAHI_ENTRY_GROUP_ESTABLISHED :
+            /* The entry group has been established successfully */
+            fprintf(stderr, "Service '%s' successfully established.\n", name);
+            break;
+
+        case AVAHI_ENTRY_GROUP_COLLISION : {
+            char *n;
+            
+            /* A service name collision happened. Let's pick a new name */
+            n = avahi_alternative_service_name(name);
+            avahi_free(name);
+            name = n;
+            
+            fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
+            
+            /* And recreate the services */
+            create_services(avahi_entry_group_get_client(g));
+            break;
+        }
 
-        /* A service name collision happened. Let's pick a new name */
-        n = avahi_alternative_service_name(name);
-        avahi_free(name);
-        name = n;
+        case AVAHI_ENTRY_GROUP_FAILURE :
 
-        fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
+            /* Some kind of failure happened while we were registering our services */
+            avahi_simple_poll_quit(simple_poll);
+            break;
 
-        /* And recreate the services */
-        create_services(avahi_entry_group_get_client(g));
+        case AVAHI_ENTRY_GROUP_UNCOMMITED:
+        case AVAHI_ENTRY_GROUP_REGISTERING:
+            ;
     }
 }
 
@@ -111,7 +125,6 @@ static void create_services(AvahiClient *c) {
 
 fail:
     avahi_simple_poll_quit(simple_poll);
-    return;
 }
 
 static void client_callback(AvahiClient *c, AvahiClientState state, void * userdata) {
@@ -119,25 +132,35 @@ static void client_callback(AvahiClient *c, AvahiClientState state, void * userd
 
     /* Called whenever the client or server state changes */
 
-    if (state == AVAHI_CLIENT_S_RUNNING) {
+    switch (state) {
+        case AVAHI_CLIENT_S_RUNNING:
         
-        /* The server has startup successfully and registered its host
-         * name on the network, so it's time to create our services */
-        if (group)
-            create_services(c);
-    
-    } else if (state == AVAHI_CLIENT_S_COLLISION) {
-        
-        /* 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. */
-        if (group)
-            avahi_entry_group_reset(group);
-        
-    } else if (state == AVAHI_CLIENT_DISCONNECTED) {
+            /* The server has startup successfully and registered its host
+             * name on the network, so it's time to create our services */
+            if (!group)
+                create_services(c);
+            break;
 
-        fprintf(stderr, "Server connection terminated.\n");
-        avahi_simple_poll_quit(simple_poll);
+        case AVAHI_CLIENT_S_COLLISION:
+        
+            /* 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. */
+            if (group)
+                avahi_entry_group_reset(group);
+            break;
+            
+        case AVAHI_CLIENT_DISCONNECTED:
+
+            fprintf(stderr, "Server connection terminated.\n");
+            avahi_simple_poll_quit(simple_poll);
+
+            break;
+
+        case AVAHI_CLIENT_S_FAILURE:
+        case AVAHI_CLIENT_S_INVALID:
+        case AVAHI_CLIENT_S_REGISTERING:
+            ;
     }
 }
 
index 0d8c04caaeb8e35ee3601c384fc27f9004fa8c4a..dc5170b773dc06984f42ba52d5fb6ed1ea108909 100644 (file)
@@ -47,22 +47,38 @@ static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntry
 
     /* Called whenever the entry group state changes */
 
-    if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
-        /* The entry group has been established successfully */
-        fprintf(stderr, "Service '%s' successfully established.\n", name);
-    
-    else if (state == AVAHI_ENTRY_GROUP_COLLISION) {
-        char *n;
-
-        /* A service name collision happened. Let's pick a new name */
-        n = avahi_alternative_service_name(name);
-        avahi_free(name);
-        name = n;
+    switch (state) {
+        
+        case AVAHI_ENTRY_GROUP_ESTABLISHED:
+
+            /* The entry group has been established successfully */
+            fprintf(stderr, "Service '%s' successfully established.\n", name);
+            break;
+
+        case AVAHI_ENTRY_GROUP_COLLISION: {
+            char *n;
+            
+            /* A service name collision happened. Let's pick a new name */
+            n = avahi_alternative_service_name(name);
+            avahi_free(name);
+            name = n;
+            
+            fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
+            
+            /* And recreate the services */
+            create_services(s);
+            break;
+        }
+            
+        case AVAHI_ENTRY_GROUP_FAILURE :
 
-        fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
+            /* Some kind of failure happened while we were registering our services */
+            avahi_simple_poll_quit(simple_poll);
+            break;
 
-        /* And recreate the services */
-        create_services(s);
+        case AVAHI_ENTRY_GROUP_UNCOMMITED:
+        case AVAHI_ENTRY_GROUP_REGISTERING:
+            ;
     }
 }
 
@@ -111,7 +127,6 @@ static void create_services(AvahiServer *s) {
 
 fail:
     avahi_simple_poll_quit(simple_poll);
-    return;
 }
 
 static void server_callback(AvahiServer *s, AvahiServerState state, void * userdata) {
@@ -119,35 +134,54 @@ static void server_callback(AvahiServer *s, AvahiServerState state, void * userd
 
     /* Called whenever the server state changes */
 
-    if (state == AVAHI_SERVER_RUNNING) {
-        /* The serve has startup successfully and registered its host
-         * name on the network, so it's time to create our services */
-
-        if (group)
-            create_services(s);
-    
-    } else if (state == AVAHI_SERVER_COLLISION) {
-        char *n;
-        int 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));
-        fprintf(stderr, "Host name collision, retrying with '%s'\n", n);
-        r = avahi_server_set_host_name(s, n);
-        avahi_free(n);
-
-        if (r < 0) {
-            fprintf(stderr, "Failed to set new host name: %s\n", avahi_strerror(r));
+    switch (state) {
+
+        case AVAHI_SERVER_RUNNING:
+            /* The serve has startup successfully and registered its host
+             * name on the network, so it's time to create our services */
+            
+            if (group)
+                create_services(s);
+
+            break;
+
+        case AVAHI_SERVER_COLLISION: {
+            char *n;
+            int 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));
+            fprintf(stderr, "Host name collision, retrying with '%s'\n", n);
+            r = avahi_server_set_host_name(s, n);
+            avahi_free(n);
+            
+            if (r < 0) {
+                fprintf(stderr, "Failed to set new host name: %s\n", avahi_strerror(r));
+                
+                avahi_simple_poll_quit(simple_poll);
+                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. */
+            if (group)
+                avahi_s_entry_group_reset(group);
+
+            break;
+        }
 
+        case AVAHI_SERVER_FAILURE:
+            
+            /* Terminate on failure */
+            
+            fprintf(stderr, "Server failure: %s\n", avahi_strerror(avahi_server_errno(s)));
             avahi_simple_poll_quit(simple_poll);
-            return;
-        }
+            break;
 
-        /* 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. */
-        if (group)
-            avahi_s_entry_group_reset(group);
+        case AVAHI_SERVER_INVALID:
+        case AVAHI_SERVER_REGISTERING:
+            ;
     }
 }