summaryrefslogtreecommitdiff
path: root/unsorted/ecore/efl_io_queue_example.c
blob: c21c71e59ac58624a79fafff5376e350f3c78bac (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <Efl_Net.h>
#include <Ecore_Getopt.h>

static Eina_List *waiting = NULL;
static Eina_List *commands = NULL;
static Eina_Slice line_delimiter;
static Eo *send_queue, *receive_queue;

static void
_command_next(void)
{
   Eina_Slice slice;
   char *cmd;

   if (!commands)
     {
        efl_io_queue_eos_mark(send_queue);
        return;
     }

   cmd = commands->data;
   commands = eina_list_remove_list(commands, commands);

   slice = (Eina_Slice)EINA_SLICE_STR(cmd);
   efl_io_writer_write(send_queue, &slice, NULL);
   fprintf(stderr, "INFO: sent '" EINA_SLICE_STR_FMT "'\n",
           EINA_SLICE_STR_PRINT(slice));

   /* don't use line_delimiter directly, 'len' may be changed! */
   slice = line_delimiter;
   efl_io_writer_write(send_queue, &slice, NULL);
   free(cmd);
}

static void
_receiver_data(void *data EINA_UNUSED, const Efl_Event *event)
{
   Eina_Slice slice = efl_io_queue_slice_get(event->object);

   /* this will happen when we're called when we issue our own
    * efl_io_queue_clear() below.
    */
   if (slice.len == 0) return;

   /*
    * If the server didn't send us the line terminator and closed the
    * connection (ie: efl_io_reader_eos_get() == true) or if the
    * efl_io_copier_buffer_limit_set() was reached (note we do not set
    * it in this example), then we may have a line without a trailing
    * delimiter. Check for that.
    */
   if (!eina_slice_endswith(slice,  line_delimiter))
     {
        fprintf(stderr, "WARNING: received without line-delimiter '"
                EINA_SLICE_STR_FMT "'\n",
                EINA_SLICE_STR_PRINT(slice));
     }
   else
     {
        slice.len -= line_delimiter.len;
        fprintf(stderr, "INFO: received '" EINA_SLICE_STR_FMT "'\n",
                EINA_SLICE_STR_PRINT(slice));
     }

   efl_io_queue_clear(event->object);
   _command_next();
}

static void
_dialer_connected(void *data EINA_UNUSED, const Efl_Event *event)
{
   fprintf(stderr, "INFO: connected to %s (%s)\n",
           efl_net_dialer_address_dial_get(event->object),
           efl_net_socket_address_remote_get(event->object));

   _command_next();
}

static void
_copier_done(void *data EINA_UNUSED, const Efl_Event *event)
{
   fprintf(stderr, "INFO: %s done\n", efl_name_get(event->object));

   waiting = eina_list_remove(waiting, event->object);
   if (!waiting)
     efl_loop_quit(efl_loop_get(event->object), EINA_VALUE_EMPTY);
}

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

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

EFL_CALLBACKS_ARRAY_DEFINE(copier_cbs,
                           { EFL_IO_COPIER_EVENT_DONE, _copier_done },
                           { EFL_IO_COPIER_EVENT_ERROR, _copier_error });

static char *
_unescape(const char *str)
{
   char *ret = strdup(str);
   char *c, *w;
   Eina_Bool escaped = EINA_FALSE;

   for (c = ret, w = ret; *c != '\0'; c++)
     {
        if (escaped)
          {
             escaped = EINA_FALSE;
             switch (*c)
               {
                case 'n': *w = '\n'; break;
                case 'r': *w = '\r'; break;
                case 't': *w = '\t'; break;
                default: w++; /* no change */
               }
             w++;
          }
        else
          {
             if (*c == '\\')
               escaped = EINA_TRUE;
             else
               w++;
          }
     }
   *w = '\0';
   return ret;
}

static const Ecore_Getopt options = {
  "efl_io_queue_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_Io_Queue usage.\n"
  "\n"
  "This uses Efl_Io_Queue to easily interface with Efl_Io_Copier in order to "
  "send commands to a TCP server.",
  EINA_FALSE,
  {
    ECORE_GETOPT_STORE_STR('d', "line-delimiter",
                           "Changes the line delimiter to be used in both send and receive. Defaults to \\r\\n"),
    ECORE_GETOPT_STORE_ULONG('l', "buffer-limit",
                             "If set will limit buffer size to this limit of bytes. If used alongside with --line-delimiter and that delimiter was not found but bffer limit was reached, the line event will be triggered without the delimiter at the end."),
    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 server address as\n"
                                   "IP:PORT to connect using TCP and an IPv4 (A.B.C.D:PORT) or IPv6 ([A:B:C:D::E]:PORT).\n",
                                   "server_address"),
    ECORE_GETOPT_APPEND_METAVAR(0, NULL,
                                "Commands to send",
                                "commands",
                                ECORE_GETOPT_TYPE_STR),
    ECORE_GETOPT_SENTINEL
  }
};

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)
{
  if (waiting)
    {
       fprintf(stderr, "ERROR: %d operations were waiting!\n",
               eina_list_count(waiting));
       eina_list_free(waiting);
       waiting = NULL;
    }
}

