modify APIs related to checking interval of remote resource 43/86643/5
authoryoungman <yman.jung@samsung.com>
Fri, 2 Sep 2016 06:21:04 +0000 (15:21 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Mon, 12 Sep 2016 05:06:15 +0000 (22:06 -0700)
Change-Id: I16212be83e1f2908ebd16172c9551821024ce9c5
Signed-off-by: youngman <yman.jung@samsung.com>
include/iotcon-remote-resource.h
src/ic-common.h
src/ic-ioty.c
src/ic-ioty.h
src/ic-remote-resource-caching.c
src/ic-remote-resource-monitoring.c
src/ic-remote-resource.c
src/ic-remote-resource.h

index 7976e12..7c38e00 100644 (file)
@@ -449,8 +449,9 @@ typedef void (*iotcon_remote_resource_cached_representation_changed_cb)(
  * @details Use this function to start caching the resource's attribute.\n
  * Although, remote resource is not observable, it keeps the representation up-to-date.
  * Because It checks whether representation is changed, periodically.\n
- * The default checking interval is 10 seconds, but it may be changed by an administrator. \n
- * Also, you can get the cached representation even when the remote resource is off-line.
+ * Therefore, you can get the cached representation even when the remote resource is off-line.
+ * The default checking interval is 10 seconds.
+ * It can be changed by iotcon_remote_resource_set_checking_interval().
  *
  * @since_tizen 3.0
  * @privlevel public
@@ -523,7 +524,8 @@ typedef void (*iotcon_remote_resource_state_changed_cb)(iotcon_remote_resource_h
  * @details When remote resource's state are changed, registered callbacks will be called
  * in turn. Although, remote resource does not call iotcon_start_presence(), it knows
  * the state of resource. Because it checks the state of resource, periodically.\n
- * The default checking interval is 10 seconds, but it may be changed by an administrator.
+ * The default checking interval is 10 seconds.
+ * It can be changed by iotcon_remote_resource_set_checking_interval().
  *
  * @since_tizen 3.0
  * @privlevel public
@@ -871,47 +873,50 @@ int iotcon_remote_resource_get_cached_representation(
                iotcon_representation_h *representation);
 
 /**
- * @brief Gets the time interval of monitoring & caching API of remote resource.
- * @details This API get the time interval of iotcon_remote_resource_start_monitoring(),
- * and iotcon_remote_resource_start_caching().\n
- * The functions operate GET method, every saved time interval.
- * Default time interval is 10 seconds.
+ * @brief Gets the checking interval which is using on the monitoring & caching of remote resource.
+ * @details This API get the checking interval which is using in the process of monitoring
+ * and caching() of remote resource.\n
+ * The monitoring and caching will operate "GET method" with the checking interval.
+ * Default checking interval is 10 seconds.
  *
  * @since_tizen 3.0
  *
- * @param[out] time_interval Seconds for time interval
+ * @param[out] interval Seconds for time interval
  *
  * @return 0 on success, otherwise a negative error value.
  * @retval #IOTCON_ERROR_NONE Successful
  * @retval #IOTCON_ERROR_NOT_SUPPORTED  Not supported
  * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
  *
- * @see iotcon_remote_resource_set_time_interval()
+ * @see iotcon_remote_resource_set_checking_interval()
  * @see iotcon_remote_resource_start_monitoring()
  * @see iotcon_remote_resource_start_caching()
  */
-int iotcon_remote_resource_get_time_interval(int *time_interval);
+int iotcon_remote_resource_get_checking_interval(iotcon_remote_resource_h resource,
+               int *interval);
 
 /**
- * @brief Sets the time interval of monitoring & caching API of remote resource.
- * @details This API set the time interval of iotcon_remote_resource_start_monitoring(),
- * and iotcon_remote_resource_start_caching().
+ * @brief Sets the checking interval which is using on the monitoring & caching of remote resource.
+ * @details This API set the checking interval which is using in the process of monitoring
+ * and caching() of remote resource.\n
+ * The monitoring and caching will operate "GET method" with the checking interval changed by this function.
+ * Default checking interval is 10 seconds.
  *
  * @since_tizen 3.0
  *
- * @param[in] time_interval Seconds for time interval (must be in range from 1 to 3600)
+ * @param[in] interval Seconds for time interval (must be in range from 1 to 3600)
  *
  * @return 0 on success, otherwise a negative error value.
  * @retval #IOTCON_ERROR_NONE Successful
  * @retval #IOTCON_ERROR_NOT_SUPPORTED  Not supported
  * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
  *
- * @see iotcon_remote_resource_get_time_interval()
+ * @see iotcon_remote_resource_get_checking_interval()
  * @see iotcon_remote_resource_start_monitoring()
  * @see iotcon_remote_resource_start_caching()
  */
-int iotcon_remote_resource_set_time_interval(int time_interval);
-
+int iotcon_remote_resource_set_checking_interval(iotcon_remote_resource_h resource,
+               int interval);
 
 /**
  * @}
index 0c09ed1..20cc074 100644 (file)
@@ -22,7 +22,6 @@
 #define IC_INT64_TO_POINTER(i) ((void*)(intptr_t)(i))
 #define IC_POINTER_TO_INT64(p) ((int64_t)(intptr_t)(p))
 
-#define IC_REMOTE_RESOURCE_DEFAULT_TIME_INTERVAL 10 /* 10 sec */
 #define IC_PRESENCE_TTL_SECONDS_DEFAULT 60 /* 60 sec */
 #define IC_PRESENCE_TTL_SECONDS_MAX (60 * 60 * 24) /* 60 sec/min * 60 min/hr * 24 hr/day */
 
index 660b231..b0724d2 100644 (file)
@@ -48,9 +48,6 @@
 #include "ic-cbor.h"
 
 static bool icl_state;
-static int icl_remote_resource_time_interval = IC_REMOTE_RESOURCE_DEFAULT_TIME_INTERVAL;
-static GHashTable *icl_monitoring_table;
-static GHashTable *icl_caching_table;
 static char icl_svr_db_file[PATH_MAX];
 static OCPersistentStorage icl_ioty_ps;
 
@@ -1140,152 +1137,159 @@ int icl_ioty_remote_resource_delete(iotcon_remote_resource_h resource,
        return IOTCON_ERROR_NONE;
 }
 
-static void _icl_ioty_monitoring_get_cb(iotcon_remote_resource_h resource,
+
+static void _icl_ioty_encap_get_cb(iotcon_remote_resource_h resource,
                iotcon_error_e err,
                iotcon_request_type_e request_type,
                iotcon_response_h response,
                void *user_data)
 {
+       int ret;
+       iotcon_representation_h repr;
        iotcon_remote_resource_state_e state;
-       icl_monitoring_container_s *cb_container = user_data;
 
        RET_IF(NULL == resource);
        RET_IF(IOTCON_ERROR_NONE == err && NULL == response);
 
-       if (cb_container->is_destroyed) {
-               DBG("cb_container already destroyed");
-               icl_destroy_monitoring_container(cb_container);
-               return;
-       }
+       /* MONITORING */
+       if (resource->monitoring.presence) {
+               switch (err) {
+               case IOTCON_ERROR_NONE:
+                       state = (IOTCON_RESPONSE_OK == response->result) ?
+                               IOTCON_REMOTE_RESOURCE_ALIVE : IOTCON_REMOTE_RESOURCE_LOST_SIGNAL;
+                       break;
+               default:
+                       state = IOTCON_REMOTE_RESOURCE_LOST_SIGNAL;
+               }
 
-       if (IOTCON_ERROR_NONE != err) {
-               state = IOTCON_REMOTE_RESOURCE_LOST_SIGNAL;
-       } else {
-               state = (IOTCON_RESPONSE_OK == response->result) ?
-                       IOTCON_REMOTE_RESOURCE_ALIVE : IOTCON_REMOTE_RESOURCE_LOST_SIGNAL;
+               if (state != resource->monitoring.state) {
+                       resource->monitoring.cb(resource, state, resource->monitoring.user_data);
+                       resource->monitoring.state = state;
+               }
        }
 
-       if (state != cb_container->state) {
-               cb_container->cb(cb_container->resource, state, cb_container->user_data);
-               cb_container->state = state;
+       /* CACHING */
+       if (resource->caching.observe) {
+               switch (err) {
+               case IOTCON_ERROR_NONE:
+                       repr = response->repr;
+                       break;
+               default:
+                       WARN("iotcon_remote_resource_get() Fail(%d)", err);
+                       repr = NULL;
+               }
+
+               ret = icl_representation_compare(resource->caching.repr, repr);
+               if (IC_EQUAL != ret) { /* updated */
+                       if (resource->caching.repr)
+                               iotcon_representation_destroy(resource->caching.repr);
+                       resource->caching.repr = repr;
+                       if (response)
+                               response->repr = NULL;
+
+                       if (resource->caching.cb)
+                               resource->caching.cb(resource, repr, resource->caching.user_data);
+               }
        }
 }
 
-static gboolean _icl_ioty_monitoring_idle_cb(gpointer p)
+
+static gboolean _icl_ioty_encap_timeout_cb(gpointer p)
 {
        int ret;
-       icl_monitoring_container_s *cb_container = p;
+       iotcon_remote_resource_h resource = p;
 
-       RETV_IF(NULL == cb_container, G_SOURCE_REMOVE);
+       RETV_IF(NULL == resource, G_SOURCE_REMOVE);
 
        /* get */
-       ret = icl_ioty_remote_resource_get(cb_container->resource, NULL,
-                       _icl_ioty_monitoring_get_cb, cb_container);
+       ret = icl_ioty_remote_resource_get(resource, NULL, _icl_ioty_encap_get_cb, NULL);
        if (IOTCON_ERROR_NONE != ret) {
                ERR("icl_ioty_remote_resource_get() Fail(%d)", ret);
                return G_SOURCE_REMOVE;
        }
 
        /* get timeout */
-       if (cb_container->timeout)
-               g_source_remove(cb_container->timeout);
-       cb_container->timeout = g_timeout_add_seconds(icl_remote_resource_time_interval,
-                       _icl_ioty_monitoring_idle_cb, cb_container);
+       if (resource->timer_id)
+               g_source_remove(resource->timer_id);
+
+       resource->timer_id = g_timeout_add_seconds(resource->checking_interval,
+                       _icl_ioty_encap_timeout_cb, resource);
 
        return G_SOURCE_REMOVE;
 }
 
-static void _icl_ioty_monitoring_presence_cb(iotcon_presence_h presence,
-               iotcon_error_e err, iotcon_presence_response_h response, void *user_data)
+
+int icl_ioty_remote_resource_set_checking_interval(iotcon_remote_resource_h resource,
+               int time_interval)
 {
-       icl_monitoring_container_s *cb_container = user_data;
+       int ret;
 
-       RET_IF(NULL == cb_container);
+       if (resource->timer_id)
+               g_source_remove(resource->timer_id);
 
-       if (IOTCON_PRESENCE_OK == response->result &&
-                       IOTCON_PRESENCE_RESOURCE_DESTROYED != response->trigger)
-               return;
+       ret = icl_ioty_remote_resource_get(resource, NULL, _icl_ioty_encap_get_cb, NULL);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("icl_ioty_remote_resource_get() Fail(%d)", ret);
+               return ret;
+       }
 
-       g_idle_add(_icl_ioty_monitoring_idle_cb, cb_container);
-}
+       resource->timer_id = g_timeout_add_seconds(time_interval, _icl_ioty_encap_timeout_cb,
+                       resource);
 
-static void _icl_ioty_remote_resource_monitoring_table_insert(
-               iotcon_remote_resource_h resource, icl_monitoring_container_s *cb_container)
-{
-       RET_IF(NULL == resource);
-       RET_IF(NULL == cb_container);
-
-       if (NULL == icl_monitoring_table) {
-               icl_monitoring_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
-                               icl_destroy_monitoring_container);
-       }
-       g_hash_table_insert(icl_monitoring_table, resource, cb_container);
+       return IOTCON_ERROR_NONE;
 }
 
-static int icl_remote_resource_monitoring_table_remove(
-               iotcon_remote_resource_h resource)
-{
-       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
 
-       if (NULL == icl_monitoring_table)
-               return IOTCON_ERROR_NO_DATA;
+static void _icl_ioty_monitoring_presence_cb(iotcon_presence_h presence,
+               iotcon_error_e err, iotcon_presence_response_h response, void *user_data)
+{
+       iotcon_remote_resource_h resource = user_data;
 
-       if (NULL == g_hash_table_lookup(icl_monitoring_table, resource))
-               return IOTCON_ERROR_NO_DATA;
+       RET_IF(NULL == resource);
 
-       g_hash_table_remove(icl_monitoring_table, resource);
+       if (IOTCON_PRESENCE_OK == response->result
+                       && IOTCON_PRESENCE_RESOURCE_DESTROYED != response->trigger)
+               return;
 
-       return IOTCON_ERROR_NONE;
+       _icl_ioty_encap_timeout_cb(resource);
 }
 
-
 int icl_ioty_remote_resource_start_monitoring(iotcon_remote_resource_h resource,
                iotcon_remote_resource_state_changed_cb cb, void *user_data)
 {
        FN_CALL;
        int ret;
-       iotcon_presence_h presence;
-       icl_monitoring_container_s *cb_container;
 
        RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
        RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
 
-       cb_container = calloc(1, sizeof(icl_monitoring_container_s));
-       if (NULL == cb_container) {
-               ERR("calloc() Fail(%d)", errno);
-               return IOTCON_ERROR_OUT_OF_MEMORY;
-       }
-       cb_container->state = IOTCON_REMOTE_RESOURCE_ALIVE;
-       cb_container->resource = resource;
-       icl_remote_resource_ref(cb_container->resource);
-       cb_container->cb = cb;
-       cb_container->user_data = user_data;
+       resource->monitoring.state = IOTCON_REMOTE_RESOURCE_ALIVE;
+       resource->monitoring.cb = cb;
+       resource->monitoring.user_data = user_data;
 
        /* presence */
-       ret = icl_ioty_add_presence_cb(resource->host_address, resource->connectivity_type,
-                       NULL, _icl_ioty_monitoring_presence_cb, cb_container, &presence);
+       ret = icl_ioty_add_presence_cb(resource->host_address,
+                       resource->connectivity_type,
+                       NULL,
+                       _icl_ioty_monitoring_presence_cb,
+                       resource,
+                       &resource->monitoring.presence);
        if (IOTCON_ERROR_NONE != ret) {
                ERR("icl_ioty_add_presence_cb() Fail(%d)", ret);
-               icl_destroy_monitoring_container(cb_container);
                return ret;
        }
-       cb_container->presence = presence;
 
        /* get */
-       ret = icl_ioty_remote_resource_get(resource, NULL, _icl_ioty_monitoring_get_cb,
-                       cb_container);
+       if (resource->caching.observe)
+               return IOTCON_ERROR_NONE;
+
+       ret = icl_ioty_remote_resource_set_checking_interval(resource,
+                       resource->checking_interval);
        if (IOTCON_ERROR_NONE != ret) {
-               ERR("icl_ioty_remote_resource_get() Fail(%d)", ret);
-               icl_destroy_monitoring_container(cb_container);
+               ERR("icl_ioty_remote_resource_set_checking_interval() Fail(%d)", ret);
                return ret;
        }
 
-       /* get timeout */
-       cb_container->timeout = g_timeout_add_seconds(icl_remote_resource_time_interval,
-                       _icl_ioty_monitoring_idle_cb, cb_container);
-
-       _icl_ioty_remote_resource_monitoring_table_insert(resource, cb_container);
-
        return IOTCON_ERROR_NONE;
 }
 
@@ -1295,154 +1299,60 @@ int icl_ioty_remote_resource_stop_monitoring(iotcon_remote_resource_h resource)
 
        RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
 
-       ret = icl_remote_resource_monitoring_table_remove(resource);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("icl_remote_resource_monitoring_table_remove() Fail(%d)", ret);
-               return IOTCON_ERROR_INVALID_PARAMETER;
-       }
-
-       return IOTCON_ERROR_NONE;
-}
-
-static void _icl_ioty_caching_get_cb(iotcon_remote_resource_h resource,
-               iotcon_error_e err,
-               iotcon_request_type_e request_type,
-               iotcon_response_h response,
-               void *user_data)
-{
-       FN_CALL;
-       int ret;
-       iotcon_representation_h repr;
-       icl_caching_container_s *cb_container = user_data;
-
-       RET_IF(NULL == resource);
-       RET_IF(IOTCON_ERROR_NONE == err && NULL == response);
-
-       if (cb_container->is_destroyed) {
-               DBG("cb_container already destroyed");
-               icl_destroy_caching_container(cb_container);
-               return;
+       if (0 == resource->caching.observe && resource->timer_id) {
+               g_source_remove(resource->timer_id);
+               resource->timer_id = 0;
        }
 
-       if (IOTCON_ERROR_NONE != err) {
-               WARN("iotcon_remote_resource_get Fail(%d)", err);
-               repr = NULL;
-       } else {
-               repr = response->repr;
-       }
-
-       ret = icl_representation_compare(resource->cached_repr, repr);
-       if (IC_EQUAL != ret) { /* updated */
-               if (resource->cached_repr)
-                       iotcon_representation_destroy(resource->cached_repr);
-               resource->cached_repr = repr;
-               if (response)
-                       response->repr = NULL;
-
-               if (cb_container->cb)
-                       cb_container->cb(resource, resource->cached_repr, cb_container->user_data);
-       }
-}
-
-static gboolean _icl_ioty_caching_idle_cb(gpointer p)
-{
-       int ret;
-       icl_caching_container_s *cb_container = p;
-
-       RETV_IF(NULL == cb_container, G_SOURCE_REMOVE);
-
-       /* get */
-       ret = icl_ioty_remote_resource_get(cb_container->resource, NULL,
-                       _icl_ioty_caching_get_cb, cb_container);
+       ret = icl_ioty_remove_presence_cb(resource->monitoring.presence);
        if (IOTCON_ERROR_NONE != ret) {
-               ERR("icl_ioty_remote_resource_get() Fail(%d)", ret);
-               return G_SOURCE_REMOVE;
+               ERR("icl_ioty_remove_presence_cb() Fail(%d)", ret);
+               return ret;
        }
+       resource->monitoring.presence = NULL;
 
-       /* get timeout */
-       if (cb_container->timeout)
-               g_source_remove(cb_container->timeout);
-       cb_container->timeout = g_timeout_add_seconds(icl_remote_resource_time_interval,
-                       _icl_ioty_caching_idle_cb, cb_container);
-
-       return G_SOURCE_REMOVE;
+       return IOTCON_ERROR_NONE;
 }
 
 static void _icl_ioty_caching_observe_cb(iotcon_remote_resource_h resource,
                iotcon_error_e err, int sequence_number, iotcon_response_h response, void *user_data)
 {
-       FN_CALL;
-       icl_caching_container_s *cb_container = user_data;
-
-       RET_IF(NULL == resource);
-       RET_IF(NULL == cb_container);
-
-       g_idle_add(_icl_ioty_caching_idle_cb, cb_container);
-}
-
-static void _icl_ioty_remote_resource_caching_table_insert(
-               iotcon_remote_resource_h resource, icl_caching_container_s *cb_container)
-{
        RET_IF(NULL == resource);
-       RET_IF(NULL == cb_container);
-
-       if (NULL == icl_caching_table) {
-               icl_caching_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
-                               icl_destroy_caching_container);
-       }
-       g_hash_table_insert(icl_caching_table, resource, cb_container);
-}
-
-static int _icl_ioty_remote_resource_caching_table_remove(
-               iotcon_remote_resource_h resource)
-{
-       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
 
-       if (NULL == icl_caching_table)
-               return IOTCON_ERROR_NO_DATA;
-
-       if (NULL == g_hash_table_lookup(icl_caching_table, resource))
-               return IOTCON_ERROR_NO_DATA;
-
-       g_hash_table_remove(icl_caching_table, resource);
-
-       return IOTCON_ERROR_NONE;
+       _icl_ioty_encap_timeout_cb(resource);
 }
 
-
-
 int icl_ioty_remote_resource_start_caching(iotcon_remote_resource_h resource,
                iotcon_remote_resource_cached_representation_changed_cb cb, void *user_data)
 {
        FN_CALL;
        int ret;
        OCDoHandle handle = NULL;
-       icl_caching_container_s *cb_container;
 
        RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
 
-       cb_container = calloc(1, sizeof(icl_caching_container_s));
-       if (NULL == cb_container) {
-               ERR("calloc() Fail(%d)", errno);
-               return IOTCON_ERROR_OUT_OF_MEMORY;
-       }
-       cb_container->resource = resource;
-       icl_remote_resource_ref(cb_container->resource);
-       cb_container->cb = cb;
-       cb_container->user_data = user_data;
+       resource->caching.cb = cb;
+       resource->caching.user_data = user_data;
 
+       /* observe */
        ret = _icl_ioty_remote_resource_observe(resource, IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER,
-                       NULL, _icl_ioty_caching_observe_cb, cb_container, &handle);
+                       NULL, _icl_ioty_caching_observe_cb, NULL, &handle);
        if (IOTCON_ERROR_NONE != ret) {
                ERR("_icl_ioty_remote_resource_observe() Fail(%d)", ret);
-               icl_destroy_caching_container(cb_container);
                return ret;
        }
-       cb_container->observe_handle = IC_POINTER_TO_INT64(handle);
+       resource->caching.observe = IC_POINTER_TO_INT64(handle);
 
-       _icl_ioty_caching_idle_cb(cb_container);
+       /* get */
+       if (resource->monitoring.presence)
+               return IOTCON_ERROR_NONE;
 
-       _icl_ioty_remote_resource_caching_table_insert(resource, cb_container);
+       ret = icl_ioty_remote_resource_set_checking_interval(resource,
+                       resource->checking_interval);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("icl_ioty_remote_resource_set_checking_interval() Fail(%d)", ret);
+               return ret;
+       }
 
        return IOTCON_ERROR_NONE;
 }
@@ -1453,11 +1363,18 @@ int icl_ioty_remote_resource_stop_caching(iotcon_remote_resource_h resource)
 
        RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
 
-       ret = _icl_ioty_remote_resource_caching_table_remove(resource);
+       if (NULL == resource->monitoring.presence && resource->timer_id) {
+               g_source_remove(resource->timer_id);
+               resource->timer_id = 0;
+       }
+
+       ret = icl_ioty_remote_resource_observe_cancel(resource,
+                       resource->caching.observe);
        if (IOTCON_ERROR_NONE != ret) {
-               ERR("_icl_ioty_remote_resource_caching_table_remove() Fail(%d)", ret);
+               ERR("icl_ioty_remote_resource_observe_cancel() Fail(%d)", ret);
                return IOTCON_ERROR_INVALID_PARAMETER;
        }
+       resource->caching.observe = 0;
 
        return IOTCON_ERROR_NONE;
 }
