1 /***
2 This file is part of avahi.
3
4 avahi 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 avahi 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 avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 USA.
18 ***/
19
20 #include "client-publish-service.h"
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
entry_group_callback(AvahiEntryGroup * g,AvahiEntryGroupState state,AVAHI_GCC_UNUSED void * userdata)25 static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) {
26 assert(g == group || group == NULL);
27 group = g;
28
29 /* Called whenever the entry group state changes */
30
31 switch (state) {
32 case AVAHI_ENTRY_GROUP_ESTABLISHED :
33 /* The entry group has been established successfully */
34 fprintf(stderr, "Service '%s' successfully established.\n", name);
35 break;
36
37 case AVAHI_ENTRY_GROUP_COLLISION : {
38 char *n;
39
40 /* A service name collision with a remote service
41 * happened. Let's pick a new name */
42 n = avahi_alternative_service_name(name);
43 avahi_free(name);
44 name = n;
45
46 fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
47
48 /* And recreate the services */
49 create_services(avahi_entry_group_get_client(g));
50 break;
51 }
52
53 case AVAHI_ENTRY_GROUP_FAILURE :
54
55 fprintf(stderr, "Entry group failure: %s\n", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
56
57 /* Some kind of failure happened while we were registering our services */
58 avahi_simple_poll_quit(simple_poll);
59 break;
60
61 case AVAHI_ENTRY_GROUP_UNCOMMITED:
62 case AVAHI_ENTRY_GROUP_REGISTERING:
63 ;
64 }
65 }
66
create_services(AvahiClient * c)67 static void create_services(AvahiClient *c) {
68 char *n, r[128];
69 int ret;
70 assert(c);
71
72 /* If this is the first time we're called, let's create a new
73 * entry group if necessary */
74
75 if (!group)
76 if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) {
77 fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_client_errno(c)));
78 goto fail;
79 }
80
81 /* If the group is empty (either because it was just created, or
82 * because it was reset previously, add our entries. */
83
84 if (avahi_entry_group_is_empty(group)) {
85 fprintf(stderr, "Adding service '%s'\n", name);
86
87 /* Create some random TXT data */
88 snprintf(r, sizeof(r), "random=%i", rand());
89
90 /* We will now add two services and one subtype to the entry
91 * group. The two services have the same name, but differ in
92 * the service type (IPP vs. BSD LPR). Only services with the
93 * same name should be put in the same entry group. */
94
95 /* Add the service for IPP */
96 if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, name, "_faustcompiler._tcp", NULL, NULL, 7777, "test=blah", r, NULL)) < 0) {
97
98 if (ret == AVAHI_ERR_COLLISION)
99 goto collision;
100
101 fprintf(stderr, "Failed to add _faustcompiler._tcp service: %s\n", avahi_strerror(ret));
102 goto fail;
103 }
104
105 /* Tell the server to register the service */
106 if ((ret = avahi_entry_group_commit(group)) < 0) {
107 fprintf(stderr, "Failed to commit entry group: %s\n", avahi_strerror(ret));
108 goto fail;
109 }
110 }
111
112 return;
113
114 collision:
115
116 /* A service name collision with a local service happened. Let's
117 * pick a new name */
118 n = avahi_alternative_service_name(name);
119 avahi_free(name);
120 name = n;
121
122 fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
123
124 avahi_entry_group_reset(group);
125
126 create_services(c);
127 return;
128
129 fail:
130 avahi_simple_poll_quit(simple_poll);
131 }
132
client_callback(AvahiClient * c,AvahiClientState state,AVAHI_GCC_UNUSED void * userdata)133 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
134 assert(c);
135
136 /* Called whenever the client or server state changes */
137
138 switch (state) {
139 case AVAHI_CLIENT_S_RUNNING:
140
141 /* The server has startup successfully and registered its host
142 * name on the network, so it's time to create our services */
143 create_services(c);
144 break;
145
146 case AVAHI_CLIENT_FAILURE:
147
148 fprintf(stderr, "Client failure: %s\n", avahi_strerror(avahi_client_errno(c)));
149 avahi_simple_poll_quit(simple_poll);
150
151 break;
152
153 case AVAHI_CLIENT_S_COLLISION:
154
155 /* Let's drop our registered services. When the server is back
156 * in AVAHI_SERVER_RUNNING state we will register them
157 * again with the new host name. */
158
159 case AVAHI_CLIENT_S_REGISTERING:
160
161 /* The server records are now being established. This
162 * might be caused by a host name change. We need to wait
163 * for our own records to register until the host name is
164 * properly esatblished. */
165
166 if (group)
167 avahi_entry_group_reset(group);
168
169 break;
170
171 case AVAHI_CLIENT_CONNECTING:
172 ;
173 }
174 }
175
registerService(void * poll)176 void* registerService(void* poll) {
177
178 /* Run the main loop */
179 avahi_simple_poll_loop((AvahiSimplePoll *)poll);
180
181 }
182
launchRegisterService(const char * nameService)183 int launchRegisterService(const char* nameService) {
184
185 printf("Name of Service to add = %s\n", nameService);
186
187 //AvahiClient *client = NULL;
188 int error;
189 int ret = 1;
190 struct timeval tv;
191
192 /* Allocate main loop object */
193 if (!(simple_poll = avahi_simple_poll_new())) {
194 fprintf(stderr, "Failed to create simple poll object.\n");
195 return 0;
196 }
197
198 // char host_name[256];
199 // gethostname(host_name, sizeof(host_name));
200
201 name = avahi_strdup(nameService);
202
203 /* Allocate a new client */
204 client = avahi_client_new(avahi_simple_poll_get(simple_poll), (AvahiClientFlags)0, client_callback, NULL, &error);
205
206 /* Check wether creating the client object succeeded */
207 if (!client) {
208 fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
209 return 0;
210 }
211
212
213 //pthread_t myNewThread;
214 if(!pthread_create(&myNewThread, NULL, registerService, simple_poll))
215 printf("thread was created\n");
216 else
217 printf("New thread not created\n");
218
219 return 1;
220 }
stopRegisterService(const char * nameService)221 void stopRegisterService(const char* nameService){
222
223 pthread_cancel(myNewThread);
224 /* Cleanup things */
225
226 if (client)
227 avahi_client_free(client);
228
229 if (simple_poll)
230 avahi_simple_poll_free(simple_poll);
231
232 avahi_free((void*)nameService);
233 }
234
235 #ifdef __cplusplus
236 }
237 #endif
238
239