]> git.meshlink.io Git - catta/blobdiff - avahi-client/client.c
client: use Ping() to test whether avahi is around
[catta] / avahi-client / client.c
index 389a3d11ce0bedcc17b2dfdd79cb1a41ba90b963..70cc35d9e9290b9ecd1878ddc295fcd60c70a4b8 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /***
   This file is part of avahi.
 
@@ -35,6 +33,7 @@
 #include <avahi-common/dbus.h>
 #include <avahi-common/malloc.h>
 #include <avahi-common/dbus-watch-glue.h>
+#include <avahi-common/i18n.h>
 
 #include "client.h"
 #include "internal.h"
@@ -56,7 +55,7 @@ int avahi_client_set_dbus_error(AvahiClient *client, DBusError *error) {
     return avahi_client_set_errno(client, avahi_error_dbus_to_number(error->name));
 }
 
-static void client_set_state (AvahiClient *client, AvahiServerState state) {
+static void client_set_state(AvahiClient *client, AvahiClientState state) {
     assert(client);
 
     if (client->state == state)
@@ -142,9 +141,10 @@ static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message,
 
         if (strcmp(name, AVAHI_DBUS_NAME) == 0) {
 
-            if (avahi_client_is_connected(client)) {
+            if (old[0] &&
+                avahi_client_is_connected(client)) {
 
-                /* Regardless if the server lost or acquired its name or
+                /* Regardless if the server lost its name or
                  * if the name was transfered: our services are no longer
                  * available, so we disconnect ourselves */
                 avahi_client_set_errno(client, AVAHI_ERR_DISCONNECTED);
@@ -324,7 +324,7 @@ static int get_server_state(AvahiClient *client, int *ret_error) {
         dbus_error_is_set (&error))
         goto fail;
 
-    client_set_state(client, (AvahiServerState) state);
+    client_set_state(client, (AvahiClientState) state);
 
     dbus_message_unref(message);
     dbus_message_unref(reply);
@@ -478,6 +478,7 @@ static DBusConnection* avahi_dbus_bus_get(DBusError *error) {
 AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientFlags flags, AvahiClientCallback callback, void *userdata, int *ret_error) {
     AvahiClient *client = NULL;
     DBusError error;
+    DBusMessage *message = NULL, *reply = NULL;
 
     avahi_init_i18n();
 
@@ -523,7 +524,7 @@ AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientFlags flags,
         goto fail;
     }
 
-    if (!dbus_connection_add_filter (client->bus, filter_func, client, NULL)) {
+    if (!dbus_connection_add_filter(client->bus, filter_func, client, NULL)) {
         if (ret_error)
             *ret_error = AVAHI_ERR_NO_MEMORY;
         goto fail;
@@ -551,7 +552,7 @@ AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientFlags flags,
     if (dbus_error_is_set(&error))
         goto fail;
 
-    dbus_bus_add_match (
+    dbus_bus_add_match(
         client->bus,
         "type='signal', "
         "interface='" DBUS_INTERFACE_LOCAL "'",
@@ -560,10 +561,12 @@ AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientFlags flags,
     if (dbus_error_is_set(&error))
         goto fail;
 
+    if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, "org.freedesktop.DBus.Peer", "Ping")))
+        goto fail;
 
-    if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error)) ||
-        dbus_error_is_set(&error)) {
+    reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
 
+    if (!reply || dbus_error_is_set (&error)) {
         /* We free the error so its not set, that way the fail target
          * will return the NO_DAEMON error rather than a DBUS error */
         dbus_error_free(&error);
@@ -586,10 +589,20 @@ AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientFlags flags,
             goto fail;
     }
 
+    dbus_message_unref(message);
+
+    if (reply)
+        dbus_message_unref(reply);
+
     return client;
 
 fail:
 
+    if (message)
+        dbus_message_unref(message);
+    if (reply)
+        dbus_message_unref(reply);
+
     if (client)
         avahi_client_free(client);