static uint16_t get_poll_timeout(kaa_client_t *kaa_client) { KAA_RETURN_IF_NIL(kaa_client, 0); uint16_t select_timeout; kaa_tcp_channel_get_max_timeout(&kaa_client->channel, &select_timeout); if ((kaa_client->external_process_max_delay > 0) && (select_timeout > kaa_client->external_process_max_delay)) { select_timeout = kaa_client->external_process_max_delay; } if ((KAA_BOOTSTRAP_RESPONSE_PERIOD > 0) && (select_timeout > KAA_BOOTSTRAP_RESPONSE_PERIOD)) { select_timeout = KAA_BOOTSTRAP_RESPONSE_PERIOD; } return select_timeout; }
int kaa_demo_event_loop() { kaa_error_t error_code = kaa_start(kaa_context_); if (error_code) { KAA_LOG_FATAL(kaa_context_->logger, error_code,"Failed to start Kaa workflow"); return -1; } uint16_t select_timeout; error_code = kaa_tcp_channel_get_max_timeout(&operations_channel, &select_timeout); if (error_code) { KAA_LOG_FATAL(kaa_context_->logger, error_code,"Failed to get Operations channel keepalive timeout"); return -1; } if (select_timeout > KAA_DEMO_LOG_GENERATION_FREQUENCY) { select_timeout = KAA_DEMO_LOG_GENERATION_FREQUENCY; } fd_set read_fds, write_fds, except_fds; int ops_fd = 0, bootstrap_fd = 0; struct timeval select_tv = { 0, 0 }; int max_fd = 0; while (log_record_counter < KAA_DEMO_LOGS_TO_SEND) { FD_ZERO(&read_fds); FD_ZERO(&write_fds); FD_ZERO(&except_fds); max_fd = 0; kaa_tcp_channel_get_descriptor(&operations_channel, &ops_fd); if (max_fd < ops_fd) max_fd = ops_fd; kaa_tcp_channel_get_descriptor(&bootstrap_channel, &bootstrap_fd); if (max_fd < bootstrap_fd) max_fd = bootstrap_fd; if (kaa_tcp_channel_is_ready(&operations_channel, FD_READ)) FD_SET(ops_fd, &read_fds); if (kaa_tcp_channel_is_ready(&operations_channel, FD_WRITE)) FD_SET(ops_fd, &write_fds); if (kaa_tcp_channel_is_ready(&bootstrap_channel, FD_READ)) FD_SET(bootstrap_fd, &read_fds); if (kaa_tcp_channel_is_ready(&bootstrap_channel, FD_WRITE)) FD_SET(bootstrap_fd, &write_fds); select_tv.tv_sec = select_timeout; select_tv.tv_usec = 0; int poll_result = select(max_fd + 1, &read_fds, &write_fds, NULL, &select_tv); if (poll_result == 0) { kaa_demo_add_log_record(); kaa_tcp_channel_check_keepalive(&operations_channel); kaa_tcp_channel_check_keepalive(&bootstrap_channel); } else if (poll_result > 0) { if (bootstrap_fd >= 0) { if (FD_ISSET(bootstrap_fd, &read_fds)) { KAA_LOG_DEBUG(kaa_context_->logger, KAA_ERR_NONE,"Processing IN event for the Bootstrap client socket %d", bootstrap_fd); error_code = kaa_tcp_channel_process_event(&bootstrap_channel, FD_READ); if (error_code) KAA_LOG_ERROR(kaa_context_->logger, KAA_ERR_NONE,"Failed to process IN event for the Bootstrap client socket %d", bootstrap_fd); } if (FD_ISSET(bootstrap_fd, &write_fds)) { KAA_LOG_DEBUG(kaa_context_->logger, KAA_ERR_NONE,"Processing OUT event for the Bootstrap client socket %d", bootstrap_fd); error_code = kaa_tcp_channel_process_event(&bootstrap_channel, FD_WRITE); if (error_code) KAA_LOG_ERROR(kaa_context_->logger, error_code,"Failed to process OUT event for the Bootstrap client socket %d", bootstrap_fd); } } if (ops_fd >= 0) { if (FD_ISSET(ops_fd, &read_fds)) { KAA_LOG_DEBUG(kaa_context_->logger, KAA_ERR_NONE,"Processing IN event for the Operations client socket %d", ops_fd); error_code = kaa_tcp_channel_process_event(&operations_channel, FD_READ); if (error_code) KAA_LOG_ERROR(kaa_context_->logger, error_code,"Failed to process IN event for the Operations client socket %d", ops_fd); } if (FD_ISSET(ops_fd, &write_fds)) { KAA_LOG_DEBUG(kaa_context_->logger, KAA_ERR_NONE,"Processing OUT event for the Operations client socket %d", ops_fd); error_code = kaa_tcp_channel_process_event(&operations_channel, FD_WRITE); if (error_code) KAA_LOG_ERROR(kaa_context_->logger, error_code,"Failed to process OUT event for the Operations client socket %d", ops_fd); } } } else { KAA_LOG_ERROR(kaa_context_->logger, KAA_ERR_BAD_STATE,"Failed to poll descriptors: %s", strerror(errno)); return -1; } } return 0; }
void thread_run_fn(uintptr_t arg) { if(!arg) { sndc_printf("Kaa client thread function Error, no args\n"); return; } kaa_client_t *self = (kaa_client_t *)arg; KAA_LOG_TRACE(self->kaa_context->logger, KAA_ERR_NONE, "Kaa client working thread started...."); sndc_sem_post(&self->logging_semophore); sndc_sock_fdset r_set; sndc_sock_fdset w_set; sndc_sock_fdset x_set; int r = 0; int max_fd = 0; uint32_t msec = 0; int ops_fd = -1, bootstrap_fd = -1; bool_t fdset = false; uint16_t timeout = self->max_update_time; kaa_error_t error_code; KAA_LOG_TRACE(self->kaa_context->logger, KAA_ERR_NONE, "Kaa client working thread(%s) wait starting...", self->thread_name); sndc_sem_wait(&self->start_semophore); KAA_LOG_INFO(self->kaa_context->logger, KAA_ERR_NONE, "Kaa client working thread(%s) started", self->thread_name); while(self->operate) { max_fd = 0; SNDC_FD_ZERO(&r_set); SNDC_FD_ZERO(&w_set); SNDC_FD_ZERO(&x_set); // This semaphore is used to synchronize main thread and kaa_client thread, // mostly for logging proposes. sndc_sem_tryWait(&self->logging_semophore); if (self->operations_channel.context) kaa_tcp_channel_get_descriptor(&self->operations_channel, &ops_fd); if(self->bootstrap_channel.context) kaa_tcp_channel_get_descriptor(&self->bootstrap_channel, &bootstrap_fd); KAA_LOG_DEBUG(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: descriptors: bootstrap(%d), operations(%d)", bootstrap_fd, ops_fd); print_mem_stat(self); if (bootstrap_fd >= 0) { fdset = false; if (kaa_tcp_channel_is_ready(&self->bootstrap_channel, FD_READ)) { SNDC_FD_SET(bootstrap_fd, &r_set); KAA_LOG_DEBUG(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: Bootstrap READ set wait"); fdset = true; } if (kaa_tcp_channel_is_ready(&self->bootstrap_channel, FD_WRITE)) { SNDC_FD_SET(bootstrap_fd, &w_set); KAA_LOG_DEBUG(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: Bootstrap WRITE set wait"); fdset = true; } if (fdset) { max_fd = MAX(max_fd, bootstrap_fd); SNDC_FD_SET(bootstrap_fd, &x_set); } } if (ops_fd >= 0) { fdset = false; if (kaa_tcp_channel_is_ready(&self->operations_channel, FD_READ)) { SNDC_FD_SET(ops_fd, &r_set); KAA_LOG_DEBUG(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: Operations READ set wait"); fdset = true; } if (kaa_tcp_channel_is_ready(&self->operations_channel, FD_WRITE)) { SNDC_FD_SET(ops_fd, &w_set); KAA_LOG_DEBUG(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: Operations WRITE set wait"); fdset = true; } if (fdset) { max_fd = MAX(max_fd, ops_fd); SNDC_FD_SET(ops_fd, &x_set); } } kaa_tcp_channel_get_max_timeout(&self->operations_channel, &timeout); self->timeval.tv_sec = timeout; if (timeout > self->max_update_time) self->timeval.tv_sec = self->max_update_time; self->timeval.tv_usec = 0; sndc_sem_post(&self->logging_semophore); r = sndc_sock_select(max_fd+1,&r_set,&w_set,&x_set,&self->timeval); sndc_sem_tryWait(&self->logging_semophore); if (r == 0) { msec = sndc_sys_getTimestamp_msec(); KAA_LOG_DEBUG(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: timeout (%d) expired", self->timeval.tv_sec); if (self->bootstrap_state == BOOTSRAP_FINISHED && bootstrap_fd == -1) { sndc_printf("Bootstrap channel deinit, Operations channel init %d\n", bootstrap_fd); KAA_LOG_INFO(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: Bootstrap channel finish processing switching to Operations channel"); kaa_client_deinit_bootstrap_channel(self); kaa_client_init_operations_channel(self); } if (self->bootstrap_channel.context) { error_code = kaa_tcp_channel_check_keepalive(&self->bootstrap_channel); if (error_code) { KAA_LOG_ERROR(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: Failed Keepalive Bootstrap(%d) check", bootstrap_fd); } } if (self->operations_channel.context) { error_code = kaa_tcp_channel_check_keepalive(&self->operations_channel); if (error_code) { KAA_LOG_ERROR(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: Failed Keepalive Operations(%d) check", ops_fd); } } } else if (r > 0) { sndc_printf("FD SET return %d events\n", r); KAA_LOG_DEBUG(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: select() return %d events", r); if (bootstrap_fd >= 0) { fdset = false; if (SNDC_FD_ISSET(bootstrap_fd, &r_set)) { fdset = true; KAA_LOG_DEBUG(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: Read Event Bootstrap(%d)", bootstrap_fd); error_code = kaa_tcp_channel_process_event(&self->bootstrap_channel, FD_READ); if (error_code) { KAA_LOG_ERROR(self->kaa_context->logger, error_code, "IO LOOP: Failed Read Event Bootstrap(%d)", bootstrap_fd); } } if (fdset) r--; if (r > 0) { fdset = false; if (SNDC_FD_ISSET(bootstrap_fd, &w_set)) { fdset = true; KAA_LOG_DEBUG(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: Write Event Bootstrap(%d)", bootstrap_fd); error_code = kaa_tcp_channel_process_event(&self->bootstrap_channel, FD_WRITE); if (error_code) { KAA_LOG_ERROR(self->kaa_context->logger, error_code, "IO LOOP: Failed Write Event Bootstrap(%d)", bootstrap_fd); } } if (fdset) r--; } if (r > 0) { fdset = false; if (SNDC_FD_ISSET(bootstrap_fd, &x_set)) { fdset = true; sndc_printf("Exception Event Bootstrap %d\n", bootstrap_fd); KAA_LOG_DEBUG(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: Exception Event Bootstrap(%d)", bootstrap_fd); error_code = kaa_tcp_channel_process_event(&self->bootstrap_channel, FD_EXCEPTION); if (error_code) { KAA_LOG_ERROR(self->kaa_context->logger, error_code, "IO LOOP: Failed Exception Event Bootstrap(%d)", bootstrap_fd); } } if (fdset) r--; } } if (r > 0 && ops_fd >= 0) { fdset = false; if (SNDC_FD_ISSET(ops_fd, &r_set)) { fdset = true; KAA_LOG_DEBUG(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: Read Event Operations(%d)", ops_fd); error_code = kaa_tcp_channel_process_event(&self->operations_channel, FD_READ); if (error_code) { KAA_LOG_ERROR(self->kaa_context->logger, error_code, "IO LOOP: Failed Read Event Operations(%d)", ops_fd); } } if (fdset) r--; if (r > 0) { fdset = false; if (SNDC_FD_ISSET(ops_fd, &w_set)) { fdset = true; KAA_LOG_DEBUG(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: Write Event Operations(%d)", ops_fd); error_code = kaa_tcp_channel_process_event(&self->operations_channel, FD_WRITE); if (error_code) { KAA_LOG_ERROR(self->kaa_context->logger, error_code, "IO LOOP: Failed Write Event Operations(%d)", ops_fd); } } } if (fdset) r--; if (r > 0) { fdset = false; if (SNDC_FD_ISSET(ops_fd, &x_set)) { fdset = true; sndc_printf("Exception Event Operations %d\n", ops_fd); KAA_LOG_DEBUG(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: Exception Event Operations(%d)", ops_fd); error_code = kaa_tcp_channel_process_event(&self->operations_channel, FD_EXCEPTION); if (error_code) { KAA_LOG_ERROR(self->kaa_context->logger, error_code, "IO LOOP: Failed Exception Event Operations(%d)", ops_fd); } } } } } else { KAA_LOG_ERROR(self->kaa_context->logger, KAA_ERR_BAD_STATE, "IO LOOP: Error %d returned from select()", r); } } KAA_LOG_INFO(self->kaa_context->logger, KAA_ERR_NONE, "IO LOOP: Finished."); }