]> git.meshlink.io Git - catta/blob - examples/core-publish-service.c
8d6377e6b70becd99ab37487d09fa44061ccf411
[catta] / examples / core-publish-service.c
1 /***
2   This file is part of catta.
3
4   catta is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8
9   catta is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with catta; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <time.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <assert.h>
28
29 #include <catta/core.h>
30 #include <catta/publish.h>
31 #include <catta/simple-watch.h>
32 #include <catta/malloc.h>
33 #include <catta/alternative.h>
34 #include <catta/error.h>
35
36 static CattaSEntryGroup *group = NULL;
37 static CattaSimplePoll *simple_poll = NULL;
38 static char *name = NULL;
39
40 static void create_services(CattaServer *s);
41
42 static void entry_group_callback(CattaServer *s, CattaSEntryGroup *g, CattaEntryGroupState state, CATTA_GCC_UNUSED void *userdata) {
43     assert(s);
44     assert(g == group);
45
46     /* Called whenever the entry group state changes */
47
48     switch (state) {
49
50         case CATTA_ENTRY_GROUP_ESTABLISHED:
51
52             /* The entry group has been established successfully */
53             fprintf(stderr, "Service '%s' successfully established.\n", name);
54             break;
55
56         case CATTA_ENTRY_GROUP_COLLISION: {
57             char *n;
58
59             /* A service name collision happened. Let's pick a new name */
60             n = catta_alternative_service_name(name);
61             catta_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(s);
68             break;
69         }
70
71         case CATTA_ENTRY_GROUP_FAILURE :
72
73             fprintf(stderr, "Entry group failure: %s\n", catta_strerror(catta_server_errno(s)));
74
75             /* Some kind of failure happened while we were registering our services */
76             catta_simple_poll_quit(simple_poll);
77             break;
78
79         case CATTA_ENTRY_GROUP_UNCOMMITED:
80         case CATTA_ENTRY_GROUP_REGISTERING:
81             ;
82     }
83 }
84
85 static void create_services(CattaServer *s) {
86     char r[128];
87     int ret;
88     assert(s);
89
90     /* If this is the first time we're called, let's create a new entry group */
91     if (!group)
92         if (!(group = catta_s_entry_group_new(s, entry_group_callback, NULL))) {
93             fprintf(stderr, "catta_entry_group_new() failed: %s\n", catta_strerror(catta_server_errno(s)));
94             goto fail;
95         }
96
97     fprintf(stderr, "Adding service '%s'\n", name);
98
99     /* Create some random TXT data */
100     snprintf(r, sizeof(r), "random=%i", rand());
101
102     /* Add the service for IPP */
103     if ((ret = catta_server_add_service(s, group, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, 0, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL)) < 0) {
104         fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", catta_strerror(ret));
105         goto fail;
106     }
107
108     /* Add the same service for BSD LPR */
109     if ((ret = catta_server_add_service(s, group, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) {
110         fprintf(stderr, "Failed to add _printer._tcp service: %s\n", catta_strerror(ret));
111         goto fail;
112     }
113
114     /* Add an additional (hypothetic) subtype */
115     if ((ret = catta_server_add_service_subtype(s, group, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, "_magic._sub._printer._tcp") < 0)) {
116         fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", catta_strerror(ret));
117         goto fail;
118     }
119
120     /* Tell the server to register the service */
121     if ((ret = catta_s_entry_group_commit(group)) < 0) {
122         fprintf(stderr, "Failed to commit entry_group: %s\n", catta_strerror(ret));
123         goto fail;
124     }
125
126     return;
127
128 fail:
129     catta_simple_poll_quit(simple_poll);
130 }
131
132 static void server_callback(CattaServer *s, CattaServerState state, CATTA_GCC_UNUSED void * userdata) {
133     assert(s);
134
135     /* Called whenever the server state changes */
136
137     switch (state) {
138
139         case CATTA_SERVER_RUNNING:
140             /* The serve has startup successfully and registered its host
141              * name on the network, so it's time to create our services */
142
143             if (!group)
144                 create_services(s);
145
146             break;
147
148         case CATTA_SERVER_COLLISION: {
149             char *n;
150             int r;
151
152             /* A host name collision happened. Let's pick a new name for the server */
153             n = catta_alternative_host_name(catta_server_get_host_name(s));
154             fprintf(stderr, "Host name collision, retrying with '%s'\n", n);
155             r = catta_server_set_host_name(s, n);
156             catta_free(n);
157
158             if (r < 0) {
159                 fprintf(stderr, "Failed to set new host name: %s\n", catta_strerror(r));
160
161                 catta_simple_poll_quit(simple_poll);
162                 return;
163             }
164
165         }
166
167             /* Fall through */
168
169         case CATTA_SERVER_REGISTERING:
170
171             /* Let's drop our registered services. When the server is back
172              * in CATTA_SERVER_RUNNING state we will register them
173              * again with the new host name. */
174             if (group)
175                 catta_s_entry_group_reset(group);
176
177             break;
178
179         case CATTA_SERVER_FAILURE:
180
181             /* Terminate on failure */
182
183             fprintf(stderr, "Server failure: %s\n", catta_strerror(catta_server_errno(s)));
184             catta_simple_poll_quit(simple_poll);
185             break;
186
187         case CATTA_SERVER_INVALID:
188             ;
189     }
190 }
191
192 int main(CATTA_GCC_UNUSED int argc, CATTA_GCC_UNUSED char*argv[]) {
193     CattaServerConfig config;
194     CattaServer *server = NULL;
195     int error;
196     int ret = 1;
197
198     /* Initialize the pseudo-RNG */
199     srand(time(NULL));
200
201     /* Allocate main loop object */
202     if (!(simple_poll = catta_simple_poll_new())) {
203         fprintf(stderr, "Failed to create simple poll object.\n");
204         goto fail;
205     }
206
207     name = catta_strdup("MegaPrinter");
208
209     /* Let's set the host name for this server. */
210     catta_server_config_init(&config);
211     config.host_name = catta_strdup("gurkiman");
212     config.publish_workstation = 0;
213     config.publish_no_reverse = 1;
214
215     /* Allocate a new server */
216     server = catta_server_new(catta_simple_poll_get(simple_poll), &config, server_callback, NULL, &error);
217
218     /* Free the configuration data */
219     catta_server_config_free(&config);
220
221     /* Check wether creating the server object succeeded */
222     if (!server) {
223         fprintf(stderr, "Failed to create server: %s\n", catta_strerror(error));
224         goto fail;
225     }
226
227     /* Run the main loop */
228     catta_simple_poll_loop(simple_poll);
229
230     ret = 0;
231
232 fail:
233
234     /* Cleanup things */
235
236     if (server)
237         catta_server_free(server);
238
239     if (simple_poll)
240         catta_simple_poll_free(simple_poll);
241
242     catta_free(name);
243
244     return ret;
245 }