]> git.meshlink.io Git - catta/blob - examples/publish-service.c
37ecc02b8aab5e7338d6a88343abc6d91450115a
[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     gint ret;
66     g_assert(s);
67
68     /* If this is the first time we're called, let's create a new entry group */
69     if (!group) {
70         if (!(group = avahi_entry_group_new(s, entry_group_callback, NULL))) {
71             g_message("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_server_errno(s)));
72             goto fail;
73         }
74     }
75     
76     g_message("Adding service '%s'", name);
77
78     /* Create some random TXT data */
79     snprintf(r, sizeof(r), "random=%i", rand());
80
81     /* Add the service for IPP */
82     if ((ret = avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL)) < 0) {
83         g_message("Failed to add _ipp._tcp service: %s", avahi_strerror(ret));
84         goto fail;
85     }
86
87     /* Add the same service for BSD LPR */
88     if ((ret = avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) {
89         g_message("Failed to add _printer._tcp service: %s", avahi_strerror(ret));
90         goto fail;
91     }
92
93     /* Tell the server to register the service */
94     if ((ret = avahi_entry_group_commit(group)) < 0) {
95         g_message("Failed to commit entry_group: %s", avahi_strerror(ret));
96         goto fail;
97     }
98
99     return;
100
101 fail:
102     g_main_loop_quit(main_loop);
103     return;
104 }
105
106 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
107     g_assert(s);
108
109     /* Called whenever the server state changes */
110
111     if (state == AVAHI_SERVER_RUNNING)
112         /* The serve has startup successfully and registered its host
113          * name on the network, so it's time to create our services */
114         create_services(s);
115     
116     else if (state == AVAHI_SERVER_COLLISION) {
117         gchar *n;
118         gint r;
119         
120         /* A host name collision happened. Let's pick a new name for the server */
121         n = avahi_alternative_host_name(avahi_server_get_host_name(s));
122         g_message("Host name collision, retrying with '%s'", n);
123         r = avahi_server_set_host_name(s, n);
124         g_free(n);
125
126         if (r < 0) {
127             g_message("Failed to set new host name: %s", avahi_strerror(r));
128
129             g_main_loop_quit(main_loop);
130             return;
131         }
132
133         /* Let's drop our registered services. When the server is back
134          * in AVAHI_SERVER_RUNNING state we will register them
135          * again with the new host name. */
136         if (group)
137             avahi_entry_group_reset(group);
138     }
139 }
140
141 int main(int argc, char*argv[]) {
142     AvahiServerConfig config;
143     AvahiServer *server = NULL;
144     gint error;
145     int ret = 1;
146     
147     srand(time(NULL));
148     
149     name = g_strdup("MegaPrinter");
150
151     /* Let's set the host name for this server. */
152     avahi_server_config_init(&config);
153     config.host_name = g_strdup("gurkiman");
154     config.publish_workstation = FALSE;
155     
156     /* Allocate a new server */
157     server = avahi_server_new(NULL, &config, server_callback, NULL, &error);
158
159     /* Free the configuration data */
160     avahi_server_config_free(&config);
161
162     /* Check wether creating the server object succeeded */
163     if (!server) {
164         g_message("Failed to create server: %s", avahi_strerror(error));
165         goto fail;
166     }
167     
168     /* Run the main loop */
169     main_loop = g_main_loop_new(NULL, FALSE);
170     g_main_loop_run(main_loop);
171
172     ret = 0;
173     
174 fail:
175     
176     /* Cleanup things */
177     if (group)
178         avahi_entry_group_free(group);
179
180     if (server)
181         avahi_server_free(server);
182
183     if (main_loop)
184         g_main_loop_unref(main_loop);
185
186     g_free(name);
187     
188     return ret;
189 }