summaryrefslogtreecommitdiff
path: root/unsorted/ecore/efl_net_dialer_udp_example.c
blob: 3b13a60ca7e629ce8b15ac9a872c0ed94e59c856 (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <Efl_Net.h>
#include <Ecore_Getopt.h>
#include <fcntl.h>
#include <ctype.h>

static int needed_reads = 0;

static void
_connected(void *data EINA_UNUSED, const Efl_Event *event)
{
   const char *str;
   Eina_Iterator *it;

   fprintf(stderr,
           "INFO: connected to '%s' (%s)\n"
           "INFO:  - local address=%s\n"
           "INFO:  - read-after-write=%d reads required\n"
           "INFO:  - cork=%hhu\n"
           "INFO:  - timeout_dial=%fs\n"
           "INFO:  - reuse address=%hhu\n"
           "INFO:  - reuse port=%hhu\n"
           "INFO:  - multicast TTL: %u\n"
           "INFO:  - multicast loopback: %u\n"
           "INFO:  - multicast groups:\n",
           efl_net_dialer_address_dial_get(event->object),
           efl_net_socket_address_remote_get(event->object),
           efl_net_socket_address_local_get(event->object),
           needed_reads,
           efl_net_socket_udp_cork_get(event->object),
           efl_net_dialer_timeout_dial_get(event->object),
           efl_net_socket_udp_reuse_address_get(event->object),
           efl_net_socket_udp_reuse_port_get(event->object),
           efl_net_socket_udp_multicast_time_to_live_get(event->object),
           efl_net_socket_udp_multicast_loopback_get(event->object));

   it = efl_net_socket_udp_multicast_groups_get(event->object);
   EINA_ITERATOR_FOREACH(it, str)
     fprintf(stderr, "   * %s\n", str);
   eina_iterator_free(it);
}

static void
_can_read(void *data EINA_UNUSED, const Efl_Event *event)
{
   Eina_Rw_Slice rw_slice;
   Eina_Error err;
   Eina_Bool can_read = efl_io_reader_can_read_get(event->object);

   /* NOTE: this message may appear with can read=0 BEFORE
    * "read '...'" because efl_io_readr_read() will change the status
    * of can_read to FALSE prior to return so we can print it!
    */
   fprintf(stderr, "INFO: can read=%d (needed reads=%d)\n", can_read, needed_reads);
   if (!can_read) return;

   if (!needed_reads) return;

   rw_slice.len = efl_net_socket_udp_next_datagram_size_query(event->object);
   rw_slice.mem = malloc(rw_slice.len);
   EINA_SAFETY_ON_NULL_RETURN(rw_slice.mem);
   err = efl_io_reader_read(event->object, &rw_slice);
   if (err)
     {
        free(rw_slice.mem);
        if (err == EAGAIN) /* EAGAIN for spurious packets */
          return;
        fprintf(stderr, "ERROR: could not read: %s\n", eina_error_msg_get(err));
        efl_loop_quit(efl_loop_get(event->object), eina_value_int_init(EXIT_FAILURE));
        return;
     }

   fprintf(stderr, "INFO: read '" EINA_SLICE_STR_FMT "'\n", EINA_SLICE_STR_PRINT(rw_slice));

   needed_reads--;
   if (!needed_reads)
     efl_loop_quit(efl_loop_get(event->object), EINA_VALUE_EMPTY);
   free(rw_slice.mem);
}

static void
_can_write(void *data EINA_UNUSED, const Efl_Event *event)
{
   static int needed_writes = 2;
   Eina_Slice slice;
   Eina_Error err;
   Eina_Bool can_write = efl_io_writer_can_write_get(event->object);

   /* NOTE: this message may appear with can write=0 BEFORE
    * "wrote '...'" because efl_io_writer_write() will change the status
    * of can_write to FALSE prior to return so we can print it!
    */
   fprintf(stderr, "INFO: can write=%d (needed writes=%d)\n", can_write, needed_writes);
   if (!can_write) return;

   if (needed_writes == 2)
     {
        slice = (Eina_Slice)EINA_SLICE_STR_LITERAL("Hello World");

        err = efl_io_writer_write(event->object, &slice, NULL);
        if (err)
          {
             fprintf(stderr, "ERROR: could not write: %s\n", eina_error_msg_get(err));
             efl_loop_quit(efl_loop_get(event->object), eina_value_int_init(EXIT_FAILURE));
             return;
          }

        fprintf(stderr, "INFO: wrote '" EINA_SLICE_STR_FMT "'\n", EINA_SLICE_STR_PRINT(slice));
     }
   else if (needed_writes == 1)
     {
        slice = (Eina_Slice)EINA_SLICE_STR_LITERAL("Second Write");
        err = efl_io_writer_write(event->object, &slice, NULL);
        if (err)
          {
             fprintf(stderr, "ERROR: could not write: %s\n", eina_error_msg_get(err));
             efl_loop_quit(efl_loop_get(event->object), eina_value_int_init(EXIT_FAILURE));
             return;
          }

        fprintf(stderr, "INFO: wrote '" EINA_SLICE_STR_FMT "'\n", EINA_SLICE_STR_PRINT(slice));

        /* if CORK was used, then say we're done to generate the single final datagram */
        efl_net_socket_udp_cork_set(event->object, EINA_FALSE);
     }
   else if (needed_writes == 0)
     {
        if (needed_reads)
          fprintf(stderr, "INFO: done writing, now wait for %d reads\n", needed_reads);
        else
          {
             fprintf(stderr, "INFO: we're done\n");
             efl_loop_quit(efl_loop_get(event->object), EINA_VALUE_EMPTY);
          }
        return;
     }

   needed_writes--;
}

static void
_resolved(void *data EINA_UNUSED, const Efl_Event *event)
{
   fprintf(stderr, "INFO: resolved %s => %s\n",
           efl_net_dialer_address_dial_get(event->object),
           efl_net_socket_address_remote_get(event->object));
}

static void
_error(void *data EINA_UNUSED, const Efl_Event *event)
{
   const Eina_Error *perr = event->info;
   fprintf(stderr, "INFO: error: %d '%s'\n", *perr, eina_error_msg_get(*perr));

   efl_loop_quit(efl_loop_get(event->object), eina_value_int_init(EXIT_FAILURE));
}

EFL_CALLBACKS_ARRAY_DEFINE(dialer_cbs,
                           { EFL_NET_DIALER_EVENT_CONNECTED, _connected },
                           { EFL_NET_DIALER_EVENT_RESOLVED, _resolved },
                           { EFL_NET_DIALER_EVENT_ERROR, _error },
                           { EFL_IO_READER_EVENT_CAN_READ_CHANGED, _can_read },
                           { EFL_IO_WRITER_EVENT_CAN_WRITE_CHANGED, _can_write }
                           );

static const Ecore_Getopt options = {
  "efl_net_dialer_udp_example", /* program name */
  NULL, /* usage line */
  "1", /* version */
  "(C) 2016 Enlightenment Project", /* copyright */
  "BSD 2-Clause", /* license */
  /* long description, may be multiline and contain \n */
  "Example of Efl_Net_Dialer_Udp usage, sending a message and receiving a reply\n",
  EINA_FALSE,
  {
    ECORE_GETOPT_STORE_STR('b', "bind", "Bind to a particular address in the format IP:PORT."),
    ECORE_GETOPT_STORE_TRUE('r', "read-after-write", "Do a read after writes are done."),
    ECORE_GETOPT_STORE_TRUE('c', "cork", "use UDP_CORK around messages to generate a single datagram."),
    ECORE_GETOPT_STORE_TRUE('R', "dont-route", "Do not route packets via a gateway."),
    ECORE_GETOPT_STORE_DOUBLE('t', "connect-timeout", "timeout in seconds for the connection phase"),
    ECORE_GETOPT_STORE_UINT(0, "multicast-ttl",
                            "Multicast time to live in number of hops from 0-255. Defaults to 1 (only local network)."),
    ECORE_GETOPT_STORE_FALSE(0, "multicast-noloopback",
                            "Disable multicast loopback."),
    ECORE_GETOPT_APPEND('M', "multicast-group", "Join a multicast group in the form 'IP@INTERFACE', with optional '@INTERFACE', where INTERFACE is the IP address of the interface to join the multicast.", ECORE_GETOPT_TYPE_STR),
    ECORE_GETOPT_VERSION('V', "version"),
    ECORE_GETOPT_COPYRIGHT('C', "copyright"),
    ECORE_GETOPT_LICENSE('L', "license"),
    ECORE_GETOPT_HELP('h', "help"),
    ECORE_GETOPT_STORE_METAVAR_STR(0, NULL,
                                   "The address (URL) to dial", "address"),
    ECORE_GETOPT_SENTINEL
  }
};

static Eo *dialer = NULL;

EAPI_MAIN void
efl_pause(void *data EINA_UNUSED,
          const Efl_Event *ev EINA_UNUSED)
{
}

EAPI_MAIN void
efl_resume(void *data EINA_UNUSED,
           const Efl_Event *ev EINA_UNUSED)
{
}

EAPI_MAIN void
efl_terminate(void *data EINA_UNUSED,
              const Efl_Event *ev EINA_UNUSED)
{
   /* FIXME: For the moment the main loop doesn't get
      properly destroyed on shutdown which disallow
      relying on parent destroying their children */
   if (dialer)
     {
        efl_del(dialer);
        dialer = NULL;
     }

   fprintf(stderr, "INFO: main loop finished.\n");
}

EAPI_MAIN void
efl_main(void *data EINA_UNUSED,
         const Efl_Event *ev)
{
   char *address = NULL;
   char *bind_address = NULL;
   Eina_List *mcast_groups = NULL, *lst;
   char *str;
   Eina_Bool cork = EINA_FALSE;
   Eina_Bool do_read = EINA_FALSE;
   Eina_Bool dont_route = EINA_FALSE;
   unsigned mcast_ttl = 1;
   Eina_Bool mcast_loopback = EINA_TRUE;
   Eina_Bool quit_option = EINA_FALSE;
   double timeout_dial = 30.0;
   Ecore_Getopt_Value values[] = {
     ECORE_GETOPT_VALUE_STR(bind_address),
     ECORE_GETOPT_VALUE_BOOL(do_read),
     ECORE_GETOPT_VALUE_BOOL(cork),
     ECORE_GETOPT_VALUE_BOOL(dont_route),
     ECORE_GETOPT_VALUE_DOUBLE(timeout_dial),
     ECORE_GETOPT_VALUE_UINT(mcast_ttl),
     ECORE_GETOPT_VALUE_BOOL(mcast_loopback),
     ECORE_GETOPT_VALUE_LIST(mcast_groups),

     /* standard block to provide version, copyright, license and help */
     ECORE_GETOPT_VALUE_BOOL(quit_option), /* -V/--version quits */
     ECORE_GETOPT_VALUE_BOOL(quit_option), /* -C/--copyright quits */
     ECORE_GETOPT_VALUE_BOOL(quit_option), /* -L/--license quits */
     ECORE_GETOPT_VALUE_BOOL(quit_option), /* -h/--help quits */

     /* positional argument */
     ECORE_GETOPT_VALUE_STR(address),

     ECORE_GETOPT_VALUE_NONE /* sentinel */
   };
   int args;
   Eo *loop;
   Eina_Error err;

   args = ecore_getopt_parse(&options, values, 0, NULL);
   if (args < 0)
     {
        fputs("ERROR: Could not parse command line options.\n", stderr);
        goto end;
     }

   if (quit_option) goto end;

   loop = ev->object;

   args = ecore_getopt_parse_positional(&options, values, 0, NULL, args);
   if (args < 0)
     {
        fputs("ERROR: Could not parse positional arguments.\n", stderr);
        goto end;
     }

   dialer = efl_add(EFL_NET_DIALER_UDP_CLASS, loop,
                    efl_name_set(efl_added, "dialer"),
                    efl_net_socket_udp_bind_set(efl_added, bind_address),
                    efl_net_socket_udp_cork_set(efl_added, cork),
                    efl_net_socket_udp_dont_route_set(efl_added, dont_route),
                    efl_net_socket_udp_reuse_address_set(efl_added, EINA_TRUE), /* optional, but nice for testing */
                    efl_net_socket_udp_reuse_port_set(efl_added, EINA_TRUE), /* optional, but nice for testing... not secure unless you know what you're doing */
                    efl_net_socket_udp_multicast_time_to_live_set(efl_added, mcast_ttl),
                    efl_net_socket_udp_multicast_loopback_set(efl_added, mcast_loopback),
                    efl_net_dialer_timeout_dial_set(efl_added, timeout_dial),
                    efl_event_callback_array_add(efl_added, dialer_cbs(), NULL));

   EINA_LIST_FOREACH(mcast_groups, lst, str)
     efl_net_socket_udp_multicast_join(dialer, str);

   err = efl_net_dialer_dial(dialer, address);
   if (err != 0)
     {
        fprintf(stderr, "ERROR: could not dial '%s': %s",
                address, eina_error_msg_get(err));
        goto no_mainloop;
     }

   if (do_read) needed_reads = cork ? 1 : 2;

   return ;

 no_mainloop:
   efl_del(dialer);

 end:
   EINA_LIST_FREE(mcast_groups, str)
     free(str);

   efl_loop_quit(efl_loop_get(ev->object), eina_value_int_init(EXIT_FAILURE));
}

EFL_MAIN_EX();