EAPI_MAIN void
efl_main(void *data EINA_UNUSED,
         const Efl_Event *ev)
{
   char *address = NULL;
   char *line_delimiter_str = NULL;
   char *cmd;
   unsigned long buffer_limit = 0;
   Eina_Bool quit_option = EINA_FALSE;
   Ecore_Getopt_Value values[] = {
     ECORE_GETOPT_VALUE_STR(line_delimiter_str),
     ECORE_GETOPT_VALUE_ULONG(buffer_limit),

     /* 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_LIST(commands),

     ECORE_GETOPT_VALUE_NONE /* sentinel */
   };
   Eina_Error err;
   int args;
   Eo *dialer, *sender, *receiver, *loop;

   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;

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

   line_delimiter_str = _unescape(line_delimiter_str ? line_delimiter_str : "\\r\\n");

   if (!commands)
     {
        fputs("ERROR: missing commands to send.\n", stderr);
        goto end;
     }

   line_delimiter = (Eina_Slice)EINA_SLICE_STR(line_delimiter_str);

   /*
    * Without a send_queue we'd have to manually implement an
    * Efl_Io_Reader object that would provide partial data when
    * Efl_Io_Reader.read() is called by Efl_Io_Copier. This is
    * cumbersome... we just want to write a full command and have the
    * queue to handle that for us.
    *
    * Our example's usage is to write each command at once followed by
    * the line_delimiter, then wait for a reply from the server, then
    * write another.
    */
   send_queue = efl_add_ref(EFL_IO_QUEUE_CLASS, NULL,
                        efl_name_set(efl_added, "send_queue"),
                        efl_io_queue_limit_set(efl_added, buffer_limit));
   if (!send_queue)
     {
        fprintf(stderr, "ERROR: could not create Efl_Io_Queue (send)\n");
        goto end;
     }

   /*
    * Without a receive_queue we'd have to manually implement an
    * Efl_Io_Writer object that would handle write of partial data
    * with Efl_Io_Writer.write() is called by Efl_Io_Copier.
    *
    * For output we could have another solution as well: use NULL
    * destination and handle "line" or "data" events manually,
    * stealing the buffer so it doesn't grow.
    *
    * Our example's usage is to peek its data with slice_get() then
    * clear().
    */
   receive_queue = efl_add_ref(EFL_IO_QUEUE_CLASS, NULL,
                           efl_name_set(efl_added, "receive_queue"),
                           efl_io_queue_limit_set(efl_added, buffer_limit),
                           efl_event_callback_add(efl_added, EFL_IO_QUEUE_EVENT_SLICE_CHANGED, _receiver_data, NULL));
   if (!receive_queue)
     {
        fprintf(stderr, "ERROR: could not create Efl_Io_Queue (receive)\n");
        goto error_receive_queue;
     }

   /*
    * From here on it's mostly the same all Efl_Io_Copier would do,
    * check efl_io_copier_simple_example.c and efl_io_copier_example.c
    */

   /*
    * some objects such as the Efl.Io.Copier and Efl.Net.Dialer.Tcp
    * depend on main loop, thus their parent must be a loop
    * provider. We use the loop itself.
    */
   loop = ev->object;

   /* The TCP client to use to send/receive network data */
   dialer = efl_add(EFL_NET_DIALER_TCP_CLASS, loop,
                    efl_name_set(efl_added, "dialer"),
                    efl_event_callback_add(efl_added, EFL_NET_DIALER_EVENT_DIALER_CONNECTED, _dialer_connected, NULL));
   if (!dialer)
     {
        fprintf(stderr, "ERROR: could not create Efl_Net_Dialer_Tcp\n");
        goto error_dialer;
     }

   /* sender: send_queue->network */
   sender = efl_add(EFL_IO_COPIER_CLASS, loop,
                    efl_name_set(efl_added, "sender"),
                    efl_io_copier_line_delimiter_set(efl_added, line_delimiter),
                    efl_io_copier_source_set(efl_added, send_queue),
                    efl_io_copier_destination_set(efl_added, dialer),
                    efl_event_callback_array_add(efl_added, copier_cbs(), NULL));
   if (!sender)
     {
        fprintf(stderr, "ERROR: could not create Efl_Io_Copier (sender)\n");
        goto error_sender;
     }

   /* receiver: network->receive_queue */
   receiver = efl_add(EFL_IO_COPIER_CLASS, loop,
                      efl_name_set(efl_added, "receiver"),
                      efl_io_copier_line_delimiter_set(efl_added, line_delimiter),
                      efl_io_copier_source_set(efl_added, dialer),
                      efl_io_copier_destination_set(efl_added, receive_queue),
                      efl_event_callback_array_add(efl_added, copier_cbs(), NULL));
   if (!receiver)
     {
        fprintf(stderr, "ERROR: could not create Efl_Io_Copier (receiver)\n");
        goto error_receiver;
     }

   err = efl_net_dialer_dial(dialer, address);
   if (err)
     {
        fprintf(stderr, "ERROR: could not dial %s: %s\n",
                address, eina_error_msg_get(err));
        goto error_dialing;
     }

   waiting = eina_list_append(waiting, sender);
   waiting = eina_list_append(waiting, receiver);

   return ;

 error_dialing:
   efl_io_closer_close(receiver);
   efl_del(receiver);
 error_receiver:
   efl_io_closer_close(sender);
   efl_del(sender);
 error_sender:
   efl_del(dialer);
 error_dialer:
   efl_unref(receive_queue);
 error_receive_queue:
   efl_unref(send_queue);
 end:
   EINA_LIST_FREE(commands, cmd)
     {
        fprintf(stderr, "ERROR: unsent command: %s\n", cmd);
        free(cmd);
     }

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

EFL_MAIN_EX();