From 4de3df3db7df43474176533d0b5fac851dd4a9b4 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 20 May 2005 14:54:00 +0000 Subject: Add support for server state change callbacks git-svn-id: file:///home/lennart/svn/public/avahi/trunk@78 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-core/avahi-test.c | 85 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 18 deletions(-) (limited to 'avahi-core/avahi-test.c') diff --git a/avahi-core/avahi-test.c b/avahi-core/avahi-test.c index f3ed3cc..a090a12 100644 --- a/avahi-core/avahi-test.c +++ b/avahi-core/avahi-test.c @@ -29,6 +29,11 @@ #include #include "core.h" +#include "alternative.h" + +static AvahiEntryGroup *group = NULL; +static AvahiServer *server = NULL; +static gchar *service_name = NULL; static gboolean quit_timeout(gpointer data) { g_main_loop_quit(data); @@ -55,45 +60,89 @@ static void subscription(AvahiSubscription *s, AvahiRecord *r, gint interface, g g_free(t); } + +static void remove_entries(void); +static void create_entries(gboolean new_name); + static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) { - g_message("entry group state: %i", state); + g_message("=======> entry group state: %i", state); + + if (state == AVAHI_ENTRY_GROUP_COLLISION) { + remove_entries(); + create_entries(TRUE); + } } -int main(int argc, char *argv[]) { - AvahiServer *avahi; - GMainLoop *loop = NULL; - AvahiSubscription *s; - AvahiKey *k; - AvahiEntryGroup *g; - - avahi = avahi_server_new(NULL, NULL); +static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) { + g_message("=======> server state: %i", state); + + if (state == AVAHI_SERVER_RUNNING) + create_entries(FALSE); + else if (state == AVAHI_SERVER_COLLISION) { + gchar *n; + remove_entries(); + + n = avahi_alternative_host_name(avahi_server_get_host_name(s)); + avahi_server_set_host_name(s, n); + g_free(n); + } +} + +static void remove_entries(void) { + if (group) + avahi_entry_group_free(group); - g = avahi_entry_group_new(avahi, entry_group_callback, NULL); + group = NULL; +} + +static void create_entries(gboolean new_name) { + remove_entries(); + + group = avahi_entry_group_new(server, entry_group_callback, NULL); -/* avahi_server_add_text(avahi, g, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE, "HALLO", avahi_server_get_hostname(avahi), NULL); */ -/* avahi_server_add_text(avahi, g, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE, "hallo", "waldo", NULL); */ + if (!service_name) + service_name = g_strdup("Test Service"); + else if (new_name) { + gchar *n = avahi_alternative_service_name(avahi_server_get_host_name(server)); + g_free(service_name); + service_name = n; + } - avahi_server_add_service(avahi, g, 0, AF_UNSPEC, "_http._tcp", "gurke", NULL, NULL, 80, "foo", NULL); + avahi_server_add_service(server, group, 0, AF_UNSPEC, "_http._tcp", service_name, NULL, NULL, 80, "foo", NULL); + avahi_server_add_service(server, group, 0, AF_UNSPEC, "_ftp._tcp", service_name, NULL, NULL, 21, "foo", NULL); + avahi_server_add_service(server, group, 0, AF_UNSPEC, "_webdav._tcp", service_name, NULL, NULL, 80, "foo", NULL); - avahi_entry_group_commit(g); + avahi_entry_group_commit(group); - avahi_server_dump(avahi, stdout); +} +int main(int argc, char *argv[]) { + GMainLoop *loop = NULL; +/* AvahiSubscription *s; */ +/* AvahiKey *k; */ + server = avahi_server_new(NULL, NULL, server_callback, NULL); + /* k = avahi_key_new("HALLO", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT); */ /* s = avahi_subscription_new(avahi, k, 0, AF_UNSPEC, subscription, NULL); */ /* avahi_key_unref(k); */ loop = g_main_loop_new(NULL, FALSE); - g_timeout_add(1000*5, dump_timeout, avahi); + g_timeout_add(1000*5, dump_timeout, server); /* g_timeout_add(1000*30, quit_timeout, loop); */ g_main_loop_run(loop); g_main_loop_unref(loop); /* avahi_subscription_free(s); */ - avahi_entry_group_free(g); - avahi_server_free(avahi); + + if (group) + avahi_entry_group_free(group); + + if (server) + avahi_server_free(server); + + g_free(service_name); return 0; } -- cgit