@@ -2014,48 +1931,3 @@ int icl_ioty_response_send(iotcon_response_h response_handle)
        return IOTCON_ERROR_NONE;
 }
 
-static void _icl_ioty_caching_table_update(gpointer key, gpointer value,
-               gpointer user_data)
-{
-       icl_caching_container_s *cb_container = value;
-
-       RET_IF(NULL == cb_container);
-
-       g_source_remove(cb_container->timeout);
-       cb_container->timeout = g_timeout_add_seconds(icl_remote_resource_time_interval,
-                       _icl_ioty_caching_idle_cb, cb_container);
-}
-
-static void _icl_ioty_monitoring_table_update(gpointer key, gpointer value,
-               gpointer user_data)
-{
-       icl_monitoring_container_s *cb_container = value;
-
-       RET_IF(NULL == cb_container);
-
-       g_source_remove(cb_container->timeout);
-       cb_container->timeout = g_timeout_add_seconds(icl_remote_resource_time_interval,
-                       _icl_ioty_monitoring_idle_cb, cb_container);
-}
-
-int icl_ioty_remote_resource_set_time_interval(int time_interval)
-{
-       icl_remote_resource_time_interval = time_interval;
-
-       if (icl_caching_table)
-               g_hash_table_foreach(icl_caching_table, _icl_ioty_caching_table_update, NULL);
-
-       if (icl_monitoring_table)
-               g_hash_table_foreach(icl_monitoring_table, _icl_ioty_monitoring_table_update, NULL);
-
-       return IOTCON_ERROR_NONE;
-}
-
-int icl_ioty_remote_resource_get_time_interval(int *time_interval)
-{
-       RETV_IF(NULL == time_interval, IOTCON_ERROR_INVALID_PARAMETER);
-
-       *time_interval = icl_remote_resource_time_interval;
-
-       return IOTCON_ERROR_NONE;
-}
index 590d0d4..dbd02eb 100644 (file)
@@ -105,8 +105,8 @@ int icl_ioty_remote_resource_stop_monitoring(iotcon_remote_resource_h resource);
 int icl_ioty_remote_resource_start_caching(iotcon_remote_resource_h resource,
                iotcon_remote_resource_cached_representation_changed_cb cb, void *user_data);
 int icl_ioty_remote_resource_stop_caching(iotcon_remote_resource_h resource);
