summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorCedric Bail <cedric.bail@free.fr>2014-02-27 20:08:17 -0300
committerCedric Bail <cedric.bail@free.fr>2014-02-27 20:09:09 -0300
commit0e1c064fd0749eeae37a243bdcbb1c66b66eff67 (patch)
tree05268a88d86bcce696d30aa0964a99aba962e450 /src/examples
parentb955ea0b9e86c00ed4d4538aed0eea1536bcda27 (diff)
ecore_avahi: add an avahi browse example (that doesn't work yet).
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/ecore_avahi/.gitignore1
-rw-r--r--src/examples/ecore_avahi/Makefile.am12
-rw-r--r--src/examples/ecore_avahi/ecore_avahi_browse_example.c221
3 files changed, 233 insertions, 1 deletions
diff --git a/src/examples/ecore_avahi/.gitignore b/src/examples/ecore_avahi/.gitignore
index 7190ca0c94..11a383d416 100644
--- a/src/examples/ecore_avahi/.gitignore
+++ b/src/examples/ecore_avahi/.gitignore
@@ -1 +1,2 @@
1ecore_avahi_example 1ecore_avahi_example
2ecore_avahi_browse_example
diff --git a/src/examples/ecore_avahi/Makefile.am b/src/examples/ecore_avahi/Makefile.am
index d447d658b4..462ece3f50 100644
--- a/src/examples/ecore_avahi/Makefile.am
+++ b/src/examples/ecore_avahi/Makefile.am
@@ -13,7 +13,8 @@ AM_CPPFLAGS = \
13@AVAHI_CLIENT_CFLAGS@ 13@AVAHI_CLIENT_CFLAGS@
14 14
15EXTRA_PROGRAMS = \ 15EXTRA_PROGRAMS = \
16ecore_avahi_example 16ecore_avahi_example \
17ecore_avahi_browse_example
17 18
18ecore_avahi_example_SOURCES = ecore_avahi_example.c 19ecore_avahi_example_SOURCES = ecore_avahi_example.c
19ecore_avahi_example_LDADD = $(top_builddir)/src/lib/ecore/libecore.la \ 20ecore_avahi_example_LDADD = $(top_builddir)/src/lib/ecore/libecore.la \
@@ -22,6 +23,13 @@ $(top_builddir)/src/lib/eo/libeo.la \
22$(top_builddir)/src/lib/eina/libeina.la \ 23$(top_builddir)/src/lib/eina/libeina.la \
23@AVAHI_CLIENT_LIBS@ 24@AVAHI_CLIENT_LIBS@
24 25
26ecore_avahi_browse_example_SOURCES = ecore_avahi_browse_example.c
27ecore_avahi_browse_example_LDADD = $(top_builddir)/src/lib/ecore/libecore.la \
28$(top_builddir)/src/lib/ecore_avahi/libecore_avahi.la \
29$(top_builddir)/src/lib/eo/libeo.la \
30$(top_builddir)/src/lib/eina/libeina.la \
31@AVAHI_CLIENT_LIBS@
32
25examples: $(EXTRA_PROGRAMS) 33examples: $(EXTRA_PROGRAMS)
26 34
27clean-local: 35clean-local:
@@ -29,7 +37,9 @@ clean-local:
29 37
30install-examples: 38install-examples:
31 mkdir -p $(datadir)/ecore_avahi/examples 39 mkdir -p $(datadir)/ecore_avahi/examples
40 $(install_sh_DATA) -c ecore_avahi_browse_example.c $(datadir)/ecore_avahi/examples
32 $(install_sh_DATA) -c ecore_avahi_example.c $(datadir)/ecore_avahi/examples 41 $(install_sh_DATA) -c ecore_avahi_example.c $(datadir)/ecore_avahi/examples
33 42
34uninstall-local: 43uninstall-local:
35 rm -f $(datadir)/ecore_avahi/examples/ecore_avahi_example.c 44 rm -f $(datadir)/ecore_avahi/examples/ecore_avahi_example.c
45 rm -f $(datadir)/ecore_avahi/examples/ecore_avahi_browse_example.c
diff --git a/src/examples/ecore_avahi/ecore_avahi_browse_example.c b/src/examples/ecore_avahi/ecore_avahi_browse_example.c
new file mode 100644
index 0000000000..b3ca07c723
--- /dev/null
+++ b/src/examples/ecore_avahi/ecore_avahi_browse_example.c
@@ -0,0 +1,221 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <stdlib.h>
6#include <stdio.h>
7
8#ifdef HAVE_AVAHI
9
10#include <avahi-client/client.h>
11#include <avahi-client/lookup.h>
12#include <avahi-client/publish.h>
13
14#include <avahi-common/simple-watch.h>
15#include <avahi-common/malloc.h>
16#include <avahi-common/error.h>
17#include <avahi-common/alternative.h>
18#include <avahi-common/timeval.h>
19
20#include <Ecore.h>
21#include <Ecore_Avahi.h>
22
23#define FREE_CLEAN(h, cb) do { if (h) cb(h); h = NULL; } while (0);
24
25typedef struct _Ecore_Avahi_Example Ecore_Avahi_Example;
26struct _Ecore_Avahi_Example
27{
28 AvahiServiceBrowser *sb;
29 AvahiClient *client;
30 const char *name;
31};
32
33static void
34resolve_callback(AvahiServiceResolver *r, AvahiIfIndex interface EINA_UNUSED,
35 AvahiProtocol protocol EINA_UNUSED, AvahiResolverEvent event,
36 const char *name, const char *type, const char *domain,
37 const char *host_name, const AvahiAddress *address, uint16_t port,
38 AvahiStringList *txt, AvahiLookupResultFlags flags,
39 void* userdata EINA_UNUSED)
40{
41 assert(r);
42
43 /* Called whenever a service has been resolved successfully or timed out */
44
45 switch (event) {
46 case AVAHI_RESOLVER_FAILURE:
47 fprintf(stderr, "(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(avahi_service_resolver_get_client(r))));
48 break;
49
50 case AVAHI_RESOLVER_FOUND: {
51 char a[AVAHI_ADDRESS_STR_MAX], *t;
52
53 fprintf(stderr, "Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
54
55 avahi_address_snprint(a, sizeof(a), address);
56 t = avahi_string_list_to_string(txt);
57 fprintf(stderr,
58 "\t%s:%u (%s)\n"
59 "\tTXT=%s\n"
60 "\tcookie is %u\n"
61 "\tis_local: %i\n"
62 "\tour_own: %i\n"
63 "\twide_area: %i\n"
64 "\tmulticast: %i\n"
65 "\tcached: %i\n",
66 host_name, port, a,
67 t,
68 avahi_string_list_get_service_cookie(txt),
69 !!(flags & AVAHI_LOOKUP_RESULT_LOCAL),
70 !!(flags & AVAHI_LOOKUP_RESULT_OUR_OWN),
71 !!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA),
72 !!(flags & AVAHI_LOOKUP_RESULT_MULTICAST),
73 !!(flags & AVAHI_LOOKUP_RESULT_CACHED));
74
75 avahi_free(t);
76 }
77 }
78
79 avahi_service_resolver_free(r);
80}
81
82static void
83_browse_callback(AvahiServiceBrowser *b, AvahiIfIndex interface,
84 AvahiProtocol protocol, AvahiBrowserEvent event,
85 const char *name, const char *type, const char *domain,
86 AvahiLookupResultFlags flags EINA_UNUSED,
87 void* userdata)
88{
89 AvahiClient *c = userdata;
90 assert(b);
91
92 /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
93
94 switch (event) {
95 case AVAHI_BROWSER_FAILURE:
96
97 fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
98 ecore_main_loop_quit();
99 return;
100
101 case AVAHI_BROWSER_NEW:
102 fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
103
104 /* We ignore the returned resolver object. In the callback
105 function we free it. If the server is terminated before
106 the callback function is called the server will free
107 the resolver for us. */
108
109 if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, c)))
110 fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c)));
111
112 break;
113
114 case AVAHI_BROWSER_REMOVE:
115 fprintf(stderr, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
116 break;
117
118 case AVAHI_BROWSER_ALL_FOR_NOW:
119 case AVAHI_BROWSER_CACHE_EXHAUSTED:
120 fprintf(stderr, "(Browser) %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
121 break;
122 }
123}
124
125static void
126_ecore_avahi_client_cb(AvahiClient *c, AvahiClientState state, void * userdata)
127{
128 Ecore_Avahi_Example *example = userdata;
129
130 fprintf(stderr, "_ecore_avahi_client_cb: %i\n", state);
131 switch (state)
132 {
133 case AVAHI_CLIENT_S_REGISTERING:
134 case AVAHI_CLIENT_S_COLLISION:
135 case AVAHI_CLIENT_S_RUNNING:
136 fprintf(stderr, "time to create sb\n");
137 if (!example->sb)
138 {
139 example->sb = avahi_service_browser_new(c, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_ipp._tcp", NULL, 0, _browse_callback, c);
140 if (!example->sb)
141 {
142 fprintf(stderr, "Failed to setup an Avahi Service Browser.");
143 ecore_main_loop_quit();
144 return ;
145 }
146 }
147 break;
148
149 case AVAHI_CLIENT_FAILURE:
150 fprintf(stderr, "Server connection failure: %s.", avahi_strerror(avahi_client_errno(c)));
151 if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED)
152 {
153 FREE_CLEAN(example->sb, avahi_service_browser_free);
154 FREE_CLEAN(example->client, avahi_client_free);
155
156 ecore_main_loop_quit();
157
158 break;
159 }
160 case AVAHI_CLIENT_CONNECTING:
161 fprintf(stderr, "not connected yet\n");
162 FREE_CLEAN(example->sb, avahi_service_browser_free);
163 break;
164 }
165}
166
167int
168main(int argc, char **argv)
169{
170 Ecore_Avahi_Example example = { 0 };
171 Ecore_Avahi *handler;
172 const AvahiPoll *poll_api;
173 int exit_code = 0;
174 int error = 0;
175
176 if (argc < 1)
177 {
178 fprintf(stderr, "Usage : %s\n", argv[0]);
179 return -1;
180 }
181
182 eina_init();
183 ecore_init();
184
185 handler = ecore_avahi_add();
186 poll_api = ecore_avahi_poll_get(handler);
187
188 if (!poll_api)
189 {
190 fprintf(stderr, "Build EFL with Avahi support.\n");
191 exit_code = -1;
192 goto fail;
193 }
194
195 example.name = NULL;
196 example.client = avahi_client_new(poll_api, AVAHI_CLIENT_NO_FAIL, _ecore_avahi_client_cb, &example, &error);
197
198 if (!example.client)
199 {
200 fprintf(stderr, "Failed to create avahi client: %s.\n", avahi_strerror(error));
201 exit_code = -1;
202 goto fail;
203 }
204
205 ecore_main_loop_begin();
206
207 avahi_client_free(example.client);
208
209 fail:
210 ecore_shutdown();
211 eina_shutdown();
212
213 return exit_code;
214}
215#else
216int
217main(int argc, char **argv)
218{
219 fprintf(stderr, "This example require Avahi to be build !\n");
220}
221#endif