summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorGuillaume Friloux <guillaume.friloux@gmail.com>2013-12-23 16:38:21 +0100
committerGuillaume Friloux <guillaume.friloux@gmail.com>2013-12-23 16:38:21 +0100
commitee723d9fd84c80c1abb8997c470f7f39d43b723c (patch)
tree79404cd4d8bc8b3b01d38f4db5ed4e6d4befd620 /src/lib
parent9e41d430b72960267fcf412ac885f5c47e745687 (diff)
Add libstore that will take care of sending json data to elasticsearch
server.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/Makefile.mk17
-rw-r--r--src/lib/store/store_event.c55
-rw-r--r--src/lib/store/store_main.c211
-rw-r--r--src/lib/store/store_private.h48
-rw-r--r--src/lib/store/store_utils.c21
5 files changed, 351 insertions, 1 deletions
diff --git a/src/lib/Makefile.mk b/src/lib/Makefile.mk
index bb78459..654fa69 100644
--- a/src/lib/Makefile.mk
+++ b/src/lib/Makefile.mk
@@ -3,7 +3,9 @@ MAINTAINERCLEANFILES += src/lib/*.gc{no,da}
3noinst_LTLIBRARIES = \ 3noinst_LTLIBRARIES = \
4src/lib/libconf.la \ 4src/lib/libconf.la \
5src/lib/libspy.la \ 5src/lib/libspy.la \
6src/lib/librules.la 6src/lib/librules.la \
7src/lib/libstore.la \
8src/lib/libcjson.la
7 9
8src_lib_libconf_la_SOURCES = \ 10src_lib_libconf_la_SOURCES = \
9src/lib/conf/conf_main.c \ 11src/lib/conf/conf_main.c \
@@ -34,3 +36,16 @@ src/lib/libconf.la
34src_lib_librules_la_LIBADD = \ 36src_lib_librules_la_LIBADD = \
35src/lib/libconf.la 37src/lib/libconf.la
36 38
39src_lib_libstore_la_SOURCES = \
40src/lib/store/store_main.c \
41src/lib/store/store_event.c \
42src/lib/store/store_utils.c \
43src/lib/store/store_private.h \
44src/include/Store.h
45src_lib_libstore_la_CFLAGS = $(LIBS_CFLAGS) $(EXTRA_CPPFLAGS)
46src_lib_libstore_la_LDFLAGS = $(LIBS_LIBS)
47
48src_lib_libcjson_la_SOURCES = \
49src/lib/extras/cJSON.c
50src_lib_libcjson_la_CFLAGS = $(LIBS_CFLAGS) $(EXTRA_CPPFLAGS)
51src_lib_libcjson_la_LDFLAGS = $(LIBS_LIBS)
diff --git a/src/lib/store/store_event.c b/src/lib/store/store_event.c
new file mode 100644
index 0000000..4f23c7d
--- /dev/null
+++ b/src/lib/store/store_event.c
@@ -0,0 +1,55 @@
1#include "store_private.h"
2
3Eina_Bool
4store_event_data(void *data,
5 int type EINA_UNUSED,
6 void *event_info)
7{
8 Store_Add *sa = data;
9 Ecore_Con_Event_Url_Data *url_data = event_info;
10
11 if ((sa != ecore_con_url_data_get(url_data->url_con)) || (!sa))
12 return EINA_TRUE;
13
14 DBG("sa[%p] url_data[%p] data_len=%i", sa, url_data, url_data->size);
15
16 eina_strbuf_append_length(sa->data.buf,
17 (const char *)url_data->data,
18 url_data->size);
19 sa->data.len += url_data->size;
20
21 return EINA_TRUE;
22}
23
24Eina_Bool
25store_event_complete(void *data,
26 int type EINA_UNUSED,
27 void *event_info)
28{
29 Store_Add *sa = data;
30 Ecore_Con_Event_Url_Complete *url_complete = event_info;
31 char *errstr;
32 int http_code;
33
34 DBG("sa[%p] url_complete[%p] data_get[%p]",
35 sa, url_complete, ecore_con_url_data_get(url_complete->url_con));
36
37 if ((sa != ecore_con_url_data_get(url_complete->url_con)) || (!sa))
38 return EINA_TRUE;
39
40 http_code = ecore_con_url_status_code_get(sa->ec);
41 if (http_code != 200)
42 {
43 errstr = store_utils_dupf("Server replied HTTP code %i", http_code);
44 sa->cb.error((void *)sa->cb.data, sa->store, errstr);
45 free(errstr);
46 return EINA_TRUE;
47 }
48
49 sa->cb.done((void *)sa->cb.data,
50 sa->store,
51 (char *)eina_strbuf_string_get(sa->data.buf),
52 eina_strbuf_length_get(sa->data.buf));
53 store_add_free(sa);
54 return EINA_TRUE;
55}
diff --git a/src/lib/store/store_main.c b/src/lib/store/store_main.c
new file mode 100644
index 0000000..912c8f9
--- /dev/null
+++ b/src/lib/store/store_main.c
@@ -0,0 +1,211 @@
1#include "store_private.h"
2
3static int _store_init_count = 0;
4int _store_log_dom_global = -1;
5
6
7void
8store_add_free(Store_Add *sa)
9{
10 if (sa->ec) ecore_con_url_free(sa->ec);
11 if (sa->data.buf) eina_strbuf_free(sa->data.buf);
12 if (sa->ev.ed) ecore_event_handler_del(sa->ev.ed);
13 if (sa->ev.ec) ecore_event_handler_del(sa->ev.ec);
14 free(sa);
15}
16
17Eina_Bool
18store_add(Store *store,
19 const char *buf,
20 size_t len,
21 Store_Done_Cb done_cb,
22 Store_Error_Cb error_cb,
23 const void *data)
24{
25 Store_Add *sa;
26 Eina_Bool r;
27
28 sa = calloc(1, sizeof(Store_Add));
29 if (!sa)
30 {
31 ERR("Failed to allocate Store_Add structure");
32 return EINA_FALSE;
33 }
34
35 sa->ec = ecore_con_url_new(store->url);
36 if (!sa->ec)
37 {
38 ERR("Failed to create ecore_con_url object");
39 goto sa_free;
40 }
41
42 sa->data.buf = eina_strbuf_new();
43 if (!sa->data.buf)
44 {
45 ERR("Failed to allocate storage buffer");
46 goto sa_con_url_free;
47 }
48
49 sa->ev.ed = ecore_event_handler_add(ECORE_CON_EVENT_URL_DATA,
50 store_event_data, sa);
51 if (!sa->ev.ed)
52 {
53 ERR("Failed to create event handler");
54 goto sa_buf_free;
55 }
56 sa->ev.ec = ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE,
57 store_event_complete, sa);
58 if (!sa->ev.ec)
59 {
60 ERR("Failed to create event handler");
61 goto sa_event_handler_free_ed;
62 }
63
64 sa->store = store;
65 sa->cb.done = done_cb;
66 sa->cb.error = error_cb;
67 sa->cb.data = data;
68DBG("store[%p] sa[%p", store, sa);
69 ecore_con_url_data_set(sa->ec, sa);
70 r = ecore_con_url_post(sa->ec, buf, len, "text/json");
71 if (!r)
72 {
73 ERR("Failed to issue POST method");
74 goto sa_event_handler_free_ec;
75 }
76
77 return EINA_TRUE;
78
79sa_event_handler_free_ec:
80 ecore_event_handler_del(sa->ev.ec);
81sa_event_handler_free_ed:
82 ecore_event_handler_del(sa->ev.ed);
83sa_buf_free:
84 eina_strbuf_free(sa->data.buf);
85sa_con_url_free:
86 ecore_con_url_free(sa->ec);
87sa_free:
88 free(sa);
89 return EINA_FALSE;
90}
91
92void
93store_data_set(Store *store, const void *data)
94{
95 store->data = data;
96}
97
98void *
99store_data_get(Store *store)
100{
101 return (void *)store->data;
102}
103
104Store *
105store_new(const char *url)
106{
107 Store *store;
108
109 store = calloc(1, sizeof(Store));
110 if (!store)
111 {
112 ERR("Failed to allocate Store object");
113 return NULL;
114 }
115 store->url = eina_stringshare_add(url);
116 if (!store->url)
117 {
118 ERR("Failed to allocate URL string");
119 goto store_free;
120 }
121
122 return store;
123
124store_free:
125 free(store);
126 return NULL;
127}
128
129void
130store_free(Store *store)
131{
132 EINA_SAFETY_ON_NULL_RETURN(store);
133
134 eina_stringshare_replace(&store->url, NULL);
135 free(store);
136}
137
138int
139store_init(void)
140{
141 if (++_store_init_count != 1)
142 return _store_init_count;
143
144 if (!eina_init())
145 {
146 fprintf(stderr, "Store can not initialize Eina\n");
147 return --_store_init_count;
148 }
149
150 _store_log_dom_global = eina_log_domain_register("store", EINA_COLOR_RED);
151 if (_store_log_dom_global < 0)
152 {
153 EINA_LOG_ERR("Store can not create a general log domain");
154 goto shutdown_eina;
155 }
156
157 if (!ecore_init())
158 {
159 ERR("Can not initialize Ecore");
160 goto unregister_log_domain;
161 }
162
163 if (!ecore_con_init())
164 {
165 ERR("Can not initialize Ecore_Con");
166 goto shutdown_ecore;
167 }
168
169 if (!ecore_con_url_init())
170 {
171 ERR("Can not initialize Ecore_Con_Url");
172 goto shutdown_ecore_con;
173 }
174
175 if (!ecore_con_url_pipeline_get())
176 ecore_con_url_pipeline_set(EINA_TRUE);
177
178 return _store_init_count;
179
180shutdown_ecore_con:
181 ecore_con_shutdown();
182shutdown_ecore:
183 ecore_shutdown();
184unregister_log_domain:
185 eina_log_domain_unregister(_store_log_dom_global);
186 _store_log_dom_global = -1;
187shutdown_eina:
188 eina_shutdown();
189 return --_store_init_count;
190}
191
192int
193store_shutdown(void)
194{
195 if (_store_init_count <= 0)
196 {
197 fprintf(stderr, "Store init count not greater than 0 in shutdown.");
198 return 0;
199 }
200
201 if (--_store_init_count != 0)
202 return _store_init_count;
203
204 ecore_con_url_shutdown();
205 ecore_con_shutdown();
206 ecore_shutdown();
207 eina_log_domain_unregister(_store_log_dom_global);
208 _store_log_dom_global = -1;
209 eina_shutdown();
210 return _store_init_count;
211}
diff --git a/src/lib/store/store_private.h b/src/lib/store/store_private.h
new file mode 100644
index 0000000..3f09f25
--- /dev/null
+++ b/src/lib/store/store_private.h
@@ -0,0 +1,48 @@
1#include <Store.h>
2
3extern int _store_log_dom_global;
4
5#define ERR(...) EINA_LOG_DOM_ERR(_store_log_dom_global, __VA_ARGS__)
6#define DBG(...) EINA_LOG_DOM_DBG(_store_log_dom_global, __VA_ARGS__)
7#define NFO(...) EINA_LOG_DOM_INFO(_store_log_dom_global, __VA_ARGS__)
8#define WRN(...) EINA_LOG_DOM_WARN(_store_log_dom_global, __VA_ARGS__)
9#define CRI(...) EINA_LOG_DOM_CRIT(_store_log_dom_global, __VA_ARGS__)
10
11
12struct _Store
13{
14 const char *url;
15 const void *data;
16};
17
18typedef struct _Store_Add
19{
20 Store *store;
21 Ecore_Con_Url *ec;
22
23 struct
24 {
25 Eina_Strbuf *buf;
26 size_t len;
27 } data;
28
29 struct
30 {
31 const void *data;
32 Store_Done_Cb done;
33 Store_Error_Cb error;
34 } cb;
35
36 struct
37 {
38 Ecore_Event_Handler *ed,
39 *ec;
40 } ev;
41} Store_Add;
42
43void store_add_free(Store_Add *sa);
44
45Eina_Bool store_event_data(void *data, int type, void *event_info);
46Eina_Bool store_event_complete(void *data, int type, void *event_info);
47
48char * store_utils_dupf(const char *s, ...);
diff --git a/src/lib/store/store_utils.c b/src/lib/store/store_utils.c
new file mode 100644
index 0000000..18cdf64
--- /dev/null
+++ b/src/lib/store/store_utils.c
@@ -0,0 +1,21 @@
1#define _GNU_SOURCE
2#include <stdio.h>
3
4#include "store_private.h"
5
6char *
7store_utils_dupf(const char *s, ...)
8{
9 va_list args;
10 int len;
11 char *str;
12
13 va_start(args, s);
14 len = vasprintf(&str, s, args);
15 va_end(args);
16
17 if (len == -1)
18 return NULL;
19
20 return str;
21}