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