summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjacobkeeler <jacob.keeler@livioradio.com>2019-02-27 17:49:02 -0500
committerjacobkeeler <jacob.keeler@livioradio.com>2019-02-27 17:49:02 -0500
commit090ebab24ddb3c761386f20e873b0142fb42db51 (patch)
treee7d1db0adbb5f3f5390f7370f1b01992b2acdb28
parenteba1f131162fdcb2be3164d98c39eab56d37b7dc (diff)
downloadsdl_core-090ebab24ddb3c761386f20e873b0142fb42db51.tar.gz
Add lock for `published_services` map
Also modify getter functions for services
-rw-r--r--src/components/application_manager/include/application_manager/app_service_manager.h16
-rw-r--r--src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/hmi/as_perform_app_service_interaction_request_from_hmi.cc7
-rw-r--r--src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/perform_app_service_interaction_request.cc7
-rw-r--r--src/components/application_manager/src/app_service_manager.cc106
4 files changed, 71 insertions, 65 deletions
diff --git a/src/components/application_manager/include/application_manager/app_service_manager.h b/src/components/application_manager/include/application_manager/app_service_manager.h
index 5a345e60fb..80e6b5521f 100644
--- a/src/components/application_manager/include/application_manager/app_service_manager.h
+++ b/src/components/application_manager/include/application_manager/app_service_manager.h
@@ -133,15 +133,13 @@ class AppServiceManager {
ApplicationSharedPtr& app,
bool& hmi_service);
- std::pair<std::string, AppService> ActiveServiceByType(
- std::string service_type);
+ AppService* ActiveServiceByType(std::string service_type);
- std::pair<std::string, AppService> EmbeddedServiceForType(
- std::string service_type);
+ AppService* EmbeddedServiceForType(std::string service_type);
- std::pair<std::string, AppService> FindServiceByName(std::string name);
+ AppService* FindServiceByName(std::string name);
- std::pair<std::string, AppService> FindServiceByID(std::string service_id);
+ AppService* FindServiceByID(std::string service_id);
std::string DefaultServiceByType(std::string service_type);
@@ -162,6 +160,8 @@ class AppServiceManager {
private:
ApplicationManager& app_manager_;
resumption::LastState& last_state_;
+
+ sync_primitives::RecursiveLock published_services_lock_;
std::map<std::string, AppService> published_services_;
void AppServiceUpdated(
@@ -171,8 +171,8 @@ class AppServiceManager {
void GetProviderFromService(const AppService& service,
ApplicationSharedPtr& app,
bool& hmi_service);
- std::pair<std::string, AppService> FindServiceByPolicyAppID(
- std::string policy_app_id, std::string type);
+ AppService* FindServiceByPolicyAppID(std::string policy_app_id,
+ std::string type);
std::string GetPolicyAppID(AppService service);
};
diff --git a/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/hmi/as_perform_app_service_interaction_request_from_hmi.cc b/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/hmi/as_perform_app_service_interaction_request_from_hmi.cc
index c384e73d33..2b66993d8f 100644
--- a/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/hmi/as_perform_app_service_interaction_request_from_hmi.cc
+++ b/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/hmi/as_perform_app_service_interaction_request_from_hmi.cc
@@ -79,7 +79,7 @@ void ASPerformAppServiceInteractionRequestFromHMI::Run() {
std::string service_id = msg_params[strings::service_id].asString();
auto service =
application_manager_.GetAppServiceManager().FindServiceByID(service_id);
- if (service.first.empty()) {
+ if (!service) {
smart_objects::SmartObject response_params;
response_params[strings::info] = "The requested service ID does not exist";
SendResponse(false,
@@ -99,9 +99,8 @@ void ASPerformAppServiceInteractionRequestFromHMI::Run() {
}
// Only activate service if it is not already active
- bool activate_service =
- request_service_active &&
- !service.second.record[strings::service_active].asBool();
+ bool activate_service = request_service_active &&
+ !service->record[strings::service_active].asBool();
if (activate_service) {
application_manager_.GetAppServiceManager().ActivateAppService(service_id);
}
diff --git a/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/perform_app_service_interaction_request.cc b/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/perform_app_service_interaction_request.cc
index d21c413b29..952d1834fb 100644
--- a/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/perform_app_service_interaction_request.cc
+++ b/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/perform_app_service_interaction_request.cc
@@ -66,7 +66,7 @@ void PerformAppServiceInteractionRequest::Run() {
std::string service_id = msg_params[strings::service_id].asString();
auto service =
application_manager_.GetAppServiceManager().FindServiceByID(service_id);
- if (service.first.empty()) {
+ if (!service) {
SendResponse(false,
mobile_apis::Result::INVALID_ID,
"The requested service ID does not exist");
@@ -81,9 +81,8 @@ void PerformAppServiceInteractionRequest::Run() {
}
// Only activate service if it is not already active
- bool activate_service =
- request_service_active &&
- !service.second.record[strings::service_active].asBool();
+ bool activate_service = request_service_active &&
+ !service->record[strings::service_active].asBool();
if (activate_service) {
if (app->is_foreground()) {
// App is in foreground, we can just activate the service
diff --git a/src/components/application_manager/src/app_service_manager.cc b/src/components/application_manager/src/app_service_manager.cc
index ed9120ad19..73219df456 100644
--- a/src/components/application_manager/src/app_service_manager.cc
+++ b/src/components/application_manager/src/app_service_manager.cc
@@ -70,11 +70,13 @@ smart_objects::SmartObject AppServiceManager::PublishAppService(
std::string str_to_hash = "";
std::string service_id = "";
+ published_services_lock_.Acquire();
do {
str_to_hash = manifest[strings::service_type].asString() +
std::to_string(std::rand());
service_id = encryption::MakeHash(str_to_hash);
} while (published_services_.find(service_id) != published_services_.end());
+ published_services_lock_.Release();
AppService app_service;
app_service.connection_key = connection_key;
@@ -104,9 +106,11 @@ smart_objects::SmartObject AppServiceManager::PublishAppService(
}
app_service.default_service = GetPolicyAppID(app_service) == default_app_id;
- published_services_.insert(
- std::pair<std::string, AppService>(service_id, app_service));
-
+ {
+ sync_primitives::AutoLock lock(published_services_lock_);
+ published_services_.insert(
+ std::pair<std::string, AppService>(service_id, app_service));
+ }
smart_objects::SmartObject msg_params;
msg_params[strings::system_capability][strings::system_capability_type] =
mobile_apis::SystemCapabilityType::APP_SERVICES;
@@ -117,9 +121,8 @@ smart_objects::SmartObject AppServiceManager::PublishAppService(
// Activate the new service if it is the default for its service type, or if
// no service is active of its service type
- std::pair<std::string, AppService> active_service =
- ActiveServiceByType(service_type);
- if (active_service.first.empty() || app_service.default_service) {
+ AppService* active_service = ActiveServiceByType(service_type);
+ if (!active_service || app_service.default_service) {
ActivateAppService(service_id);
}
@@ -128,7 +131,7 @@ smart_objects::SmartObject AppServiceManager::PublishAppService(
bool AppServiceManager::UnpublishAppService(const std::string service_id) {
LOG4CXX_AUTO_TRACE(logger_);
-
+ sync_primitives::AutoLock lock(published_services_lock_);
auto it = published_services_.find(service_id);
if (it == published_services_.end()) {
LOG4CXX_ERROR(logger_, "Service id does not exist in published services");
@@ -141,22 +144,22 @@ bool AppServiceManager::UnpublishAppService(const std::string service_id) {
msg_params[strings::system_capability][strings::system_capability_type] =
mobile_apis::SystemCapabilityType::APP_SERVICES;
- auto record = it->second.record;
+ auto& record = it->second.record;
if (record[strings::service_active].asBool()) {
record[strings::service_active] = false;
// Activate embedded service, if available
auto embedded_service = EmbeddedServiceForType(
record[strings::service_manifest][strings::service_type].asString());
- if (!embedded_service.first.empty()) {
- embedded_service.second.record[strings::service_active] = true;
- AppServiceUpdated(embedded_service.second.record,
+ if (embedded_service) {
+ embedded_service->record[strings::service_active] = true;
+ AppServiceUpdated(embedded_service->record,
mobile_apis::ServiceUpdateReason::ACTIVATED,
msg_params);
}
}
AppServiceUpdated(
- it->second.record, mobile_apis::ServiceUpdateReason::REMOVED, msg_params);
+ record, mobile_apis::ServiceUpdateReason::REMOVED, msg_params);
MessageHelper::BroadcastCapabilityUpdate(msg_params, app_manager_);
published_services_.erase(it);
@@ -166,6 +169,7 @@ bool AppServiceManager::UnpublishAppService(const std::string service_id) {
void AppServiceManager::UnpublishServices(const uint32_t connection_key) {
LOG4CXX_AUTO_TRACE(logger_);
LOG4CXX_DEBUG(logger_, "Unpublishing all app services: " << connection_key);
+ sync_primitives::AutoLock lock(published_services_lock_);
for (auto it = published_services_.begin(); it != published_services_.end();
++it) {
if (it->second.connection_key == connection_key) {
@@ -176,6 +180,7 @@ void AppServiceManager::UnpublishServices(const uint32_t connection_key) {
void AppServiceManager::OnAppActivated(ApplicationConstSharedPtr app) {
LOG4CXX_AUTO_TRACE(logger_);
+ sync_primitives::AutoLock lock(published_services_lock_);
auto it = published_services_.begin();
// Activate all services published by the app
for (; it != published_services_.end(); ++it) {
@@ -188,6 +193,7 @@ void AppServiceManager::OnAppActivated(ApplicationConstSharedPtr app) {
std::vector<smart_objects::SmartObject> AppServiceManager::GetAllServices() {
LOG4CXX_AUTO_TRACE(logger_);
std::vector<smart_objects::SmartObject> services;
+ sync_primitives::AutoLock lock(published_services_lock_);
for (auto it = published_services_.begin(); it != published_services_.end();
++it) {
services.push_back(it->second.record);
@@ -200,7 +206,7 @@ void AppServiceManager::GetProviderByType(const std::string& service_type,
bool& hmi_service) {
LOG4CXX_AUTO_TRACE(logger_);
auto active_service = ActiveServiceByType(service_type);
- if (active_service.first.empty()) {
+ if (!active_service) {
LOG4CXX_ERROR(logger_,
"There is no active service for the given service type: "
<< service_type);
@@ -208,13 +214,14 @@ void AppServiceManager::GetProviderByType(const std::string& service_type,
}
LOG4CXX_DEBUG(logger_, "Found provider for service type: " << service_type);
- GetProviderFromService(active_service.second, app, hmi_service);
+ GetProviderFromService(*active_service, app, hmi_service);
}
void AppServiceManager::GetProviderByID(const std::string& service_id,
ApplicationSharedPtr& app,
bool& hmi_service) {
LOG4CXX_AUTO_TRACE(logger_);
+ sync_primitives::AutoLock lock(published_services_lock_);
auto it = published_services_.find(service_id);
if (it == published_services_.end()) {
LOG4CXX_ERROR(logger_, "Service id does not exist in published services");
@@ -240,6 +247,7 @@ void AppServiceManager::GetProviderFromService(const AppService& service,
bool AppServiceManager::SetDefaultService(const std::string service_id) {
LOG4CXX_AUTO_TRACE(logger_);
+ sync_primitives::AutoLock lock(published_services_lock_);
auto it = published_services_.find(service_id);
if (it == published_services_.end()) {
LOG4CXX_ERROR(logger_, "Unable to find published service " << service_id);
@@ -254,8 +262,8 @@ bool AppServiceManager::SetDefaultService(const std::string service_id) {
if (!default_app_id.empty()) {
auto default_service =
FindServiceByPolicyAppID(default_app_id, service_type);
- if (!default_service.first.empty()) {
- default_service.second.default_service = false;
+ if (default_service) {
+ default_service->default_service = false;
}
}
service.default_service = true;
@@ -268,6 +276,7 @@ bool AppServiceManager::SetDefaultService(const std::string service_id) {
bool AppServiceManager::RemoveDefaultService(const std::string service_id) {
LOG4CXX_AUTO_TRACE(logger_);
+ sync_primitives::AutoLock lock(published_services_lock_);
auto it = published_services_.find(service_id);
if (it == published_services_.end()) {
LOG4CXX_ERROR(logger_, "Unable to find published service " << service_id);
@@ -291,6 +300,7 @@ bool AppServiceManager::RemoveDefaultService(const std::string service_id) {
bool AppServiceManager::ActivateAppService(const std::string service_id) {
LOG4CXX_AUTO_TRACE(logger_);
+ sync_primitives::AutoLock lock(published_services_lock_);
auto it = published_services_.find(service_id);
if (it == published_services_.end()) {
LOG4CXX_ERROR(logger_, "Unable to find published service " << service_id);
@@ -310,9 +320,9 @@ bool AppServiceManager::ActivateAppService(const std::string service_id) {
const std::string service_type =
service[strings::service_manifest][strings::service_type].asString();
auto active_service = ActiveServiceByType(service_type);
- if (!active_service.first.empty()) {
- active_service.second.record[strings::service_active] = false;
- AppServiceUpdated(active_service.second.record,
+ if (active_service) {
+ active_service->record[strings::service_active] = false;
+ AppServiceUpdated(active_service->record,
mobile_apis::ServiceUpdateReason::DEACTIVATED,
msg_params);
}
@@ -337,6 +347,7 @@ bool AppServiceManager::ActivateAppService(const std::string service_id) {
bool AppServiceManager::DeactivateAppService(const std::string service_id) {
LOG4CXX_AUTO_TRACE(logger_);
+ sync_primitives::AutoLock lock(published_services_lock_);
auto it = published_services_.find(service_id);
if (it == published_services_.end()) {
LOG4CXX_ERROR(logger_, "Unable to find published service " << service_id);
@@ -355,9 +366,9 @@ bool AppServiceManager::DeactivateAppService(const std::string service_id) {
const std::string service_type =
service[strings::service_manifest][strings::service_type].asString();
auto embedded_service = EmbeddedServiceForType(service_type);
- if (!embedded_service.first.empty()) {
- embedded_service.second.record[strings::service_active] = true;
- AppServiceUpdated(embedded_service.second.record,
+ if (embedded_service) {
+ embedded_service->record[strings::service_active] = true;
+ AppServiceUpdated(embedded_service->record,
mobile_apis::ServiceUpdateReason::ACTIVATED,
msg_params);
}
@@ -372,54 +383,53 @@ bool AppServiceManager::DeactivateAppService(const std::string service_id) {
return true;
}
-std::pair<std::string, AppService> AppServiceManager::ActiveServiceByType(
- std::string service_type) {
+AppService* AppServiceManager::ActiveServiceByType(std::string service_type) {
LOG4CXX_AUTO_TRACE(logger_);
+ sync_primitives::AutoLock lock(published_services_lock_);
for (auto it = published_services_.begin(); it != published_services_.end();
++it) {
if (it->second.record[strings::service_manifest][strings::service_type]
.asString() == service_type &&
it->second.record[strings::service_published].asBool() &&
it->second.record[strings::service_active].asBool()) {
- return *it;
+ return &(it->second);
}
}
- AppService empty;
- return std::make_pair(std::string(), empty);
+ return NULL;
}
-std::pair<std::string, AppService> AppServiceManager::EmbeddedServiceForType(
+AppService* AppServiceManager::EmbeddedServiceForType(
std::string service_type) {
LOG4CXX_AUTO_TRACE(logger_);
+ sync_primitives::AutoLock lock(published_services_lock_);
for (auto it = published_services_.begin(); it != published_services_.end();
++it) {
if (it->second.record[strings::service_manifest][strings::service_type]
.asString() == service_type &&
!it->second.mobile_service) {
- return *it;
+ return &(it->second);
}
}
- AppService empty;
- return std::make_pair(std::string(), empty);
+ return NULL;
}
-std::pair<std::string, AppService> AppServiceManager::FindServiceByName(
- std::string name) {
+AppService* AppServiceManager::FindServiceByName(std::string name) {
LOG4CXX_AUTO_TRACE(logger_);
+ sync_primitives::AutoLock lock(published_services_lock_);
for (auto it = published_services_.begin(); it != published_services_.end();
++it) {
if (it->second.record[strings::service_manifest][strings::service_name]
.asString() == name) {
- return *it;
+ return &(it->second);
}
}
- AppService empty;
- return std::make_pair(std::string(), empty);
+ return NULL;
}
-std::pair<std::string, AppService> AppServiceManager::FindServiceByPolicyAppID(
+AppService* AppServiceManager::FindServiceByPolicyAppID(
std::string policy_app_id, std::string type) {
LOG4CXX_AUTO_TRACE(logger_);
+ sync_primitives::AutoLock lock(published_services_lock_);
for (auto it = published_services_.begin(); it != published_services_.end();
++it) {
if (it->second.record[strings::service_manifest][strings::service_type]
@@ -428,25 +438,22 @@ std::pair<std::string, AppService> AppServiceManager::FindServiceByPolicyAppID(
}
if (policy_app_id == GetPolicyAppID(it->second)) {
- return *it;
+ return &(it->second);
}
}
- AppService empty;
- return std::make_pair(std::string(), empty);
+ return NULL;
}
-std::pair<std::string, AppService> AppServiceManager::FindServiceByID(
- std::string service_id) {
+AppService* AppServiceManager::FindServiceByID(std::string service_id) {
LOG4CXX_AUTO_TRACE(logger_);
+ sync_primitives::AutoLock lock(published_services_lock_);
auto it = published_services_.find(service_id);
if (it == published_services_.end()) {
LOG4CXX_ERROR(logger_, "Service id does not exist in published services");
- AppService empty;
- return std::make_pair(std::string(), empty);
+ return NULL;
}
-
- return *it;
+ return &(it->second);
}
std::string AppServiceManager::DefaultServiceByType(std::string service_type) {
@@ -461,6 +468,7 @@ std::string AppServiceManager::DefaultServiceByType(std::string service_type) {
void AppServiceManager::SetServicePublished(const std::string service_id,
bool service_published) {
LOG4CXX_AUTO_TRACE(logger_);
+ sync_primitives::AutoLock lock(published_services_lock_);
auto it = published_services_.find(service_id);
if (it == published_services_.end()) {
LOG4CXX_ERROR(logger_, "Service id does not exist in published services");
@@ -485,7 +493,7 @@ bool AppServiceManager::UpdateNavigationCapabilities(
EnumConversionHelper<mobile_apis::AppServiceType::eType>::EnumToString(
mobile_apis::AppServiceType::NAVIGATION, &navi_service_type);
auto service = ActiveServiceByType(navi_service_type);
- if (service.first.empty()) {
+ if (!service) {
return false;
}
@@ -496,13 +504,13 @@ bool AppServiceManager::UpdateNavigationCapabilities(
out_params[strings::get_way_points_enabled] = false;
}
- if (!service.second.record[strings::service_manifest].keyExists(
+ if (!service->record[strings::service_manifest].keyExists(
strings::handled_rpcs)) {
return true;
}
smart_objects::SmartObject& handled_rpcs =
- service.second.record[strings::service_manifest][strings::handled_rpcs];
+ service->record[strings::service_manifest][strings::handled_rpcs];
for (size_t i = 0; i < handled_rpcs.length(); ++i) {
if (handled_rpcs[i].asInt() == mobile_apis::FunctionID::SendLocationID) {
out_params[strings::send_location_enabled] = true;