From f8fc013d7fb19baa72e1ae49ecd3d1205baacf5c Mon Sep 17 00:00:00 2001 From: Jin Yoon Date: Thu, 3 Aug 2017 16:38:59 +0900 Subject: [PATCH] Retry to connect servers every 5 sec Change-Id: Ia2784fd69e7a8091f521d26444f4e4bbfaea0c66 --- CMakeLists.txt | 1 + inc/connectivity.h | 1 + packaging/org.tizen.position-finder-client.spec | 7 +- src/connectivity.c | 104 ++++++++++++++---------- src/control.c | 17 ++++ 5 files changed, 86 insertions(+), 44 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c75592..8e591e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ pkg_check_modules(APP_PKGS REQUIRED capi-system-peripheral-io iotcon eina + ecore ) FOREACH (flag ${APP_PKGS_CFLAGS}) diff --git a/inc/connectivity.h b/inc/connectivity.h index daa5258..1f46352 100644 --- a/inc/connectivity.h +++ b/inc/connectivity.h @@ -28,5 +28,6 @@ typedef struct _connectivity_resource_s connectivity_resource_s; typedef void (*connectivity_observe_resource_cb)(connectivity_resource_s *resource_info, void *resource_data, void *user_data); extern int connectivity_observe_resource(const char *type, connectivity_observe_resource_cb cb, void *user_data); +extern void connectivity_free_abnormal_resource(void); #endif /* __POSITION_FINDER_CONNECTIVITY_H__ */ diff --git a/packaging/org.tizen.position-finder-client.spec b/packaging/org.tizen.position-finder-client.spec index 9b133ea..0a55f4e 100644 --- a/packaging/org.tizen.position-finder-client.spec +++ b/packaging/org.tizen.position-finder-client.spec @@ -14,9 +14,10 @@ BuildRequires: pkgconfig(capi-appfw-application) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(capi-appfw-service-application) -BuildRequires: pkgconfig(capi-system-peripheral-io) -BuildRequires: pkgconfig(iotcon) -BuildRequires: pkgconfig(eina) +BuildRequires: pkgconfig(capi-system-peripheral-io) +BuildRequires: pkgconfig(iotcon) +BuildRequires: pkgconfig(eina) +BuildRequires: pkgconfig(ecore) %description Thing Illumination diff --git a/src/connectivity.c b/src/connectivity.c index 3919316..8371418 100644 --- a/src/connectivity.c +++ b/src/connectivity.c @@ -20,6 +20,7 @@ */ #include +#include #include #include @@ -32,6 +33,7 @@ #define CBOR_FILE "/home/owner/apps_rw/org.tizen.position-finder-client/data/iotcon-test-svr-db-client.dat" #define DOOR_RESOURCE_URI_PREFIX "/door" +#define MAXIMUM_LIVE_INTERVAL 10l struct _connectivity_observe_resource_cb_s { connectivity_observe_resource_cb cb; @@ -46,6 +48,7 @@ struct _connectivity_resource_s { char *type; char *uri_path; iotcon_remote_resource_h resource; + time_t last_time; connectivity_observe_resource_cb_s *cb_info; }; @@ -89,6 +92,7 @@ static void _observe_cb(iotcon_remote_resource_h resource, iotcon_error_e err, i ret = iotcon_attributes_get_bool(attributes, "opened", &opened); ret_if(IOTCON_ERROR_NONE != ret); + resource_info->last_time = time(NULL); resource_info->cb_info->cb(resource_info, (void *)(int) opened, resource_info->cb_info->user_data); } @@ -131,7 +135,6 @@ static connectivity_resource_s *_pop_device_id_from_list(const char *device_id) { Eina_List *l = NULL, *ln = NULL; connectivity_resource_s *temp = NULL; - connectivity_resource_s *info = NULL; retv_if(!device_id, NULL); @@ -139,12 +142,11 @@ static connectivity_resource_s *_pop_device_id_from_list(const char *device_id) continue_if(!temp->device_id); if (!strncmp(temp->device_id, device_id, strlen(device_id))) { connectivity_info.list = eina_list_remove(connectivity_info.list, temp); - info = temp; - break; + return temp; } } - return info; + return NULL; } #endif @@ -162,7 +164,7 @@ static bool _get_res_type_cb(const char *string, void *user_data) return IOTCON_FUNC_CONTINUE; } -static int _retrieve_resource_info(iotcon_remote_resource_h resource, connectivity_resource_s *info) +static int _retrieve_resource_info(iotcon_remote_resource_h resource, connectivity_resource_s **info) { int ret = -1; char *device_id = NULL; @@ -202,45 +204,51 @@ static int _retrieve_resource_info(iotcon_remote_resource_h resource, connectivi ret = iotcon_resource_types_foreach(resource_types, _get_res_type_cb, uri_path); retv_if(IOTCON_ERROR_NONE != ret, -1); - ret = iotcon_remote_resource_clone(resource, &info->resource); - retv_if(IOTCON_ERROR_NONE != ret, -1); + *info = calloc(1, sizeof(connectivity_resource_s)); + retv_if(!*info, -1); + + ret = iotcon_remote_resource_clone(resource, &(*info)->resource); + goto_if(IOTCON_ERROR_NONE != ret, error); _I("Resource Addr[%s/%s] is in Device[%s]", host_address, uri_path, device_name); - info->device_id = strdup(device_id); - goto_if(!info->device_id, error); + (*info)->device_id = strdup(device_id); + goto_if(!(*info)->device_id, error); - info->host_address = strdup(host_address); - goto_if(!info->host_address, error); + (*info)->host_address = strdup(host_address); + goto_if(!(*info)->host_address, error); - info->device_name = strdup(device_name); - goto_if(!info->device_name, error); + (*info)->device_name = strdup(device_name); + goto_if(!(*info)->device_name, error); - info->uri_path = strdup(uri_path); - goto_if(!info->uri_path, error); + (*info)->uri_path = strdup(uri_path); + goto_if(!(*info)->uri_path, error); return 0; error: - if (info->resource) { - iotcon_remote_resource_destroy(info->resource); - info->resource = NULL; - } - if (info->uri_path) { - free(info->uri_path); - info->uri_path = NULL; - } - if (info->device_name) { - free(info->device_name); - info->device_name = NULL; - } - if (info->host_address) { - free(info->host_address); - info->host_address = NULL; - } - if (info->device_id) { - free(info->device_id); - info->device_id = NULL; + if (*info) { + if ((*info)->resource) { + iotcon_remote_resource_destroy((*info)->resource); + (*info)->resource = NULL; + } + if ((*info)->uri_path) { + free((*info)->uri_path); + (*info)->uri_path = NULL; + } + if ((*info)->device_name) { + free((*info)->device_name); + (*info)->device_name = NULL; + } + if ((*info)->host_address) { + free((*info)->host_address); + (*info)->host_address = NULL; + } + if ((*info)->device_id) { + free((*info)->device_id); + (*info)->device_id = NULL; + } + free(*info); } return -1; @@ -253,6 +261,9 @@ static void _free_resource_info(connectivity_resource_s *info) if (info->resource) iotcon_remote_resource_destroy(info->resource); + if (info->cb_info) + free(info->cb_info); + if (info->uri_path) free(info->uri_path); @@ -292,13 +303,11 @@ static int _register_observe(connectivity_resource_s *info) return 0; } -#if 0 static void _unregister_observe(connectivity_resource_s *info) { ret_if(!info); iotcon_remote_resource_observe_deregister(info->resource); } -#endif static bool _found_resource_cb(iotcon_remote_resource_h resource, iotcon_error_e result, void *user_data) { @@ -316,12 +325,9 @@ static bool _found_resource_cb(iotcon_remote_resource_h resource, iotcon_error_e retv_if(-1 == ret, IOTCON_FUNC_CONTINUE); if (1 == ret) return IOTCON_FUNC_CONTINUE; - info = calloc(1, sizeof(connectivity_resource_s)); - retv_if(!info, -1); - info->cb_info = user_data; - - ret = _retrieve_resource_info(resource, info); + ret = _retrieve_resource_info(resource, &info); goto_if(-1 == ret, error); + info->cb_info = user_data; if (-1 == _register_observe(info)) { _E("Cannot observe [%s:%s:%s:%s]", info->device_id, info->host_address, info->device_name, info->uri_path); @@ -392,3 +398,19 @@ error: } return -1; } + +void connectivity_free_abnormal_resource(void) +{ + Eina_List *l = NULL, *ln = NULL; + connectivity_resource_s *temp = NULL; + time_t cur_time = time(NULL); + + EINA_LIST_FOREACH_SAFE(connectivity_info.list, l, ln, temp) { + if (cur_time > temp->last_time + MAXIMUM_LIVE_INTERVAL) { + _E("Lost signal [%s]", temp->device_id); + connectivity_info.list = eina_list_remove(connectivity_info.list, temp); + _unregister_observe(temp); + _free_resource_info(temp); + } + } +} diff --git a/src/control.c b/src/control.c index f8e7eda..bdf5776 100644 --- a/src/control.c +++ b/src/control.c @@ -26,12 +26,14 @@ #include #include #include +#include #include "log.h" #include "connectivity.h" #define MAX_QUEUE_ELEMENTS 120 #define DOOR_RESOURCE_TYPE "org.tizen.door" +#define FINDING_SERVER_INTERVAL 5.0f static void _start_internal_function(void); static void _stop_internal_function(void); @@ -57,8 +59,20 @@ void _observe_resource_cb(connectivity_resource_s *resource_info, void *resource _I("Result value is [%d]\n", result); } +static Eina_Bool _timer_cb(void *data) +{ + int ret = -1; + + connectivity_free_abnormal_resource(); + ret = connectivity_observe_resource(DOOR_RESOURCE_TYPE, _observe_resource_cb, NULL); + retv_if(ret == -1, ECORE_CALLBACK_RENEW); + + return ECORE_CALLBACK_RENEW; +} + bool service_app_create(void *data) { + Ecore_Timer *timer = NULL; int ret = -1; _start_internal_function(); @@ -66,6 +80,9 @@ bool service_app_create(void *data) ret = connectivity_observe_resource(DOOR_RESOURCE_TYPE, _observe_resource_cb, NULL); retv_if(ret == -1, false); + timer = ecore_timer_add(FINDING_SERVER_INTERVAL, _timer_cb, NULL); + retv_if(!timer, false); + return true; } -- 2.7.4