]> git.meshlink.io Git - catta/blob - examples/publish-service.c
c9d0141d5949f3e6f5917df43f1695eac81024f6
[catta] / examples / publish-service.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 <time.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include <avahi-core/core.h>
31
32 static AvahiEntryGroup *group = NULL;
33 static GMainLoop *main_loop = NULL;
34 static gchar *name = NULL;
35
36 static void create_services(AvahiServer *s);
37
38 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) {
39     g_assert(s);
40     g_assert(g == group);
41
42     /* Called whenever the entry group state changes */
43
44     if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
45         /* The entry group has been established successfully */
46         g_message("Service '%s' successfully established.", name);
47     
48     else if (state == AVAHI_ENTRY_GROUP_COLLISION) {
49         gchar *n;
50
51         /* A service name collision happened. Let's pick a new name */
52         n = avahi_alternative_service_name(name);
53         g_free(name);
54         name = n;
55
56         g_message("Service name collision, renaming service to '%s'", name);
57
58         /* And recreate the services */
59         create_services(s);
60     }
61 }
62
63 static void create_services(AvahiServer *s) {
64     gchar r[128];
65     g_assert(s);
66
67     /* If this is the first time we're called, let's create a new entry group */
68     if (!group)
69         group = avahi_entry_group_new(s, entry_group_callback, NULL);
70
71     g_message("Adding service '%s'", name);
72
73     /* Create some random TXT data */
74     snprintf(r, sizeof(r), "random=%i", rand());
75
76     /* Add the service for IPP */
77     if (avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL) < 0) {
78         g_message("Failed to add _ipp._tcp service.");
79         g_main_loop_quit(main_loop);
80         return;
81     }
82
83     /* Add the same service for BSD LPR */
84     if (avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_printer._tcp", NULL, NULL, 515, NULL) < 0) {
85         g_message("Failed to add _printer._tcp service.");
86         g_main_loop_quit(main_loop);
87         return;
88     }
89
90     /* Tell the server to register the service */
91     avahi_entry_group_commit(group);
92 }
93
94 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
95     g_assert(s);
96
97     /* Called whenever the server state changes */
98
99     if (state == AVAHI_SERVER_RUNNING)
100         /* The serve has startup successfully and registered its host
101          * name on the network, so it's time to create our services */
102         create_services(s);
103     
104     else if (state == AVAHI_SERVER_COLLISION) {
105         gchar *n;
106         
107         /* A host name collision happened. Let's pick a new name for the server */
108         n = avahi_alternative_host_name(avahi_server_get_host_name(s));
109         g_message("Host name collision, retrying with '%s'", n);
110         avahi_server_set_host_name(s, n);
111         g_free(n);
112
113         /* Let's drop our registered services. When the server is back
114          * in AVAHI_SERVER_RUNNING state we will register them
115          * again with the new host name. */
116         if (group)
117             avahi_entry_group_reset(group);
118     }
119 }
120
121 int main(int argc, char*argv[]) {
122     AvahiServerConfig config;
123     AvahiServer *server = NULL;
124
125     srand(time(NULL));
126     
127     name = g_strdup("MegaPrinter");
128
129     /* Let's set the host name for this server. */
130     avahi_server_config_init(&config);
131     config.host_name = g_strdup("gurkiman");
132     
133     /* Allocate a new server */
134     server = avahi_server_new(NULL, &config, server_callback, NULL);
135
136     /* Free the configuration data */
137     avahi_server_config_free(&config);
138     
139     /* Run the main loop */
140     main_loop = g_main_loop_new(NULL, FALSE);
141     g_main_loop_run(main_loop);
142
143     /* Cleanup things */
144     if (group)
145         avahi_entry_group_free(group);
146
147     if (server)
148         avahi_server_free(server);
149
150     if (main_loop)
151         g_main_loop_unref(main_loop);
152
153     g_free(name);
154     
155     return 0;
156 }