-int icl_ioty_remote_resource_set_time_interval(int time_interval);
-int icl_ioty_remote_resource_get_time_interval(int *time_interval);
+int icl_ioty_remote_resource_set_checking_interval(iotcon_remote_resource_h resource,
+               int time_interval);
 
 
 /* server */
index 432339c..211b6d8 100644 (file)
@@ -40,6 +40,10 @@ API int iotcon_remote_resource_start_caching(iotcon_remote_resource_h resource,
                ERR("The resource should be cloned.");
                return IOTCON_ERROR_INVALID_PARAMETER;
        }
+       if (resource->caching.observe) {
+               ERR("Already Start Caching");
+               return IOTCON_ERROR_ALREADY;
+       }
 
        INFO("Start Caching");
 
@@ -49,9 +53,11 @@ API int iotcon_remote_resource_start_caching(iotcon_remote_resource_h resource,
        case IOTCON_CONNECTIVITY_IPV4:
        case IOTCON_CONNECTIVITY_IPV6:
        case IOTCON_CONNECTIVITY_ALL:
+               icl_remote_resource_ref(resource);
                ret = icl_ioty_remote_resource_start_caching(resource, cb, user_data);
                if (IOTCON_ERROR_NONE != ret) {
                        ERR("icl_ioty_remote_resource_start_caching() Fail(%d)", ret);
+                       icl_remote_resource_unref(resource);
                        return ret;
                }
                break;
@@ -59,6 +65,7 @@ API int iotcon_remote_resource_start_caching(iotcon_remote_resource_h resource,
                ERR("Invalid Connectivity Type(%d)", connectivity_type);
                return IOTCON_ERROR_INVALID_PARAMETER;
        }
+
        return IOTCON_ERROR_NONE;
 }
 
