From 38f1ea1446d2cb7ee0e4023fe1ef2a13756e6d63 Mon Sep 17 00:00:00 2001 From: youngman Date: Tue, 16 Jun 2015 16:45:30 +0900 Subject: [PATCH] Modify ic_options_s(has_parent->ref_count) Change-Id: I97662564c441b44444495f128a50f4b5181fa26d Signed-off-by: youngman --- lib/icl-client.c | 17 +++---------- lib/icl-options.c | 55 +++++++++++++++++-------------------------- lib/icl-options.h | 3 +-- lib/icl-response.c | 34 +++++++++++++------------- lib/include/iotcon-constant.h | 1 - lib/include/iotcon-struct.h | 1 - test/crud-test-client.c | 9 ++----- test/repr-test-server.c | 5 ++-- 8 files changed, 45 insertions(+), 80 deletions(-) diff --git a/lib/icl-client.c b/lib/icl-client.c index 1698b87..faa148a 100644 --- a/lib/icl-client.c +++ b/lib/icl-client.c @@ -88,7 +88,7 @@ API void iotcon_client_free(iotcon_client_h resource) /* null COULD be allowed */ if (resource->header_options) - ic_options_free(resource->header_options); + iotcon_options_free(resource->header_options); iotcon_resource_types_free(resource->types); free(resource); } @@ -183,21 +183,10 @@ API int iotcon_client_set_options(iotcon_client_h resource, if (resource->header_options) iotcon_options_free(resource->header_options); - if (NULL == header_options) { - resource->header_options = NULL; - return IOTCON_ERROR_NONE; - } - - if (true == header_options->has_parent) + if (header_options) resource->header_options = ic_options_ref(header_options); else - resource->header_options = header_options; - if (NULL == resource->header_options) { - ERR("header_options is NULL"); - return IOTCON_ERROR_NO_DATA; - } - - resource->header_options->has_parent = true; + resource->header_options = NULL; return IOTCON_ERROR_NONE; } diff --git a/lib/icl-options.c b/lib/icl-options.c index b63c3d2..d949d63 100644 --- a/lib/icl-options.c +++ b/lib/icl-options.c @@ -23,6 +23,17 @@ #include "icl-utils.h" #include "icl-options.h" +iotcon_options_h ic_options_ref(iotcon_options_h options) +{ + RETV_IF(NULL == options, NULL); + RETV_IF(options->ref_count <= 0, NULL); + + options->ref_count++; + + return options; +} + + API iotcon_options_h iotcon_options_new() { iotcon_options_h options = calloc(1, sizeof(struct ic_options)); @@ -32,25 +43,22 @@ API iotcon_options_h iotcon_options_new() } options->hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free); - return options; -} - - -void ic_options_free(iotcon_options_h options) -{ - RET_IF(NULL == options); + options->ref_count = 1; - g_hash_table_unref(options->hash); - free(options); + return options; } API void iotcon_options_free(iotcon_options_h options) { RET_IF(NULL == options); - RETM_IF(true == options->has_parent, "iotcon_options has parent"); - ic_options_free(options); + options->ref_count--; + + if (0 == options->ref_count) { + g_hash_table_unref(options->hash); + free(options); + } } @@ -60,10 +68,8 @@ API void iotcon_options_free(iotcon_options_h options) API int iotcon_options_insert(iotcon_options_h options, unsigned short id, const char *data) { - FN_CALL; - RETV_IF(NULL == options, IOTCON_ERROR_INVALID_PARAMETER); - RETVM_IF(options->has_parent, IOTCON_ERROR_INVALID_PARAMETER, + RETVM_IF(1 < options->ref_count, IOTCON_ERROR_INVALID_PARAMETER, "Don't modify it. It is already set."); RETVM_IF(IOTCON_OPTIONS_MAX <= g_hash_table_size(options->hash), IOTCON_ERROR_OUT_OF_MEMORY, "Options already have maximum elements."); @@ -86,7 +92,7 @@ API int iotcon_options_delete(iotcon_options_h options, unsigned short id) gboolean ret; RETV_IF(NULL == options, IOTCON_ERROR_INVALID_PARAMETER); - RETVM_IF(options->has_parent, IOTCON_ERROR_INVALID_PARAMETER, + RETVM_IF(1 < options->ref_count, IOTCON_ERROR_INVALID_PARAMETER, "Don't modify it. It is already set."); ret = g_hash_table_remove(options->hash, GUINT_TO_POINTER(id)); @@ -129,22 +135,3 @@ API int iotcon_options_foreach(iotcon_options_h options, return IOTCON_ERROR_NONE; } - - -iotcon_options_h ic_options_ref(iotcon_options_h options) -{ - iotcon_options_h ref; - - RETV_IF(NULL == options, NULL); - - ref = calloc(1, sizeof(struct ic_options)); - if (NULL == ref) { - ERR("calloc() Fail(%d)", errno); - return NULL; - } - - ref->hash = g_hash_table_ref(options->hash); - - return ref; -} - diff --git a/lib/icl-options.h b/lib/icl-options.h index c38e31d..958f5ef 100644 --- a/lib/icl-options.h +++ b/lib/icl-options.h @@ -19,11 +19,10 @@ #include "iotcon-struct.h" struct ic_options { - bool has_parent; + int ref_count; GHashTable *hash; }; -void ic_options_free(iotcon_options_h options); iotcon_options_h ic_options_ref(iotcon_options_h options); #endif /* __IOT_CONNECTIVITY_MANAGER_LIBRARY_OPTIONS_H__ */ diff --git a/lib/icl-response.c b/lib/icl-response.c index 82bd46c..3963355 100644 --- a/lib/icl-response.c +++ b/lib/icl-response.c @@ -57,7 +57,7 @@ API void iotcon_response_free(iotcon_response_h resp) if (resp->new_uri) free(resp->new_uri); if (resp->header_options) - ic_options_free(resp->header_options); + iotcon_options_free(resp->header_options); free(resp); } @@ -83,6 +83,7 @@ API int iotcon_response_set(iotcon_response_h resp, iotcon_response_property_e p value = va_arg(args, int); if (value < IOTCON_RESPONSE_RESULT_OK || IOTCON_RESPONSE_RESULT_MAX <= value) { ERR("Invalid value(%d)", value); + va_end(args); return IOTCON_ERROR_INVALID_PARAMETER; } resp->result = value; @@ -92,32 +93,29 @@ API int iotcon_response_set(iotcon_response_h resp, iotcon_response_property_e p if (resp->new_uri) free(resp->new_uri); - if (new_resource_uri) - resp->new_uri = ic_utils_strdup(new_resource_uri); - else + if (new_resource_uri) { + resp->new_uri = strdup(new_resource_uri); + if (NULL == resp->new_uri) { + ERR("strdup() Fail(%d)", errno); + va_end(args); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + } else { resp->new_uri = NULL; + } break; case IOTCON_RESPONSE_HEADER_OPTIONS: options = va_arg(args, iotcon_options_h); if (resp->header_options) - ic_options_free(resp->header_options); - if (NULL == options) { - resp->header_options = NULL; - break; - } - if (true == options->has_parent) + iotcon_options_free(resp->header_options); + + if (options) resp->header_options = ic_options_ref(options); else - resp->header_options = options; - if (NULL == resp->header_options) { - ERR("header_options is NULL"); - return IOTCON_ERROR_NO_DATA; - } - - resp->header_options->has_parent = true; + resp->header_options = NULL; break; - case IOTCON_RESPONSE_NONE: default: + ERR("Invalid Response Property(%d)", prop); break; } diff --git a/lib/include/iotcon-constant.h b/lib/include/iotcon-constant.h index df89821..9fb853b 100644 --- a/lib/include/iotcon-constant.h +++ b/lib/include/iotcon-constant.h @@ -84,7 +84,6 @@ typedef enum { } iotcon_resource_property_e; typedef enum { - IOTCON_RESPONSE_NONE = 0, IOTCON_RESPONSE_RESOURCE_URI = 1, IOTCON_RESPONSE_RESULT = 2, IOTCON_RESPONSE_REPRESENTATION = 3, diff --git a/lib/include/iotcon-struct.h b/lib/include/iotcon-struct.h index 9b48c7d..610f69a 100644 --- a/lib/include/iotcon-struct.h +++ b/lib/include/iotcon-struct.h @@ -67,7 +67,6 @@ typedef int (*iotcon_query_foreach_cb)(const char *key, const char *value, void *user_data); int iotcon_query_foreach(iotcon_query_h query, iotcon_query_foreach_cb cb, void *user_data); -iotcon_query_h iotcon_query_clone(iotcon_query_h query); /** diff --git a/test/crud-test-client.c b/test/crud-test-client.c index 76770ee..3675074 100644 --- a/test/crud-test-client.c +++ b/test/crud-test-client.c @@ -117,13 +117,10 @@ static void _on_put(iotcon_options_h header_options, iotcon_repr_h recv_repr, iotcon_repr_h send_repr = iotcon_repr_new(); - iotcon_query_h query_params = iotcon_query_new(); /* send POST request */ - iotcon_post(door_resource, send_repr, query_params, _on_post, NULL); + iotcon_post(door_resource, send_repr, NULL, _on_post, NULL); iotcon_repr_free(send_repr); - iotcon_query_free(query_params); - } static void _on_get(iotcon_options_h header_options, iotcon_repr_h recv_repr, @@ -138,12 +135,10 @@ static void _on_get(iotcon_options_h header_options, iotcon_repr_h recv_repr, iotcon_repr_h send_repr = iotcon_repr_new(); iotcon_repr_set_bool(send_repr, "opened", true); - iotcon_query_h query_params = iotcon_query_new(); /* send PUT request */ - iotcon_put(door_resource, send_repr, query_params, _on_put, NULL); + iotcon_put(door_resource, send_repr, NULL, _on_put, NULL); iotcon_repr_free(send_repr); - iotcon_query_free(query_params); } static int _get_res_type_fn(const char *string, void *user_data) diff --git a/test/repr-test-server.c b/test/repr-test-server.c index c94f1f2..f589357 100644 --- a/test/repr-test-server.c +++ b/test/repr-test-server.c @@ -93,15 +93,14 @@ static void _room_request_handler_get(iotcon_request_h request, light_repr = iotcon_repr_new(); iotcon_repr_set_uri(light_repr, "/a/light"); iotcon_repr_set_int(light_repr, "brightness", 50); + iotcon_repr_append_child(room_repr, light_repr); + iotcon_repr_free(light_repr); /* create a switch Representation */ switch_repr = iotcon_repr_new(); iotcon_repr_set_uri(switch_repr, "/a/switch"); iotcon_repr_set_bool(switch_repr, "switch", false); - - iotcon_repr_append_child(room_repr, light_repr); iotcon_repr_append_child(room_repr, switch_repr); - iotcon_repr_free(light_repr); iotcon_repr_free(switch_repr); ret = iotcon_request_get_query(request, &query); -- 2.7.4