Tizen 2.1 base
[framework/uifw/ecore.git] / src / examples / ecore_pipe_simple_example.c
1 //Compile with:
2 //gcc -g -Wall `pkg-config --cflags --libs ecore` -o ecore_pipe_simple_example ecore_pipe_simple_example.c
3
4 #include <unistd.h>
5 #include <Ecore.h>
6
7 static void
8 do_lengthy_task(Ecore_Pipe *pipe)
9 {
10    int i, j;
11    char *buffer;
12    for (i = 0; i < 20; i++)
13      {
14         sleep(1);
15         buffer = malloc(sizeof(char) * i);
16         for (j = 0; j < i; j++)
17           buffer[j] = 'a' + j;
18         ecore_pipe_write(pipe, buffer, i);
19         free(buffer);
20      }
21    ecore_pipe_write(pipe, "close", 5);
22 }
23
24 static void
25 handler(void *data, void *buf, unsigned int len)
26 {
27    char *str = malloc(sizeof(char) * len + 1);
28    memcpy(str, buf, len);
29    str[len] = '\0';
30    printf("received %d bytes\n", len);
31    printf("content: %s\n", (const char *)str);
32    free(str);
33    if (len && !strncmp(buf, "close", len < 5 ? len : 5))
34      {
35         printf("close requested\n");
36         ecore_main_loop_quit();
37      }
38 }
39
40 int
41 main(int argc, char *argv[])
42 {
43    Ecore_Pipe *pipe;
44    pid_t child_pid;
45
46    ecore_init();
47
48    pipe = ecore_pipe_add(handler, NULL);
49
50    child_pid = fork();
51    if (!child_pid)
52      {
53         ecore_pipe_read_close(pipe);
54         do_lengthy_task(pipe);
55      }
56    else
57      {
58         ecore_pipe_write_close(pipe);
59         ecore_main_loop_begin();
60      }
61
62    ecore_pipe_del(pipe);
63    ecore_shutdown();
64
65    return 0;
66 }
67