]> git.meshlink.io Git - catta/blob - examples/core-publish-service.c
publish example: exit cleanly on SIGINT, SIGTERM
[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 <signal.h>
28 #include <errno.h>
29 #include <assert.h>
30
31 #include <catta/core.h>
32 #include <catta/publish.h>
33 #include <catta/simple-watch.h>
34 #include <catta/malloc.h>
35 #include <catta/alternative.h>
36 #include <catta/error.h>
37
38 static CattaSEntryGroup *group = NULL;
39 static CattaSimplePoll *simple_poll = NULL;
40 static char *name = NULL;
41
42 static void create_services(CattaServer *s);
43
44 static void entry_group_callback(CattaServer *s, CattaSEntryGroup *g, CattaEntryGroupState state, CATTA_GCC_UNUSED void *userdata) {
45     assert(s);
46     assert(g == group);
47
48     /* Called whenever the entry group state changes */
49
50     switch (state) {
51
52         case CATTA_ENTRY_GROUP_ESTABLISHED:
53
54             /* The entry group has been established successfully */
55             fprintf(stderr, "Service '%s' successfully established.\n", name);
56             break;
57
58         case CATTA_ENTRY_GROUP_COLLISION: {
59             char *n;
60
61             /* A service name collision happened. Let's pick a new name */
62             n = catta_alternative_service_name(name);
63             catta_free(name);
64             name = n;
65
66             fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
67
68             /* And recreate the services */
69             create_services(s);
70             break;
71         }
72
73         case CATTA_ENTRY_GROUP_FAILURE :
74
75             fprintf(stderr, "Entry group failure: %s\n", catta_strerror(catta_server_errno(s)));
76
77             /* Some kind of failure happened while we were registering our services */
78             catta_simple_poll_quit(simple_poll);
79             break;
80
81         case CATTA_ENTRY_GROUP_UNCOMMITED:
82         case CATTA_ENTRY_GROUP_REGISTERING:
83             ;
84     }
85 }
86
87 static void create_services(CattaServer *s) {
88     char r[128];
89     int ret;
90     assert(s);
91
92     /* If this is the first time we're called, let's create a new entry group */
93     if (!group)
94         if (!(group = catta_s_entry_group_new(s, entry_group_callback, NULL))) {
95             fprintf(stderr, "catta_entry_group_new() failed: %s\n", catta_strerror(catta_server_errno(s)));
96             goto fail;
97         }
98
99     fprintf(stderr, "Adding service '%s'\n", name);
100
101     /* Create some random TXT data */
102     snprintf(r, sizeof(r), "random=%i", rand());
103
104     /* Add the service for IPP */
105     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) {
106         fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", catta_strerror(ret));
107         goto fail;
108     }
109
110     /* Add the same service for BSD LPR */
111     if ((ret = catta_server_add_service(s, group, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) {
112         fprintf(stderr, "Failed to add _printer._tcp service: %s\n", catta_strerror(ret));
113         goto fail;
114     }
115
116     /* Add an additional (hypothetic) subtype */
117     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)) {
118         fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", catta_strerror(ret));
119         goto fail;
120     }
121
122     /* Tell the server to register the service */
123     if ((ret = catta_s_entry_group_commit(group)) < 0) {
124         fprintf(stderr, "Failed to commit entry_group: %s\n", catta_strerror(ret));
125         goto fail;
126     }
127
128     return;
129
130 fail:
131     catta_simple_poll_quit(simple_poll);
132 }
133
134 static void server_callback(CattaServer *s, CattaServerState state, CATTA_GCC_UNUSED void * userdata) {
135     assert(s);
136
137     /* Called whenever the server state changes */
138
139     switch (state) {
140
141         case CATTA_SERVER_RUNNING:
142             /* The serve has startup successfully and registered its host
143              * name on the network, so it's time to create our services */
144
145             if (!group)
146                 create_services(s);
147
148             break;
149
150         case CATTA_SERVER_COLLISION: {
151             char *n;
152             int r;
153
154             /* A host name collision happened. Let's pick a new name for the server */
155             n = catta_alternative_host_name(catta_server_get_host_name(s));
156             fprintf(stderr, "Host name collision, retrying with '%s'\n", n);
157             r = catta_server_set_host_name(s, n);
158             catta_free(n);
159
160             if (r < 0) {
161                 fprintf(stderr, "Failed to set new host name: %s\n", catta_strerror(r));
162
163                 catta_simple_poll_quit(simple_poll);
164                 return;
165             }
166
167         }
168
169             /* Fall through */
170
171         case CATTA_SERVER_REGISTERING:
172
173             /* Let's drop our registered services. When the server is back
174              * in CATTA_SERVER_RUNNING state we will register them
175              * again with the new host name. */
176             if (group)
177                 catta_s_entry_group_reset(group);
178
179             break;
180
181         case CATTA_SERVER_FAILURE:
182
183             /* Terminate on failure */
184
185             fprintf(stderr, "Server failure: %s\n", catta_strerror(catta_server_errno(s)));
186             catta_simple_poll_quit(simple_poll);
187             break;
188
189         case CATTA_SERVER_INVALID:
190             ;
191     }
192 }
193
194 static void signal_exit(int signum) {
195     int errnosave = errno;
196     catta_simple_poll_quit(simple_poll);
197     errno = errnosave;
198
199     (void)signum; // ignore
200 }
201
202 int main(CATTA_GCC_UNUSED int argc, CATTA_GCC_UNUSED char*argv[]) {
203     CattaServerConfig config;
204     CattaServer *server = NULL;
205     int error;
206     int ret = 1;
207
208     /* Initialize the pseudo-RNG */
209     srand(time(NULL));
210
211     /* Allocate main loop object */
212     if (!(simple_poll = catta_simple_poll_new())) {
213         fprintf(stderr, "Failed to create simple poll object.\n");
214         goto fail;
215     }
216
217     name = catta_strdup("MegaPrinter");
218
219     /* Let's set the host name for this server. */
220     catta_server_config_init(&config);
221     config.host_name = catta_strdup("gurkiman");
222     config.publish_workstation = 0;
223     config.publish_no_reverse = 1;
224
225     /* Allocate a new server */
226     server = catta_server_new(catta_simple_poll_get(simple_poll), &config, server_callback, NULL, &error);
227
228     /* Free the configuration data */
229     catta_server_config_free(&config);
230
231     /* Check wether creating the server object succeeded */
232     if (!server) {
233         fprintf(stderr, "Failed to create server: %s\n", catta_strerror(error));
234         goto fail;
235     }
236
237     /* exit cleanly on signals */
238     signal(SIGINT, signal_exit);
239     signal(SIGTERM, signal_exit);
240
241     /* Run the main loop */
242     catta_simple_poll_loop(simple_poll);
243
244     ret = 0;
245
246 fail:
247
248     /* Cleanup things */
249
250     if (server)
251         catta_server_free(server);
252
253     if (simple_poll)
254         catta_simple_poll_free(simple_poll);
255
256     catta_free(name);
257
258     return ret;
259 }