diff options
Diffstat (limited to 'src/components/policy/src/policy/src/update_status_manager.cc')
-rw-r--r-- | src/components/policy/src/policy/src/update_status_manager.cc | 135 |
1 files changed, 122 insertions, 13 deletions
diff --git a/src/components/policy/src/policy/src/update_status_manager.cc b/src/components/policy/src/policy/src/update_status_manager.cc index 872e0c3e9..e5cc74a31 100644 --- a/src/components/policy/src/policy/src/update_status_manager.cc +++ b/src/components/policy/src/policy/src/update_status_manager.cc @@ -42,13 +42,22 @@ UpdateStatusManager::UpdateStatusManager() : listener_(NULL), exchange_in_progress_(false), update_required_(false), + update_scheduled_(false), exchange_pending_(false), - last_update_status_(policy::StatusUnknown), - update_response_timer_(this) { + apps_search_in_progress_(false), + last_update_status_(policy::StatusUnknown) { + update_status_thread_delegate_ = new UpdateThreadDelegate(this); + thread_ = threads::CreateThread("UpdateStatusThread", update_status_thread_delegate_); + thread_->start(); } UpdateStatusManager::~UpdateStatusManager() { - LOG4CXX_DEBUG(logger_, "Destroy update Status manager"); + LOG4CXX_AUTO_TRACE(logger_); + DCHECK(update_status_thread_delegate_); + DCHECK(thread_); + thread_->join(); + delete update_status_thread_delegate_; + threads::DeleteThread(thread_); } void UpdateStatusManager::set_listener(PolicyListener* listener) { @@ -57,26 +66,34 @@ void UpdateStatusManager::set_listener(PolicyListener* listener) { void UpdateStatusManager::OnUpdateSentOut(uint32_t update_timeout) { LOG4CXX_INFO(logger_, "OnUpdateSentOut"); - update_response_timer_.start(update_timeout); + DCHECK(update_status_thread_delegate_); + const unsigned milliseconds_in_second = 1000; + update_status_thread_delegate_->updateTimeOut(update_timeout * + milliseconds_in_second); set_exchange_in_progress(true); + set_exchange_pending(true); + set_update_required(false); } void UpdateStatusManager::OnUpdateTimeoutOccurs() { LOG4CXX_INFO(logger_, "OnUpdateTimeoutOccurs"); set_update_required(true); set_exchange_in_progress(false); + set_exchange_pending(false); + DCHECK(update_status_thread_delegate_); + update_status_thread_delegate_->updateTimeOut(0); // Stop Timer } void UpdateStatusManager::OnValidUpdateReceived() { LOG4CXX_INFO(logger_, "OnValidUpdateReceived"); - update_response_timer_.stop(); - set_update_required(false); + update_status_thread_delegate_->updateTimeOut(0); // Stop Timer + set_exchange_pending(false); set_exchange_in_progress(false); } void UpdateStatusManager::OnWrongUpdateReceived() { LOG4CXX_INFO(logger_, "OnWrongUpdateReceived"); - update_response_timer_.stop(); + update_status_thread_delegate_->updateTimeOut(0); // Stop Timer set_update_required(true); set_exchange_in_progress(false); } @@ -107,8 +124,8 @@ void UpdateStatusManager::OnPolicyInit(bool is_update_required) { update_required_ = is_update_required; } -PolicyTableStatus UpdateStatusManager::GetUpdateStatus() { - LOG4CXX_INFO(logger_, "GetUpdateStatus"); +PolicyTableStatus UpdateStatusManager::GetUpdateStatus() const { + LOG4CXX_AUTO_TRACE(logger_); if (!exchange_in_progress_ && !exchange_pending_ && !update_required_) { return PolicyTableStatus::StatusUpToDate; } @@ -120,11 +137,61 @@ PolicyTableStatus UpdateStatusManager::GetUpdateStatus() { return PolicyTableStatus::StatusUpdatePending; } +bool UpdateStatusManager::IsUpdateRequired() const { + return update_required_ || update_scheduled_; +} + +bool UpdateStatusManager::IsUpdatePending() const { + return exchange_pending_; +} + +void UpdateStatusManager::ScheduleUpdate() { + update_scheduled_ = true; + update_required_ = true; +} + +void UpdateStatusManager::ResetUpdateSchedule() { + update_scheduled_ = false; +} + +std::string UpdateStatusManager::StringifiedUpdateStatus() const { + switch (GetUpdateStatus()) { + case policy::StatusUpdatePending: + return "UPDATING"; + case policy::StatusUpdateRequired: + return "UPDATE_NEEDED"; + case policy::StatusUpToDate: + return "UP_TO_DATE"; + default: { + return "UNKNOWN"; + } + } +} + +void policy::UpdateStatusManager::OnAppsSearchStarted() { + LOG4CXX_AUTO_TRACE(logger_); + sync_primitives::AutoLock lock(apps_search_in_progress_lock_); + apps_search_in_progress_ = true; +} + +void policy::UpdateStatusManager::OnAppsSearchCompleted() { + LOG4CXX_AUTO_TRACE(logger_); + sync_primitives::AutoLock lock(apps_search_in_progress_lock_); + apps_search_in_progress_ = false; +} + +bool policy::UpdateStatusManager::IsAppsSearchInProgress() { + LOG4CXX_AUTO_TRACE(logger_); + sync_primitives::AutoLock lock(apps_search_in_progress_lock_); + return apps_search_in_progress_; +} + void UpdateStatusManager::CheckUpdateStatus() { - LOG4CXX_INFO(logger_, "CheckUpdateStatus"); + LOG4CXX_AUTO_TRACE(logger_); policy::PolicyTableStatus status = GetUpdateStatus(); if (listener_ && last_update_status_ != status) { - listener_->OnUpdateStatusChanged(status); + LOG4CXX_INFO(logger_, "Send OnUpdateStatusChanged"); + listener_->OnUpdateStatusChanged(StringifiedUpdateStatus()); } last_update_status_ = status; } @@ -152,8 +219,50 @@ void UpdateStatusManager::set_update_required(bool value) { CheckUpdateStatus(); } -UpdateStatusManager::UpdateResponseTimer::~UpdateResponseTimer() { - LOG4CXX_DEBUG(logger_, "Destroy update Status manager timer"); +UpdateStatusManager::UpdateThreadDelegate::UpdateThreadDelegate(UpdateStatusManager* update_status_manager): + timeout_(0), + stop_flag_(false), + state_lock_(true), + update_status_manager_(update_status_manager) { + LOG4CXX_INFO(logger_, "Create UpdateThreadDelegate"); +} + +UpdateStatusManager::UpdateThreadDelegate::~UpdateThreadDelegate() { + LOG4CXX_INFO(logger_, "Delete UpdateThreadDelegate"); +} + +void UpdateStatusManager::UpdateThreadDelegate::threadMain() { + LOG4CXX_DEBUG(logger_, "UpdateStatusManager thread started (started normal)"); + sync_primitives::AutoLock auto_lock(state_lock_); + while (false == stop_flag_) { + if (timeout_ > 0) { + LOG4CXX_DEBUG(logger_, "Timeout is greater then 0"); + sync_primitives::ConditionalVariable::WaitStatus wait_status = + termination_condition_.WaitFor(auto_lock, timeout_); + if (sync_primitives::ConditionalVariable::kTimeout == wait_status ) { + if (update_status_manager_) { + update_status_manager_->OnUpdateTimeoutOccurs(); + } + } + } else { + // Time is not active, wait, while timeout will be seted, + // or UpdateStatusManager will be deleted + termination_condition_.Wait(auto_lock); + } + } +} + +void UpdateStatusManager::UpdateThreadDelegate::exitThreadMain() { + sync_primitives::AutoLock auto_lock(state_lock_); + stop_flag_ = true; + LOG4CXX_INFO(logger_, "before notify"); + termination_condition_.NotifyOne(); +} + +void UpdateStatusManager::UpdateThreadDelegate::updateTimeOut(const uint32_t timeout_ms) { + sync_primitives::AutoLock auto_lock(state_lock_); + timeout_ = timeout_ms; + termination_condition_.NotifyOne(); } } // namespace policy |