summaryrefslogtreecommitdiff
path: root/src/lib/store/store_main.c
blob: f1c964fc03e4fd065ad636ae9c6ec806719d5aa2 (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
#include "store_private.h"

/**
 * @addtogroup Lib-Store-Functions
 * @{
 */

/**
 * @cond IGNORE
 */

static int _store_init_count = 0;
int _store_log_dom_global = -1;

/**
 * @brief Frees a Store_Add structure.
 * @param sa Store_Add structure to free.
 */
void
store_add_free(Store_Add *sa)
{
   if (sa->ec) ecore_con_url_free(sa->ec);
   if (sa->data.buf) eina_strbuf_free(sa->data.buf);
   if (sa->ev.ed) ecore_event_handler_del(sa->ev.ed);
   if (sa->ev.ec) ecore_event_handler_del(sa->ev.ec);
   free(sa);
}

/**
 * @endcond
 */

/**
 * @brief Store given data to store's url.
 * @param store Store structure.
 * @param buf Buffer to store.
 * @param len Length of @buf.
 * @param done_cb Callback to call when data is stored.
 * @param error_cb Callback to call if an error occured.
 * @param data Data to pass to callbacks.
 * @return EINA_TRUE if we try to store data.
 *         EINA_FALSE if an error occured when creating storing process.
 */
Eina_Bool
store_add(Store *store,
          const char *buf,
          size_t len,
          Store_Done_Cb done_cb,
          Store_Error_Cb error_cb,
          const void *data)
{
   Store_Add *sa;
   Eina_Bool r;

   sa = calloc(1, sizeof(Store_Add));
   if (!sa)
     {
        ERR("Failed to allocate Store_Add structure");
        return EINA_FALSE;
     }

   sa->ec = ecore_con_url_new(store->url);
   if (!sa->ec)
     {
        ERR("Failed to create ecore_con_url object");
        goto sa_free;
     }

   sa->data.buf = eina_strbuf_new();
   if (!sa->data.buf)
     {
        ERR("Failed to allocate storage buffer");
        goto sa_con_url_free;
     }

   sa->ev.ed = ecore_event_handler_add(ECORE_CON_EVENT_URL_DATA,
                                       store_event_data, sa);
   if (!sa->ev.ed)
     {
        ERR("Failed to create event handler");
        goto sa_buf_free;
     }
   sa->ev.ec = ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE,
                                       store_event_complete, sa);
   if (!sa->ev.ec)
     {
        ERR("Failed to create event handler");
        goto sa_event_handler_free_ed;
     }

   sa->store = store;
   sa->cb.done = done_cb;
   sa->cb.error = error_cb;
   sa->cb.data = data;
   sa->data.sent = strdup(buf);
   DBG("store[%p] sa[%p] buf[%s]", store, sa, buf);
   ecore_con_url_data_set(sa->ec, sa);
   r = ecore_con_url_post(sa->ec, buf, len, "text/json");
   if (!r)
     {
        ERR("Failed to issue POST method");
        goto sa_event_handler_free_ec;
     }

   return EINA_TRUE;

sa_event_handler_free_ec:
   ecore_event_handler_del(sa->ev.ec);
sa_event_handler_free_ed:
   ecore_event_handler_del(sa->ev.ed);
sa_buf_free:
   eina_strbuf_free(sa->data.buf);
sa_con_url_free:
   ecore_con_url_free(sa->ec);
sa_free:
   free(sa);
   return EINA_FALSE;
}

/**
 * @brief Attach given data to the Store structure.
 * @param store Store structure to attach data to.
 * @param data Data to attach to structure.
 */
void
store_data_set(Store *store, const void *data)
{
   store->data = data;
}

/**
 * @brief Get unmodified data attached to the store structure.
 * @param store Store structure.
 * @return Pointer to attached data.
 */
void *
store_data_get(Store *store)
{
   return (void *)store->data;
}

/**
 * @brief Create a new store structure.
 * @param url URL for storing data.
 * @return Pointer to newly created Store structure.
 */
Store *
store_new(const char *url)
{
   Store *store;

   store = calloc(1, sizeof(Store));
   if (!store)
     {
        ERR("Failed to allocate Store object");
        return NULL;
     }
   store->url = eina_stringshare_add(url);
   if (!store->url)
     {
        ERR("Failed to allocate URL string");
        goto store_free;
     }

   return store;

store_free:
   free(store);
   return NULL;
}

/**
 * @brief Frees an allocated Store structure.
 * @param store Store structure to free.
 */
void
store_free(Store *store)
{
   EINA_SAFETY_ON_NULL_RETURN(store);

   eina_stringshare_replace(&store->url, NULL);
   free(store);
}

/**
 * @brief Initialize the store library and all it's required submodules.
 * @return 1 or greater on success, 0 otherwise.
 */
int
store_init(void)
{
   if (++_store_init_count != 1)
     return _store_init_count;

   if (!eina_init())
     {
        fprintf(stderr, "Store can not initialize Eina\n");
        return --_store_init_count;
     }

   _store_log_dom_global = eina_log_domain_register("store", EINA_COLOR_RED);
   if (_store_log_dom_global < 0)
     {
        EINA_LOG_ERR("Store can not create a general log domain");
        goto shutdown_eina;
     }

   if (!ecore_init())
     {
        ERR("Can not initialize Ecore");
        goto unregister_log_domain;
     }

   if (!ecore_con_init())
     {
        ERR("Can not initialize Ecore_Con");
        goto shutdown_ecore;
     }

   if (!ecore_con_url_init())
     {
        ERR("Can not initialize Ecore_Con_Url");
        goto shutdown_ecore_con;
     }

   if (!ecore_con_url_pipeline_get())
     ecore_con_url_pipeline_set(EINA_TRUE);

   return _store_init_count;

shutdown_ecore_con:
   ecore_con_shutdown();
shutdown_ecore:
   ecore_shutdown();
unregister_log_domain:
   eina_log_domain_unregister(_store_log_dom_global);
   _store_log_dom_global = -1;
shutdown_eina:
   eina_shutdown();
   return --_store_init_count;
}

/**
 * @brief Shutdown store and all it's submodules if possible.
 * @return 0 if store shuts down, greater than 0 otherwise.
 *         This function shuts down all things set up in store_init()
 *         and cleans up its memory.
 */
int
store_shutdown(void)
{
   if (_store_init_count <= 0)
     {
        fprintf(stderr, "Store init count not greater than 0 in shutdown.");
        return 0;
     }

   if (--_store_init_count != 0)
     return _store_init_count;

   ecore_con_url_shutdown();
   ecore_con_shutdown();
   ecore_shutdown();
   eina_log_domain_unregister(_store_log_dom_global);
   _store_log_dom_global = -1;
   eina_shutdown();
   return _store_init_count;
}

/**
 * @}
 */