@@ -72,6 +79,11 @@ API int iotcon_remote_resource_stop_caching(iotcon_remote_resource_h resource)
                        IOTCON_ERROR_PERMISSION_DENIED);
        RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
 
+       if (0 == resource->caching.observe) {
+               ERR("Not Cached");
+               return IOTCON_ERROR_INVALID_PARAMETER;
+       }
+
        INFO("Stop Caching");
 
        connectivity_type = resource->connectivity_type;
@@ -85,6 +97,7 @@ API int iotcon_remote_resource_stop_caching(iotcon_remote_resource_h resource)
                        ERR("icl_ioty_remote_resource_stop_caching() Fail(%d)", ret);
                        return ret;
                }
+               icl_remote_resource_unref(resource);
                break;
        default:
                ERR("Invalid Connectivity Type(%d)", connectivity_type);
@@ -102,9 +115,9 @@ API int iotcon_remote_resource_get_cached_representation(
        RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED);
        RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
        RETV_IF(NULL == representation, IOTCON_ERROR_INVALID_PARAMETER);
-       WARN_IF(NULL == resource->cached_repr, "No Cached Representation");
+       WARN_IF(NULL == resource->caching.repr, "No Cached Representation");
 
-       *representation = resource->cached_repr;
+       *representation = resource->caching.repr;
 
        return IOTCON_ERROR_NONE;
 }
