summaryrefslogtreecommitdiffstats
path: root/avahi-client/client.c
diff options
context:
space:
mode:
authorTrent Lloyd <lathiat@bur.st>2005-08-11 07:31:07 +0000
committerTrent Lloyd <lathiat@bur.st>2005-08-11 07:31:07 +0000
commit7231641e5c53a9d1c255e06a7a134fa60a919f88 (patch)
treeaed7db1bde5e7036c8f3ab224ce70cd5157d703d /avahi-client/client.c
parent8d8721a8d112e7aaf78cea118a9699b7bf9c28b6 (diff)
* Work on avahi-client
- Support for connecting and registering services works - Needs some more error handling (DBus Errors from the daemon) - Needs browser support yet * Fix avahi-utils to pass make dist-check * Add new error codes git-svn-id: file:///home/lennart/svn/public/avahi/trunk@288 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe
Diffstat (limited to 'avahi-client/client.c')
-rw-r--r--avahi-client/client.c165
1 files changed, 130 insertions, 35 deletions
diff --git a/avahi-client/client.c b/avahi-client/client.c
index f5de858..43b9fc1 100644
--- a/avahi-client/client.c
+++ b/avahi-client/client.c
@@ -26,6 +26,7 @@
#include <avahi-client/client.h>
#include <avahi-common/dbus.h>
#include <avahi-common/llist.h>
+#include <avahi-common/error.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -36,26 +37,84 @@
#include <stdlib.h>
-struct _AvahiClient
+#include "client.h"
+#include "internal.h"
+
+int
+avahi_client_set_errno (AvahiClient *client, int errno)
+{
+ if (client == NULL) return errno;
+
+ client->errno = errno;
+
+ return errno;
+}
+
+static
+void avahi_client_state_change (AvahiClient *client, int state)
+{
+ if (client == NULL || client->callback == NULL)
+ return;
+
+ client->callback (client, state, client->user_data);
+}
+
+void
+avahi_client_state_request_callback (DBusPendingCall *call, void *data)
+{
+ AvahiClient *client = data;
+ DBusError error;
+ DBusMessage *reply;
+ int state, type;
+
+ dbus_error_init (&error);
+
+ reply = dbus_pending_call_steal_reply (call);
+
+ type = dbus_message_get_type (reply);
+
+ if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
+ {
+ dbus_message_get_args (reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID);
+
+ if (dbus_error_is_set (&error))
+ {
+ fprintf (stderr, "internal error parsing client state change for client\n");
+ return;
+ }
+
+ printf ("statechange (client) to %d\n", state);
+
+ avahi_client_state_change (client, state);
+ } else if (type == DBUS_MESSAGE_TYPE_ERROR) {
+ dbus_set_error_from_message (&error, reply);
+ fprintf (stderr, "Error from reply: %s\n", error.message);
+ }
+
+ dbus_pending_call_unref (call);
+}
+
+void
+avahi_client_schedule_state_request (AvahiClient *client)
{
- DBusConnection *bus;
- AVAHI_LLIST_HEAD(AvahiEntryGroup, groups);
-};
+ DBusMessage *message;
+ DBusPendingCall *pcall;
+
+ if (client == NULL) return;
-struct _AvahiEntryGroup {
- char *path;
- AvahiClient *parent;
- AVAHI_LLIST_FIELDS(AvahiEntryGroup, groups);
-};
+ message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState");
+
+ dbus_connection_send_with_reply (client->bus, message, &pcall, -1);
+
+ dbus_pending_call_set_notify (pcall, avahi_client_state_request_callback, client, NULL);
+}
static DBusHandlerResult
filter_func (DBusConnection *bus, DBusMessage *message, void *data)
{
+ AvahiClient *client = data;
DBusError error;
- g_assert (bus != NULL);
- g_assert (message != NULL);
-
printf ("dbus: interface=%s, path=%s, member=%s\n",
dbus_message_get_interface (message),
dbus_message_get_path (message),
@@ -74,24 +133,65 @@ filter_func (DBusConnection *bus, DBusMessage *message, void *data)
}
if (strcmp (name, AVAHI_DBUS_NAME) == 0) {
+
if (old == NULL && new != NULL) {
fprintf(stderr, "Avahi Daemon connected\n");
+ avahi_client_state_change (client, AVAHI_CLIENT_RECONNECTED);
} else if (old != NULL && new == NULL) {
fprintf(stderr, "Avahi Daemon disconnected\n");
+ avahi_client_state_change (client, AVAHI_CLIENT_DISCONNECTED);
+ /* XXX: we really need to expire all entry groups */
+ }
+ }
+ } else if (dbus_message_is_signal (message, AVAHI_DBUS_NAME, "StateChanged")) {
+ printf ("server statehcange\n");
+ } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged")) {
+ const char *path;
+ AvahiEntryGroup *n, *group = NULL;
+ path = dbus_message_get_path (message);
+
+ for (n = client->groups; n != NULL; n = n->groups_next)
+ {
+ if (strcmp (n->path, path) == 0)
+ {
+ group = n;
+ break;
}
}
- return DBUS_HANDLER_RESULT_HANDLED;
+ if (group == NULL)
+ {
+ fprintf (stderr, "Received state change for unknown EntryGroup object (%s)\n", path);
+ } else {
+ int state;
+ DBusError error;
+
+ dbus_error_init (&error);
+
+ dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID);
+
+ if (dbus_error_is_set (&error))
+ {
+ fprintf (stderr, "internal error parsing entrygroup statechange for %s\n", group->path);
+ goto out;
+ }
+
+ printf ("statechange (%s) to %d\n", group->path, state);
+
+ avahi_entry_group_state_change (group, state);
+ }
}
+ return DBUS_HANDLER_RESULT_HANDLED;
+
out:
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
AvahiClient *
-avahi_client_new ()
+avahi_client_new (AvahiClientCallback callback, void *user_data)
{
- AvahiClient *tmp;
+ AvahiClient *tmp = NULL;
DBusError error;
tmp = g_new (AvahiClient, 1);
@@ -148,9 +248,14 @@ avahi_client_new ()
fprintf (stderr, "Error adding filter match: %s\n", error.message);
dbus_error_free (&error);
goto fail;
+ }
- }
+ tmp->callback = callback;
+ tmp->user_data = user_data;
+ avahi_client_schedule_state_request (tmp);
+
+ avahi_client_set_errno (tmp, AVAHI_OK);
return tmp;
fail:
@@ -166,8 +271,7 @@ avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char
DBusError error;
char *ret, *new;
- g_assert (client != NULL);
- g_assert (method != NULL);
+ if (client == NULL || method == NULL) return NULL;
dbus_error_init (&error);
@@ -177,6 +281,7 @@ avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char
{
if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &param, DBUS_TYPE_INVALID))
{
+ avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
fprintf (stderr, "Failed to append string argument to %s message\n", method);
return NULL;
}
@@ -189,6 +294,8 @@ avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char
fprintf (stderr, "Error sending %s message: %s\n", method, error.message);
dbus_error_free (&error);
dbus_message_unref (message);
+
+ avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
return NULL;
}
@@ -196,6 +303,8 @@ avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char
{
dbus_message_unref (message);
fprintf (stderr, "Could not connect to Avahi daemon\n");
+
+ avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
return NULL;
}
@@ -205,11 +314,14 @@ avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char
{
fprintf (stderr, "Failed to parse %s reply: %s\n", method, error.message);
dbus_error_free (&error);
+
+ avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
return NULL;
}
new = strdup (ret);
+ avahi_client_set_errno (client, AVAHI_OK);
return new;
}
@@ -236,20 +348,3 @@ avahi_client_get_host_name_fqdn (AvahiClient *client)
{
return avahi_client_get_string_reply_and_block (client, "GetHostNameFqdn", NULL);
}
-
-AvahiEntryGroup*
-avahi_entry_group_new (AvahiClient *client)
-{
- AvahiEntryGroup *tmp;
-
- tmp = malloc (sizeof (AvahiEntryGroup));
-
- tmp->parent = client;
-
- AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, tmp);
-
- return tmp;
-fail:
- if (tmp) free (tmp);
- return NULL;
-}