4 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7 * Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
27 #include "decarta_log.h"
28 #include "decarta_config.h"
29 #include "http_wrapper.h"
30 #include "decarta_xml.h"
31 #include "decarta_route.h"
34 DecartaRouteCB callback;
36 guint request_id; /**< used to remove from table after success */
39 static GHashTable *http_reqid_hash = NULL;
40 static bool is_found = false;
43 __route_del_table_node_cb(gpointer key,
47 if (!user_data || !key || !value) {
50 HttpHandle http_async = NULL;
52 http_async = (HttpHandle)value;
53 char *user_key = (char *)user_data;
54 char *table_key = (char *)key;
56 if (!g_strcmp0(user_key, table_key)) {
57 DECARTA_LOGD("req_id [%s] http_async [%d] closed!", table_key, http_async);
59 http_close(http_async);
63 DECARTA_LOGD("req_id [%s] http_async [%d] not close", table_key, http_async);
68 search_route_cb (gboolean success,
72 DECARTA_LOGD("search_route_cb");
73 DecartaRouteCBData* cb_data = (DecartaRouteCBData*)user_data;
74 DecartaError err = DECARTA_ERROR_NONE;
75 DecartaRoute *route = NULL;
76 gchar *req_id_str = NULL;
77 gchar *error_code = NULL;
78 gchar *error_msg = NULL;
79 if (success != TRUE) err = DECARTA_ERROR_HTTP;
80 else if (!xml_route_parse(recv_data, &route, &req_id_str, &error_code, &error_msg)) err = DECARTA_ERROR_XML;
82 if (cb_data && cb_data->callback) cb_data->callback (err, req_id_str, route, error_code, error_msg, cb_data->userdata);
83 DECARTA_LOGD("search_route_cb callback called");
85 /** remove the success request id from hash table */
86 char *request_id_str = g_strdup_printf("%u", cb_data->request_id);
87 if (http_reqid_hash) {
88 DECARTA_LOGD("remove the http async after success!");
89 g_hash_table_foreach_remove(http_reqid_hash, (GHRFunc)__route_del_table_node_cb, (gpointer)request_id_str);
90 if (!g_hash_table_size(http_reqid_hash)) {
91 g_hash_table_destroy(http_reqid_hash);
92 http_reqid_hash = NULL;
96 g_free(request_id_str);
103 decarta_route_free (route);
120 decarta_search_route (const DecartaRouteRequest *request,
121 DecartaRoute **route)
123 DECARTA_LOGD("decarta_search_route");
124 if (!request || !route) return DECARTA_ERROR_PARAMETER;
125 if (!load_config()) return DECARTA_ERROR_CONFIGURATION_FILE;
127 int err = DECARTA_ERROR_NONE;
130 unsigned int size = 0;
131 gchar *req_id_str = NULL;
132 gchar *error_code = NULL;
133 gchar *error_msg = NULL;
134 if (!xml_route_request_get(request, &req, &size)) return DECARTA_ERROR_PARAMETER;
135 HttpHandle http = http_open(HTTP_TYPE_SYNC);
136 if (http_send(http, req, get_host(), &resp, NULL, NULL)) {
137 if (!xml_route_parse(resp, route, &req_id_str, &error_code, &error_msg)) err = DECARTA_ERROR_XML;
138 } else err = DECARTA_ERROR_HTTP;
159 decarta_search_route_async (const DecartaRouteRequest *request,
160 DecartaRouteCB callback,
163 DECARTA_LOGD("decarta_search_route_async");
164 if (!request || !callback) return DECARTA_ERROR_PARAMETER;
165 if (!load_config()) return DECARTA_ERROR_CONFIGURATION_FILE;
168 unsigned int size = 0;
169 DecartaRouteCBData* cb_data = g_new0(DecartaRouteCBData, 1);
170 cb_data->callback = callback;
171 cb_data->userdata = userdata;
172 cb_data->request_id = request->request_id;
174 if (!xml_route_request_get(request, &req, &size)) {
182 return DECARTA_ERROR_PARAMETER;
185 char *request_id_str = g_strdup_printf("%u", request->request_id);
186 if (request_id_str == NULL) {
194 return DECARTA_ERROR_PARAMETER;
197 HttpHandle http_async = http_open(HTTP_TYPE_ASYNC);
198 DECARTA_LOGD("decarta_search_route_async: http %d, req_id %d", http_async, request->request_id);
200 if (!http_reqid_hash) {
201 http_reqid_hash = g_hash_table_new(g_str_hash, g_str_equal);
202 if (http_reqid_hash == NULL) {
203 if (request_id_str) {
204 g_free(request_id_str);
213 http_close(http_async);
214 return DECARTA_ERROR_HTTP;
216 g_hash_table_insert(http_reqid_hash, (gpointer)request_id_str, (gpointer)http_async);
217 DECARTA_LOGD("first insert http [0x%x], req_id [%d]", http_async, request->request_id);
219 g_hash_table_insert(http_reqid_hash, (gpointer)request_id_str, (gpointer)http_async);
220 DECARTA_LOGD("Not the first insert http [0x%x], req_id [%d]", http_async, request->request_id);
223 if(!http_send(http_async, req, get_host(), NULL, (HttpCallback)search_route_cb, cb_data)){
224 g_hash_table_foreach_remove(http_reqid_hash, (GHRFunc)__route_del_table_node_cb, (gpointer)request_id_str);
225 if (!g_hash_table_size(http_reqid_hash)) {
226 g_hash_table_destroy(http_reqid_hash);
227 http_reqid_hash = NULL;
237 return DECARTA_ERROR_HTTP;
240 // cb_data will be freed in callback
244 return DECARTA_ERROR_NONE;
248 decarta_search_route_async_cancel (guint request_id)
250 DECARTA_LOGD("decarta_search_route_async_cancel: req_id %d called", request_id);
252 if (http_reqid_hash) {
253 char *request_id_str = g_strdup_printf("%u", request_id);
254 if (request_id_str == NULL) {
255 return DECARTA_ERROR_NOT_FOUND;
258 /** cancel the request_id http async request*/
260 g_hash_table_foreach_remove(http_reqid_hash, (GHRFunc)__route_del_table_node_cb, (gpointer)request_id_str);
261 g_free(request_id_str);
262 if (is_found == false) {
263 return DECARTA_ERROR_NOT_FOUND;
266 DECARTA_LOGD("decarta_search_route_async_cancel: req_id %d http_reqid_hash NOT exist!", request_id);
267 return DECARTA_ERROR_NOT_FOUND;
270 return DECARTA_ERROR_NONE;