index 1b7c3e4..c2ec76a 100644 (file)
@@ -40,6 +40,14 @@ API int iotcon_remote_resource_start_monitoring(
                ERR("The resource should be cloned.");
                return IOTCON_ERROR_INVALID_PARAMETER;
        }
+       if (resource->monitoring.presence) {
+               ERR("Already Start Monitoring");
+               return IOTCON_ERROR_ALREADY;
+       }
+
+       INFO("Start Monitoring");
+
+       resource->monitoring.state = IOTCON_REMOTE_RESOURCE_ALIVE;
 
        connectivity_type = resource->connectivity_type;
 
@@ -47,9 +55,11 @@ API int iotcon_remote_resource_start_monitoring(
        case IOTCON_CONNECTIVITY_IPV4:
        case IOTCON_CONNECTIVITY_IPV6:
        case IOTCON_CONNECTIVITY_ALL:
+               icl_remote_resource_ref(resource);
                ret = icl_ioty_remote_resource_start_monitoring(resource, cb, user_data);
                if (IOTCON_ERROR_NONE != ret) {
                        ERR("icl_ioty_remote_resource_start_monitoring() Fail(%d)", ret);
+                       icl_remote_resource_unref(resource);
                        return ret;
                }
                break;
@@ -62,8 +72,7 @@ API int iotcon_remote_resource_start_monitoring(
 }
 
 
-API int iotcon_remote_resource_stop_monitoring(
-               iotcon_remote_resource_h resource)
+API int iotcon_remote_resource_stop_monitoring(iotcon_remote_resource_h resource)
 {
        int ret, connectivity_type;
 
@@ -72,6 +81,11 @@ API int iotcon_remote_resource_stop_monitoring(
                        IOTCON_ERROR_PERMISSION_DENIED);
        RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
 
+       if (NULL == resource->monitoring.presence) {
+               ERR("Not Monitoring");
+               return IOTCON_ERROR_INVALID_PARAMETER;
+       }
+
        INFO("Stop Monitoring");
 
        connectivity_type = resource->connectivity_type;
@@ -85,6 +99,7 @@ API int iotcon_remote_resource_stop_monitoring(
                        ERR("icl_ioty_remote_resource_stop_monitoring() Fail(%d)", ret);
                        return ret;
                }
+               icl_remote_resource_unref(resource);
                break;
        default:
                ERR("Invalid Connectivity Type(%d)", connectivity_type);
@@ -94,5 +109,3 @@ API int iotcon_remote_resource_stop_monitoring(
        return IOTCON_ERROR_NONE;
 }
 
-
-
index c1a73b3..740075a 100644 (file)
@@ -29,7 +29,8 @@
 #include "ic-resource-interfaces.h"
 #include "ic-ioty.h"
 
-#define ICL_REMOTE_RESOURCE_MAX_TIME_INTERVAL 3600 /* 60 min */
+#define ICL_REMOTE_RESOURCE_MAX_CHECKING_INTERVAL 3600 /* 60 min */
+#define ICL_REMOTE_RESOURCE_DEFAULT_CHECKING_INTERVAL 10 /* 10 sec */
 
 /* The length of resource_type should be less than or equal to 61.
  * If resource_type is NULL, then All resources in host are discovered. */
@@ -108,6 +109,7 @@ API int iotcon_remote_resource_create(const char *host_address,
        resource->policies = policies;
        resource->types = icl_resource_types_ref(resource_types);
        resource->ifaces = icl_resource_interfaces_ref(resource_ifaces);
+       resource->checking_interval = ICL_REMOTE_RESOURCE_DEFAULT_CHECKING_INTERVAL;
        resource->ref_count = 1;
 
        *resource_handle = resource;
@@ -140,6 +142,12 @@ static void _icl_remote_resource_destroy(iotcon_remote_resource_h resource)
        if (resource->header_options)
                iotcon_options_destroy(resource->header_options);
 
+       if (resource->monitoring.presence)
+               iotcon_remote_resource_stop_monitoring(resource);
+
+       if (resource->caching.observe)
+               iotcon_remote_resource_stop_caching(resource);
+
        free(resource);
 }
 
@@ -220,6 +228,7 @@ API int iotcon_remote_resource_clone(iotcon_remote_resource_h src,
        resource->device_id = ic_utils_strdup(src->device_id);
        resource->device_name = ic_utils_strdup(src->device_name);
        resource->policies = src->policies;
+       resource->checking_interval = src->checking_interval;
        resource->ref_count = 1;
 
        if (src->header_options) {
@@ -399,39 +408,39 @@ API int iotcon_remote_resource_set_options(iotcon_remote_resource_h resource,
        return IOTCON_ERROR_NONE;
 }
 
-API int iotcon_remote_resource_get_time_interval(int *time_interval)
+API int iotcon_remote_resource_get_checking_interval(iotcon_remote_resource_h resource,
+               int *interval)
 {
-       int ret, arg_time_interval;
-
        RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED);
-       RETV_IF(NULL == time_interval, IOTCON_ERROR_INVALID_PARAMETER);
-
-       ret = icl_ioty_remote_resource_get_time_interval(&arg_time_interval);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("icl_ioty_remote_resource_get_time_interval() Fail(%d)", ret);
-               return ret;
-       }
+       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(NULL == interval, IOTCON_ERROR_INVALID_PARAMETER);
 
-       *time_interval = arg_time_interval;
+       *interval = resource->checking_interval;
 
        return IOTCON_ERROR_NONE;
 }
 
 
-API int iotcon_remote_resource_set_time_interval(int time_interval)
+API int iotcon_remote_resource_set_checking_interval(iotcon_remote_resource_h resource,
+               int interval)
 {
        int ret;
 
        RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED);
-       RETV_IF(ICL_REMOTE_RESOURCE_MAX_TIME_INTERVAL < time_interval || time_interval <= 0,
+       RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
+       RETV_IF(ICL_REMOTE_RESOURCE_MAX_CHECKING_INTERVAL < interval || interval <= 0,
                        IOTCON_ERROR_INVALID_PARAMETER);
 
-       ret = icl_ioty_remote_resource_set_time_interval(time_interval);
-       if (IOTCON_ERROR_NONE != ret) {
-               ERR("icl_ioty_remote_resource_set_time_interval() Fail(%d)", ret);
-               return ret;
+       if (resource->monitoring.presence || resource->caching.observe) {
+               ret = icl_ioty_remote_resource_set_checking_interval(resource, interval);
+               if (IOTCON_ERROR_NONE != ret) {
+                       ERR("icl_ioty_remote_resource_set_checking_interval() Fail(%d)", ret);
+                       return ret;
+               }
        }
 
+       resource->checking_interval = interval;
+
        return IOTCON_ERROR_NONE;
 }
 
index 0a903d0..de21b65 100644 (file)
 #include "iotcon-remote-resource.h"
 #include "ic-options.h"
 
+typedef struct icl_remote_resource_caching {
+       iotcon_representation_h repr;
+       iotcon_remote_resource_cached_representation_changed_cb cb;
+       void *user_data;
+       int64_t observe;
+} icl_remote_resource_caching_s;
+
+typedef struct icl_remote_resource_monitoring {
+       iotcon_remote_resource_state_changed_cb cb;
+       void *user_data;
+       iotcon_presence_h presence;
+       iotcon_remote_resource_state_e state;
+} icl_remote_resource_monitoring_s;
+
 struct icl_remote_resource {
        bool is_found;
        int ref_count;
@@ -36,7 +50,10 @@ struct icl_remote_resource {
        iotcon_resource_interfaces_h ifaces;
        iotcon_connectivity_type_e connectivity_type;
        int64_t observe_handle;
-       iotcon_representation_h cached_repr;
+       int checking_interval;
+       unsigned int timer_id;
+       icl_remote_resource_caching_s caching;
+       icl_remote_resource_monitoring_s monitoring;
 };
 
 void icl_remote_resource_ref(iotcon_remote_resource_h resource);