summaryrefslogtreecommitdiff
path: root/src/examples/ecore_avahi/ecore_avahi_example.c
blob: f9f247ef2de69ba9e823a185f53c02dd1089e5bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>

#ifdef HAVE_AVAHI

#include <avahi-client/client.h>
#include <avahi-client/lookup.h>
#include <avahi-client/publish.h>

#include <avahi-common/simple-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
#include <avahi-common/alternative.h>
#include <avahi-common/timeval.h>

#include <Ecore.h>
#include <Ecore_Avahi.h>

typedef struct _Ecore_Avahi_Example Ecore_Avahi_Example;
struct _Ecore_Avahi_Example
{
   AvahiClient *client;
   AvahiEntryGroup *group;
   const char *server;
   int port;
};

static void
_ecore_avahi_group_cb(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata)
{
   Ecore_Avahi_Example *example = userdata;

   switch (state)
     {
      case AVAHI_ENTRY_GROUP_ESTABLISHED :
         /* The entry group has been established successfully */
         fprintf(stderr, "Service '%s' successfully established.\n", example->server);
         break;

      case AVAHI_ENTRY_GROUP_COLLISION :
	 fprintf(stderr, "Service name collision.\n");
	 ecore_main_loop_quit();
	 break;

      case AVAHI_ENTRY_GROUP_FAILURE :

         fprintf(stderr, "Entry group failure: %s\n", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
         /* Some kind of failure happened while we were registering our services */
	 ecore_main_loop_quit();
         break;

      case AVAHI_ENTRY_GROUP_UNCOMMITED:
      case AVAHI_ENTRY_GROUP_REGISTERING:
         ;
     }
}

static void
_ecore_avahi_service_create(AvahiClient *c, Ecore_Avahi_Example *example)
{
   int error;

   example->group = avahi_entry_group_new(c, _ecore_avahi_group_cb, example);
   if (!example->group)
     {
	fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_client_errno(c)));
	goto fail;
     }

   /* If the group is empty (either because it was just created, or
    * because it was reset previously, add our entries.  */
   if (!avahi_entry_group_is_empty(example->group)) return ;

   error = avahi_entry_group_add_service(example->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0,
					 example->server, "_ipp._tcp", NULL, NULL, example->port, "name=ThisIsATest", NULL);
   if (error < 0)
     {
	fprintf(stderr, "Failed to add _ipp._tcp service with error: %s.\n", avahi_strerror(error));
	goto fail;
     }

   error = avahi_entry_group_commit(example->group);
   if (error < 0)
     {
	fprintf(stderr, "Failed to commit entry group with error: %s\n", avahi_strerror(error));
	goto fail;
     }

   return ;

 fail:
   ecore_main_loop_quit();
}

static void
_ecore_avahi_client_cb(AvahiClient *c, AvahiClientState state, void * userdata)
{
   Ecore_Avahi_Example *example = userdata;

   switch (state)
     {
      case AVAHI_CLIENT_S_RUNNING:
         /* The server has started successfully and registered its host
          * name on the network, so it's time to create our services */
         _ecore_avahi_service_create(c, example);
         break;
      case AVAHI_CLIENT_FAILURE:
         fprintf(stderr, "Avahi client failure: %s\n", avahi_strerror(avahi_client_errno(c)));
         break;
      case AVAHI_CLIENT_S_COLLISION:
         /* Let's drop our registered services. When the server is back
          * in AVAHI_SERVER_RUNNING state we will register them
          * again with the new host name. */
      case AVAHI_CLIENT_S_REGISTERING:
         /* The server records are now being established. This
          * might be caused by a host name change. We need to wait
          * for our own records to register until the host name is
          * properly esatblished. */
         if (example->group) avahi_entry_group_reset(example->group);
         break;
      case AVAHI_CLIENT_CONNECTING:
         ;
     }
}

int
main(int argc, char **argv)
{
   Ecore_Avahi_Example example = { 0 };
   Ecore_Avahi *handler;
   const AvahiPoll *poll_api;
   int exit_code = 0;
   int error = 0;

   if (argc < 3)
     {
	fprintf(stderr, "Usage : %s name port\n", argv[0]);
        return -1;
     }

   eina_init();
   ecore_init();

   handler = ecore_avahi_add();
   poll_api = ecore_avahi_poll_get(handler);

   if (!poll_api)
     {
	fprintf(stderr, "Build EFL with Avahi support.\n");
	exit_code = -1;
	goto fail;
     }

   example.server = eina_stringshare_add(argv[1]);
   example.port = atoi(argv[2]);
   example.client = avahi_client_new(poll_api, AVAHI_CLIENT_NO_FAIL, _ecore_avahi_client_cb, &example, &error);

   if (!example.client)
     {
	fprintf(stderr, "Failed to create avahi client: %s.\n", avahi_strerror(error));
	exit_code = -1;
	goto fail;
     }

   ecore_main_loop_begin();

   avahi_client_free(example.client);

 fail:
   eina_stringshare_del(example.server);

   ecore_shutdown();
   eina_shutdown();

   return exit_code;
}
#else
int
main(int argc, char **argv)
{
   fprintf(stderr, "This example require Avahi to be build !\n");
}
#endif