static bool is_timeout(kaa_log_collector_t *self) { KAA_RETURN_IF_NIL2(self, self->timeouts, false); bool is_timeout = false; kaa_time_t now = KAA_TIME(); kaa_list_node_t *it = kaa_list_begin(self->timeouts); while (it) { timeout_info_t *info = (timeout_info_t *)kaa_list_get_data(it); if (now >= info->timeout) { KAA_LOG_WARN(self->logger, KAA_ERR_TIMEOUT, "Log delivery timeout occurred (bucket_id %u)", info->log_bucket_id); is_timeout = true; break; } it = kaa_list_next(it); } if (is_timeout) { it = kaa_list_begin(self->timeouts); while (it) { timeout_info_t *info = (timeout_info_t *)kaa_list_get_data(it); ext_log_storage_unmark_by_bucket_id(self->log_storage_context, info->log_bucket_id); it = kaa_list_next(it); } kaa_list_clear(self->timeouts, NULL); ext_log_upload_strategy_on_timeout(self->log_upload_strategy_context); } return is_timeout; }
static kaa_error_t remember_request(kaa_log_collector_t *self, uint16_t bucket_id) { KAA_RETURN_IF_NIL(self, KAA_ERR_BADPARAM); timeout_info_t *info = (timeout_info_t *)KAA_MALLOC(sizeof(timeout_info_t)); KAA_RETURN_IF_NIL(info, KAA_ERR_NOMEM); info->log_bucket_id = bucket_id; info->timeout = KAA_TIME() + (kaa_time_t)ext_log_upload_strategy_get_timeout(self->log_upload_strategy_context); kaa_list_node_t *it = kaa_list_push_back(self->timeouts, info); if (!it) { KAA_FREE(info); return KAA_ERR_NOMEM; } return KAA_ERR_NONE; }
static kaa_error_t remember_request(kaa_log_collector_t *self, uint16_t bucket_id, uint16_t count) { // TODO(KAA-982): Use asserts if (!self) { return KAA_ERR_BADPARAM; } timeout_info_t *info = KAA_MALLOC(sizeof(*info)); if (!info) { return KAA_ERR_NOMEM; } info->log_bucket_id = bucket_id; info->deadline = KAA_TIME() + (kaa_time_t)ext_log_upload_strategy_get_timeout(self->log_upload_strategy_context); info->log_count = count; kaa_list_node_t *it = kaa_list_push_back(self->timeouts, info); if (!it) { KAA_FREE(info); return KAA_ERR_NOMEM; } return KAA_ERR_NONE; }
kaa_error_t kaa_client_start(kaa_client_t *kaa_client, external_process_fn external_process, void *external_process_context, kaa_time_t max_delay) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_error_t error_code = kaa_check_readiness(kaa_client->context); if (error_code != KAA_ERR_NONE) { KAA_LOG_ERROR(kaa_client->context->logger, error_code, "Cannot start Kaa client: Kaa context is not fully initialized"); return error_code; } kaa_client->external_process = external_process; kaa_client->external_process_context = external_process_context; kaa_client->external_process_max_delay = max_delay; kaa_client->external_process_last_call = KAA_TIME(); KAA_LOG_INFO(kaa_client->context->logger, KAA_ERR_NONE, "Starting Kaa client..."); while (kaa_client->operate) { if (kaa_client->external_process) { if (KAA_TIME() - kaa_client->external_process_last_call >= (kaa_time_t)kaa_client->external_process_max_delay) { kaa_client->external_process(kaa_client->external_process_context); kaa_client->external_process_last_call = KAA_TIME(); } } if (kaa_process_failover(kaa_client->context)) { kaa_client->bootstrap_complete = false; } else { if(kaa_client->channel_id>0) { if (kaa_client->channel_state == KAA_CLIENT_CHANNEL_STATE_NOT_CONNECTED) { error_code = kaa_client_process_channel_disconnected(kaa_client); } else if (kaa_client->channel_state == KAA_CLIENT_CHANNEL_STATE_CONNECTED) { error_code = kaa_client_process_channel_connected(kaa_client); if (error_code == KAA_ERR_TIMEOUT) kaa_client_deinit_channel(kaa_client); } } else { //No initialized channels if (kaa_client->bootstrap_complete) { KAA_LOG_INFO(kaa_client->context->logger, KAA_ERR_NONE, "Channel [0x%08X] Boostrap complete, reinitializing to Operations ...", kaa_client->channel_id); kaa_client->bootstrap_complete = false; kaa_client_deinit_channel(kaa_client); kaa_client_init_channel(kaa_client, KAA_CLIENT_CHANNEL_TYPE_OPERATIONS); if (error_code == KAA_ERR_BAD_STATE) { kaa_client_deinit_channel(kaa_client); kaa_client->bootstrap_complete = false; } } else { KAA_LOG_INFO(kaa_client->context->logger, KAA_ERR_NONE, "Channel [0x%08X] Operations error, reinitializing to Bootstrap ...", kaa_client->channel_id); kaa_client->bootstrap_complete = true; kaa_client_deinit_channel(kaa_client); kaa_client_init_channel(kaa_client, KAA_CLIENT_CHANNEL_TYPE_BOOTSTRAP); } } } #ifndef KAA_DISABLE_FEATURE_LOGGING ext_log_upload_timeout(kaa_client->context->log_collector); #endif } KAA_LOG_INFO(kaa_client->context->logger, KAA_ERR_NONE, "Kaa client stopped"); return error_code; }
kaa_time_t ext_get_systime(void) { return KAA_TIME(); }