dmap-mdns-avahi.c 3.03 KB
Newer Older
1
/* Copyright (C) 2006 William Jon McCann <mccann@jhu.edu>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * Modifications Copyright (C) 2008 W. Michael Petullo <mike@flyn.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "config.h"

#include <glib.h>
#include <avahi-glib/glib-malloc.h>
#include <avahi-glib/glib-watch.h>
#include <avahi-common/error.h>

#include "dmap-mdns-avahi.h"

static AvahiClient *client = NULL;
static AvahiEntryGroup *entry_group = NULL;
static gsize client_init = 0;

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
35
client_cb (AvahiClient * client, AvahiClientState state, gpointer data)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
{
	/* FIXME
	 * check to make sure we're in the _RUNNING state before we publish
	 * check for COLLISION state and remove published information
	 */

	/* Called whenever the client or server state changes */

	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
		 */

		break;

	case AVAHI_CLIENT_S_COLLISION:

W. Michael Petullo's avatar
W. Michael Petullo committed
54 55 56 57 58 59 60 61
		/* 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 (entry_group) {
			avahi_entry_group_reset (entry_group);
		}
		break;
62 63

	case AVAHI_CLIENT_FAILURE:
W. Michael Petullo's avatar
W. Michael Petullo committed
64 65 66
		g_warning ("Client failure: %s\n",
			   avahi_strerror (avahi_client_errno (client)));
		break;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

	case AVAHI_CLIENT_CONNECTING:
	case AVAHI_CLIENT_S_REGISTERING:
	default:
		break;
	}
}

AvahiClient *
dmap_mdns_avahi_get_client (void)
{
	if (g_once_init_enter (&client_init)) {
		AvahiClientFlags flags = 0;
		AvahiGLibPoll *apoll;
		int error = 0;

		avahi_set_allocator (avahi_glib_allocator ());

		apoll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT);
		if (apoll == NULL) {
W. Michael Petullo's avatar
W. Michael Petullo committed
87 88
			g_warning
				("Unable to create AvahiGlibPoll object for mDNS");
89 90 91 92 93
		}

		client = avahi_client_new (avahi_glib_poll_get (apoll),
					   flags,
					   (AvahiClientCallback) client_cb,
W. Michael Petullo's avatar
W. Michael Petullo committed
94
					   NULL, &error);
95
		if (error != 0) {
W. Michael Petullo's avatar
W. Michael Petullo committed
96 97
			g_warning ("Unable to initialize mDNS: %s",
				   avahi_strerror (error));
98 99 100 101 102 103 104 105 106
		}

		g_once_init_leave (&client_init, 1);
	}

	return client;
}

void
W. Michael Petullo's avatar
W. Michael Petullo committed
107
dmap_mdns_avahi_set_entry_group (AvahiEntryGroup * eg)
108
{
109 110 111 112
	/* FIXME: No longer a valid assumption with new multiple-protocol
	 * per process code. Refactor?
	 * g_assert (eg == NULL || entry_group == NULL);
	 */
113 114 115
	g_assert (avahi_entry_group_get_client (eg) == client);
	entry_group = eg;
}