]> git.meshlink.io Git - catta/blob - examples/client-publish-service.c
update example to include some code to show how to modify an existing service
[catta] / examples / client-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-client/client.h>
32 #include <avahi-client/publish.h>
33
34 #include <avahi-common/alternative.h>
35 #include <avahi-common/simple-watch.h>
36 #include <avahi-common/malloc.h>
37 #include <avahi-common/error.h>
38 #include <avahi-common/timeval.h>
39
40 static AvahiEntryGroup *group = NULL;
41 static AvahiSimplePoll *simple_poll = NULL;
42 static char *name = NULL;
43
44 static void create_services(AvahiClient *c);
45
46 static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) {
47     assert(g == group);
48
49     /* Called whenever the entry group state changes */
50
51     switch (state) {
52         case AVAHI_ENTRY_GROUP_ESTABLISHED :
53             /* The entry group has been established successfully */
54             fprintf(stderr, "Service '%s' successfully established.\n", name);
55             break;
56
57         case AVAHI_ENTRY_GROUP_COLLISION : {
58             char *n;
59             
60             /* A service name collision happened. Let's pick a new name */
61             n = avahi_alternative_service_name(name);
62             avahi_free(name);
63             name = n;
64             
65             fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
66             
67             /* And recreate the services */
68             create_services(avahi_entry_group_get_client(g));
69             break;
70         }
71
72         case AVAHI_ENTRY_GROUP_FAILURE :
73
74             /* Some kind of failure happened while we were registering our services */
75             avahi_simple_poll_quit(simple_poll);
76             break;
77
78         case AVAHI_ENTRY_GROUP_UNCOMMITED:
79         case AVAHI_ENTRY_GROUP_REGISTERING:
80             ;
81     }
82 }
83
84 static void create_services(AvahiClient *c) {
85     char r[128];
86     int ret;
87     assert(c);
88
89     /* If this is the first time we're called, let's create a new entry group */
90     if (!group)
91         if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) {
92             fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_client_errno(c)));
93             goto fail;
94         }
95     
96     fprintf(stderr, "Adding service '%s'\n", name);
97     
98     /* Create some random TXT data */
99     snprintf(r, sizeof(r), "random=%i", rand());
100
101     /* Add the service for IPP */
102     if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL)) < 0) {
103         fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret));
104         goto fail;
105     }
106
107     /* Add the same service for BSD LPR */
108     if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) {
109         fprintf(stderr, "Failed to add _printer._tcp service: %s\n", avahi_strerror(ret));
110         goto fail;
111     }
112
113     /* Add an additional (hypothetic) subtype */
114     if ((ret = avahi_entry_group_add_service_subtype(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, "_magic._sub._printer._tcp") < 0)) {
115         fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret));
116         goto fail;
117     }
118     
119     /* Tell the server to register the service */
120     if ((ret = avahi_entry_group_commit(group)) < 0) {
121         fprintf(stderr, "Failed to commit entry_group: %s\n", avahi_strerror(ret));
122         goto fail;
123     }
124
125     return;
126
127 fail:
128     avahi_simple_poll_quit(simple_poll);
129 }
130
131 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
132     assert(c);
133
134     /* Called whenever the client or server state changes */
135
136     switch (state) {
137         case AVAHI_CLIENT_S_RUNNING:
138         
139             /* The server has startup successfully and registered its host
140              * name on the network, so it's time to create our services */
141             if (!group)
142                 create_services(c);
143             break;
144
145         case AVAHI_CLIENT_S_COLLISION:
146         
147             /* Let's drop our registered services. When the server is back
148              * in AVAHI_SERVER_RUNNING state we will register them
149              * again with the new host name. */
150             if (group)
151                 avahi_entry_group_reset(group);
152             break;
153             
154         case AVAHI_CLIENT_FAILURE:
155             
156             fprintf(stderr, "Client failure: %s\n", avahi_strerror(avahi_client_errno(c)));
157             avahi_simple_poll_quit(simple_poll);
158             
159             break;
160
161         case AVAHI_CLIENT_CONNECTING:
162         case AVAHI_CLIENT_S_REGISTERING:
163             ;
164     }
165 }
166
167 static void modify_callback(AVAHI_GCC_UNUSED AvahiTimeout *e, void *userdata) {
168     AvahiClient *client = userdata;
169
170     fprintf(stderr, "Doing some weird modification\n");
171
172     avahi_free(name); 
173     name = avahi_strdup("Modified MegaPrinter"); 
174
175     /* If the server is currently running, we need to remove our
176      * service and create it anew */
177     if (avahi_client_get_state(client) == AVAHI_CLIENT_S_RUNNING) {
178
179         /* Remove the old services */
180         if (group)
181             avahi_entry_group_reset(group);
182
183         /* And create them again with the new name */
184         create_services(client);
185     }
186 }
187
188 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
189     AvahiClient *client = NULL;
190     int error;
191     int ret = 1;
192     struct timeval tv;
193     
194     /* Allocate main loop object */
195     if (!(simple_poll = avahi_simple_poll_new())) {
196         fprintf(stderr, "Failed to create simple poll object.\n");
197         goto fail;
198     }
199     
200     name = avahi_strdup("MegaPrinter");
201
202     /* Allocate a new client */
203     client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error);
204
205     /* Check wether creating the client object succeeded */
206     if (!client) {
207         fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
208         goto fail;
209     }
210
211     /* After 20s do some weird modification to the service */
212     avahi_simple_poll_get(simple_poll)->timeout_new(
213         avahi_simple_poll_get(simple_poll),
214         avahi_elapse_time(&tv, 1000*10, 0),
215         modify_callback,
216         client);
217     
218     /* Run the main loop */
219     avahi_simple_poll_loop(simple_poll);
220     
221     ret = 0;
222     
223 fail:
224     
225     /* Cleanup things */
226
227     if (client)
228         avahi_client_free(client);
229
230     if (simple_poll)
231         avahi_simple_poll_free(simple_poll);
232
233     avahi_free(name);
234     
235     return ret;
236 }