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