summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorCedric BAIL <cedric@efl.so>2013-11-10 08:51:42 +0100
committerCedric BAIL <cedric.bail@samsung.com>2013-12-17 19:37:05 +0900
commit0a1cc79a917b770ef66afe8b99731f6d5754ca44 (patch)
tree99d52907af9d893cb1fc08d75cd1b9f2ab2bfd0c /src/examples
parent99e3ce489adbdc711571fa7d71acbd03a413ec00 (diff)
Ecore_Avahi: initial commit.
TODO: - Add tests, how ? - Integrate with Eo, needed ?
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/ecore_avahi/.gitignore1
-rw-r--r--src/examples/ecore_avahi/Makefile.am35
-rw-r--r--src/examples/ecore_avahi/ecore_avahi_example.c187
3 files changed, 223 insertions, 0 deletions
diff --git a/src/examples/ecore_avahi/.gitignore b/src/examples/ecore_avahi/.gitignore
new file mode 100644
index 0000000000..7190ca0c94
--- /dev/null
+++ b/src/examples/ecore_avahi/.gitignore
@@ -0,0 +1 @@
ecore_avahi_example
diff --git a/src/examples/ecore_avahi/Makefile.am b/src/examples/ecore_avahi/Makefile.am
new file mode 100644
index 0000000000..d447d658b4
--- /dev/null
+++ b/src/examples/ecore_avahi/Makefile.am
@@ -0,0 +1,35 @@
1MAINTAINERCLEANFILES = Makefile.in
2
3AM_CPPFLAGS = \
4-I$(top_builddir)/src/lib/efl \
5-I$(top_srcdir)/src/lib/eina \
6-I$(top_srcdir)/src/lib/eo \
7-I$(top_srcdir)/src/lib/ecore \
8-I$(top_srcdir)/src/lib/ecore_avahi\
9-I$(top_builddir)/src/lib/eina \
10-I$(top_builddir)/src/lib/eo \
11-I$(top_builddir)/src/lib/ecore \
12-I$(top_builddir)/src/lib/ecore_avahi \
13@AVAHI_CLIENT_CFLAGS@
14
15EXTRA_PROGRAMS = \
16ecore_avahi_example
17
18ecore_avahi_example_SOURCES = ecore_avahi_example.c
19ecore_avahi_example_LDADD = $(top_builddir)/src/lib/ecore/libecore.la \
20$(top_builddir)/src/lib/ecore_avahi/libecore_avahi.la \
21$(top_builddir)/src/lib/eo/libeo.la \
22$(top_builddir)/src/lib/eina/libeina.la \
23@AVAHI_CLIENT_LIBS@
24
25examples: $(EXTRA_PROGRAMS)
26
27clean-local:
28 rm -f $(EXTRA_PROGRAMS)
29
30install-examples:
31 mkdir -p $(datadir)/ecore_avahi/examples
32 $(install_sh_DATA) -c ecore_avahi_example.c $(datadir)/ecore_avahi/examples
33
34uninstall-local:
35 rm -f $(datadir)/ecore_avahi/examples/ecore_avahi_example.c
diff --git a/src/examples/ecore_avahi/ecore_avahi_example.c b/src/examples/ecore_avahi/ecore_avahi_example.c
new file mode 100644
index 0000000000..f9f247ef2d
--- /dev/null
+++ b/src/examples/ecore_avahi/ecore_avahi_example.c
@@ -0,0 +1,187 @@
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
23typedef struct _Ecore_Avahi_Example Ecore_Avahi_Example;
24struct _Ecore_Avahi_Example
25{
26 AvahiClient *client;
27 AvahiEntryGroup *group;
28 const char *server;
29 int port;
30};
31
32static void
33_ecore_avahi_group_cb(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata)
34{
35 Ecore_Avahi_Example *example = userdata;
36
37 switch (state)
38 {
39 case AVAHI_ENTRY_GROUP_ESTABLISHED :
40 /* The entry group has been established successfully */
41 fprintf(stderr, "Service '%s' successfully established.\n", example->server);
42 break;
43
44 case AVAHI_ENTRY_GROUP_COLLISION :
45 fprintf(stderr, "Service name collision.\n");
46 ecore_main_loop_quit();
47 break;
48
49 case AVAHI_ENTRY_GROUP_FAILURE :
50
51 fprintf(stderr, "Entry group failure: %s\n", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
52 /* Some kind of failure happened while we were registering our services */
53 ecore_main_loop_quit();
54 break;
55
56 case AVAHI_ENTRY_GROUP_UNCOMMITED:
57 case AVAHI_ENTRY_GROUP_REGISTERING:
58 ;
59 }
60}
61
62static void
63_ecore_avahi_service_create(AvahiClient *c, Ecore_Avahi_Example *example)
64{
65 int error;
66
67 example->group = avahi_entry_group_new(c, _ecore_avahi_group_cb, example);
68 if (!example->group)
69 {
70 fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_client_errno(c)));
71 goto fail;
72 }
73
74 /* If the group is empty (either because it was just created, or
75 * because it was reset previously, add our entries. */
76 if (!avahi_entry_group_is_empty(example->group)) return ;
77
78 error = avahi_entry_group_add_service(example->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0,
79 example->server, "_ipp._tcp", NULL, NULL, example->port, "name=ThisIsATest", NULL);
80 if (error < 0)
81 {
82 fprintf(stderr, "Failed to add _ipp._tcp service with error: %s.\n", avahi_strerror(error));
83 goto fail;
84 }
85
86 error = avahi_entry_group_commit(example->group);
87 if (error < 0)
88 {
89 fprintf(stderr, "Failed to commit entry group with error: %s\n", avahi_strerror(error));
90 goto fail;
91 }
92
93 return ;
94
95 fail:
96 ecore_main_loop_quit();
97}
98
99static void
100_ecore_avahi_client_cb(AvahiClient *c, AvahiClientState state, void * userdata)
101{
102 Ecore_Avahi_Example *example = userdata;
103
104 switch (state)
105 {
106 case AVAHI_CLIENT_S_RUNNING:
107 /* The server has started successfully and registered its host
108 * name on the network, so it's time to create our services */
109 _ecore_avahi_service_create(c, example);
110 break;
111 case AVAHI_CLIENT_FAILURE:
112 fprintf(stderr, "Avahi client failure: %s\n", avahi_strerror(avahi_client_errno(c)));
113 break;
114 case AVAHI_CLIENT_S_COLLISION:
115 /* Let's drop our registered services. When the server is back
116 * in AVAHI_SERVER_RUNNING state we will register them
117 * again with the new host name. */
118 case AVAHI_CLIENT_S_REGISTERING:
119 /* The server records are now being established. This
120 * might be caused by a host name change. We need to wait
121 * for our own records to register until the host name is
122 * properly esatblished. */
123 if (example->group) avahi_entry_group_reset(example->group);
124 break;
125 case AVAHI_CLIENT_CONNECTING:
126 ;
127 }
128}
129
130int
131main(int argc, char **argv)
132{
133 Ecore_Avahi_Example example = { 0 };
134 Ecore_Avahi *handler;
135 const AvahiPoll *poll_api;
136 int exit_code = 0;
137 int error = 0;
138
139 if (argc < 3)
140 {
141 fprintf(stderr, "Usage : %s name port\n", argv[0]);
142 return -1;
143 }
144
145 eina_init();
146 ecore_init();
147
148 handler = ecore_avahi_add();
149 poll_api = ecore_avahi_poll_get(handler);
150
151 if (!poll_api)
152 {
153 fprintf(stderr, "Build EFL with Avahi support.\n");
154 exit_code = -1;
155 goto fail;
156 }
157
158 example.server = eina_stringshare_add(argv[1]);
159 example.port = atoi(argv[2]);
160 example.client = avahi_client_new(poll_api, AVAHI_CLIENT_NO_FAIL, _ecore_avahi_client_cb, &example, &error);
161
162 if (!example.client)
163 {
164 fprintf(stderr, "Failed to create avahi client: %s.\n", avahi_strerror(error));
165 exit_code = -1;
166 goto fail;
167 }
168
169 ecore_main_loop_begin();
170
171 avahi_client_free(example.client);
172
173 fail:
174 eina_stringshare_del(example.server);
175
176 ecore_shutdown();
177 eina_shutdown();
178
179 return exit_code;
180}
181#else
182int
183main(int argc, char **argv)
184{
185 fprintf(stderr, "This example require Avahi to be build !\n");
186}
187#endif