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