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.
28 #include "decarta_log.h"
29 #include "decarta_config.h"
30 #include "http_wrapper.h"
31 #include "decarta_xml.h"
32 #include "decarta_maps.h"
35 DecartaMapCB callback;
37 guint request_id; /**< used to remove from table after success */
40 static GHashTable *http_reqid_hash = NULL;
41 static guint request_id = 1;
44 __maps_del_table_node_cb(gpointer key,
48 if (!user_data || !key || !value) {
51 HttpHandle http_async = NULL;
53 http_async = (HttpHandle)value;
54 char *user_key = (char *)user_data;
55 char *table_key = (char *)key;
57 if (!g_strcmp0(user_key, table_key)) {
58 DECARTA_LOGD("req_id [%s] http_async [%d] closed!", table_key, http_async);
60 http_close(http_async);
63 DECARTA_LOGD("req_id [%s] http_async [%d] not close", table_key, http_async);
68 __maps_del_table_all_cb(gpointer key,
76 HttpHandle http_async = NULL;
77 http_async = (HttpHandle)value;
78 char *table_key = (char *)key;
80 DECARTA_LOGD("req_id [%s] http_async [%d] closed!", table_key, http_async);
82 http_close(http_async);
87 http_map_cb (gboolean success,
91 DECARTA_LOGD("map_cb");
92 DecartaMapCBData* cb_data = (DecartaMapCBData*)userdata;
93 DecartaError err = DECARTA_ERROR_NONE;
94 DecartaMap *map = NULL;
95 if (success != TRUE) err = DECARTA_ERROR_HTTP;
96 else if (!xml_map_parse (recv_data, &map)) err = DECARTA_ERROR_XML;
97 if (cb_data->callback) cb_data->callback (err, map, cb_data->userdata);
99 /** remove the success request id from hash table */
100 char *request_id_str = g_strdup_printf("%u", cb_data->request_id);
101 if (http_reqid_hash) {
102 DECARTA_LOGD("remove the http async after success!");
103 g_hash_table_foreach_remove(http_reqid_hash, (GHRFunc)__maps_del_table_node_cb, (gpointer)request_id_str);
104 if (!g_hash_table_size(http_reqid_hash)) {
105 g_hash_table_destroy(http_reqid_hash);
106 http_reqid_hash = NULL;
109 if (request_id_str) {
110 g_free(request_id_str);
114 if (map) decarta_map_free (map);
119 decarta_search_map (const DecartaPosition *pos,
123 DECARTA_LOGD("decarta_search_geocode");
124 if (!pos || !map) 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 if (!xml_map_request_get(pos, zoom, &req, &size)) return DECARTA_ERROR_PARAMETER;
132 HttpHandle http = http_open(HTTP_TYPE_SYNC);
133 if (http_send(http, req, get_host(), &resp, NULL, NULL)) {
135 if (!xml_map_parse (resp, map)) err = DECARTA_ERROR_XML;
136 //if( 0 != process_tile_grid(resp, map)) err = DECARTA_ERROR_XML;
138 } else err = DECARTA_ERROR_HTTP;
146 decarta_search_map_async (const DecartaPosition *pos,
148 DecartaMapCB callback,
151 DECARTA_LOGD("decarta_search_map_async");
152 if (!pos || !callback) return DECARTA_ERROR_PARAMETER;
153 if (!load_config()) return DECARTA_ERROR_CONFIGURATION_FILE;
156 unsigned int size = 0;
157 DecartaMapCBData* cb_data = g_new0(DecartaMapCBData, 1);
158 cb_data->callback = callback;
159 cb_data->userdata = userdata;
161 cb_data->request_id = request_id;
163 if (!xml_map_request_get(pos, zoom, &req, &size)) {
171 return DECARTA_ERROR_PARAMETER;
174 char *request_id_str = g_strdup_printf("%u", request_id);
175 if (request_id_str == NULL) {
183 return DECARTA_ERROR_PARAMETER;
186 HttpHandle http_async = http_open(HTTP_TYPE_ASYNC);
188 if (!http_reqid_hash) {
189 http_reqid_hash = g_hash_table_new(g_str_hash, g_str_equal);
190 if (http_reqid_hash == NULL) {
191 if (request_id_str) {
192 g_free(request_id_str);
201 http_close(http_async);
202 return DECARTA_ERROR_HTTP;
204 g_hash_table_insert(http_reqid_hash, (gpointer)request_id_str, (gpointer)http_async);
205 DECARTA_LOGD("first insert http [0x%x], req_id [%d]", http_async, request_id);
207 g_hash_table_insert(http_reqid_hash, (gpointer)request_id_str, (gpointer)http_async);
208 DECARTA_LOGD("Not the first insert http [0x%x], req_id [%d]", http_async, request_id);
211 if(!http_send(http_async, req, get_host(), NULL, (HttpCallback)http_map_cb, cb_data)){
212 g_hash_table_foreach_remove(http_reqid_hash, (GHRFunc)__maps_del_table_node_cb, (gpointer)request_id_str);
213 if (!g_hash_table_size(http_reqid_hash)) {
214 g_hash_table_destroy(http_reqid_hash);
215 http_reqid_hash = NULL;
224 return DECARTA_ERROR_HTTP;
231 return DECARTA_ERROR_NONE;
235 decarta_search_map_async_cancel (void)
237 if (http_reqid_hash) {
238 /** cancel the all rest mapping http async requests*/
239 g_hash_table_foreach_remove(http_reqid_hash, (GHRFunc)__maps_del_table_all_cb, NULL);
241 DECARTA_LOGD("decarta_search_map_async_cancel: http_reqid_hash NOT exist!");