summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorLauro Moura <lauromoura@expertisesolutions.com.br>2016-04-12 16:00:23 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-05-25 21:32:03 -0300
commit7241b7375de1eb4327f5bca21dcfc5a889b9eb72 (patch)
treececbe8991d1b38db8f8c67965ae2a5e6cf2aa503 /src/examples
parentde9be13d458f2739a536e5863aa8caddd3f7f715 (diff)
eio: Adds Eo-based Eio API
The legacy Eio_File factory functions are replaced by an Eo object called Eo_Job that return promises wrapping the async file operations. With this commit, the legacy Eio callbacks are replaced by the following Eo/Promises counterparts : * Done_Cb -> Promise then success callback * Error_Cb -> Promise then error callback * Main_Cb -> Promise progress callback * Filter_Cb -> Job object event (more below) Events are used to deliver and get the filter data. To differentiate between the named and direct versions, they come in "filter,direct" and "filter,name" versions. Monitors were wrapped inside a new class Eo_Sentry. The user creates a sentry object and adds monitoring targets to it, listening to events on it. The sentry event info is composed of two strings. The source string is the path being monitored, i.e. the one passed to eio_sentry_add, and the trigger string is the path that actually triggered the event, e.g. a new file created in a monitored directory.
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/eio/Makefile.am44
-rw-r--r--src/examples/eio/eio_job_ls.c79
-rw-r--r--src/examples/eio/eio_job_open.c80
-rw-r--r--src/examples/eio/eio_job_open_multi.c103
-rw-r--r--src/examples/eio/eio_sentry.c55
5 files changed, 350 insertions, 11 deletions
diff --git a/src/examples/eio/Makefile.am b/src/examples/eio/Makefile.am
index 53b95fe..4fccc70 100644
--- a/src/examples/eio/Makefile.am
+++ b/src/examples/eio/Makefile.am
@@ -24,31 +24,53 @@ endif
24 24
25EXTRA_PROGRAMS = \ 25EXTRA_PROGRAMS = \
26eio_file_ls \ 26eio_file_ls \
27eio_file_copy 27eio_file_copy \
28eio_job_open \
29eio_job_open_multi \
30eio_job_ls \
31eio_sentry
28 32
29eio_file_ls_SOURCES = eio_file_ls.c 33EIO_EXAMPLES_LDADD = \
30eio_file_ls_LDADD = \
31$(top_builddir)/src/lib/eio/libeio.la \ 34$(top_builddir)/src/lib/eio/libeio.la \
32$(top_builddir)/src/lib/eo/libeo.la \ 35$(top_builddir)/src/lib/eo/libeo.la \
33$(top_builddir)/src/lib/ecore/libecore.la \ 36$(top_builddir)/src/lib/ecore/libecore.la \
37$(top_builddir)/src/lib/efl/libefl.la \
34$(top_builddir)/src/lib/eet/libeet.la \ 38$(top_builddir)/src/lib/eet/libeet.la \
35$(top_builddir)/src/lib/emile/libemile.la \ 39$(top_builddir)/src/lib/emile/libemile.la \
36$(top_builddir)/src/lib/eina/libeina.la \ 40$(top_builddir)/src/lib/eina/libeina.la \
37@EIO_LDFLAGS@ 41@EIO_LDFLAGS@
38 42
43eio_file_ls_SOURCES = eio_file_ls.c
44eio_file_ls_LDADD = \
45$(EIO_EXAMPLES_LDADD)
46
39eio_file_copy_SOURCES = eio_file_copy.c 47eio_file_copy_SOURCES = eio_file_copy.c
40eio_file_copy_LDADD = \ 48eio_file_copy_LDADD = \
41$(top_builddir)/src/lib/eio/libeio.la \ 49$(EIO_EXAMPLES_LDADD)
42$(top_builddir)/src/lib/eo/libeo.la \ 50
43$(top_builddir)/src/lib/ecore/libecore.la \ 51eio_job_open_SOURCES = eio_job_open.c
44$(top_builddir)/src/lib/eet/libeet.la \ 52eio_job_open_LDADD = \
45$(top_builddir)/src/lib/emile/libemile.la \ 53$(EIO_EXAMPLES_LDADD)
46$(top_builddir)/src/lib/eina/libeina.la \ 54
47@EIO_LDFLAGS@ 55eio_job_open_multi_SOURCES = eio_job_open_multi.c
56eio_job_open_multi_LDADD = \
57$(EIO_EXAMPLES_LDADD)
58
59eio_job_ls_SOURCES = eio_job_ls.c
60eio_job_ls_LDADD = \
61$(EIO_EXAMPLES_LDADD)
62
63eio_sentry_SOURCES = eio_sentry.c
64eio_sentry_LDADD = \
65$(EIO_EXAMPLES_LDADD)
48 66
49SRCS = \ 67SRCS = \
50eio_file_ls.c \ 68eio_file_ls.c \
51eio_file_copy.c 69eio_file_copy.c \
70eio_job_open.c \
71eio_job_open_multi.c \
72eio_job_ls.c \
73eio_sentry.c
52 74
53DATA_FILES = Makefile.examples 75DATA_FILES = Makefile.examples
54 76
diff --git a/src/examples/eio/eio_job_ls.c b/src/examples/eio/eio_job_ls.c
new file mode 100644
index 0000000..354325e
--- /dev/null
+++ b/src/examples/eio/eio_job_ls.c
@@ -0,0 +1,79 @@
1#if HAVE_CONFIG_H
2#include <config.h>
3#endif
4
5
6#include <stdlib.h>
7#include <stdio.h>
8
9#include <Eina.h>
10#include <Eio.h>
11#include <Ecore.h>
12
13void done_cb(void *data, void *value EINA_UNUSED)
14{
15 Eio_Job *job = data;
16 printf("%s done listing files.\n", __FUNCTION__);
17 ecore_main_loop_quit();
18 eo_unref(job);
19}
20
21void error_cb(void *data, Eina_Error *error)
22{
23 Eio_Job *job = data;
24 EINA_SAFETY_ON_NULL_RETURN(error);
25 const char *msg = eina_error_msg_get(*error);
26 printf("%s error: %s\n", __FUNCTION__, msg);
27 ecore_main_loop_quit();
28
29 eo_unref(job);
30}
31
32void filter_cb(void *data, const Eo_Event *event)
33{
34 Eio_Filter_Name_Data *event_info = event->info;
35 static Eina_Bool should_filter = EINA_FALSE;
36
37 printf("Filtering file %s\n", event_info->file);
38
39 should_filter = !should_filter;
40 event_info->filter = should_filter;
41}
42
43// Progress used to be the "Eio_Main_Cb" family of callbacks in the legacy API.
44void progress_cb(void *data, const char *filename)
45{
46 EINA_SAFETY_ON_NULL_RETURN(filename);
47 printf("%s listing filename: %s\n", __FUNCTION__, filename);
48}
49
50void list_files(void *data)
51{
52 Eina_Promise *promise;
53 const char *path = data;
54
55 Eio_Job *job = eo_add(EIO_JOB_CLASS, NULL);
56 eo_event_callback_add(job, EIO_JOB_EVENT_FILTER_NAME, (Eo_Event_Cb)&filter_cb, NULL);
57 eio_job_file_ls(job, path, &promise);
58 eina_promise_progress_cb_add(promise, (Eina_Promise_Progress_Cb)&progress_cb, NULL, NULL);
59 eina_promise_then(promise, (Eina_Promise_Cb)&done_cb, (Eina_Promise_Error_Cb)&error_cb, job);
60}
61
62int main(int argc, char const *argv[])
63{
64 eio_init();
65 ecore_init();
66
67 const char *path = getenv("HOME");
68
69 if (argc > 1)
70 path = argv[1];
71
72 Ecore_Job *job = ecore_job_add(&list_files, path);
73
74 ecore_main_loop_begin();
75
76 ecore_shutdown();
77 eio_shutdown();
78 return 0;
79}
diff --git a/src/examples/eio/eio_job_open.c b/src/examples/eio/eio_job_open.c
new file mode 100644
index 0000000..fb235c2
--- /dev/null
+++ b/src/examples/eio/eio_job_open.c
@@ -0,0 +1,80 @@
1#ifdef HAVE_CONFIG_H
2#include <config.h>
3#endif
4
5#include <stdlib.h>
6#include <stdio.h>
7
8#include <Eina.h>
9#include <Eio.h>
10#include <Ecore.h>
11
12void error_cb(void *data, Eina_Error *error)
13{
14 EINA_SAFETY_ON_NULL_RETURN(error);
15 EINA_SAFETY_ON_NULL_RETURN(data);
16
17 const char *msg = eina_error_msg_get(*error);
18 EINA_LOG_ERR("error: %s", msg);
19
20 ecore_main_loop_quit();
21}
22
23void done_closing_cb(int *result EINA_UNUSED)
24{
25 printf("%s closed file.\n", __FUNCTION__);
26
27 ecore_main_loop_quit();
28}
29
30void closing_job(Eio_Job *job, Eina_File *file)
31{
32 Eina_Promise *promise = NULL;
33 printf("%s Will close the file...\n", __FUNCTION__);
34 eio_job_file_close(job, file, &promise);
35 eina_promise_then(promise, (Eina_Promise_Cb)&done_closing_cb, (Eina_Promise_Error_Cb)&error_cb, job);
36}
37
38void done_open_cb(void *data, Eina_File **file)
39{
40 EINA_SAFETY_ON_NULL_RETURN(data);
41 EINA_SAFETY_ON_NULL_RETURN(file);
42 EINA_SAFETY_ON_NULL_RETURN(*file);
43
44 Eio_Job *job = data;
45
46 const char *name = eina_file_filename_get(*file);
47 printf("%s opened file %s\n", __FUNCTION__, name);
48
49 closing_job(job, *file);
50}
51
52void open_file(const char *path)
53{
54 Eina_Promise *promise;
55
56 Eio_Job *job = eo_add(EIO_JOB_CLASS, NULL);
57 eio_job_file_open(job, path, EINA_FALSE, &promise);
58 eina_promise_then(promise, (Eina_Promise_Cb)&done_open_cb, (Eina_Promise_Error_Cb)&error_cb, job);
59
60 eo_unref(job);
61}
62
63int main(int argc, char const *argv[])
64{
65 eio_init();
66 ecore_init();
67
68 const char *path = getenv("HOME");
69
70 if (argc > 1)
71 path = argv[1];
72
73 open_file(path);
74
75 ecore_main_loop_begin();
76
77 ecore_shutdown();
78 eio_shutdown();
79 return 0;
80}
diff --git a/src/examples/eio/eio_job_open_multi.c b/src/examples/eio/eio_job_open_multi.c
new file mode 100644
index 0000000..8082d4b
--- /dev/null
+++ b/src/examples/eio/eio_job_open_multi.c
@@ -0,0 +1,103 @@
1#ifdef HAVE_CONFIG_H
2#include <config.h>
3#endif
4
5#include <stdlib.h>
6#include <stdio.h>
7
8#include <Eina.h>
9#include <Eio.h>
10#include <Ecore.h>
11
12void error_cb(void *data, Eina_Error *error)
13{
14 EINA_SAFETY_ON_NULL_RETURN(error);
15 EINA_SAFETY_ON_NULL_RETURN(data);
16
17 const char *msg = eina_error_msg_get(*error);
18 EINA_LOG_ERR("error: %s", msg);
19
20 Eio_Job *job = data;
21 eo_unref(job);
22
23 ecore_main_loop_quit();
24}
25
26void done_closing_cb(void *data, Eina_Iterator **result EINA_UNUSED)
27{
28 EINA_SAFETY_ON_NULL_RETURN(data);
29
30 printf("%s closed file.\n", __FUNCTION__);
31
32 Eio_Job *job = data;
33 eo_unref(job);
34
35 ecore_main_loop_quit();
36}
37
38void closing_job(Eio_Job *job, Eina_File *file1, Eina_File *file2)
39{
40 Eina_Promise *promise;
41 Eina_Promise *tasks[3] = {NULL, NULL, NULL};
42
43 printf("%s Closing files.\n", __FUNCTION__);
44 eio_job_file_close(job, file1, &tasks[0]);
45 eio_job_file_close(job, file2, &tasks[1]);
46 promise = eina_promise_all(eina_carray_iterator_new((void**)&tasks[0]));
47 eina_promise_then(promise, (Eina_Promise_Cb)&done_closing_cb, (Eina_Promise_Error_Cb)&error_cb, job);
48}
49
50void done_open_cb(void *data, Eina_Iterator **iterator)
51{
52 EINA_SAFETY_ON_NULL_RETURN(data);
53 EINA_SAFETY_ON_NULL_RETURN(iterator);
54 EINA_SAFETY_ON_NULL_RETURN(*iterator);
55 Eio_Job *job = data;
56
57 Eina_File **file = NULL;
58 Eina_File **files = calloc(sizeof(Eina_File*),2);
59 int i = 0;
60 while (eina_iterator_next(*iterator, (void**)&file))
61 {
62 files[i] = *file;
63 const char *name = eina_file_filename_get(*file);
64 printf("%s opened file %s\n", __FUNCTION__, name);
65 i++;
66 }
67 closing_job(job, files[0], files[1]);
68 free(files);
69}
70
71void open_file(const char *path, const char *path2)
72{
73 Eina_Promise *promise;
74 Eina_Promise *tasks[3] = {NULL, NULL, NULL};
75
76 Eio_Job *job = eo_add(EIO_JOB_CLASS, NULL);
77 eio_job_file_open(job, path, EINA_FALSE, &tasks[0]);
78 eio_job_file_open(job, path2, EINA_FALSE, &tasks[1]);
79 promise = eina_promise_all(eina_carray_iterator_new((void**)&tasks[0]));
80 eina_promise_then(promise, (Eina_Promise_Cb)&done_open_cb, (Eina_Promise_Error_Cb)&error_cb, job);
81}
82
83int main(int argc, char const *argv[])
84{
85 eio_init();
86 ecore_init();
87
88 const char *path = getenv("HOME");
89 const char *path2 = "./";
90
91 if (argc > 1)
92 path = argv[1];
93 if (argc > 2)
94 path2 = argv[2];
95
96 open_file(path, path2);
97
98 ecore_main_loop_begin();
99
100 ecore_shutdown();
101 eio_shutdown();
102 return 0;
103}
diff --git a/src/examples/eio/eio_sentry.c b/src/examples/eio/eio_sentry.c
new file mode 100644
index 0000000..a6218a7
--- /dev/null
+++ b/src/examples/eio/eio_sentry.c
@@ -0,0 +1,55 @@
1#if HAVE_CONFIG_H
2#include <config.h>
3#endif
4
5
6#include <stdlib.h>
7#include <stdio.h>
8
9#include <Eina.h>
10#include <Eio.h>
11#include <Ecore.h>
12
13Eina_Bool
14sentry_cb(void *data, const Eo_Event *event)
15{
16 Eio_Sentry_Event *event_info = event->info;
17
18 printf("Event on monitored path %s", event_info->source);
19 printf("Created file %s\n", event_info->trigger);
20
21 ecore_main_loop_quit();
22
23 return EINA_FALSE;
24}
25
26void
27monitor_stuff(void *data)
28{
29 const char *path = data;
30 Eio_Sentry *sentry = eo_add(EIO_SENTRY_CLASS, NULL);
31 eo_event_callback_add(sentry, EIO_SENTRY_EVENT_FILE_CREATED, (Eo_Event_Cb)&sentry_cb, NULL);
32
33 printf("Starting monitoring path %s\n", path);
34 eio_sentry_add(sentry, path);
35}
36
37int
38main(int argc, char const *argv[])
39{
40 eio_init();
41 ecore_init();
42
43 const char *path = getenv("HOME");
44
45 if (argc > 1)
46 path = argv[1];
47
48 Ecore_Job *job = ecore_job_add(&monitor_stuff, path);
49
50 ecore_main_loop_begin();
51
52 ecore_shutdown();
53 eio_shutdown();
54 return 0;
55}