From 56b01ce0d32d1a4059125a350ad108d35c529c9f Mon Sep 17 00:00:00 2001 From: Andrii Kalinich Date: Wed, 4 Jul 2018 16:20:20 +0300 Subject: Added lock for VRcommands map Also renamed AddCommands function Added DeleteCommand function --- .../application_manager/help_prompt_manager_impl.h | 15 +++++++ .../src/help_prompt_manager_impl.cc | 48 ++++++++++++++-------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/components/application_manager/include/application_manager/help_prompt_manager_impl.h b/src/components/application_manager/include/application_manager/help_prompt_manager_impl.h index f11f11c06d..a34f034e3e 100644 --- a/src/components/application_manager/include/application_manager/help_prompt_manager_impl.h +++ b/src/components/application_manager/include/application_manager/help_prompt_manager_impl.h @@ -34,6 +34,7 @@ #define SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_HELP_PROMPT_MANAGER_IMPL_H_ #include "application_manager/help_prompt_manager.h" +#include "utils/lock.h" #include "utils/timer.h" namespace application_manager { @@ -100,6 +101,19 @@ class HelpPromptManagerImpl : public HelpPromptManager { private: DISALLOW_COPY_AND_ASSIGN(HelpPromptManagerImpl); + /** + * @brief Add new smart object with VR command to the map + * @param cmd_id ID of VR command + * @param command smart object containing VR command structure + */ + void AddCommand(const uint32_t cmd_id, + const smart_objects::SmartObject& command); + /** + * @brief Delete VR command from map by its cmd_id + * @param cmd_id ID of VR command + * @return true if command was successfully deleted otherwise returns false + */ + bool DeleteCommand(const uint32_t cmd_id); /** * @brief Send TTS request to HMI */ @@ -138,6 +152,7 @@ class HelpPromptManagerImpl : public HelpPromptManager { Application& app_; ApplicationManager& app_manager_; VRCommandsMap vr_commands_; + sync_primitives::Lock vr_commands_lock_; SendingType sending_type_; std::size_t count_requests_commands_; bool is_tts_send_; diff --git a/src/components/application_manager/src/help_prompt_manager_impl.cc b/src/components/application_manager/src/help_prompt_manager_impl.cc index 696733e9f0..d951470104 100644 --- a/src/components/application_manager/src/help_prompt_manager_impl.cc +++ b/src/components/application_manager/src/help_prompt_manager_impl.cc @@ -71,12 +71,13 @@ void HelpPromptManagerImpl::OnVrCommandAdded( << " will not be added"); return; } - AddCommands(cmd_id, command); + AddCommand(cmd_id, command); SendRequests(); } -void HelpPromptManagerImpl::AddCommands( +void HelpPromptManagerImpl::AddCommand( const uint32_t cmd_id, const smart_objects::SmartObject& command) { + sync_primitives::AutoLock lock(vr_commands_lock_); auto it = vr_commands_.find(cmd_id); if (vr_commands_.end() != it) { LOG4CXX_DEBUG(logger_, "Commands with id:" << cmd_id << " already exists"); @@ -115,6 +116,26 @@ void HelpPromptManagerImpl::AddCommands( count_requests_commands_ += count_new_commands; } +bool HelpPromptManagerImpl::DeleteCommand(const uint32_t cmd_id) { + LOG4CXX_AUTO_TRACE(logger_); + sync_primitives::AutoLock lock(vr_commands_lock_); + + auto it = vr_commands_.find(cmd_id); + if (vr_commands_.end() == it) { + LOG4CXX_WARN(logger_, "VR command with id: " << cmd_id << " not found"); + return false; + } + + count_requests_commands_ -= + kLimitCommand == count_requests_commands_ ? 0 : it->second->length(); + + vr_commands_.erase(it); + LOG4CXX_DEBUG(logger_, + "VR command with id: " << cmd_id << " deleted for appID: " + << app_.app_id()); + return true; +} + void HelpPromptManagerImpl::OnVrCommandDeleted(const uint32_t cmd_id) { LOG4CXX_AUTO_TRACE(logger_); if (SendingType::kNoneSend == sending_type_) { @@ -124,15 +145,7 @@ void HelpPromptManagerImpl::OnVrCommandDeleted(const uint32_t cmd_id) { << " will not be deleted"); return; } - auto it = vr_commands_.find(cmd_id); - if (vr_commands_.end() != it) { - count_requests_commands_ -= - kLimitCommand == count_requests_commands_ ? 0 : it->second->length(); - - vr_commands_.erase(it); - LOG4CXX_DEBUG(logger_, - "VR command with id: " << cmd_id << " deleted for appID: " - << app_.app_id()); + if (DeleteCommand(cmd_id)) { SendRequests(); } } @@ -153,6 +166,7 @@ void HelpPromptManagerImpl::OnSetGlobalPropertiesReceived( void HelpPromptManagerImpl::OnAppActivated(const bool is_restore) { LOG4CXX_AUTO_TRACE(logger_); LOG4CXX_DEBUG(logger_, "Activated for appID:" << app_.app_id()); + if (is_restore) { LOG4CXX_DEBUG(logger_, "restoring"); is_tts_send_ = (app_.help_prompt() && app_.help_prompt()->length()); @@ -166,16 +180,16 @@ void HelpPromptManagerImpl::OnAppActivated(const bool is_restore) { " with the vrHelp and helpPrompt" " has been sent"); return; - } - else { + } else { sending_type_ = is_tts_send_ ? SendingType::kSendVRHelp : sending_type_; - sending_type_ = is_ui_send_ ? SendingType::kSendHelpPrompt : sending_type_; + sending_type_ = + is_ui_send_ ? SendingType::kSendHelpPrompt : sending_type_; is_tts_send_ = false; is_ui_send_ = false; const DataAccessor accessor = app_.commands_map(); const CommandsMap& commands = accessor.GetData(); for (auto& command : commands) { - AddCommands(command.first, *command.second); + AddCommand(command.first, *command.second); } } } @@ -269,6 +283,8 @@ void HelpPromptManagerImpl::SendBothRequests() { void HelpPromptManagerImpl::SendRequests() { LOG4CXX_AUTO_TRACE(logger_); + + sync_primitives::AutoLock lock(vr_commands_lock_); if (vr_commands_.empty()) { LOG4CXX_DEBUG(logger_, "vr_commands_ is empty"); return; @@ -348,7 +364,7 @@ void HelpPromptManagerImpl::CreateVRMsg( HelpPromptManagerImpl::SendingType HelpPromptManagerImpl::GetSendingType( const smart_objects::SmartObject& msg, const bool is_response) { LOG4CXX_AUTO_TRACE(logger_); - if (false == is_response) { + if (!is_response) { if (msg.keyExists(strings::help_prompt)) { is_tts_send_ = true; } -- cgit v1.2.1