]> git.meshlink.io Git - catta/blob - avahi-core/avahi-test.c
Add support for server state change callbacks
[catta] / avahi-core / avahi-test.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <stdlib.h>
30
31 #include "core.h"
32 #include "alternative.h"
33
34 static AvahiEntryGroup *group = NULL;
35 static AvahiServer *server = NULL;
36 static gchar *service_name = NULL;
37
38 static gboolean quit_timeout(gpointer data) {
39     g_main_loop_quit(data);
40     return FALSE;
41 }
42
43 static gboolean dump_timeout(gpointer data) {
44     AvahiServer *Avahi = data;
45     avahi_server_dump(Avahi, stdout);
46     return TRUE;
47 }
48
49 static void subscription(AvahiSubscription *s, AvahiRecord *r, gint interface, guchar protocol, AvahiSubscriptionEvent event, gpointer userdata) {
50     gchar *t;
51     
52     g_assert(s);
53     g_assert(r);
54     g_assert(interface > 0);
55     g_assert(protocol != AF_UNSPEC);
56
57     g_message("SUBSCRIPTION: record [%s] on %i.%i is %s", t = avahi_record_to_string(r), interface, protocol,
58               event == AVAHI_SUBSCRIPTION_NEW ? "new" : "removed");
59
60     g_free(t);
61 }
62
63
64 static void remove_entries(void);
65 static void create_entries(gboolean new_name);
66
67 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) {
68     g_message("=======> entry group state: %i", state);
69
70     if (state == AVAHI_ENTRY_GROUP_COLLISION) {
71         remove_entries();
72         create_entries(TRUE);
73     }
74 }
75
76 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
77     g_message("=======> server state: %i", state);
78
79     if (state == AVAHI_SERVER_RUNNING)
80         create_entries(FALSE);
81     else if (state == AVAHI_SERVER_COLLISION) {
82         gchar *n;
83         remove_entries();
84
85         n = avahi_alternative_host_name(avahi_server_get_host_name(s));
86         avahi_server_set_host_name(s, n);
87         g_free(n);
88     }
89 }
90
91 static void remove_entries(void) {
92     if (group)
93         avahi_entry_group_free(group);
94
95     group = NULL;
96 }
97
98 static void create_entries(gboolean new_name) {
99     remove_entries();
100     
101     group = avahi_entry_group_new(server, entry_group_callback, NULL);   
102     
103     if (!service_name)
104         service_name = g_strdup("Test Service");
105     else if (new_name) {
106         gchar *n = avahi_alternative_service_name(avahi_server_get_host_name(server));
107         g_free(service_name);
108         service_name = n;
109     }
110     
111     avahi_server_add_service(server, group, 0, AF_UNSPEC, "_http._tcp", service_name, NULL, NULL, 80, "foo", NULL);
112     avahi_server_add_service(server, group, 0, AF_UNSPEC, "_ftp._tcp", service_name, NULL, NULL, 21, "foo", NULL);   
113     avahi_server_add_service(server, group, 0, AF_UNSPEC, "_webdav._tcp", service_name, NULL, NULL, 80, "foo", NULL);   
114     
115     avahi_entry_group_commit(group);   
116
117 }
118 int main(int argc, char *argv[]) {
119     GMainLoop *loop = NULL;
120 /*     AvahiSubscription *s; */
121 /*     AvahiKey *k; */
122     
123     server = avahi_server_new(NULL, NULL, server_callback, NULL);
124
125 /*     k = avahi_key_new("HALLO", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT); */
126 /*     s = avahi_subscription_new(avahi, k, 0, AF_UNSPEC, subscription, NULL); */
127 /*     avahi_key_unref(k); */
128
129     loop = g_main_loop_new(NULL, FALSE);
130     
131     g_timeout_add(1000*5, dump_timeout, server); 
132 /*     g_timeout_add(1000*30, quit_timeout, loop);    */
133     
134     g_main_loop_run(loop);
135     g_main_loop_unref(loop);
136
137 /*     avahi_subscription_free(s);  */
138
139     if (group)
140         avahi_entry_group_free(group);   
141
142     if (server)
143         avahi_server_free(server);
144
145     g_free(service_name);
146     
147     return 0;
148 }