]> git.meshlink.io Git - catta/blob - examples/core-publish-service.c
* Add avahi-client examples to doxygen
[catta] / examples / core-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 #include <assert.h>
30
31 #include <avahi-core/core.h>
32 #include <avahi-common/simple-watch.h>
33 #include <avahi-common/malloc.h>
34 #include <avahi-common/alternative.h>
35 #include <avahi-common/error.h>
36
37 static AvahiSEntryGroup *group = NULL;
38 static AvahiSimplePoll *simple_poll = NULL;
39 static char *name = NULL;
40
41 static void create_services(AvahiServer *s);
42
43 static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
44     assert(s);
45     assert(g == group);
46
47     /* Called whenever the entry group state changes */
48
49     if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
50         /* The entry group has been established successfully */
51         fprintf(stderr, "Service '%s' successfully established.\n", name);
52     
53     else if (state == AVAHI_ENTRY_GROUP_COLLISION) {
54         char *n;
55
56         /* A service name collision happened. Let's pick a new name */
57         n = avahi_alternative_service_name(name);
58         avahi_free(name);
59         name = n;
60
61         fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
62
63         /* And recreate the services */
64         create_services(s);
65     }
66 }
67
68 static void create_services(AvahiServer *s) {
69     char r[128];
70     int ret;
71     assert(s);
72
73     /* If this is the first time we're called, let's create a new entry group */
74     if (!group) {
75         if (!(group = avahi_s_entry_group_new(s, entry_group_callback, NULL))) {
76             fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_server_errno(s)));
77             goto fail;
78         }
79     }
80     
81     fprintf(stderr, "Adding service '%s'\n", name);
82
83     /* Create some random TXT data */
84     snprintf(r, sizeof(r), "random=%i", rand());
85
86     /* Add the service for IPP */
87     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) {
88         fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret));
89         goto fail;
90     }
91
92     /* Add the same service for BSD LPR */
93     if ((ret = avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) {
94         fprintf(stderr, "Failed to add _printer._tcp service: %s\n", avahi_strerror(ret));
95         goto fail;
96     }
97
98     /* Tell the server to register the service */
99     if ((ret = avahi_s_entry_group_commit(group)) < 0) {
100         fprintf(stderr, "Failed to commit entry_group: %s\n", avahi_strerror(ret));
101         goto fail;
102     }
103
104     return;
105
106 fail:
107     avahi_simple_poll_quit(simple_poll);
108     return;
109 }
110
111 static void server_callback(AvahiServer *s, AvahiServerState state, void * userdata) {
112     assert(s);
113
114     /* Called whenever the server state changes */
115
116     if (state == AVAHI_SERVER_RUNNING)
117         /* The serve has startup successfully and registered its host
118          * name on the network, so it's time to create our services */
119         create_services(s);
120     
121     else if (state == AVAHI_SERVER_COLLISION) {
122         char *n;
123         int r;
124         
125         /* A host name collision happened. Let's pick a new name for the server */
126         n = avahi_alternative_host_name(avahi_server_get_host_name(s));
127         fprintf(stderr, "Host name collision, retrying with '%s'\n", n);
128         r = avahi_server_set_host_name(s, n);
129         avahi_free(n);
130
131         if (r < 0) {
132             fprintf(stderr, "Failed to set new host name: %s\n", avahi_strerror(r));
133
134             avahi_simple_poll_quit(simple_poll);
135             return;
136         }
137
138         /* Let's drop our registered services. When the server is back
139          * in AVAHI_SERVER_RUNNING state we will register them
140          * again with the new host name. */
141         if (group)
142             avahi_s_entry_group_reset(group);
143     }
144 }
145
146 int main(int argc, char*argv[]) {
147     AvahiServerConfig config;
148     AvahiServer *server = NULL;
149     int error;
150     int ret = 1;
151     
152     /* Initialize the pseudo-RNG */
153     srand(time(NULL));
154
155     /* Allocate main loop object */
156     if (!(simple_poll = avahi_simple_poll_new())) {
157         fprintf(stderr, "Failed to create simple poll object.\n");
158         goto fail;
159     }
160     
161     name = avahi_strdup("MegaPrinter");
162
163     /* Let's set the host name for this server. */
164     avahi_server_config_init(&config);
165     config.host_name = avahi_strdup("gurkiman");
166     config.publish_workstation = 0;
167     
168     /* Allocate a new server */
169     server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, server_callback, NULL, &error);
170
171     /* Free the configuration data */
172     avahi_server_config_free(&config);
173
174     /* Check wether creating the server object succeeded */
175     if (!server) {
176         fprintf(stderr, "Failed to create server: %s\n", avahi_strerror(error));
177         goto fail;
178     }
179     
180     /* Run the main loop */
181     for (;;)
182         if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
183             break;
184     
185     ret = 0;
186     
187 fail:
188     
189     /* Cleanup things */
190     if (group)
191         avahi_s_entry_group_free(group);
192
193     if (server)
194         avahi_server_free(server);
195
196     if (simple_poll)
197         avahi_simple_poll_free(simple_poll);
198
199     avahi_free(name);
200     
201     return ret;
202 }