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