From 756ce400a252b2b12b7fa99ec2a9be778379e049 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Thu, 30 Jun 2022 17:31:38 +0800 Subject: [PATCH 01/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../include/utils/impl_utils.h | 15 ++++++ .../include/versionmanager/version_manager.h | 2 +- .../componentmanager/component_manager.cpp | 4 +- .../src/utils/impl_utils.cpp | 34 +++++++++++++ .../versionmanager/version_info_manager.cpp | 48 +++++++++++++++++++ 5 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp create mode 100644 services/distributedhardwarefwkserviceimpl/src/versionmanager/version_info_manager.cpp diff --git a/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h b/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h index 992cc601..9ff9981f 100644 --- a/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h +++ b/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h @@ -18,6 +18,8 @@ #include +#include "nlohmann/json.hpp" + #include "device_type.h" namespace OHOS { @@ -53,14 +55,27 @@ struct CompVersion { std::string handlerVersion; std::string sourceVersion; std::string sinkVersion; + + // virtual int32_t FromJsonString(const std::string &jsonStr); + // virtual std::string ToJsonString(); }; struct DHVersion { std::string uuid; std::string dhVersion; std::unordered_map compVersions; + + DHVersion(std::string uuid, std::string dhVersion, std::unordered_map compVersions) + : uuid(uuid), dhVersion(dhVersion), compVersions(compVersions) {} + + // virtual std::string GetKey() const; + virtual int32_t FromJsonString(const std::string &jsonStr); + virtual std::string ToJsonString(); }; +void ToJson(nlohmann::json &jsonObject, const DHVersion &dhVersion); +void FromJson(const nlohmann::json &jsonObject, DHVersion &dhVersion); + struct TaskParam { std::string networkId; std::string uuid; diff --git a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h index 0626567a..546cf54f 100644 --- a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h @@ -28,7 +28,7 @@ namespace OHOS { namespace DistributedHardware { const std::string DH_LOCAL_VERSION = "1.0"; -class VersionManager { +class VersionManager : public DistributedKv::KvStoreObserver { DECLARE_SINGLE_INSTANCE_BASE(VersionManager); public: diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index fa9d0939..52f57055 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -339,7 +339,7 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std } param.attrs = capability->GetDHAttrs(); - param.version = GetSinkVersion(networkId, uuid, dhType); + param.version = GetSinkVersionFromRpc(networkId, uuid, dhType); if (param.version.empty()) { DHLOGI("Get Sink Version failed, uuid = %s, dhId = %s", GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str()); @@ -352,7 +352,7 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std return DH_FWK_SUCCESS; } -std::string ComponentManager::GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType) +std::string ComponentManager::GetSinkVersionFromRpc(const std::string &networkId, const std::string &uuid, DHType dhType) { DHLOGI("networkId = %s ", GetAnonyString(networkId).c_str()); auto sinkVersion = GetVersionFromCache(uuid, dhType); diff --git a/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp b/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp new file mode 100644 index 00000000..1f318981 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "impl_utils.h" + +namespace OHOS { +namespace DistributedHardware { +void DHVersion::FromJsonString(const std::string &jsonStr) +{ + nlohmann::json jsonObj = nlohmann::json::parse(jsonStr); + FromJson(jsonObj, *this); + return DH_FWK_SUCCESS; +} + +void ToJson(nlohmann::json &jsonObject, const DHVersion &dhVersion) +{ + jsonObject[UUID] = dhVersion.uuid; + jsonObject[DH_VERSION] = dhVersion.dhVersion; + +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_info_manager.cpp new file mode 100644 index 00000000..8cbb4b40 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_info_manager.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_HARDWARE_CAPABILITY_INFO_MANAGER_H +#define OHOS_DISTRIBUTED_HARDWARE_CAPABILITY_INFO_MANAGER_H + +#include +#include +#include + +#include "kvstore_observer.h" + +#include "capability_info.h" +#include "capability_info_event.h" +#include "capability_utils.h" +#include "db_adapter.h" +#include "event.h" +#include "eventbus_handler.h" +#include "event_bus.h" +#include "event_sender.h" +#include "single_instance.h" + +class DBAdapter; +namespace OHOS { +namespace DistributedHardware { +namespace { + constexpr int32_t MANUAL_SYNC_TIMEOUT = 1; +} +class CapabilityInfoManager : public std::enable_shared_from_this, + public EventSender, + public DistributedKv::KvStoreObserver, + public EventBusHandler { +public: + +}; +} \ No newline at end of file -- Gitee From 8c690402c8ea02528fba9a713d78e8bdcb6c3e57 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 1 Jul 2022 16:27:03 +0800 Subject: [PATCH 02/36] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- common/utils/include/constants.h | 9 ++++ common/utils/include/device_type.h | 2 +- .../componentloader/component_loader.h | 2 +- .../include/utils/impl_utils.h | 8 ++-- .../versionmanager/version_info_manager.h} | 2 + .../src/utils/impl_utils.cpp | 44 +++++++++++++++++-- .../src/versionmanager/version_manager.cpp | 6 ++- 7 files changed, 63 insertions(+), 10 deletions(-) rename services/distributedhardwarefwkserviceimpl/{src/versionmanager/version_info_manager.cpp => include/versionmanager/version_info_manager.h} (97%) diff --git a/common/utils/include/constants.h b/common/utils/include/constants.h index 37b1dfa6..c79cdb03 100644 --- a/common/utils/include/constants.h +++ b/common/utils/include/constants.h @@ -36,12 +36,21 @@ namespace DistributedHardware { const std::string DH_TYPE = "dh_type"; const std::string DH_ATTRS = "dh_attrs"; const std::string DH_LOG_TITLE_TAG = "DHFWK"; + const std::string DH_VER = "dh_ver"; + const std::string COMP_VER = "comp_ver"; + const std::string NAME = "name"; + const std::string TYPE = "type"; + const std::string HANDLER = "handler"; + const std::string SOURCE_VER = "source_ver"; + const std::string SINK_VER = "sink_ver"; const std::string DH_TASK_NAME_PREFIX = "Task_"; const std::string DH_FWK_PKG_NAME = "ohos.dhardware"; const std::string DH_COMPONENT_VERSIONS = "componentVersions"; const std::string DH_COMPONENT_TYPE = "dhType"; const std::string DH_COMPONENT_SINK_VER = "version"; const std::string DH_COMPONENT_DEFAULT_VERSION = "1.0"; + + } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/common/utils/include/device_type.h b/common/utils/include/device_type.h index 06f42514..4c60c270 100644 --- a/common/utils/include/device_type.h +++ b/common/utils/include/device_type.h @@ -28,7 +28,7 @@ enum class DHType : uint32_t { SPEAKER = 0x04, // Speaker DISPLAY = 0x08, // Display GPS = 0x10, // GPS - INPUT = 0x20, // Key board + INPUT = 0x20, // Key board HFP = 0x40, // HFP External device A2D = 0x80, // A2DP External device VIRMODEM_MIC = 0x100, // Cellular call MIC diff --git a/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h b/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h index b0dca951..b4b52da8 100644 --- a/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h +++ b/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h @@ -79,7 +79,7 @@ private: std::string Readfile(const std::string &filePath); private: - DHVersion localDHVersion_; + DHVersion localDHVersion_ {"", "", ""}; std::map compHandlerMap_; std::atomic isLocalVersionInit_; }; diff --git a/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h b/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h index 9ff9981f..263b6e67 100644 --- a/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h +++ b/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h @@ -62,19 +62,21 @@ struct CompVersion { struct DHVersion { std::string uuid; + std::string deviceId; std::string dhVersion; std::unordered_map compVersions; - DHVersion(std::string uuid, std::string dhVersion, std::unordered_map compVersions) - : uuid(uuid), dhVersion(dhVersion), compVersions(compVersions) {} + DHVersion(std::string uuid, std::string deviceId, std::string dhVersion) + : uuid(uuid), deviceId(deviceId), dhVersion(dhVersion) {} - // virtual std::string GetKey() const; virtual int32_t FromJsonString(const std::string &jsonStr); virtual std::string ToJsonString(); }; void ToJson(nlohmann::json &jsonObject, const DHVersion &dhVersion); void FromJson(const nlohmann::json &jsonObject, DHVersion &dhVersion); +void ToJson(nlohmann::json &jsonObject, const CompVersion &dhVersion); +void FromJson(const nlohmann::json &jsonObject, CompVersion &dhVersion); struct TaskParam { std::string networkId; diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_info_manager.h similarity index 97% rename from services/distributedhardwarefwkserviceimpl/src/versionmanager/version_info_manager.cpp rename to services/distributedhardwarefwkserviceimpl/include/versionmanager/version_info_manager.h index 8cbb4b40..ee09ea2c 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_info_manager.h @@ -44,5 +44,7 @@ class CapabilityInfoManager : public std::enable_shared_from_this { public: +private: + DHVersion dhVersion_; }; } \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp b/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp index 1f318981..88e25b8a 100644 --- a/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp @@ -15,6 +15,8 @@ #include "impl_utils.h" +#include "dh_utils_tool.h" + namespace OHOS { namespace DistributedHardware { void DHVersion::FromJsonString(const std::string &jsonStr) @@ -24,11 +26,47 @@ void DHVersion::FromJsonString(const std::string &jsonStr) return DH_FWK_SUCCESS; } +std::string DHVersion::ToJsonString() +{ + nlohmann::json jsonObj; + ToJson(jsonObj, *this); + return jsonObj.dump(); +} + void ToJson(nlohmann::json &jsonObject, const DHVersion &dhVersion) { - jsonObject[UUID] = dhVersion.uuid; - jsonObject[DH_VERSION] = dhVersion.dhVersion; - + jsonObject[DEV_ID] = GetDeviceIdByUUID(dhVersion.uuid); + jsonObject[DH_VER] = dhVersion.dhVersion; + + nlohmann::json compVers; + for(const auto &compVersion : dhVersion.compVersions) { + nlohmann::json compVer; + compVer[NAME] = compVersion.second.name; + compVer[TYPE] = compVersion.second.dhType; + compVer[HANDLER] = compVersion.second.handlerVersion; + compVer[SOURCE_VER] = compVersion.second.sourceVersion; + compVer[SINK_VER] = compVersion.second.sinkVersion; + compVers.push_back(compVer); + } + + jsonObject[COMP_VER] = compVers; +} + +void FromJson(const nlohmann::json &jsonObject, DHVersion &dhVersion) +{ + if (jsonObject.find(DEV_ID) != jsonObject.end()) { + dhVersion.deviceId = jsonObject.at(DEV_ID).get(); + } + + if (jsonObject.find(DH_VER) != jsonObject.end()) { + dhVersion.dhVersion = jsonObject.at(DH_VER).get(); + } + + nlohmann::json compVers; + if (jsonObject.find(COMP_VER) != jsonObject.end()) { + = jsonObject.at(COMP_VER); + } } + } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index c965b413..3161edd0 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -35,9 +35,11 @@ int32_t VersionManager::Init() DHLOGE("GetLocalDHVersion fail"); return ret; } - dhVersion.dhVersion = DH_LOCAL_VERSION; - ShowLocalVersion(dhVersion); + dhVersion.dhVersion = GetLocalDeviceVersion(); std::string strUUID = DHContext::GetInstance().GetDeviceInfo().uuid; + dhVersion.uuid = strUUID; + dhVersion.deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; + ShowLocalVersion(dhVersion); AddDHVersion(strUUID, dhVersion); return DH_FWK_SUCCESS; } -- Gitee From 7463471b63fd5c2673d2793cd43ee164ba506e8e Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Tue, 5 Jul 2022 19:24:57 +0800 Subject: [PATCH 03/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- common/utils/include/constants.h | 3 +- .../BUILD.gn | 2 + .../componentloader/component_loader.h | 2 +- .../version_info_event.h} | 56 ++++++----- .../resourcemanager/version_info_manager.h | 69 +++++++++++++ .../include/utils/impl_utils.h | 10 +- .../include/versionmanager/version_manager.h | 2 +- .../src/distributed_hardware_manager.cpp | 5 + .../resourcemanager/version_info_manager.cpp | 99 +++++++++++++++++++ .../src/utils/impl_utils.cpp | 17 ++-- .../src/versionmanager/version_manager.cpp | 2 + 11 files changed, 225 insertions(+), 42 deletions(-) rename services/distributedhardwarefwkserviceimpl/include/{versionmanager/version_info_manager.h => resourcemanager/version_info_event.h} (44%) create mode 100644 services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h create mode 100644 services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp diff --git a/common/utils/include/constants.h b/common/utils/include/constants.h index c79cdb03..18fda5b9 100644 --- a/common/utils/include/constants.h +++ b/common/utils/include/constants.h @@ -28,6 +28,7 @@ namespace DistributedHardware { const std::string COMPONENTSLOAD_PROFILE_PATH = R"(/etc/distributed_hardware_components_cfg.json)"; const std::string APP_ID = "dtbhardware_manager_service"; const std::string GLOBAL_CAPABILITY_ID = "global_capability_info"; + const std::string GLOBAL_VERSION_ID = "global_version_info"; const std::string RESOURCE_SEPARATOR = "###"; const std::string DH_ID = "dh_id"; const std::string DEV_ID = "dev_id"; @@ -49,8 +50,6 @@ namespace DistributedHardware { const std::string DH_COMPONENT_TYPE = "dhType"; const std::string DH_COMPONENT_SINK_VER = "version"; const std::string DH_COMPONENT_DEFAULT_VERSION = "1.0"; - - } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/services/distributedhardwarefwkserviceimpl/BUILD.gn b/services/distributedhardwarefwkserviceimpl/BUILD.gn index 9d093513..4cf6f905 100644 --- a/services/distributedhardwarefwkserviceimpl/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/BUILD.gn @@ -52,6 +52,7 @@ ohos_shared_library("distributedhardwarefwksvr_impl") { "src/resourcemanager/capability_info_manager.cpp", "src/resourcemanager/capability_utils.cpp", "src/resourcemanager/db_adapter.cpp", + "src/resourcemanager/version_info_manager.cpp", "src/task/disable_task.cpp", "src/task/enable_task.cpp", "src/task/offline_task.cpp", @@ -61,6 +62,7 @@ ohos_shared_library("distributedhardwarefwksvr_impl") { "src/task/task_executor.cpp", "src/task/task_factory.cpp", "src/utils/dh_context.cpp", + "src/utils/impl_utils.cpp", "src/versionmanager/version_manager.cpp", ] diff --git a/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h b/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h index b4b52da8..b0dca951 100644 --- a/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h +++ b/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h @@ -79,7 +79,7 @@ private: std::string Readfile(const std::string &filePath); private: - DHVersion localDHVersion_ {"", "", ""}; + DHVersion localDHVersion_; std::map compHandlerMap_; std::atomic isLocalVersionInit_; }; diff --git a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_info_manager.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_event.h similarity index 44% rename from services/distributedhardwarefwkserviceimpl/include/versionmanager/version_info_manager.h rename to services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_event.h index ee09ea2c..d5db9b40 100644 --- a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_info_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_event.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,38 +13,44 @@ * limitations under the License. */ -#ifndef OHOS_DISTRIBUTED_HARDWARE_CAPABILITY_INFO_MANAGER_H -#define OHOS_DISTRIBUTED_HARDWARE_CAPABILITY_INFO_MANAGER_H +#ifndef OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_EVENT_H +#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_EVENT_H -#include -#include -#include +#include -#include "kvstore_observer.h" - -#include "capability_info.h" -#include "capability_info_event.h" -#include "capability_utils.h" -#include "db_adapter.h" +#include "distributed_hardware_log.h" #include "event.h" -#include "eventbus_handler.h" -#include "event_bus.h" #include "event_sender.h" -#include "single_instance.h" -class DBAdapter; namespace OHOS { namespace DistributedHardware { -namespace { - constexpr int32_t MANUAL_SYNC_TIMEOUT = 1; -} -class CapabilityInfoManager : public std::enable_shared_from_this, - public EventSender, - public DistributedKv::KvStoreObserver, - public EventBusHandler { +class VersionInfoEvent : public Event { + TYPEINDENT(VersionInfoEvent) + +public: + enum class EventType : uint32_t { + UNDEFINED = 0, + RECOVER = 1, + }; + public: + explicit VersionInfoEvent(EventSender &sender) : Event(sender) + { + action_ = EventType::UNDEFINED; + } + + VersionInfoEvent(EventSender &sender, EventType action) : Event(sender), action_(action) {} + + virtual ~VersionInfoEvent() {} + + EventType GetAction() const + { + return action_; + } private: - DHVersion dhVersion_; + EventType action_; }; -} \ No newline at end of file +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h new file mode 100644 index 00000000..b2ac6962 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_H +#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_H + +#include +#include +#include + +#include "kvstore_observer.h" + +#include "db_adapter.h" +#include "event.h" +#include "eventbus_handler.h" +#include "event_bus.h" +#include "event_sender.h" +#include "impl_utils.h" +#include "single_instance.h" +#include "version_info_event.h" + +class DBAdapter; +namespace OHOS { +namespace DistributedHardware { +class VersionInfoManager : public std::enable_shared_from_this, + public EventSender, + public DistributedKv::KvStoreObserver, + public EventBusHandler { + DECLARE_SINGLE_INSTANCE_BASE(VersionInfoManager); +public: + VersionInfoManager() : dbAdapterPtr_(nullptr) {} + ~VersionInfoManager() {} + + int32_t Init(); + int32_t UnInit(); + + // int32_t SyncVersionInfoFromDB(const std::string &deviceId); + + // int32_t SyncRemoteVersionInfos(); + + int32_t AddVersion(const std::vector &resInfos); + + virtual void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; + void OnEvent(VersionInfoEvent &e) override; +private: + // void HandleVersionAddChange(const std::vector &insertRecords); + // void HandleVersionUpdateChange(const std::vector &updateRecords); + // void HandleVersionDeleteChange(const std::vector &deleteRecords); + + +private: + mutable std::mutex verInfoMgrMutex_; + std::shared_ptr dbAdapterPtr_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h b/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h index 263b6e67..24276d7d 100644 --- a/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h +++ b/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h @@ -21,6 +21,7 @@ #include "nlohmann/json.hpp" #include "device_type.h" +#include "constants.h" namespace OHOS { namespace DistributedHardware { @@ -66,17 +67,12 @@ struct DHVersion { std::string dhVersion; std::unordered_map compVersions; - DHVersion(std::string uuid, std::string deviceId, std::string dhVersion) - : uuid(uuid), deviceId(deviceId), dhVersion(dhVersion) {} - - virtual int32_t FromJsonString(const std::string &jsonStr); - virtual std::string ToJsonString(); + void FromJsonString(const std::string &jsonStr); + std::string ToJsonString() const; }; void ToJson(nlohmann::json &jsonObject, const DHVersion &dhVersion); void FromJson(const nlohmann::json &jsonObject, DHVersion &dhVersion); -void ToJson(nlohmann::json &jsonObject, const CompVersion &dhVersion); -void FromJson(const nlohmann::json &jsonObject, CompVersion &dhVersion); struct TaskParam { std::string networkId; diff --git a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h index 546cf54f..0626567a 100644 --- a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h @@ -28,7 +28,7 @@ namespace OHOS { namespace DistributedHardware { const std::string DH_LOCAL_VERSION = "1.0"; -class VersionManager : public DistributedKv::KvStoreObserver { +class VersionManager { DECLARE_SINGLE_INSTANCE_BASE(VersionManager); public: diff --git a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp index ffccdaa8..0563b666 100644 --- a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp @@ -29,6 +29,7 @@ #include "task_board.h" #include "task_executor.h" #include "task_factory.h" +#include "version_info_manager.h" #include "version_manager.h" namespace OHOS { @@ -54,6 +55,8 @@ int32_t DistributedHardwareManager::Initialize() DHLOGI("start"); CapabilityInfoManager::GetInstance()->Init(); + VersionInfoManager::GetInstance().Init(); + ComponentLoader::GetInstance().Init(); LocalHardwareManager::GetInstance().Init(); @@ -78,6 +81,8 @@ int32_t DistributedHardwareManager::Release() ComponentLoader::GetInstance().UnInit(); + VersionInfoManager::GetInstance().UnInit(); + CapabilityInfoManager::GetInstance()->UnInit(); return DH_FWK_SUCCESS; diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp new file mode 100644 index 00000000..71c8c3f3 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "version_info_manager.h" + +#include "anonymous_string.h" +#include "constants.h" +#include "dh_context.h" +#include "dh_utils_tool.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "event_bus.h" +#include "task_executor.h" +#include "task_factory.h" +#include "version_info_event.h" +#include "version_manager.h" + +class DBAdapter; +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "VersionInfoManager" +IMPLEMENT_SINGLE_INSTANCE(VersionInfoManager); + +int32_t VersionInfoManager::Init() +{ + DHLOGI("VersionInfoManager instance init!"); + std::lock_guard lock(verInfoMgrMutex_); + dbAdapterPtr_ = std::make_shared(APP_ID, GLOBAL_VERSION_ID, shared_from_this()); + if (dbAdapterPtr_->Init() != DH_FWK_SUCCESS) { + DHLOGE("Init dbAdapterPtr_ failed"); + return ERR_DH_FWK_RESOURCE_INIT_DB_FAILED; + } + VersionInfoEvent versionInfoEvent(*this); + DHContext::GetInstance().GetEventBus()->AddHandler(versionInfoEvent.GetType(), *this); + DHLOGI("VersionInfoManager instance init success"); + return DH_FWK_SUCCESS; +} + +int32_t VersionInfoManager::UnInit() +{ + DHLOGI("VersionInfoManager UnInit"); + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_UNINIT_DB_FAILED; + } + dbAdapterPtr_->UnInit(); + dbAdapterPtr_.reset(); + return DH_FWK_SUCCESS; +} + +int32_t VersionInfoManager::AddVersion(const std::vector &versions) +{ + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + std::vector keys; + std::vector values; + for (auto &version : versions) { + const std::string key = version.deviceId; + DHLOGI("AddVersion, Key: %s", GetAnonyString(version.deviceId).c_str()); + keys.push_back(key); + // cstd::string value = version.ToJsonString(); + values.push_back(version.ToJsonString()); + // VersionManager::GetInstance().AddDHVersion(version.uuid, version); + } + if (dbAdapterPtr_->PutDataBatch(keys, values) != DH_FWK_SUCCESS) { + DHLOGE("Fail to storage batch to kv"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + return DH_FWK_SUCCESS; +} + +void VersionInfoManager::OnChange(const DistributedKv::ChangeNotification &changeNotification) +{ + +} + +void VersionInfoManager::OnEvent(VersionInfoEvent &e) +{ + +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp b/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp index 88e25b8a..1506d33e 100644 --- a/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp @@ -23,10 +23,9 @@ void DHVersion::FromJsonString(const std::string &jsonStr) { nlohmann::json jsonObj = nlohmann::json::parse(jsonStr); FromJson(jsonObj, *this); - return DH_FWK_SUCCESS; } -std::string DHVersion::ToJsonString() +std::string DHVersion::ToJsonString() const { nlohmann::json jsonObj; ToJson(jsonObj, *this); @@ -35,7 +34,7 @@ std::string DHVersion::ToJsonString() void ToJson(nlohmann::json &jsonObject, const DHVersion &dhVersion) { - jsonObject[DEV_ID] = GetDeviceIdByUUID(dhVersion.uuid); + jsonObject[DEV_ID] = dhVersion.deviceId; jsonObject[DH_VER] = dhVersion.dhVersion; nlohmann::json compVers; @@ -62,11 +61,17 @@ void FromJson(const nlohmann::json &jsonObject, DHVersion &dhVersion) dhVersion.dhVersion = jsonObject.at(DH_VER).get(); } - nlohmann::json compVers; if (jsonObject.find(COMP_VER) != jsonObject.end()) { - = jsonObject.at(COMP_VER); + for (auto compVerObj : jsonObject.at(COMP_VER)) { + CompVersion compVer; + compVer.name = compVerObj.at(NAME).get(); + compVer.dhType = compVerObj.at(TYPE).get(); + compVer.handlerVersion = compVerObj.at(HANDLER).get(); + compVer.sourceVersion = compVerObj.at(SOURCE_VER).get(); + compVer.sinkVersion = compVerObj.at(SINK_VER).get(); + dhVersion.compVersions.insert(std::pair(compVer.dhType, compVer)); + } } } - } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index 3161edd0..640a77f0 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -19,6 +19,7 @@ #include "componentloader/component_loader.h" #include "dh_context.h" #include "distributed_hardware_log.h" +#include "version_info_manager.h" namespace OHOS { namespace DistributedHardware { @@ -64,6 +65,7 @@ int32_t VersionManager::AddDHVersion(const std::string &uuid, const DHVersion &d DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); std::lock_guard lock(versionMutex_); dhVersions_.insert(std::pair(uuid, dhVersion)); + VersionInfoManager::GetInstance().AddVersion(std::vector { dhVersion }); return DH_FWK_SUCCESS; } -- Gitee From 4268c2b79d1412e4ae12f9470ba94ab6a291d135 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Wed, 6 Jul 2022 09:02:50 +0800 Subject: [PATCH 04/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../resourcemanager/version_info_manager.h | 10 +- .../include/versionmanager/version_manager.h | 2 +- .../componentmanager/component_manager.cpp | 13 +- .../src/resourcemanager/db_adapter.cpp | 11 +- .../resourcemanager/version_info_manager.cpp | 184 ++++++++++++++++-- .../src/versionmanager/version_manager.cpp | 25 ++- 6 files changed, 214 insertions(+), 31 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h index b2ac6962..517b3f69 100644 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h @@ -47,17 +47,17 @@ public: int32_t UnInit(); // int32_t SyncVersionInfoFromDB(const std::string &deviceId); - + int32_t VersionInfoManager::GetVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion); // int32_t SyncRemoteVersionInfos(); - int32_t AddVersion(const std::vector &resInfos); + int32_t AddVersion(const DHVersion &version); virtual void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; void OnEvent(VersionInfoEvent &e) override; private: - // void HandleVersionAddChange(const std::vector &insertRecords); - // void HandleVersionUpdateChange(const std::vector &updateRecords); - // void HandleVersionDeleteChange(const std::vector &deleteRecords); + void HandleVersionAddChange(const std::vector &insertRecords); + void HandleVersionUpdateChange(const std::vector &updateRecords); + void HandleVersionDeleteChange(const std::vector &deleteRecords); private: diff --git a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h index 0626567a..27580f08 100644 --- a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h @@ -36,7 +36,7 @@ public: ~VersionManager() {} int32_t Init(); void UnInit(); - int32_t AddDHVersion(const std::string &uuid, const DHVersion &dhVersion); + int32_t AddDHVersionCache(const std::string &uuid, const DHVersion &dhVersion); int32_t RemoveDHVersion(const std::string &uuid); int32_t GetDHVersion(const std::string &uuid, DHVersion &dhVersion); int32_t GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion); diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index 52f57055..ca43fd2a 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -339,7 +339,16 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std } param.attrs = capability->GetDHAttrs(); - param.version = GetSinkVersionFromRpc(networkId, uuid, dhType); + + param.version = ""; + std::string version(""); + + CompVersion compversion; + int32_t ret = VersionManager::GetInstance().GetCompVersion(uuid, dhType, compversion); + if (ret == DH_FWK_SUCCESS) { + param.version = + } + GetSinkVersion(networkId, uuid, dhType); if (param.version.empty()) { DHLOGI("Get Sink Version failed, uuid = %s, dhId = %s", GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str()); @@ -352,7 +361,7 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std return DH_FWK_SUCCESS; } -std::string ComponentManager::GetSinkVersionFromRpc(const std::string &networkId, const std::string &uuid, DHType dhType) +std::string ComponentManager::GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType) { DHLOGI("networkId = %s ", GetAnonyString(networkId).c_str()); auto sinkVersion = GetVersionFromCache(uuid, dhType); diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp index 092d2dab..719eac8f 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp @@ -263,8 +263,15 @@ int32_t DBAdapter::ManualSync(const std::string &networkId) void DBAdapter::SyncDBForRecover() { DHLOGI("Sync store id: %s after db recover", storeId_.storeId.c_str()); - CapabilityInfoEvent recoverEvent(*this, CapabilityInfoEvent::EventType::RECOVER); - DHContext::GetInstance().GetEventBus()->PostEvent(recoverEvent); + if (storeId_.storeId == GLOBAL_CAPABILITY_ID) { + CapabilityInfoEvent recoverEvent(*this, CapabilityInfoEvent::EventType::RECOVER); + DHContext::GetInstance().GetEventBus()->PostEvent(recoverEvent); + } + + if (storeId_.storeId == GLOBAL_VERSION_ID) { + VersionInfoEvent recoverEvent(*this, VersionInfoEvent::EventType::RECOVER); + DHContext::GetInstance().GetEventBus()->PostEvent(recoverEvent); + } } int32_t DBAdapter::RegisterChangeListener() diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp index 71c8c3f3..cd718f48 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp @@ -62,38 +62,194 @@ int32_t VersionInfoManager::UnInit() return DH_FWK_SUCCESS; } -int32_t VersionInfoManager::AddVersion(const std::vector &versions) +int32_t VersionInfoManager::AddVersion(const DHVersion &version) { std::lock_guard lock(verInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + + if (GetDHVersionFromDB(version.deviceId, version) == DH_FWK_SUCCESS) { + DHLOGI("dhversion already stored, Key: %s", GetAnonyString(version.deviceId).c_str()); + return DH_FWK_SUCCESS; } - std::vector keys; - std::vector values; - for (auto &version : versions) { - const std::string key = version.deviceId; - DHLOGI("AddVersion, Key: %s", GetAnonyString(version.deviceId).c_str()); - keys.push_back(key); - // cstd::string value = version.ToJsonString(); - values.push_back(version.ToJsonString()); - // VersionManager::GetInstance().AddDHVersion(version.uuid, version); - } - if (dbAdapterPtr_->PutDataBatch(keys, values) != DH_FWK_SUCCESS) { - DHLOGE("Fail to storage batch to kv"); + + std::string key = version.deviceId; + std::string value = version.ToJsonString(); + DHLOGI("AddVersion, Key: %s", GetAnonyString(version.deviceId).c_str()); + if (dbAdapterPtr_->PutData(key, value) != DH_FWK_SUCCESS) { + DHLOGE("Fail to storage to kv"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } return DH_FWK_SUCCESS; } -void VersionInfoManager::OnChange(const DistributedKv::ChangeNotification &changeNotification) + +int32_t VersionInfoManager::GetVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion) { + DHLOGI("Sync DeviceInfo from DB, deviceId: %s", GetAnonyString(deviceId).c_str()); + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + std::string data(""); + if (dbAdapterPtr_->GetDataByKey(deviceId, data) != DH_FWK_SUCCESS) { + DHLOGE("Query data from DB by deviceId failed, id: %s", GetAnonyString(deviceId).c_str()); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + + dhVersion.FromJsonString(data); + return DH_FWK_SUCCESS; +} + +int32_t VersionInfoManager::SyncRemoteVersionInfos() +{ + DHLOGI("Sync full remote device info from DB"); + std::lock_guard lock(capInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + std::vector dataVector; + if (dbAdapterPtr_->GetDataByKeyPrefix("", dataVector) != DH_FWK_SUCCESS) { + DHLOGE("Query all data from DB failed"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + for (const auto &data : dataVector) { + DHVersion dhVersion; + dhVersion.FromJsonString(data); + const std::string &deviceId = dhVersion.deviceId; + const std::string &localDeviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; + if (deviceId.compare(localDeviceId) == 0) { + DHLOGE("local device info not need sync from db"); + continue; + } + // if (!DHContext::GetInstance().IsDeviceOnline(deviceId)) { + // DHLOGE("offline device, no need sync to memory, deviceId : %s ", + // GetAnonyString(deviceId).c_str()); + // continue; + // } + // globalCapInfoMap_[capabilityInfo->GetKey()] = capabilityInfo; + VersionManager::GetInstance().AddDHVersion(dhVersion.uuid, dhVersion); + } + return DH_FWK_SUCCESS; +} + +void VersionInfoManager::OnChange(const DistributedKv::ChangeNotification &changeNotification) +{ + DHLOGI("VersionInfoManager: DB data OnChange"); + if (!changeNotification.GetInsertEntries().empty()) { + DHLOGI("Handle capability data add change"); + HandleVersionAddChange(changeNotification.GetInsertEntries()); + } + if (!changeNotification.GetUpdateEntries().empty()) { + DHLOGI("Handle capability data update change"); + HandleVersionUpdateChange(changeNotification.GetUpdateEntries()); + } + if (!changeNotification.GetDeleteEntries().empty()) { + DHLOGI("Handle capability data delete change"); + HandleVersionDeleteChange(changeNotification.GetDeleteEntries()); + } } void VersionInfoManager::OnEvent(VersionInfoEvent &e) { + switch (ev.GetAction()) { + case VersionInfoEvent::EventType::RECOVER: + SyncRemoteVersionInfos(); + break; + default: + DHLOGE("Event is undefined, type is %d", ev.GetAction()); + break; + } +} +void VersionInfoManager::HandleVersionAddChange(const std::vector &insertRecords) +{ + std::lock_guard lock(capInfoMgrMutex_); + for (const auto &item : insertRecords) { + const std::string value = item.value.ToString(); + std::shared_ptr capPtr; + if (VersionUtils::GetVersionByValue(value, capPtr) != DH_FWK_SUCCESS) { + DHLOGE("Get capability by value failed"); + continue; + } + const auto keyString = capPtr->GetKey(); + DHLOGI("Add capability key: %s", capPtr->GetAnonymousKey().c_str()); + globalCapInfoMap_[keyString] = capPtr; + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(capPtr->GetDeviceId()); + if (uuid.empty()) { + DHLOGI("Find uuid failed and never enable"); + continue; + } + std::string networkId = DHContext::GetInstance().GetNetworkIdByUUID(uuid); + if (networkId.empty()) { + DHLOGI("Find network failed and never enable, uuid: %s", GetAnonyString(uuid).c_str()); + continue; + } + TaskParam taskParam = { + .networkId = networkId, + .uuid = uuid, + .dhId = capPtr->GetDHId(), + .dhType = capPtr->GetDHType() + }; + auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr); + TaskExecutor::GetInstance().PushTask(task); + } +} + +void VersionInfoManager::HandleVersionUpdateChange(const std::vector &updateRecords) +{ + std::lock_guard lock(capInfoMgrMutex_); + for (const auto &item : updateRecords) { + const std::string value = item.value.ToString(); + std::shared_ptr capPtr; + if (VersionUtils::GetVersionByValue(value, capPtr) != DH_FWK_SUCCESS) { + DHLOGE("Get capability by value failed"); + continue; + } + const auto keyString = capPtr->GetKey(); + DHLOGI("Update capability key: %s", capPtr->GetAnonymousKey().c_str()); + globalCapInfoMap_[keyString] = capPtr; + } } + +void VersionInfoManager::HandleVersionDeleteChange(const std::vector &deleteRecords) +{ + std::lock_guard lock(capInfoMgrMutex_); + for (const auto &item : deleteRecords) { + const std::string value = item.value.ToString(); + std::shared_ptr capPtr; + if (VersionUtils::GetVersionByValue(value, capPtr) != DH_FWK_SUCCESS) { + DHLOGE("Get capability by value failed"); + continue; + } + const auto keyString = capPtr->GetKey(); + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(capPtr->GetDeviceId()); + if (uuid.empty()) { + DHLOGI("Find uuid failed and never disable"); + continue; + } + std::string networkId = DHContext::GetInstance().GetNetworkIdByUUID(uuid); + if (networkId.empty()) { + DHLOGI("Find network failed and never disable, uuid: %s", GetAnonyString(uuid).c_str()); + continue; + } + TaskParam taskParam = { + .networkId = networkId, + .uuid = uuid, + .dhId = capPtr->GetDHId(), + .dhType = capPtr->GetDHType() + }; + auto task = TaskFactory::GetInstance().CreateTask(TaskType::DISABLE, taskParam, nullptr); + TaskExecutor::GetInstance().PushTask(task); + DHLOGI("Delete capability key: %s", capPtr->GetAnonymousKey().c_str()); + globalCapInfoMap_.erase(keyString); + } +} + } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index 640a77f0..5955175c 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -41,7 +41,8 @@ int32_t VersionManager::Init() dhVersion.uuid = strUUID; dhVersion.deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; ShowLocalVersion(dhVersion); - AddDHVersion(strUUID, dhVersion); + AddDHVersionCache(strUUID, dhVersion); + VersionInfoManager::GetInstance().AddVersion(dhVersion); return DH_FWK_SUCCESS; } @@ -60,12 +61,11 @@ void VersionManager::ShowLocalVersion(const DHVersion &dhVersion) const } } -int32_t VersionManager::AddDHVersion(const std::string &uuid, const DHVersion &dhVersion) +int32_t VersionManager::AddDHVersionCache(const std::string &uuid, const DHVersion &dhVersion) { DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); std::lock_guard lock(versionMutex_); dhVersions_.insert(std::pair(uuid, dhVersion)); - VersionInfoManager::GetInstance().AddVersion(std::vector { dhVersion }); return DH_FWK_SUCCESS; } @@ -87,15 +87,26 @@ int32_t VersionManager::GetDHVersion(const std::string &uuid, DHVersion &dhVersi DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); std::lock_guard lock(versionMutex_); std::unordered_map::iterator iter = dhVersions_.find(uuid); - if (iter == dhVersions_.end()) { - DHLOGE("there is no uuid: %s, get version fail", GetAnonyString(uuid).c_str()); - return ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST; - } else { + if (iter != dhVersions_.end()) { dhVersion = dhVersions_[uuid]; return DH_FWK_SUCCESS; } + DHLOGE("there is no uuid: %s in cache, get version fail", GetAnonyString(uuid).c_str()); + + int32_t ret = GetDHVersionFromDB(uuid, dhVersion); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("there is no uuid: %s in cache, get version fail", GetAnonyString(uuid).c_str()); + } + return ret; } +int32_t VersionManager::GetDHVersionFromDB(const std::string &uuid, DHVersion &dhVersion) +{ + DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); + return VersionInfoManager::GetInstance().GetDHVersionFromDB(GetDeviceIdByUUID(uuid) ,dhVersion); +} + + int32_t VersionManager::GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion) { DHVersion dhVersion; -- Gitee From 38e9cdd17e36dfc04a1225af2a9eca888d17d451 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Wed, 6 Jul 2022 17:39:10 +0800 Subject: [PATCH 05/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../resourcemanager/version_info_manager.h | 10 +- .../componentmanager/component_manager.cpp | 9 ++ .../src/distributed_hardware_manager.cpp | 2 + .../resourcemanager/version_info_manager.cpp | 147 +++++++++--------- .../src/task/online_task.cpp | 12 +- .../src/versionmanager/version_manager.cpp | 7 +- 6 files changed, 101 insertions(+), 86 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h index 517b3f69..4c4a78bc 100644 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h @@ -46,20 +46,22 @@ public: int32_t Init(); int32_t UnInit(); - // int32_t SyncVersionInfoFromDB(const std::string &deviceId); - int32_t VersionInfoManager::GetVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion); - // int32_t SyncRemoteVersionInfos(); + int32_t SyncVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion); + int32_t SyncRemoteVersionInfos(); + void CreateManualSyncCount(const std::string &deviceId); + void VersionInfoManager::RemoveManualSyncCount(const std::string &deviceId) + int32_t VersionInfoManager::ManualSync(const std::string &networkId) int32_t AddVersion(const DHVersion &version); virtual void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; void OnEvent(VersionInfoEvent &e) override; + private: void HandleVersionAddChange(const std::vector &insertRecords); void HandleVersionUpdateChange(const std::vector &updateRecords); void HandleVersionDeleteChange(const std::vector &deleteRecords); - private: mutable std::mutex verInfoMgrMutex_; std::shared_ptr dbAdapterPtr_; diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index ca43fd2a..35a14487 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -345,6 +345,15 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std CompVersion compversion; int32_t ret = VersionManager::GetInstance().GetCompVersion(uuid, dhType, compversion); + + // for (auto iter = compVersions.cbegin(); iter != compVersions.cend(); ++iter) { + // versionMap.emplace(iter->first, iter->second.sinkVersion); + // }+ + nlohmann::json json; + json[DH_COMPONENT_TYPE] = compversion.dhType; + json[DH_COMPONENT_SINK_VER] = compversion.sinkVersion; + version = json.dump(); + if (ret == DH_FWK_SUCCESS) { param.version = } diff --git a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp index 0563b666..c13d2192 100644 --- a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp @@ -118,6 +118,7 @@ int32_t DistributedHardwareManager::SendOnLineEvent(const std::string &networkId TaskExecutor::GetInstance().PushTask(task); DHContext::GetInstance().AddOnlineDevice(uuid, networkId); CapabilityInfoManager::GetInstance()->CreateManualSyncCount(GetDeviceIdByUUID(uuid)); + VersionInfoManager::GetInstance()->CreateManualSyncCount(GetDeviceIdByUUID(uuid)); return DH_FWK_SUCCESS; } @@ -159,6 +160,7 @@ int32_t DistributedHardwareManager::SendOffLineEvent(const std::string &networkI DHContext::GetInstance().RemoveOnlineDevice(realUUID); CapabilityInfoManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); + VersionInfoManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); HiSysEventWriteCompOfflineMsg(DHFWK_DEV_OFFLINE, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, GetAnonyString(networkId), "dhfwk device offline event."); diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp index cd718f48..8639dda1 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp @@ -70,8 +70,10 @@ int32_t VersionInfoManager::AddVersion(const DHVersion &version) return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; } - if (GetDHVersionFromDB(version.deviceId, version) == DH_FWK_SUCCESS) { - DHLOGI("dhversion already stored, Key: %s", GetAnonyString(version.deviceId).c_str()); + std::string data(""); + dbAdapterPtr_->GetDataByKey(deviceId, data); + if (data == version.ToJsonString()) { + DHLOGI("dhversion already stored, Key: %s", GetAnonyString(version.deviceId).c_str()); return DH_FWK_SUCCESS; } @@ -85,10 +87,9 @@ int32_t VersionInfoManager::AddVersion(const DHVersion &version) return DH_FWK_SUCCESS; } - -int32_t VersionInfoManager::GetVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion) +int32_t VersionInfoManager::SyncVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion) { - DHLOGI("Sync DeviceInfo from DB, deviceId: %s", GetAnonyString(deviceId).c_str()); + DHLOGI("Sync VersionInfo from DB, deviceId: %s", GetAnonyString(deviceId).c_str()); std::lock_guard lock(verInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); @@ -106,8 +107,8 @@ int32_t VersionInfoManager::GetVersionInfoFromDB(const std::string &deviceId, DH int32_t VersionInfoManager::SyncRemoteVersionInfos() { - DHLOGI("Sync full remote device info from DB"); - std::lock_guard lock(capInfoMgrMutex_); + DHLOGI("Sync full remote version info from DB"); + std::lock_guard lock(verInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; @@ -126,13 +127,49 @@ int32_t VersionInfoManager::SyncRemoteVersionInfos() DHLOGE("local device info not need sync from db"); continue; } - // if (!DHContext::GetInstance().IsDeviceOnline(deviceId)) { - // DHLOGE("offline device, no need sync to memory, deviceId : %s ", - // GetAnonyString(deviceId).c_str()); - // continue; - // } - // globalCapInfoMap_[capabilityInfo->GetKey()] = capabilityInfo; - VersionManager::GetInstance().AddDHVersion(dhVersion.uuid, dhVersion); + if (!DHContext::GetInstance().IsDeviceOnline(deviceId)) { + DHLOGE("offline device, no need sync to memory, deviceId : %s ", + GetAnonyString(deviceId).c_str()); + continue; + } + + VersionManager::GetInstance().AddDHVersionCache(dhVersion.uuid, dhVersion); + } + return DH_FWK_SUCCESS; +} + + +void VersionInfoManager::CreateManualSyncCount(const std::string &deviceId) +{ + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return; + } + dbAdapterPtr_->CreateManualSyncCount(deviceId); +} + +void VersionInfoManager::RemoveManualSyncCount(const std::string &deviceId) +{ + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return; + } + dbAdapterPtr_->RemoveManualSyncCount(deviceId); +} + +int32_t VersionInfoManager::ManualSync(const std::string &networkId) +{ + DHLOGI("ManualSync start, networkId: %s", GetAnonyString(networkId).c_str()); + std::unique_lock lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + if (dbAdapterPtr_->ManualSync(networkId) != DH_FWK_SUCCESS) { + DHLOGE("ManualSync failed"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } return DH_FWK_SUCCESS; } @@ -169,85 +206,41 @@ void VersionInfoManager::OnEvent(VersionInfoEvent &e) void VersionInfoManager::HandleVersionAddChange(const std::vector &insertRecords) { - std::lock_guard lock(capInfoMgrMutex_); + std::lock_guard lock(verInfoMgrMutex_); for (const auto &item : insertRecords) { const std::string value = item.value.ToString(); - std::shared_ptr capPtr; - if (VersionUtils::GetVersionByValue(value, capPtr) != DH_FWK_SUCCESS) { - DHLOGE("Get capability by value failed"); - continue; - } - const auto keyString = capPtr->GetKey(); - DHLOGI("Add capability key: %s", capPtr->GetAnonymousKey().c_str()); - globalCapInfoMap_[keyString] = capPtr; - std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(capPtr->GetDeviceId()); - if (uuid.empty()) { - DHLOGI("Find uuid failed and never enable"); - continue; - } - std::string networkId = DHContext::GetInstance().GetNetworkIdByUUID(uuid); - if (networkId.empty()) { - DHLOGI("Find network failed and never enable, uuid: %s", GetAnonyString(uuid).c_str()); - continue; - } - TaskParam taskParam = { - .networkId = networkId, - .uuid = uuid, - .dhId = capPtr->GetDHId(), - .dhType = capPtr->GetDHType() - }; - auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr); - TaskExecutor::GetInstance().PushTask(task); + DHVersion dhVersion; + dhVersion.FromJsonString(value); + const std::string &deviceId = dhVersion.deviceId; + DHLOGI("Add Version ,key: %s", GetAnonyString(deviceId).c_str()); + VersionManager::GetInstance().AddDHVersionCache(dhVersion.uuid, dhVersion); } } void VersionInfoManager::HandleVersionUpdateChange(const std::vector &updateRecords) { - std::lock_guard lock(capInfoMgrMutex_); + std::lock_guard lock(verInfoMgrMutex_); for (const auto &item : updateRecords) { const std::string value = item.value.ToString(); - std::shared_ptr capPtr; - if (VersionUtils::GetVersionByValue(value, capPtr) != DH_FWK_SUCCESS) { - DHLOGE("Get capability by value failed"); - continue; - } - const auto keyString = capPtr->GetKey(); - DHLOGI("Update capability key: %s", capPtr->GetAnonymousKey().c_str()); - globalCapInfoMap_[keyString] = capPtr; + DHVersion dhVersion; + dhVersion.FromJsonString(value); + const std::string &deviceId = dhVersion.deviceId; + DHLOGI("Update Version key: %s", GetAnonyString(deviceId).c_str()); + VersionManager::GetInstance().AddDHVersionCache(dhVersion.uuid, dhVersion); } } void VersionInfoManager::HandleVersionDeleteChange(const std::vector &deleteRecords) { - std::lock_guard lock(capInfoMgrMutex_); + std::lock_guard lock(verInfoMgrMutex_); for (const auto &item : deleteRecords) { const std::string value = item.value.ToString(); - std::shared_ptr capPtr; - if (VersionUtils::GetVersionByValue(value, capPtr) != DH_FWK_SUCCESS) { - DHLOGE("Get capability by value failed"); - continue; - } - const auto keyString = capPtr->GetKey(); - std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(capPtr->GetDeviceId()); - if (uuid.empty()) { - DHLOGI("Find uuid failed and never disable"); - continue; - } - std::string networkId = DHContext::GetInstance().GetNetworkIdByUUID(uuid); - if (networkId.empty()) { - DHLOGI("Find network failed and never disable, uuid: %s", GetAnonyString(uuid).c_str()); - continue; - } - TaskParam taskParam = { - .networkId = networkId, - .uuid = uuid, - .dhId = capPtr->GetDHId(), - .dhType = capPtr->GetDHType() - }; - auto task = TaskFactory::GetInstance().CreateTask(TaskType::DISABLE, taskParam, nullptr); - TaskExecutor::GetInstance().PushTask(task); - DHLOGI("Delete capability key: %s", capPtr->GetAnonymousKey().c_str()); - globalCapInfoMap_.erase(keyString); + DHVersion dhVersion; + dhVersion.FromJsonString(value); + const std::string &deviceId = dhVersion.deviceId; + + DHLOGI("Delete Version, key: %s", GetAnonyString(deviceId).c_str()); + VersionManager::GetInstance().RemoveDHVersion(dhVersion.uuid); } } diff --git a/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp b/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp index a576cabe..4ca12bf0 100644 --- a/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp @@ -73,10 +73,20 @@ void OnLineTask::DoSyncInfo() if (ret != DH_FWK_SUCCESS) { DHLOGW("ManualSync failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } + + ret = VersionInfoManager::GetInstance()->ManualSync(GetNetworkId()); + if (ret != DH_FWK_SUCCESS) { + DHLOGW("ManualSync version failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); + } + ret = CapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(GetDeviceIdByUUID(GetUUID())); if (ret != DH_FWK_SUCCESS) { DHLOGE("SyncDeviceInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); - return; + } + + ret = VersionInfoManager::GetInstance()->SyncDeviceInfoFromDB(GetDeviceIdByUUID(GetUUID())); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("SyncVersionInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } } diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index 5955175c..59fb4bb5 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -65,7 +65,7 @@ int32_t VersionManager::AddDHVersionCache(const std::string &uuid, const DHVersi { DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); std::lock_guard lock(versionMutex_); - dhVersions_.insert(std::pair(uuid, dhVersion)); + dhVersions_[uuid] = dhVersion; return DH_FWK_SUCCESS; } @@ -95,7 +95,7 @@ int32_t VersionManager::GetDHVersion(const std::string &uuid, DHVersion &dhVersi int32_t ret = GetDHVersionFromDB(uuid, dhVersion); if (ret != DH_FWK_SUCCESS) { - DHLOGE("there is no uuid: %s in cache, get version fail", GetAnonyString(uuid).c_str()); + DHLOGE("there is no uuid: %s in db, get version fail", GetAnonyString(uuid).c_str()); } return ret; } @@ -103,10 +103,9 @@ int32_t VersionManager::GetDHVersion(const std::string &uuid, DHVersion &dhVersi int32_t VersionManager::GetDHVersionFromDB(const std::string &uuid, DHVersion &dhVersion) { DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); - return VersionInfoManager::GetInstance().GetDHVersionFromDB(GetDeviceIdByUUID(uuid) ,dhVersion); + return VersionInfoManager::GetInstance().SyncVersionInfoFromDB(GetDeviceIdByUUID(uuid) ,dhVersion); } - int32_t VersionManager::GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion) { DHVersion dhVersion; -- Gitee From 5f5a9c70254b0677b82f099ccae794b99dde3700 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Wed, 6 Jul 2022 19:28:56 +0800 Subject: [PATCH 06/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../componentmanager/component_manager.h | 1 + .../resourcemanager/version_info_manager.h | 4 +- .../include/versionmanager/version_manager.h | 2 +- .../componentmanager/component_manager.cpp | 41 +++++++++++-------- .../resourcemanager/version_info_manager.cpp | 2 +- .../src/versionmanager/version_manager.cpp | 4 +- 6 files changed, 32 insertions(+), 22 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h b/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h index 8a592df8..ce1a9dbd 100644 --- a/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h @@ -67,6 +67,7 @@ private: bool WaitForResult(const Action &action, ActionResult result); int32_t GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, DHType dhType, EnableParam ¶m); + std::string GetSinkVersionFromVarMgr(const std::string &uuid, DHType dhType); std::string GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType); std::string GetVersionFromCache(const std::string &uuid, DHType dhType); int32_t UpdateVersionCache(const std::string &networkId, const std::string &uuid); diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h index 4c4a78bc..67526f11 100644 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h @@ -49,8 +49,8 @@ public: int32_t SyncVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion); int32_t SyncRemoteVersionInfos(); void CreateManualSyncCount(const std::string &deviceId); - void VersionInfoManager::RemoveManualSyncCount(const std::string &deviceId) - int32_t VersionInfoManager::ManualSync(const std::string &networkId) + void RemoveManualSyncCount(const std::string &deviceId); + int32_t ManualSync(const std::string &networkId); int32_t AddVersion(const DHVersion &version); diff --git a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h index 27580f08..0626567a 100644 --- a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h @@ -36,7 +36,7 @@ public: ~VersionManager() {} int32_t Init(); void UnInit(); - int32_t AddDHVersionCache(const std::string &uuid, const DHVersion &dhVersion); + int32_t AddDHVersion(const std::string &uuid, const DHVersion &dhVersion); int32_t RemoveDHVersion(const std::string &uuid); int32_t GetDHVersion(const std::string &uuid, DHVersion &dhVersion); int32_t GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion); diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index 35a14487..d6c7ca75 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -327,6 +327,8 @@ DHType ComponentManager::GetDHType(const std::string &uuid, const std::string &d return DHType::UNKNOWN; } + + int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, DHType dhType, EnableParam ¶m) { @@ -340,22 +342,13 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std param.attrs = capability->GetDHAttrs(); - param.version = ""; - std::string version(""); - - CompVersion compversion; - int32_t ret = VersionManager::GetInstance().GetCompVersion(uuid, dhType, compversion); - - // for (auto iter = compVersions.cbegin(); iter != compVersions.cend(); ++iter) { - // versionMap.emplace(iter->first, iter->second.sinkVersion); - // }+ - nlohmann::json json; - json[DH_COMPONENT_TYPE] = compversion.dhType; - json[DH_COMPONENT_SINK_VER] = compversion.sinkVersion; - version = json.dump(); - - if (ret == DH_FWK_SUCCESS) { - param.version = + // param.version = ""; + // std::string version(""); + param.version = GetSinkVersionFromVarMgr(uuid, dhType); + if (param.version.empty()) { + DHLOGI("Get Sink Version failed, uuid = %s, dhId = %s", GetAnonyString(uuid).c_str(), + GetAnonyString(dhId).c_str()); + return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED; } GetSinkVersion(networkId, uuid, dhType); if (param.version.empty()) { @@ -370,6 +363,22 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std return DH_FWK_SUCCESS; } +std::string ComponentManager::GetSinkVersionFromVarMgr(const std::string &uuid, DHType dhType) +{ + CompVersion compversion; + auto ret = VersionManager::GetInstance().GetCompVersion(uuid, dhType, compversion); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("GetCapability failed, uuid =%s, dhId = %s, errCode = %d", GetAnonyString(uuid).c_str(), + GetAnonyString(dhId).c_str(), ret); + return ""; + } + + nlohmann::json json; + json[DH_COMPONENT_TYPE] = compversion.dhType; + json[DH_COMPONENT_SINK_VER] = compversion.sinkVersion; + return json.dump(); +} + std::string ComponentManager::GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType) { DHLOGI("networkId = %s ", GetAnonyString(networkId).c_str()); diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp index 8639dda1..6d9cb8c3 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp @@ -71,7 +71,7 @@ int32_t VersionInfoManager::AddVersion(const DHVersion &version) } std::string data(""); - dbAdapterPtr_->GetDataByKey(deviceId, data); + dbAdapterPtr_->GetDataByKey(version.deviceId, data); if (data == version.ToJsonString()) { DHLOGI("dhversion already stored, Key: %s", GetAnonyString(version.deviceId).c_str()); return DH_FWK_SUCCESS; diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index 59fb4bb5..d58c1761 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -41,7 +41,7 @@ int32_t VersionManager::Init() dhVersion.uuid = strUUID; dhVersion.deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; ShowLocalVersion(dhVersion); - AddDHVersionCache(strUUID, dhVersion); + AddDHVersion(strUUID, dhVersion); VersionInfoManager::GetInstance().AddVersion(dhVersion); return DH_FWK_SUCCESS; } @@ -61,7 +61,7 @@ void VersionManager::ShowLocalVersion(const DHVersion &dhVersion) const } } -int32_t VersionManager::AddDHVersionCache(const std::string &uuid, const DHVersion &dhVersion) +int32_t VersionManager::AddDHVersion(const std::string &uuid, const DHVersion &dhVersion) { DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); std::lock_guard lock(versionMutex_); -- Gitee From 4cb66fb3e8859c7b62449f100561ba7f81bf8e57 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Wed, 6 Jul 2022 22:16:57 +0800 Subject: [PATCH 07/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../componentmanager/component_manager.h | 2 +- .../resourcemanager/version_info_manager.h | 12 +++-- .../include/versionmanager/version_manager.h | 1 + .../componentmanager/component_manager.cpp | 47 ++++++++----------- .../src/distributed_hardware_manager.cpp | 4 +- .../src/resourcemanager/db_adapter.cpp | 1 + .../resourcemanager/version_info_manager.cpp | 31 ++++++++++-- .../src/task/online_task.cpp | 5 +- .../src/versionmanager/version_manager.cpp | 10 +++- 9 files changed, 71 insertions(+), 42 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h b/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h index ce1a9dbd..68391247 100644 --- a/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h @@ -67,7 +67,7 @@ private: bool WaitForResult(const Action &action, ActionResult result); int32_t GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, DHType dhType, EnableParam ¶m); - std::string GetSinkVersionFromVarMgr(const std::string &uuid, DHType dhType); + std::string GetSinkVersionFromVerMgr(const std::string &uuid, const DHType dhType); std::string GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType); std::string GetVersionFromCache(const std::string &uuid, DHType dhType); int32_t UpdateVersionCache(const std::string &networkId, const std::string &uuid); diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h index 67526f11..000bc36c 100644 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h @@ -38,10 +38,13 @@ class VersionInfoManager : public std::enable_shared_from_this { - DECLARE_SINGLE_INSTANCE_BASE(VersionInfoManager); public: - VersionInfoManager() : dbAdapterPtr_(nullptr) {} - ~VersionInfoManager() {} + VersionInfoManager(const VersionInfoManager &) = delete; + VersionInfoManager &operator = (const VersionInfoManager &) = delete; + VersionInfoManager(VersionInfoManager &&) = delete; + VersionInfoManager &operator = (VersionInfoManager &&) = delete; + static std::shared_ptr GetInstance(); + virtual ~VersionInfoManager(); int32_t Init(); int32_t UnInit(); @@ -55,9 +58,10 @@ public: int32_t AddVersion(const DHVersion &version); virtual void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; - void OnEvent(VersionInfoEvent &e) override; + void OnEvent(VersionInfoEvent &ev) override; private: + VersionInfoManager(); void HandleVersionAddChange(const std::vector &insertRecords); void HandleVersionUpdateChange(const std::vector &updateRecords); void HandleVersionDeleteChange(const std::vector &deleteRecords); diff --git a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h index 0626567a..2dd6043d 100644 --- a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h @@ -40,6 +40,7 @@ public: int32_t RemoveDHVersion(const std::string &uuid); int32_t GetDHVersion(const std::string &uuid, DHVersion &dhVersion); int32_t GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion); + int32_t GetDHVersionFromDB(const std::string &uuid, DHVersion &dhVersion); std::string GetLocalDeviceVersion(); void ShowLocalVersion(const DHVersion &dhVersion) const; diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index d6c7ca75..cd1ff44d 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -327,8 +327,6 @@ DHType ComponentManager::GetDHType(const std::string &uuid, const std::string &d return DHType::UNKNOWN; } - - int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, DHType dhType, EnableParam ¶m) { @@ -341,42 +339,37 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std } param.attrs = capability->GetDHAttrs(); - - // param.version = ""; - // std::string version(""); - param.version = GetSinkVersionFromVarMgr(uuid, dhType); - if (param.version.empty()) { - DHLOGI("Get Sink Version failed, uuid = %s, dhId = %s", GetAnonyString(uuid).c_str(), - GetAnonyString(dhId).c_str()); - return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED; - } - GetSinkVersion(networkId, uuid, dhType); - if (param.version.empty()) { - DHLOGI("Get Sink Version failed, uuid = %s, dhId = %s", GetAnonyString(uuid).c_str(), - GetAnonyString(dhId).c_str()); - return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED; + param.version = GetSinkVersionFromVerMgr(uuid, dhType); + if (!param.version.empty()) { + DHLOGI("success. uuid = %s, dhId = %s, dhType = %#X, version = %s", GetAnonyString(uuid).c_str(), + GetAnonyString(dhId).c_str(), dhType, param.version.c_str()); + return DH_FWK_SUCCESS; } - DHLOGI("success. uuid =%s, dhId = %s, version = %s", GetAnonyString(uuid).c_str(), - GetAnonyString(dhId).c_str(), param.version.c_str()); + // Get sinkversion by rpc + param.version = GetSinkVersion(networkId, uuid, dhType); + if (!param.version.empty()) { + DHLOGI("success. uuid = %s, dhId = %s, dhType = %#X, version = %s", GetAnonyString(uuid).c_str(), + GetAnonyString(dhId).c_str(), dhType, param.version.c_str()); + return DH_FWK_SUCCESS; + } - return DH_FWK_SUCCESS; + DHLOGI("Get Sink Version failed, uuid = %s, dhId = %s, dhType = %#X,", GetAnonyString(uuid).c_str(), + GetAnonyString(dhId).c_str(), dhType); + return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED; + } -std::string ComponentManager::GetSinkVersionFromVarMgr(const std::string &uuid, DHType dhType) +std::string ComponentManager::GetSinkVersionFromVerMgr(const std::string &uuid, const DHType dhType) { CompVersion compversion; auto ret = VersionManager::GetInstance().GetCompVersion(uuid, dhType, compversion); if (ret != DH_FWK_SUCCESS) { - DHLOGE("GetCapability failed, uuid =%s, dhId = %s, errCode = %d", GetAnonyString(uuid).c_str(), - GetAnonyString(dhId).c_str(), ret); + DHLOGE("Get version from Version Manager failed, uuid =%s, dhType = %#X, errCode = %d", + GetAnonyString(uuid).c_str(), dhType, ret); return ""; } - - nlohmann::json json; - json[DH_COMPONENT_TYPE] = compversion.dhType; - json[DH_COMPONENT_SINK_VER] = compversion.sinkVersion; - return json.dump(); + return compversion.sinkVersion; } std::string ComponentManager::GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType) diff --git a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp index c13d2192..0cc1e922 100644 --- a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp @@ -55,7 +55,7 @@ int32_t DistributedHardwareManager::Initialize() DHLOGI("start"); CapabilityInfoManager::GetInstance()->Init(); - VersionInfoManager::GetInstance().Init(); + VersionInfoManager::GetInstance()->Init(); ComponentLoader::GetInstance().Init(); @@ -81,7 +81,7 @@ int32_t DistributedHardwareManager::Release() ComponentLoader::GetInstance().UnInit(); - VersionInfoManager::GetInstance().UnInit(); + VersionInfoManager::GetInstance()->UnInit(); CapabilityInfoManager::GetInstance()->UnInit(); diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp index 719eac8f..0a9859df 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp @@ -24,6 +24,7 @@ #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" #include "event_bus.h" +#include "version_info_event.h" namespace OHOS { namespace DistributedHardware { diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp index 6d9cb8c3..e4a15b27 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp @@ -32,13 +32,31 @@ namespace OHOS { namespace DistributedHardware { #undef DH_LOG_TAG #define DH_LOG_TAG "VersionInfoManager" -IMPLEMENT_SINGLE_INSTANCE(VersionInfoManager); + +VersionInfoManager::VersionInfoManager() : dbAdapterPtr_(nullptr) +{} + +VersionInfoManager::~VersionInfoManager() +{ + DHLOGI("VersionInfoManager Destruction!"); +} + +std::shared_ptr VersionInfoManager::GetInstance() +{ + static std::shared_ptr instance(new(std::nothrow) VersionInfoManager); + if (instance == nullptr) { + DHLOGE("instance is nullptr, because applying memory fail!"); + return nullptr; + } + return instance; +} int32_t VersionInfoManager::Init() { DHLOGI("VersionInfoManager instance init!"); std::lock_guard lock(verInfoMgrMutex_); dbAdapterPtr_ = std::make_shared(APP_ID, GLOBAL_VERSION_ID, shared_from_this()); + DHLOGI("dbAdapterPtr_ success"); if (dbAdapterPtr_->Init() != DH_FWK_SUCCESS) { DHLOGE("Init dbAdapterPtr_ failed"); return ERR_DH_FWK_RESOURCE_INIT_DB_FAILED; @@ -80,6 +98,7 @@ int32_t VersionInfoManager::AddVersion(const DHVersion &version) std::string key = version.deviceId; std::string value = version.ToJsonString(); DHLOGI("AddVersion, Key: %s", GetAnonyString(version.deviceId).c_str()); + DHLOGI("AddVersion, value: %s", value.c_str()); if (dbAdapterPtr_->PutData(key, value) != DH_FWK_SUCCESS) { DHLOGE("Fail to storage to kv"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; @@ -101,6 +120,8 @@ int32_t VersionInfoManager::SyncVersionInfoFromDB(const std::string &deviceId, D return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } + DHLOGI("Query data from DB by deviceId success, id: %s, data: %s", + GetAnonyString(deviceId).c_str(), data.c_str()); dhVersion.FromJsonString(data); return DH_FWK_SUCCESS; } @@ -133,7 +154,7 @@ int32_t VersionInfoManager::SyncRemoteVersionInfos() continue; } - VersionManager::GetInstance().AddDHVersionCache(dhVersion.uuid, dhVersion); + VersionManager::GetInstance().AddDHVersion(dhVersion.uuid, dhVersion); } return DH_FWK_SUCCESS; } @@ -192,7 +213,7 @@ void VersionInfoManager::OnChange(const DistributedKv::ChangeNotification &chang } -void VersionInfoManager::OnEvent(VersionInfoEvent &e) +void VersionInfoManager::OnEvent(VersionInfoEvent &ev) { switch (ev.GetAction()) { case VersionInfoEvent::EventType::RECOVER: @@ -213,7 +234,7 @@ void VersionInfoManager::HandleVersionAddChange(const std::vectorSyncDeviceInfoFromDB(GetDeviceIdByUUID(GetUUID())); + DHVersion dhVersion; + ret = VersionManager::GetInstance().GetDHVersionFromDB(GetUUID(), dhVersion); if (ret != DH_FWK_SUCCESS) { DHLOGE("SyncVersionInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index d58c1761..656903de 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -18,6 +18,7 @@ #include "anonymous_string.h" #include "componentloader/component_loader.h" #include "dh_context.h" +#include "dh_utils_tool.h" #include "distributed_hardware_log.h" #include "version_info_manager.h" @@ -42,7 +43,7 @@ int32_t VersionManager::Init() dhVersion.deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; ShowLocalVersion(dhVersion); AddDHVersion(strUUID, dhVersion); - VersionInfoManager::GetInstance().AddVersion(dhVersion); + VersionInfoManager::GetInstance()->AddVersion(dhVersion); return DH_FWK_SUCCESS; } @@ -103,7 +104,12 @@ int32_t VersionManager::GetDHVersion(const std::string &uuid, DHVersion &dhVersi int32_t VersionManager::GetDHVersionFromDB(const std::string &uuid, DHVersion &dhVersion) { DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); - return VersionInfoManager::GetInstance().SyncVersionInfoFromDB(GetDeviceIdByUUID(uuid) ,dhVersion); + int32_t ret = VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(GetDeviceIdByUUID(uuid), dhVersion); + if (ret != DH_FWK_SUCCESS) { + return ret; + } + dhVersions_[uuid] = dhVersion; + return DH_FWK_SUCCESS; } int32_t VersionManager::GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion) -- Gitee From 705583e16e20f4adeb02ecfa5af590af6c5ff665 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Thu, 7 Jul 2022 21:20:10 +0800 Subject: [PATCH 08/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../resourcemanager/version_info_manager.h | 6 +- .../include/versionmanager/version_manager.h | 2 +- .../componentmanager/component_manager.cpp | 9 ++- .../resourcemanager/version_info_manager.cpp | 63 ++++++++----------- .../src/task/online_task.cpp | 2 +- .../src/versionmanager/version_manager.cpp | 35 ++++++----- 6 files changed, 55 insertions(+), 62 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h index 000bc36c..1c7ee915 100644 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h @@ -49,14 +49,13 @@ public: int32_t Init(); int32_t UnInit(); - int32_t SyncVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion); + int32_t AddVersion(const DHVersion &version); + int32_t GetVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion); int32_t SyncRemoteVersionInfos(); void CreateManualSyncCount(const std::string &deviceId); void RemoveManualSyncCount(const std::string &deviceId); int32_t ManualSync(const std::string &networkId); - int32_t AddVersion(const DHVersion &version); - virtual void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; void OnEvent(VersionInfoEvent &ev) override; @@ -64,7 +63,6 @@ private: VersionInfoManager(); void HandleVersionAddChange(const std::vector &insertRecords); void HandleVersionUpdateChange(const std::vector &updateRecords); - void HandleVersionDeleteChange(const std::vector &deleteRecords); private: mutable std::mutex verInfoMgrMutex_; diff --git a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h index 2dd6043d..f056d897 100644 --- a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h @@ -40,7 +40,7 @@ public: int32_t RemoveDHVersion(const std::string &uuid); int32_t GetDHVersion(const std::string &uuid, DHVersion &dhVersion); int32_t GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion); - int32_t GetDHVersionFromDB(const std::string &uuid, DHVersion &dhVersion); + int32_t SyncDHVersionFromDB(const std::string &uuid, DHVersion &dhVersion); std::string GetLocalDeviceVersion(); void ShowLocalVersion(const DHVersion &dhVersion) const; diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index cd1ff44d..3c8ec547 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -341,16 +341,15 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std param.attrs = capability->GetDHAttrs(); param.version = GetSinkVersionFromVerMgr(uuid, dhType); if (!param.version.empty()) { - DHLOGI("success. uuid = %s, dhId = %s, dhType = %#X, version = %s", GetAnonyString(uuid).c_str(), - GetAnonyString(dhId).c_str(), dhType, param.version.c_str()); + DHLOGI("Get sink version from VerMgr success. uuid = %s, dhId = %s, dhType = %#X, version = %s", + GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), dhType, param.version.c_str()); return DH_FWK_SUCCESS; } - // Get sinkversion by rpc param.version = GetSinkVersion(networkId, uuid, dhType); if (!param.version.empty()) { - DHLOGI("success. uuid = %s, dhId = %s, dhType = %#X, version = %s", GetAnonyString(uuid).c_str(), - GetAnonyString(dhId).c_str(), dhType, param.version.c_str()); + DHLOGI("Get sink version from rpc success. uuid = %s, dhId = %s, dhType = %#X, version = %s", + GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), dhType, param.version.c_str()); return DH_FWK_SUCCESS; } diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp index e4a15b27..fe44f35e 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp @@ -56,7 +56,6 @@ int32_t VersionInfoManager::Init() DHLOGI("VersionInfoManager instance init!"); std::lock_guard lock(verInfoMgrMutex_); dbAdapterPtr_ = std::make_shared(APP_ID, GLOBAL_VERSION_ID, shared_from_this()); - DHLOGI("dbAdapterPtr_ success"); if (dbAdapterPtr_->Init() != DH_FWK_SUCCESS) { DHLOGE("Init dbAdapterPtr_ failed"); return ERR_DH_FWK_RESOURCE_INIT_DB_FAILED; @@ -90,7 +89,7 @@ int32_t VersionInfoManager::AddVersion(const DHVersion &version) std::string data(""); dbAdapterPtr_->GetDataByKey(version.deviceId, data); - if (data == version.ToJsonString()) { + if (data.compare(version.ToJsonString()) == 0) { DHLOGI("dhversion already stored, Key: %s", GetAnonyString(version.deviceId).c_str()); return DH_FWK_SUCCESS; } @@ -98,7 +97,6 @@ int32_t VersionInfoManager::AddVersion(const DHVersion &version) std::string key = version.deviceId; std::string value = version.ToJsonString(); DHLOGI("AddVersion, Key: %s", GetAnonyString(version.deviceId).c_str()); - DHLOGI("AddVersion, value: %s", value.c_str()); if (dbAdapterPtr_->PutData(key, value) != DH_FWK_SUCCESS) { DHLOGE("Fail to storage to kv"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; @@ -106,7 +104,7 @@ int32_t VersionInfoManager::AddVersion(const DHVersion &version) return DH_FWK_SUCCESS; } -int32_t VersionInfoManager::SyncVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion) +int32_t VersionInfoManager::GetVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion) { DHLOGI("Sync VersionInfo from DB, deviceId: %s", GetAnonyString(deviceId).c_str()); std::lock_guard lock(verInfoMgrMutex_); @@ -116,12 +114,11 @@ int32_t VersionInfoManager::SyncVersionInfoFromDB(const std::string &deviceId, D } std::string data(""); if (dbAdapterPtr_->GetDataByKey(deviceId, data) != DH_FWK_SUCCESS) { - DHLOGE("Query data from DB by deviceId failed, id: %s", GetAnonyString(deviceId).c_str()); + DHLOGE("Query data from DB by deviceId failed, deviceId: %s", GetAnonyString(deviceId).c_str()); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } - DHLOGI("Query data from DB by deviceId success, id: %s, data: %s", - GetAnonyString(deviceId).c_str(), data.c_str()); + DHLOGI("Query data from DB by deviceId success, deviceId: %s", GetAnonyString(deviceId).c_str()); dhVersion.FromJsonString(data); return DH_FWK_SUCCESS; } @@ -139,6 +136,7 @@ int32_t VersionInfoManager::SyncRemoteVersionInfos() DHLOGE("Query all data from DB failed"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } + for (const auto &data : dataVector) { DHVersion dhVersion; dhVersion.FromJsonString(data); @@ -153,13 +151,16 @@ int32_t VersionInfoManager::SyncRemoteVersionInfos() GetAnonyString(deviceId).c_str()); continue; } - - VersionManager::GetInstance().AddDHVersion(dhVersion.uuid, dhVersion); + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed"); + continue; + } + VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); } return DH_FWK_SUCCESS; } - void VersionInfoManager::CreateManualSyncCount(const std::string &deviceId) { std::lock_guard lock(verInfoMgrMutex_); @@ -199,18 +200,13 @@ void VersionInfoManager::OnChange(const DistributedKv::ChangeNotification &chang { DHLOGI("VersionInfoManager: DB data OnChange"); if (!changeNotification.GetInsertEntries().empty()) { - DHLOGI("Handle capability data add change"); + DHLOGI("Handle version data add change"); HandleVersionAddChange(changeNotification.GetInsertEntries()); } if (!changeNotification.GetUpdateEntries().empty()) { - DHLOGI("Handle capability data update change"); + DHLOGI("Handle version data update change"); HandleVersionUpdateChange(changeNotification.GetUpdateEntries()); } - if (!changeNotification.GetDeleteEntries().empty()) { - DHLOGI("Handle capability data delete change"); - HandleVersionDeleteChange(changeNotification.GetDeleteEntries()); - } - } void VersionInfoManager::OnEvent(VersionInfoEvent &ev) @@ -227,43 +223,38 @@ void VersionInfoManager::OnEvent(VersionInfoEvent &ev) void VersionInfoManager::HandleVersionAddChange(const std::vector &insertRecords) { - std::lock_guard lock(verInfoMgrMutex_); + DHLOGI("VersionInfoManager: Version add change"); for (const auto &item : insertRecords) { const std::string value = item.value.ToString(); DHVersion dhVersion; dhVersion.FromJsonString(value); const std::string &deviceId = dhVersion.deviceId; + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed"); + continue; + } DHLOGI("Add Version ,key: %s", GetAnonyString(deviceId).c_str()); - VersionManager::GetInstance().AddDHVersion(dhVersion.uuid, dhVersion); + VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); } } void VersionInfoManager::HandleVersionUpdateChange(const std::vector &updateRecords) { - std::lock_guard lock(verInfoMgrMutex_); + DHLOGI("VersionInfoManager: Version update change"); for (const auto &item : updateRecords) { const std::string value = item.value.ToString(); DHVersion dhVersion; dhVersion.FromJsonString(value); const std::string &deviceId = dhVersion.deviceId; + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed"); + continue; + } DHLOGI("Update Version key: %s", GetAnonyString(deviceId).c_str()); - VersionManager::GetInstance().AddDHVersion(dhVersion.uuid, dhVersion); + VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); } } - -void VersionInfoManager::HandleVersionDeleteChange(const std::vector &deleteRecords) -{ - std::lock_guard lock(verInfoMgrMutex_); - for (const auto &item : deleteRecords) { - const std::string value = item.value.ToString(); - DHVersion dhVersion; - dhVersion.FromJsonString(value); - const std::string &deviceId = dhVersion.deviceId; - - DHLOGI("Delete Version, key: %s", GetAnonyString(deviceId).c_str()); - VersionManager::GetInstance().RemoveDHVersion(dhVersion.uuid); - } -} - } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp b/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp index 65dd046e..4b926c33 100644 --- a/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp @@ -87,7 +87,7 @@ void OnLineTask::DoSyncInfo() } DHVersion dhVersion; - ret = VersionManager::GetInstance().GetDHVersionFromDB(GetUUID(), dhVersion); + ret = VersionManager::GetInstance().SyncDHVersionFromDB(GetUUID(), dhVersion); if (ret != DH_FWK_SUCCESS) { DHLOGE("SyncVersionInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index 656903de..d0b9777b 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -38,11 +38,10 @@ int32_t VersionManager::Init() return ret; } dhVersion.dhVersion = GetLocalDeviceVersion(); - std::string strUUID = DHContext::GetInstance().GetDeviceInfo().uuid; - dhVersion.uuid = strUUID; + dhVersion.uuid = DHContext::GetInstance().GetDeviceInfo().uuid; dhVersion.deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; ShowLocalVersion(dhVersion); - AddDHVersion(strUUID, dhVersion); + AddDHVersion(dhVersion.uuid, dhVersion); VersionInfoManager::GetInstance()->AddVersion(dhVersion); return DH_FWK_SUCCESS; } @@ -64,7 +63,7 @@ void VersionManager::ShowLocalVersion(const DHVersion &dhVersion) const int32_t VersionManager::AddDHVersion(const std::string &uuid, const DHVersion &dhVersion) { - DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); + DHLOGI("addDHVersion uuid: %s", GetAnonyString(uuid).c_str()); std::lock_guard lock(versionMutex_); dhVersions_[uuid] = dhVersion; return DH_FWK_SUCCESS; @@ -72,7 +71,7 @@ int32_t VersionManager::AddDHVersion(const std::string &uuid, const DHVersion &d int32_t VersionManager::RemoveDHVersion(const std::string &uuid) { - DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); + DHLOGI("removeDHVersion uuid: %s", GetAnonyString(uuid).c_str()); std::lock_guard lock(versionMutex_); std::unordered_map::iterator iter = dhVersions_.find(uuid); if (iter == dhVersions_.end()) { @@ -86,28 +85,32 @@ int32_t VersionManager::RemoveDHVersion(const std::string &uuid) int32_t VersionManager::GetDHVersion(const std::string &uuid, DHVersion &dhVersion) { DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); - std::lock_guard lock(versionMutex_); - std::unordered_map::iterator iter = dhVersions_.find(uuid); - if (iter != dhVersions_.end()) { - dhVersion = dhVersions_[uuid]; - return DH_FWK_SUCCESS; + { + std::lock_guard lock(versionMutex_); + std::unordered_map::iterator iter = dhVersions_.find(uuid); + if (iter != dhVersions_.end()) { + dhVersion = dhVersions_[uuid]; + return DH_FWK_SUCCESS; + } + DHLOGE("there is no uuid: %s in cache, get version fail", GetAnonyString(uuid).c_str()); } - DHLOGE("there is no uuid: %s in cache, get version fail", GetAnonyString(uuid).c_str()); - - int32_t ret = GetDHVersionFromDB(uuid, dhVersion); + int32_t ret = SyncDHVersionFromDB(uuid, dhVersion); if (ret != DH_FWK_SUCCESS) { DHLOGE("there is no uuid: %s in db, get version fail", GetAnonyString(uuid).c_str()); } return ret; } -int32_t VersionManager::GetDHVersionFromDB(const std::string &uuid, DHVersion &dhVersion) +int32_t VersionManager::SyncDHVersionFromDB(const std::string &uuid, DHVersion &dhVersion) { DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); - int32_t ret = VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(GetDeviceIdByUUID(uuid), dhVersion); + int32_t ret = VersionInfoManager::GetInstance()->GetVersionInfoFromDB(GetDeviceIdByUUID(uuid), dhVersion); if (ret != DH_FWK_SUCCESS) { return ret; } + + DHLOGI("Sync DHVersion from db success, uuid: %s", GetAnonyString(uuid).c_str()); + std::lock_guard lock(versionMutex_); dhVersions_[uuid] = dhVersion; return DH_FWK_SUCCESS; } @@ -124,6 +127,8 @@ int32_t VersionManager::GetCompVersion(const std::string &uuid, const DHType dhT DHLOGE("not find dhType: %#X", dhType); return ERR_DH_FWK_TYPE_NOT_EXIST; } + + DHLOGI("GetCompVersion success, uuid: %s, dhType%#X", GetAnonyString(uuid).c_str(), dhType); compVersion = dhVersion.compVersions[dhType]; return DH_FWK_SUCCESS; } -- Gitee From 93b7f1ba81faa9d17fdf2b3cff1792fc5b929f51 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Thu, 7 Jul 2022 21:21:33 +0800 Subject: [PATCH 09/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../include/utils/impl_utils.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h b/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h index 24276d7d..f9d1ef63 100644 --- a/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h +++ b/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h @@ -56,9 +56,6 @@ struct CompVersion { std::string handlerVersion; std::string sourceVersion; std::string sinkVersion; - - // virtual int32_t FromJsonString(const std::string &jsonStr); - // virtual std::string ToJsonString(); }; struct DHVersion { -- Gitee From dad8c4f4a2e3a6d690623a6f88d034bfba2da03f Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Thu, 7 Jul 2022 21:27:57 +0800 Subject: [PATCH 10/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../src/componentmanager/component_manager.cpp | 4 ++-- .../src/versionmanager/version_manager.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index 3c8ec547..b695e566 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -341,7 +341,7 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std param.attrs = capability->GetDHAttrs(); param.version = GetSinkVersionFromVerMgr(uuid, dhType); if (!param.version.empty()) { - DHLOGI("Get sink version from VerMgr success. uuid = %s, dhId = %s, dhType = %#X, version = %s", + DHLOGI("Get sink version from Version Mgr success. uuid = %s, dhId = %s, dhType = %#X, version = %s", GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), dhType, param.version.c_str()); return DH_FWK_SUCCESS; } @@ -353,7 +353,7 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std return DH_FWK_SUCCESS; } - DHLOGI("Get Sink Version failed, uuid = %s, dhId = %s, dhType = %#X,", GetAnonyString(uuid).c_str(), + DHLOGE("Get Sink Version failed, uuid = %s, dhId = %s, dhType = %#X,", GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), dhType); return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED; diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index d0b9777b..56db16e0 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -128,7 +128,7 @@ int32_t VersionManager::GetCompVersion(const std::string &uuid, const DHType dhT return ERR_DH_FWK_TYPE_NOT_EXIST; } - DHLOGI("GetCompVersion success, uuid: %s, dhType%#X", GetAnonyString(uuid).c_str(), dhType); + DHLOGI("GetCompVersion success, uuid: %s, dhType: %#X", GetAnonyString(uuid).c_str(), dhType); compVersion = dhVersion.compVersions[dhType]; return DH_FWK_SUCCESS; } -- Gitee From 024b04355bafd4a414ac5a3705c494ce1d44a138 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Thu, 7 Jul 2022 21:42:00 +0800 Subject: [PATCH 11/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../src/componentmanager/component_manager.cpp | 3 +-- .../src/resourcemanager/db_adapter.cpp | 2 +- .../src/resourcemanager/version_info_manager.cpp | 4 ++-- .../src/utils/impl_utils.cpp | 4 ++-- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index b695e566..55d10532 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -355,8 +355,7 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std DHLOGE("Get Sink Version failed, uuid = %s, dhId = %s, dhType = %#X,", GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), dhType); - return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED; - + return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED; } std::string ComponentManager::GetSinkVersionFromVerMgr(const std::string &uuid, const DHType dhType) diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp index 0a9859df..db668f7e 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp @@ -267,7 +267,7 @@ void DBAdapter::SyncDBForRecover() if (storeId_.storeId == GLOBAL_CAPABILITY_ID) { CapabilityInfoEvent recoverEvent(*this, CapabilityInfoEvent::EventType::RECOVER); DHContext::GetInstance().GetEventBus()->PostEvent(recoverEvent); - } + } if (storeId_.storeId == GLOBAL_VERSION_ID) { VersionInfoEvent recoverEvent(*this, VersionInfoEvent::EventType::RECOVER); diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp index fe44f35e..a9371be9 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp @@ -85,7 +85,7 @@ int32_t VersionInfoManager::AddVersion(const DHVersion &version) if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; - } + } std::string data(""); dbAdapterPtr_->GetDataByKey(version.deviceId, data); @@ -197,7 +197,7 @@ int32_t VersionInfoManager::ManualSync(const std::string &networkId) } void VersionInfoManager::OnChange(const DistributedKv::ChangeNotification &changeNotification) -{ +{ DHLOGI("VersionInfoManager: DB data OnChange"); if (!changeNotification.GetInsertEntries().empty()) { DHLOGI("Handle version data add change"); diff --git a/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp b/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp index 1506d33e..649eb947 100644 --- a/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp @@ -38,7 +38,7 @@ void ToJson(nlohmann::json &jsonObject, const DHVersion &dhVersion) jsonObject[DH_VER] = dhVersion.dhVersion; nlohmann::json compVers; - for(const auto &compVersion : dhVersion.compVersions) { + for (const auto &compVersion : dhVersion.compVersions) { nlohmann::json compVer; compVer[NAME] = compVersion.second.name; compVer[TYPE] = compVersion.second.dhType; @@ -58,7 +58,7 @@ void FromJson(const nlohmann::json &jsonObject, DHVersion &dhVersion) } if (jsonObject.find(DH_VER) != jsonObject.end()) { - dhVersion.dhVersion = jsonObject.at(DH_VER).get(); + dhVersion.dhVersion = jsonObject.at(DH_VER).get(); } if (jsonObject.find(COMP_VER) != jsonObject.end()) { -- Gitee From 9e271fe79394d33ff6375c489860965922d2b196 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Thu, 7 Jul 2022 21:48:23 +0800 Subject: [PATCH 12/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../src/componentmanager/component_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index 55d10532..e22445cb 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -355,7 +355,7 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std DHLOGE("Get Sink Version failed, uuid = %s, dhId = %s, dhType = %#X,", GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), dhType); - return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED; + return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED; } std::string ComponentManager::GetSinkVersionFromVerMgr(const std::string &uuid, const DHType dhType) -- Gitee From f86c96331bd80ce8efbca413bbced81971b59882 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 8 Jul 2022 09:46:07 +0800 Subject: [PATCH 13/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../test/unittest/common/task/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/BUILD.gn index bbddc9ad..68aba12e 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/BUILD.gn @@ -21,6 +21,7 @@ config("module_private_config") { visibility = [ ":*" ] include_dirs = [ "include", + "//third_party/json/include", "${utils_path}/include", "${utils_path}/include/log", "${services_path}/distributedhardwarefwkserviceimpl/include", -- Gitee From 6de7195837ec3920369d16e3e48a21d5f4c1a4b0 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 8 Jul 2022 09:52:51 +0800 Subject: [PATCH 14/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../test/unittest/common/BUILD.gn | 1 + .../test/unittest/common/componentloader/BUILD.gn | 1 + .../test/unittest/common/versionmanager/BUILD.gn | 1 + 3 files changed, 3 insertions(+) diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/unittest/common/BUILD.gn index 035bc692..a110f9fb 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/BUILD.gn @@ -29,6 +29,7 @@ config("module_private_config") { "${services_path}/distributedhardwarefwkserviceimpl/include", "${services_path}/distributedhardwarefwkserviceimpl/include/task", "${services_path}/distributedhardwarefwkserviceimpl/include/utils", + "//third_party/json/include", "//utils/native/base/include", ] } diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn index e83d680c..584675c7 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn @@ -29,6 +29,7 @@ config("module_private_config") { "${services_path}/distributedhardwarefwkserviceimpl/include/utils", "${common_path}/utils/include", "${common_path}/log/include", + "//third_party/json/include", ] } diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn index 60f92bc8..8273d0e3 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn @@ -29,6 +29,7 @@ config("module_private_config") { "${services_path}/distributedhardwarefwkserviceimpl/include/utils", "${common_path}/utils/include", "${common_path}/log/include", + "//third_party/json/include", "//utils/native/base/include", ] } -- Gitee From 4de73a25254d07c720e2f584eeb5a9484d3ec70a Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 8 Jul 2022 20:13:24 +0800 Subject: [PATCH 15/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../BUILD.gn | 2 +- ..._info_manager.h => version_info_adapter.h} | 34 +--- .../include/versionmanager/version_manager.h | 32 +++- .../src/componentloader/component_loader.cpp | 1 + .../componentmanager/component_manager.cpp | 6 +- .../src/distributed_hardware_manager.cpp | 14 +- .../local_hardware_manager.cpp | 1 + ...o_manager.cpp => version_info_adapter.cpp} | 165 ++++++------------ .../src/task/online_task.cpp | 6 +- .../src/versionmanager/version_manager.cpp | 149 ++++++++++++++-- .../unittest/common/versionmanager/BUILD.gn | 7 + .../src/version_manager_test.cpp | 22 +-- 12 files changed, 260 insertions(+), 179 deletions(-) rename services/distributedhardwarefwkserviceimpl/include/resourcemanager/{version_info_manager.h => version_info_adapter.h} (56%) rename services/distributedhardwarefwkserviceimpl/src/resourcemanager/{version_info_manager.cpp => version_info_adapter.cpp} (54%) diff --git a/services/distributedhardwarefwkserviceimpl/BUILD.gn b/services/distributedhardwarefwkserviceimpl/BUILD.gn index 4cf6f905..8daed4b5 100644 --- a/services/distributedhardwarefwkserviceimpl/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/BUILD.gn @@ -52,7 +52,7 @@ ohos_shared_library("distributedhardwarefwksvr_impl") { "src/resourcemanager/capability_info_manager.cpp", "src/resourcemanager/capability_utils.cpp", "src/resourcemanager/db_adapter.cpp", - "src/resourcemanager/version_info_manager.cpp", + "src/resourcemanager/version_info_adapter.cpp", "src/task/disable_task.cpp", "src/task/enable_task.cpp", "src/task/offline_task.cpp", diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_adapter.h similarity index 56% rename from services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h rename to services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_adapter.h index 1c7ee915..7196e982 100644 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_adapter.h @@ -13,59 +13,41 @@ * limitations under the License. */ -#ifndef OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_H -#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_H +#ifndef OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_ADAPTER_H +#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_ADAPTER_H #include #include #include -#include "kvstore_observer.h" - #include "db_adapter.h" -#include "event.h" -#include "eventbus_handler.h" -#include "event_bus.h" -#include "event_sender.h" #include "impl_utils.h" #include "single_instance.h" -#include "version_info_event.h" class DBAdapter; namespace OHOS { namespace DistributedHardware { -class VersionInfoManager : public std::enable_shared_from_this, - public EventSender, - public DistributedKv::KvStoreObserver, - public EventBusHandler { -public: - VersionInfoManager(const VersionInfoManager &) = delete; - VersionInfoManager &operator = (const VersionInfoManager &) = delete; - VersionInfoManager(VersionInfoManager &&) = delete; - VersionInfoManager &operator = (VersionInfoManager &&) = delete; - static std::shared_ptr GetInstance(); - virtual ~VersionInfoManager(); +class VersionInfoAdapter { +DECLARE_SINGLE_INSTANCE(VersionInfoAdapter); +public: int32_t Init(); int32_t UnInit(); int32_t AddVersion(const DHVersion &version); int32_t GetVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion); - int32_t SyncRemoteVersionInfos(); + int32_t SyncRemoteVersionInfos(std::unordered_map &dhVersions); + int32_t RemoveVersionInfoInDB(const std::string &deviceId); void CreateManualSyncCount(const std::string &deviceId); void RemoveManualSyncCount(const std::string &deviceId); int32_t ManualSync(const std::string &networkId); - virtual void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; - void OnEvent(VersionInfoEvent &ev) override; - private: - VersionInfoManager(); void HandleVersionAddChange(const std::vector &insertRecords); void HandleVersionUpdateChange(const std::vector &updateRecords); private: - mutable std::mutex verInfoMgrMutex_; + mutable std::mutex verAdapterMutex_; std::shared_ptr dbAdapterPtr_; }; } // namespace DistributedHardware diff --git a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h index f056d897..91fc61eb 100644 --- a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h @@ -20,20 +20,32 @@ #include #include +#include "kvstore_observer.h" + #include "single_instance.h" +#include "eventbus_handler.h" +#include "event_bus.h" +#include "event_sender.h" #include "distributed_hardware_errno.h" #include "device_type.h" #include "utils/impl_utils.h" +#include "version_info_event.h" namespace OHOS { namespace DistributedHardware { const std::string DH_LOCAL_VERSION = "1.0"; -class VersionManager { - DECLARE_SINGLE_INSTANCE_BASE(VersionManager); - +class VersionManager : public std::enable_shared_from_this, + public EventSender, + public DistributedKv::KvStoreObserver, + public EventBusHandler { public: - VersionManager() {} - ~VersionManager() {} + VersionManager(const VersionManager &) = delete; + VersionManager &operator = (const VersionManager &) = delete; + VersionManager(VersionManager &&) = delete; + VersionManager &operator = (VersionManager &&) = delete; + static std::shared_ptr GetInstance(); + virtual ~VersionManager(); + int32_t Init(); void UnInit(); int32_t AddDHVersion(const std::string &uuid, const DHVersion &dhVersion); @@ -41,9 +53,19 @@ public: int32_t GetDHVersion(const std::string &uuid, DHVersion &dhVersion); int32_t GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion); int32_t SyncDHVersionFromDB(const std::string &uuid, DHVersion &dhVersion); + int32_t SyncRemoteVersionInfos(); std::string GetLocalDeviceVersion(); void ShowLocalVersion(const DHVersion &dhVersion) const; + void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; + void OnEvent(VersionInfoEvent &ev) override; + +private: + VersionManager() = default; + void HandleVersionAddChange(const std::vector &insertRecords); + void HandleVersionUpdateChange(const std::vector &updateRecords); + void HandleVersionDeleteChange(const std::vector &deleteRecords); + private: std::unordered_map dhVersions_; std::mutex versionMutex_; diff --git a/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp index 02e9c091..b67e032a 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp @@ -67,6 +67,7 @@ std::map g_mapDhTypeName = { int32_t ComponentLoader::Init() { + DHLOGI("start"); DHTraceStart(COMPONENT_LOAD_START); int32_t ret = ParseConfig(); DHTraceEnd(); diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index e22445cb..8cb9291b 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -119,7 +119,7 @@ ComponentManager::ActionResult ComponentManager::StartSource() std::string uuid = DHContext::GetInstance().GetDeviceInfo().uuid; for (const auto &item : compSource_) { CompVersion compversion; - VersionManager::GetInstance().GetCompVersion(uuid, item.first, compversion); + VersionManager::GetInstance()->GetCompVersion(uuid, item.first, compversion); auto params = compversion.sourceVersion; auto future = std::async(std::launch::async, [item, params]() { return item.second->InitSource(params); }); futures.emplace(item.first, future.share()); @@ -134,7 +134,7 @@ ComponentManager::ActionResult ComponentManager::StartSink() std::string uuid = DHContext::GetInstance().GetDeviceInfo().uuid; for (const auto &item : compSink_) { CompVersion compversion; - VersionManager::GetInstance().GetCompVersion(uuid, item.first, compversion); + VersionManager::GetInstance()->GetCompVersion(uuid, item.first, compversion); auto params = compversion.sinkVersion; auto future = std::async(std::launch::async, [item, params]() { return item.second->InitSink(params); }); futures.emplace(item.first, future.share()); @@ -361,7 +361,7 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std std::string ComponentManager::GetSinkVersionFromVerMgr(const std::string &uuid, const DHType dhType) { CompVersion compversion; - auto ret = VersionManager::GetInstance().GetCompVersion(uuid, dhType, compversion); + auto ret = VersionManager::GetInstance()->GetCompVersion(uuid, dhType, compversion); if (ret != DH_FWK_SUCCESS) { DHLOGE("Get version from Version Manager failed, uuid =%s, dhType = %#X, errCode = %d", GetAnonyString(uuid).c_str(), dhType, ret); diff --git a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp index 0cc1e922..edd87fe4 100644 --- a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp @@ -29,7 +29,7 @@ #include "task_board.h" #include "task_executor.h" #include "task_factory.h" -#include "version_info_manager.h" +#include "version_info_adapter.h" #include "version_manager.h" namespace OHOS { @@ -55,13 +55,11 @@ int32_t DistributedHardwareManager::Initialize() DHLOGI("start"); CapabilityInfoManager::GetInstance()->Init(); - VersionInfoManager::GetInstance()->Init(); - ComponentLoader::GetInstance().Init(); LocalHardwareManager::GetInstance().Init(); - VersionManager::GetInstance().Init(); + VersionManager::GetInstance()->Init(); ComponentManager::GetInstance().Init(); @@ -75,14 +73,12 @@ int32_t DistributedHardwareManager::Release() ComponentManager::GetInstance().UnInit(); - VersionManager::GetInstance().UnInit(); + VersionManager::GetInstance()->UnInit(); LocalHardwareManager::GetInstance().UnInit(); ComponentLoader::GetInstance().UnInit(); - VersionInfoManager::GetInstance()->UnInit(); - CapabilityInfoManager::GetInstance()->UnInit(); return DH_FWK_SUCCESS; @@ -118,7 +114,7 @@ int32_t DistributedHardwareManager::SendOnLineEvent(const std::string &networkId TaskExecutor::GetInstance().PushTask(task); DHContext::GetInstance().AddOnlineDevice(uuid, networkId); CapabilityInfoManager::GetInstance()->CreateManualSyncCount(GetDeviceIdByUUID(uuid)); - VersionInfoManager::GetInstance()->CreateManualSyncCount(GetDeviceIdByUUID(uuid)); + VersionInfoAdapter::GetInstance().CreateManualSyncCount(GetDeviceIdByUUID(uuid)); return DH_FWK_SUCCESS; } @@ -160,7 +156,7 @@ int32_t DistributedHardwareManager::SendOffLineEvent(const std::string &networkI DHContext::GetInstance().RemoveOnlineDevice(realUUID); CapabilityInfoManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); - VersionInfoManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); + VersionInfoAdapter::GetInstance().RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); HiSysEventWriteCompOfflineMsg(DHFWK_DEV_OFFLINE, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, GetAnonyString(networkId), "dhfwk device offline event."); diff --git a/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/local_hardware_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/local_hardware_manager.cpp index 1eaec6e6..30ad456d 100644 --- a/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/local_hardware_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/local_hardware_manager.cpp @@ -37,6 +37,7 @@ LocalHardwareManager::~LocalHardwareManager() {} void LocalHardwareManager::Init() { + DHLOGI("start"); std::vector allCompTypes = ComponentLoader::GetInstance().GetAllCompTypes(); for (auto dhType : allCompTypes) { IHardwareHandler *hardwareHandler = nullptr; diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_adapter.cpp similarity index 54% rename from services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp rename to services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_adapter.cpp index a9371be9..46e4d6da 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_adapter.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "version_info_manager.h" +#include "version_info_adapter.h" #include "anonymous_string.h" #include "constants.h" @@ -31,45 +31,28 @@ class DBAdapter; namespace OHOS { namespace DistributedHardware { #undef DH_LOG_TAG -#define DH_LOG_TAG "VersionInfoManager" +#define DH_LOG_TAG "VersionInfoAdapter" -VersionInfoManager::VersionInfoManager() : dbAdapterPtr_(nullptr) -{} +IMPLEMENT_SINGLE_INSTANCE(VersionInfoAdapter); -VersionInfoManager::~VersionInfoManager() +int32_t VersionInfoAdapter::Init() { - DHLOGI("VersionInfoManager Destruction!"); -} - -std::shared_ptr VersionInfoManager::GetInstance() -{ - static std::shared_ptr instance(new(std::nothrow) VersionInfoManager); - if (instance == nullptr) { - DHLOGE("instance is nullptr, because applying memory fail!"); - return nullptr; - } - return instance; -} - -int32_t VersionInfoManager::Init() -{ - DHLOGI("VersionInfoManager instance init!"); - std::lock_guard lock(verInfoMgrMutex_); - dbAdapterPtr_ = std::make_shared(APP_ID, GLOBAL_VERSION_ID, shared_from_this()); + DHLOGI("VersionInfoAdapter instance init!"); + std::lock_guard lock(verAdapterMutex_); + dbAdapterPtr_ = std::make_shared(APP_ID, GLOBAL_VERSION_ID, VersionManager::GetInstance()); if (dbAdapterPtr_->Init() != DH_FWK_SUCCESS) { DHLOGE("Init dbAdapterPtr_ failed"); return ERR_DH_FWK_RESOURCE_INIT_DB_FAILED; } - VersionInfoEvent versionInfoEvent(*this); - DHContext::GetInstance().GetEventBus()->AddHandler(versionInfoEvent.GetType(), *this); - DHLOGI("VersionInfoManager instance init success"); + + DHLOGI("VersionInfoAdapter instance init success"); return DH_FWK_SUCCESS; } -int32_t VersionInfoManager::UnInit() +int32_t VersionInfoAdapter::UnInit() { - DHLOGI("VersionInfoManager UnInit"); - std::lock_guard lock(verInfoMgrMutex_); + DHLOGI("VersionInfoAdapter UnInit"); + std::lock_guard lock(verAdapterMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); return ERR_DH_FWK_RESOURCE_UNINIT_DB_FAILED; @@ -79,9 +62,9 @@ int32_t VersionInfoManager::UnInit() return DH_FWK_SUCCESS; } -int32_t VersionInfoManager::AddVersion(const DHVersion &version) +int32_t VersionInfoAdapter::AddVersion(const DHVersion &version) { - std::lock_guard lock(verInfoMgrMutex_); + std::lock_guard lock(verAdapterMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; @@ -104,10 +87,10 @@ int32_t VersionInfoManager::AddVersion(const DHVersion &version) return DH_FWK_SUCCESS; } -int32_t VersionInfoManager::GetVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion) +int32_t VersionInfoAdapter::GetVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion) { DHLOGI("Sync VersionInfo from DB, deviceId: %s", GetAnonyString(deviceId).c_str()); - std::lock_guard lock(verInfoMgrMutex_); + std::lock_guard lock(verAdapterMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; @@ -123,18 +106,21 @@ int32_t VersionInfoManager::GetVersionInfoFromDB(const std::string &deviceId, DH return DH_FWK_SUCCESS; } -int32_t VersionInfoManager::SyncRemoteVersionInfos() +int32_t VersionInfoAdapter::SyncRemoteVersionInfos(std::unordered_map &dhVersions) { DHLOGI("Sync full remote version info from DB"); - std::lock_guard lock(verInfoMgrMutex_); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; - } std::vector dataVector; - if (dbAdapterPtr_->GetDataByKeyPrefix("", dataVector) != DH_FWK_SUCCESS) { - DHLOGE("Query all data from DB failed"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + { + std::lock_guard lock(verAdapterMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + + if (dbAdapterPtr_->GetDataByKeyPrefix("", dataVector) != DH_FWK_SUCCESS) { + DHLOGE("Query all data from DB failed"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } } for (const auto &data : dataVector) { @@ -156,14 +142,34 @@ int32_t VersionInfoManager::SyncRemoteVersionInfos() DHLOGI("Find uuid failed"); continue; } - VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); + dhVersions.insert(std::pair(uuid, dhVersion)); } return DH_FWK_SUCCESS; } -void VersionInfoManager::CreateManualSyncCount(const std::string &deviceId) +int32_t VersionInfoAdapter::RemoveVersionInfoInDB(const std::string &deviceId) { - std::lock_guard lock(verInfoMgrMutex_); + DHLOGI("Remove capability device info, deviceId: %s", GetAnonyString(deviceId).c_str()); + std::lock_guard lock(verAdapterMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + if (deviceId.empty()) { + DHLOGE("RemoveCapabilityInfoInDB failed, deviceId is empty"); + return ERR_DH_FWK_PARA_INVALID; + } + + if (dbAdapterPtr_->RemoveDeviceData(deviceId) != DH_FWK_SUCCESS) { + DHLOGE("Remove version Device Data failed, deviceId: %s", GetAnonyString(deviceId).c_str()); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + return DH_FWK_SUCCESS; +} + +void VersionInfoAdapter::CreateManualSyncCount(const std::string &deviceId) +{ + std::lock_guard lock(verAdapterMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); return; @@ -171,9 +177,9 @@ void VersionInfoManager::CreateManualSyncCount(const std::string &deviceId) dbAdapterPtr_->CreateManualSyncCount(deviceId); } -void VersionInfoManager::RemoveManualSyncCount(const std::string &deviceId) +void VersionInfoAdapter::RemoveManualSyncCount(const std::string &deviceId) { - std::lock_guard lock(verInfoMgrMutex_); + std::lock_guard lock(verAdapterMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); return; @@ -181,10 +187,10 @@ void VersionInfoManager::RemoveManualSyncCount(const std::string &deviceId) dbAdapterPtr_->RemoveManualSyncCount(deviceId); } -int32_t VersionInfoManager::ManualSync(const std::string &networkId) +int32_t VersionInfoAdapter::ManualSync(const std::string &networkId) { DHLOGI("ManualSync start, networkId: %s", GetAnonyString(networkId).c_str()); - std::unique_lock lock(verInfoMgrMutex_); + std::unique_lock lock(verAdapterMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; @@ -195,66 +201,5 @@ int32_t VersionInfoManager::ManualSync(const std::string &networkId) } return DH_FWK_SUCCESS; } - -void VersionInfoManager::OnChange(const DistributedKv::ChangeNotification &changeNotification) -{ - DHLOGI("VersionInfoManager: DB data OnChange"); - if (!changeNotification.GetInsertEntries().empty()) { - DHLOGI("Handle version data add change"); - HandleVersionAddChange(changeNotification.GetInsertEntries()); - } - if (!changeNotification.GetUpdateEntries().empty()) { - DHLOGI("Handle version data update change"); - HandleVersionUpdateChange(changeNotification.GetUpdateEntries()); - } -} - -void VersionInfoManager::OnEvent(VersionInfoEvent &ev) -{ - switch (ev.GetAction()) { - case VersionInfoEvent::EventType::RECOVER: - SyncRemoteVersionInfos(); - break; - default: - DHLOGE("Event is undefined, type is %d", ev.GetAction()); - break; - } -} - -void VersionInfoManager::HandleVersionAddChange(const std::vector &insertRecords) -{ - DHLOGI("VersionInfoManager: Version add change"); - for (const auto &item : insertRecords) { - const std::string value = item.value.ToString(); - DHVersion dhVersion; - dhVersion.FromJsonString(value); - const std::string &deviceId = dhVersion.deviceId; - std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); - if (uuid.empty()) { - DHLOGI("Find uuid failed"); - continue; - } - DHLOGI("Add Version ,key: %s", GetAnonyString(deviceId).c_str()); - VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); - } -} - -void VersionInfoManager::HandleVersionUpdateChange(const std::vector &updateRecords) -{ - DHLOGI("VersionInfoManager: Version update change"); - for (const auto &item : updateRecords) { - const std::string value = item.value.ToString(); - DHVersion dhVersion; - dhVersion.FromJsonString(value); - const std::string &deviceId = dhVersion.deviceId; - std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); - if (uuid.empty()) { - DHLOGI("Find uuid failed"); - continue; - } - DHLOGI("Update Version key: %s", GetAnonyString(deviceId).c_str()); - VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); - } -} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp b/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp index 4b926c33..f33532b5 100644 --- a/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp @@ -23,7 +23,7 @@ #include "task_board.h" #include "task_executor.h" #include "task_factory.h" -#include "version_info_manager.h" +#include "version_info_adapter.h" #include "version_manager.h" namespace OHOS { @@ -76,7 +76,7 @@ void OnLineTask::DoSyncInfo() DHLOGW("ManualSync failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } - ret = VersionInfoManager::GetInstance()->ManualSync(GetNetworkId()); + ret = VersionInfoAdapter::GetInstance().ManualSync(GetNetworkId()); if (ret != DH_FWK_SUCCESS) { DHLOGW("ManualSync version failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } @@ -87,7 +87,7 @@ void OnLineTask::DoSyncInfo() } DHVersion dhVersion; - ret = VersionManager::GetInstance().SyncDHVersionFromDB(GetUUID(), dhVersion); + ret = VersionManager::GetInstance()->SyncDHVersionFromDB(GetUUID(), dhVersion); if (ret != DH_FWK_SUCCESS) { DHLOGE("SyncVersionInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index 56db16e0..06e62aac 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -20,17 +20,35 @@ #include "dh_context.h" #include "dh_utils_tool.h" #include "distributed_hardware_log.h" -#include "version_info_manager.h" +#include "version_info_adapter.h" namespace OHOS { namespace DistributedHardware { #undef DH_LOG_TAG #define DH_LOG_TAG "VersionManager" -IMPLEMENT_SINGLE_INSTANCE(VersionManager); + +VersionManager::~VersionManager() +{ + DHLOGI("VersionManager Destruction!"); +} + +std::shared_ptr VersionManager::GetInstance() +{ + static std::shared_ptr instance(new(std::nothrow) VersionManager); + if (instance == nullptr) { + DHLOGE("instance is nullptr, because applying memory fail!"); + return nullptr; + } + return instance; +} int32_t VersionManager::Init() { DHLOGI("start"); + VersionInfoEvent versionInfoEvent(*this); + DHContext::GetInstance().GetEventBus()->AddHandler(versionInfoEvent.GetType(), *this); + VersionInfoAdapter::GetInstance().Init(); + DHVersion dhVersion; int32_t ret = ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion); if (ret != DH_FWK_SUCCESS) { @@ -42,13 +60,14 @@ int32_t VersionManager::Init() dhVersion.deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; ShowLocalVersion(dhVersion); AddDHVersion(dhVersion.uuid, dhVersion); - VersionInfoManager::GetInstance()->AddVersion(dhVersion); + VersionInfoAdapter::GetInstance().AddVersion(dhVersion); return DH_FWK_SUCCESS; } void VersionManager::UnInit() { DHLOGI("start"); + VersionInfoAdapter::GetInstance().UnInit(); dhVersions_.clear(); } @@ -72,13 +91,16 @@ int32_t VersionManager::AddDHVersion(const std::string &uuid, const DHVersion &d int32_t VersionManager::RemoveDHVersion(const std::string &uuid) { DHLOGI("removeDHVersion uuid: %s", GetAnonyString(uuid).c_str()); - std::lock_guard lock(versionMutex_); - std::unordered_map::iterator iter = dhVersions_.find(uuid); - if (iter == dhVersions_.end()) { - DHLOGE("there is no uuid: %s, remove fail", GetAnonyString(uuid).c_str()); - return ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST; + { + std::lock_guard lock(versionMutex_); + std::unordered_map::iterator iter = dhVersions_.find(uuid); + if (iter == dhVersions_.end()) { + DHLOGE("there is no uuid: %s, remove fail", GetAnonyString(uuid).c_str()); + return ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST; + } + dhVersions_.erase(iter); } - dhVersions_.erase(iter); + VersionInfoAdapter::GetInstance().RemoveVersionInfoInDB(GetDeviceIdByUUID(uuid)); return DH_FWK_SUCCESS; } @@ -104,17 +126,33 @@ int32_t VersionManager::GetDHVersion(const std::string &uuid, DHVersion &dhVersi int32_t VersionManager::SyncDHVersionFromDB(const std::string &uuid, DHVersion &dhVersion) { DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); - int32_t ret = VersionInfoManager::GetInstance()->GetVersionInfoFromDB(GetDeviceIdByUUID(uuid), dhVersion); + int32_t ret = VersionInfoAdapter::GetInstance().GetVersionInfoFromDB(GetDeviceIdByUUID(uuid), dhVersion); if (ret != DH_FWK_SUCCESS) { return ret; } - DHLOGI("Sync DHVersion from db success, uuid: %s", GetAnonyString(uuid).c_str()); + std::lock_guard lock(versionMutex_); dhVersions_[uuid] = dhVersion; return DH_FWK_SUCCESS; } +int32_t VersionManager::SyncRemoteVersionInfos() +{ + std::unordered_map dhVersions; + int32_t ret = VersionInfoAdapter::GetInstance().SyncRemoteVersionInfos(dhVersions); + if (ret != DH_FWK_SUCCESS) { + DHLOGI("Sync remote all DHVersions fail"); + return ret; + } + + std::lock_guard lock(versionMutex_); + for (auto dhVersion : dhVersions) { + dhVersions_[dhVersion.first] = dhVersion.second; + } + return DH_FWK_SUCCESS; +} + int32_t VersionManager::GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion) { DHVersion dhVersion; @@ -137,5 +175,94 @@ std::string VersionManager::GetLocalDeviceVersion() { return DH_LOCAL_VERSION; } + +void VersionManager::OnChange(const DistributedKv::ChangeNotification &changeNotification) +{ + DHLOGI("VersionManager: DB data OnChange"); + if (!changeNotification.GetInsertEntries().empty()) { + DHLOGI("Handle version data add change"); + HandleVersionAddChange(changeNotification.GetInsertEntries()); + } + if (!changeNotification.GetUpdateEntries().empty()) { + DHLOGI("Handle version data update change"); + HandleVersionUpdateChange(changeNotification.GetUpdateEntries()); + } + if (!changeNotification.GetDeleteEntries().empty()) { + DHLOGI("Handle capability data delete change"); + HandleVersionDeleteChange(changeNotification.GetDeleteEntries()); + } +} + +void VersionManager::HandleVersionAddChange(const std::vector &insertRecords) +{ + DHLOGI("VersionManager: Version add change"); + for (const auto &item : insertRecords) { + const std::string value = item.value.ToString(); + DHVersion dhVersion; + dhVersion.FromJsonString(value); + const std::string &deviceId = dhVersion.deviceId; + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed"); + continue; + } + DHLOGI("Add Version ,uuid: %s", GetAnonyString(uuid).c_str()); + AddDHVersion(uuid, dhVersion); + } +} + +void VersionManager::HandleVersionUpdateChange(const std::vector &updateRecords) +{ + DHLOGI("VersionManager: Version update change"); + for (const auto &item : updateRecords) { + const std::string value = item.value.ToString(); + DHVersion dhVersion; + dhVersion.FromJsonString(value); + const std::string &deviceId = dhVersion.deviceId; + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed"); + continue; + } + DHLOGI("Update Version ,uuid: %s", GetAnonyString(uuid).c_str()); + AddDHVersion(uuid, dhVersion); + } +} + +void VersionManager::HandleVersionDeleteChange(const std::vector &deleteRecords) +{ + DHLOGI("VersionManager: Version delete change"); + for (const auto &item : deleteRecords) { + const std::string value = item.value.ToString(); + DHVersion dhVersion; + dhVersion.FromJsonString(value); + const std::string &deviceId = dhVersion.deviceId; + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed"); + continue; + } + std::string networkId = DHContext::GetInstance().GetNetworkIdByUUID(uuid); + if (networkId.empty()) { + DHLOGI("Find network failed and never disable, uuid: %s", GetAnonyString(uuid).c_str()); + continue; + } + DHLOGI("Delete Version ,uuid: %s", GetAnonyString(uuid).c_str()); + RemoveDHVersion(uuid); + } +} + +void VersionManager::OnEvent(VersionInfoEvent &ev) +{ + switch (ev.GetAction()) { + case VersionInfoEvent::EventType::RECOVER: + SyncRemoteVersionInfos(); + break; + default: + DHLOGE("Event is undefined, type is %d", ev.GetAction()); + break; + } +} + } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn index 8273d0e3..c95aa3c0 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn @@ -22,9 +22,11 @@ config("module_private_config") { include_dirs = [ "include", "${utils_path}/include", + "${utils_path}/include/eventbus", "${utils_path}/include/log", "${services_path}/distributedhardwarefwkserviceimpl/include", "${services_path}/distributedhardwarefwkserviceimpl/include/componentloader", + "${services_path}/distributedhardwarefwkserviceimpl/include/resourcemanager", "${services_path}/distributedhardwarefwkserviceimpl/include/versionmanager", "${services_path}/distributedhardwarefwkserviceimpl/include/utils", "${common_path}/utils/include", @@ -46,6 +48,11 @@ ohos_unittest("VersionManagerTest") { "//third_party/googletest:gtest_main", ] + external_deps = [ + "distributeddatamgr:distributeddata_inner", + "eventhandler:libeventhandler", + ] + defines = [ "HI_LOG_ENABLE", "DH_LOG_TAG=\"VersionManagerTest\"", diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp index 874900c8..895c17f9 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp @@ -64,7 +64,7 @@ const std::string TEST_COMPONENT_NAME_3 = "distributed_mic"; */ HWTEST_F(VersionManagerTest, version_manager_test_001, TestSize.Level0) { - auto ret = VersionManager::GetInstance().Init(); + auto ret = VersionManager::GetInstance()->Init(); EXPECT_EQ(DH_FWK_SUCCESS, ret); } @@ -101,10 +101,10 @@ HWTEST_F(VersionManagerTest, version_manager_test_002, TestSize.Level0) dhVersion.compVersions.insert(std::make_pair(cVs1.dhType, cVs1)); dhVersion.compVersions.insert(std::make_pair(cVs2.dhType, cVs2)); dhVersion.compVersions.insert(std::make_pair(cVs3.dhType, cVs3)); - int32_t ret = VersionManager::GetInstance().AddDHVersion(dhVersion.uuid, dhVersion); + int32_t ret = VersionManager::GetInstance()->AddDHVersion(dhVersion.uuid, dhVersion); EXPECT_EQ(DH_FWK_SUCCESS, ret); dhVersion.uuid = TEST_DEVICE_ID_2; - ret = VersionManager::GetInstance().AddDHVersion(dhVersion.uuid, dhVersion); + ret = VersionManager::GetInstance()->AddDHVersion(dhVersion.uuid, dhVersion); EXPECT_EQ(DH_FWK_SUCCESS, ret); } @@ -116,7 +116,7 @@ HWTEST_F(VersionManagerTest, version_manager_test_002, TestSize.Level0) */ HWTEST_F(VersionManagerTest, version_manager_test_003, TestSize.Level0) { - std::string strVersion = VersionManager::GetInstance().GetLocalDeviceVersion(); + std::string strVersion = VersionManager::GetInstance()->GetLocalDeviceVersion(); EXPECT_EQ(DH_LOCAL_VERSION, strVersion); } @@ -129,12 +129,12 @@ HWTEST_F(VersionManagerTest, version_manager_test_003, TestSize.Level0) HWTEST_F(VersionManagerTest, version_manager_test_004, TestSize.Level0) { DHVersion dhVersion; - int32_t ret = VersionManager::GetInstance().GetDHVersion(TEST_DEVICE_ID_2, dhVersion); + int32_t ret = VersionManager::GetInstance()->GetDHVersion(TEST_DEVICE_ID_2, dhVersion); EXPECT_EQ(DH_FWK_SUCCESS, ret); EXPECT_EQ(TEST_HANDLER_VERSION_2, dhVersion.compVersions[DHType::SPEAKER].handlerVersion); EXPECT_EQ(TEST_DH_VERSION, dhVersion.dhVersion); - ret = VersionManager::GetInstance().GetDHVersion(TEST_DEVICE_ID_3, dhVersion); - EXPECT_EQ(ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST, ret); + ret = VersionManager::GetInstance()->GetDHVersion(TEST_DEVICE_ID_3, dhVersion); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); } /** @@ -145,13 +145,13 @@ HWTEST_F(VersionManagerTest, version_manager_test_004, TestSize.Level0) */ HWTEST_F(VersionManagerTest, version_manager_test_005, TestSize.Level0) { - int32_t ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_2); + int32_t ret = VersionManager::GetInstance()->RemoveDHVersion(TEST_DEVICE_ID_2); EXPECT_EQ(DH_FWK_SUCCESS, ret); - ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_4); + ret = VersionManager::GetInstance()->RemoveDHVersion(TEST_DEVICE_ID_4); EXPECT_EQ(ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST, ret); - ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_2); + ret = VersionManager::GetInstance()->RemoveDHVersion(TEST_DEVICE_ID_2); EXPECT_EQ(ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST, ret); - ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_1); + ret = VersionManager::GetInstance()->RemoveDHVersion(TEST_DEVICE_ID_1); EXPECT_EQ(DH_FWK_SUCCESS, ret); } } // namespace DistributedHardware -- Gitee From 5bb5e6dce1735bcb41cd42271aa0a5a79c802a0b Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 8 Jul 2022 20:24:46 +0800 Subject: [PATCH 16/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../src/versionmanager/version_manager.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index 06e62aac..8d12331c 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -263,6 +263,5 @@ void VersionManager::OnEvent(VersionInfoEvent &ev) break; } } - } // namespace DistributedHardware } // namespace OHOS -- Gitee From ba778e512e348e726b6065967a3df6f835a996ac Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 8 Jul 2022 21:36:08 +0800 Subject: [PATCH 17/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../test/unittest/common/componentloader/BUILD.gn | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn index 584675c7..d6109812 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn @@ -22,9 +22,11 @@ config("module_private_config") { include_dirs = [ "include", "${utils_path}/include", + "${utils_path}/include/eventbus", "${utils_path}/include/log", "${services_path}/distributedhardwarefwkserviceimpl/include", "${services_path}/distributedhardwarefwkserviceimpl/include/versionmanager", + "${services_path}/distributedhardwarefwkserviceimpl/include/resourcemanager", "${services_path}/distributedhardwarefwkserviceimpl/include/componentloader", "${services_path}/distributedhardwarefwkserviceimpl/include/utils", "${common_path}/utils/include", @@ -45,7 +47,11 @@ ohos_unittest("ComponentLoaderTest") { "//third_party/googletest:gtest_main", ] - external_deps = [ "hisysevent_native:libhisysevent" ] + external_deps = [ + "distributeddatamgr:distributeddata_inner", + "eventhandler:libeventhandler", + "hisysevent_native:libhisysevent", + ] defines = [ "HI_LOG_ENABLE", -- Gitee From ce46581c2e8b173ea1b9bf827bfb6b4b423c7c00 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 8 Jul 2022 21:41:37 +0800 Subject: [PATCH 18/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../test/unittest/common/componentloader/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn index d6109812..62b22927 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn @@ -47,7 +47,7 @@ ohos_unittest("ComponentLoaderTest") { "//third_party/googletest:gtest_main", ] - external_deps = [ + external_deps = [ "distributeddatamgr:distributeddata_inner", "eventhandler:libeventhandler", "hisysevent_native:libhisysevent", -- Gitee From 8da0e5ecc35c6712dac89cb0905bc34fe1f93171 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Tue, 12 Jul 2022 17:23:40 +0800 Subject: [PATCH 19/36] =?UTF-8?q?=20=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../BUILD.gn | 1 - .../resourcemanager/version_info_adapter.h | 3 - .../include/versionmanager/version_manager.h | 12 +- .../src/distributed_hardware_manager.cpp | 5 +- .../src/task/online_task.cpp | 6 +- .../src/versionmanager/version_manager.cpp | 225 ++++++++++++++---- 6 files changed, 189 insertions(+), 63 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/BUILD.gn b/services/distributedhardwarefwkserviceimpl/BUILD.gn index 8daed4b5..cf5f3fe2 100644 --- a/services/distributedhardwarefwkserviceimpl/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/BUILD.gn @@ -52,7 +52,6 @@ ohos_shared_library("distributedhardwarefwksvr_impl") { "src/resourcemanager/capability_info_manager.cpp", "src/resourcemanager/capability_utils.cpp", "src/resourcemanager/db_adapter.cpp", - "src/resourcemanager/version_info_adapter.cpp", "src/task/disable_task.cpp", "src/task/enable_task.cpp", "src/task/offline_task.cpp", diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_adapter.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_adapter.h index 7196e982..c740605f 100644 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_adapter.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_adapter.h @@ -42,9 +42,6 @@ public: void RemoveManualSyncCount(const std::string &deviceId); int32_t ManualSync(const std::string &networkId); -private: - void HandleVersionAddChange(const std::vector &insertRecords); - void HandleVersionUpdateChange(const std::vector &updateRecords); private: mutable std::mutex verAdapterMutex_; diff --git a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h index 91fc61eb..3ecade65 100644 --- a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h @@ -22,13 +22,13 @@ #include "kvstore_observer.h" -#include "single_instance.h" +#include "db_adapter.h" #include "eventbus_handler.h" #include "event_bus.h" #include "event_sender.h" #include "distributed_hardware_errno.h" #include "device_type.h" -#include "utils/impl_utils.h" +#include "impl_utils.h" #include "version_info_event.h" namespace OHOS { @@ -52,11 +52,16 @@ public: int32_t RemoveDHVersion(const std::string &uuid); int32_t GetDHVersion(const std::string &uuid, DHVersion &dhVersion); int32_t GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion); - int32_t SyncDHVersionFromDB(const std::string &uuid, DHVersion &dhVersion); + int32_t SyncDHVersionFromDB(const std::string &uuid); int32_t SyncRemoteVersionInfos(); std::string GetLocalDeviceVersion(); + int32_t AddLocalVersion(); void ShowLocalVersion(const DHVersion &dhVersion) const; + void CreateManualSyncCount(const std::string &deviceId); + void RemoveManualSyncCount(const std::string &deviceId); + int32_t ManualSync(const std::string &networkId); + void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; void OnEvent(VersionInfoEvent &ev) override; @@ -68,6 +73,7 @@ private: private: std::unordered_map dhVersions_; + std::shared_ptr dbAdapterPtr_; std::mutex versionMutex_; }; } // namespace DistributedHardware diff --git a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp index edd87fe4..7ee8f213 100644 --- a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp @@ -29,7 +29,6 @@ #include "task_board.h" #include "task_executor.h" #include "task_factory.h" -#include "version_info_adapter.h" #include "version_manager.h" namespace OHOS { @@ -114,7 +113,7 @@ int32_t DistributedHardwareManager::SendOnLineEvent(const std::string &networkId TaskExecutor::GetInstance().PushTask(task); DHContext::GetInstance().AddOnlineDevice(uuid, networkId); CapabilityInfoManager::GetInstance()->CreateManualSyncCount(GetDeviceIdByUUID(uuid)); - VersionInfoAdapter::GetInstance().CreateManualSyncCount(GetDeviceIdByUUID(uuid)); + VersionManager::GetInstance()->CreateManualSyncCount(GetDeviceIdByUUID(uuid)); return DH_FWK_SUCCESS; } @@ -156,7 +155,7 @@ int32_t DistributedHardwareManager::SendOffLineEvent(const std::string &networkI DHContext::GetInstance().RemoveOnlineDevice(realUUID); CapabilityInfoManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); - VersionInfoAdapter::GetInstance().RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); + VersionManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); HiSysEventWriteCompOfflineMsg(DHFWK_DEV_OFFLINE, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, GetAnonyString(networkId), "dhfwk device offline event."); diff --git a/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp b/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp index f33532b5..24728f83 100644 --- a/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp @@ -23,7 +23,6 @@ #include "task_board.h" #include "task_executor.h" #include "task_factory.h" -#include "version_info_adapter.h" #include "version_manager.h" namespace OHOS { @@ -76,7 +75,7 @@ void OnLineTask::DoSyncInfo() DHLOGW("ManualSync failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } - ret = VersionInfoAdapter::GetInstance().ManualSync(GetNetworkId()); + ret = VersionManager::GetInstance()->ManualSync(GetNetworkId()); if (ret != DH_FWK_SUCCESS) { DHLOGW("ManualSync version failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } @@ -86,8 +85,7 @@ void OnLineTask::DoSyncInfo() DHLOGE("SyncDeviceInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } - DHVersion dhVersion; - ret = VersionManager::GetInstance()->SyncDHVersionFromDB(GetUUID(), dhVersion); + ret = VersionManager::GetInstance()->SyncDHVersionFromDB(GetUUID()); if (ret != DH_FWK_SUCCESS) { DHLOGE("SyncVersionInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index 8d12331c..e921825d 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -13,14 +13,13 @@ * limitations under the License. */ -#include "versionmanager/version_manager.h" +#include "version_manager.h" #include "anonymous_string.h" -#include "componentloader/component_loader.h" +#include "component_loader.h" #include "dh_context.h" #include "dh_utils_tool.h" #include "distributed_hardware_log.h" -#include "version_info_adapter.h" namespace OHOS { namespace DistributedHardware { @@ -45,10 +44,34 @@ std::shared_ptr VersionManager::GetInstance() int32_t VersionManager::Init() { DHLOGI("start"); + { + std::lock_guard lock(versionMutex_); + dbAdapterPtr_ = std::make_shared(APP_ID, GLOBAL_VERSION_ID, shared_from_this()); + if (dbAdapterPtr_->Init() != DH_FWK_SUCCESS) { + DHLOGE("Init dbAdapterPtr_ failed"); + return ERR_DH_FWK_RESOURCE_INIT_DB_FAILED; + } + } + VersionInfoEvent versionInfoEvent(*this); DHContext::GetInstance().GetEventBus()->AddHandler(versionInfoEvent.GetType(), *this); - VersionInfoAdapter::GetInstance().Init(); + int32_t ret = AddLocalVersion(); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("AddLocalVersion fail"); + return ret; + } + + return DH_FWK_SUCCESS; +} + +void VersionManager::UnInit() +{ + DHLOGI("start"); + dhVersions_.clear(); +} +int32_t VersionManager::AddLocalVersion() +{ DHVersion dhVersion; int32_t ret = ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion); if (ret != DH_FWK_SUCCESS) { @@ -59,18 +82,14 @@ int32_t VersionManager::Init() dhVersion.uuid = DHContext::GetInstance().GetDeviceInfo().uuid; dhVersion.deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; ShowLocalVersion(dhVersion); - AddDHVersion(dhVersion.uuid, dhVersion); - VersionInfoAdapter::GetInstance().AddVersion(dhVersion); + ret = AddDHVersion(dhVersion.uuid, dhVersion); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("AddDHVersion fail"); + return ret; + } return DH_FWK_SUCCESS; } -void VersionManager::UnInit() -{ - DHLOGI("start"); - VersionInfoAdapter::GetInstance().UnInit(); - dhVersions_.clear(); -} - void VersionManager::ShowLocalVersion(const DHVersion &dhVersion) const { for (const auto &item : dhVersion.compVersions) { @@ -85,54 +104,102 @@ int32_t VersionManager::AddDHVersion(const std::string &uuid, const DHVersion &d DHLOGI("addDHVersion uuid: %s", GetAnonyString(uuid).c_str()); std::lock_guard lock(versionMutex_); dhVersions_[uuid] = dhVersion; + + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + + std::string data(""); + dbAdapterPtr_->GetDataByKey(dhVersion.deviceId, data); + if (data.compare(dhVersion.ToJsonString()) == 0) { + DHLOGI("dhversion already stored, Key: %s", GetAnonyString(dhVersion.deviceId).c_str()); + return DH_FWK_SUCCESS; + } + + std::string key = dhVersion.deviceId; + std::string value = dhVersion.ToJsonString(); + DHLOGI("add version to db, Key: %s", GetAnonyString(dhVersion.deviceId).c_str()); + if (dbAdapterPtr_->PutData(key, value) != DH_FWK_SUCCESS) { + DHLOGE("Fail to storage to kv"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } return DH_FWK_SUCCESS; } int32_t VersionManager::RemoveDHVersion(const std::string &uuid) { DHLOGI("removeDHVersion uuid: %s", GetAnonyString(uuid).c_str()); - { - std::lock_guard lock(versionMutex_); - std::unordered_map::iterator iter = dhVersions_.find(uuid); - if (iter == dhVersions_.end()) { - DHLOGE("there is no uuid: %s, remove fail", GetAnonyString(uuid).c_str()); - return ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST; - } + + std::lock_guard lock(versionMutex_); + std::unordered_map::iterator iter = dhVersions_.find(uuid); + if (iter == dhVersions_.end()) { + DHLOGE("there is no uuid: %s, remove in cache fail", GetAnonyString(uuid).c_str()); + } else { dhVersions_.erase(iter); } - VersionInfoAdapter::GetInstance().RemoveVersionInfoInDB(GetDeviceIdByUUID(uuid)); + + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + + if (dbAdapterPtr_->RemoveDeviceData(GetDeviceIdByUUID(uuid)) != DH_FWK_SUCCESS) { + DHLOGE("Remove version Device Data failed, deviceId: %s", + GetAnonyString(GetDeviceIdByUUID(uuid)).c_str()); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } return DH_FWK_SUCCESS; } int32_t VersionManager::GetDHVersion(const std::string &uuid, DHVersion &dhVersion) { DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); - { - std::lock_guard lock(versionMutex_); - std::unordered_map::iterator iter = dhVersions_.find(uuid); - if (iter != dhVersions_.end()) { - dhVersion = dhVersions_[uuid]; - return DH_FWK_SUCCESS; - } - DHLOGE("there is no uuid: %s in cache, get version fail", GetAnonyString(uuid).c_str()); + std::lock_guard lock(versionMutex_); + std::unordered_map::iterator iter = dhVersions_.find(uuid); + if (iter != dhVersions_.end()) { + dhVersion = dhVersions_[uuid]; + return DH_FWK_SUCCESS; } - int32_t ret = SyncDHVersionFromDB(uuid, dhVersion); - if (ret != DH_FWK_SUCCESS) { - DHLOGE("there is no uuid: %s in db, get version fail", GetAnonyString(uuid).c_str()); + DHLOGI("there is no uuid: %s in cache, get version in db", GetAnonyString(uuid).c_str()); + + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + std::string data(""); + if (dbAdapterPtr_->GetDataByKey(GetDeviceIdByUUID(uuid), data) != DH_FWK_SUCCESS) { + DHLOGE("Query data from DB by deviceId failed, deviceId: %s", + GetAnonyString(GetDeviceIdByUUID(uuid)).c_str()); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } - return ret; + + DHLOGI("Query data from DB by deviceId success, deviceId: %s", + GetAnonyString(GetDeviceIdByUUID(uuid)).c_str()); + dhVersion.FromJsonString(data); + return DH_FWK_SUCCESS; } -int32_t VersionManager::SyncDHVersionFromDB(const std::string &uuid, DHVersion &dhVersion) +int32_t VersionManager::SyncDHVersionFromDB(const std::string &uuid) { DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); - int32_t ret = VersionInfoAdapter::GetInstance().GetVersionInfoFromDB(GetDeviceIdByUUID(uuid), dhVersion); - if (ret != DH_FWK_SUCCESS) { - return ret; + std::lock_guard lock(versionMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; } - DHLOGI("Sync DHVersion from db success, uuid: %s", GetAnonyString(uuid).c_str()); - std::lock_guard lock(versionMutex_); + std::string data(""); + if (dbAdapterPtr_->GetDataByKey(GetDeviceIdByUUID(uuid), data) != DH_FWK_SUCCESS) { + DHLOGE("Query data from DB by deviceId failed, deviceId: %s", + GetAnonyString(GetDeviceIdByUUID(uuid)).c_str()); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + + DHLOGI("Query data from DB by deviceId success, deviceId: %s", + GetAnonyString(GetDeviceIdByUUID(uuid)).c_str()); + DHVersion dhVersion; + dhVersion.FromJsonString(data); dhVersions_[uuid] = dhVersion; return DH_FWK_SUCCESS; } @@ -140,16 +207,40 @@ int32_t VersionManager::SyncDHVersionFromDB(const std::string &uuid, DHVersion & int32_t VersionManager::SyncRemoteVersionInfos() { std::unordered_map dhVersions; - int32_t ret = VersionInfoAdapter::GetInstance().SyncRemoteVersionInfos(dhVersions); - if (ret != DH_FWK_SUCCESS) { - DHLOGI("Sync remote all DHVersions fail"); - return ret; + std::vector dataVector; + std::lock_guard lock(versionMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + + if (dbAdapterPtr_->GetDataByKeyPrefix("", dataVector) != DH_FWK_SUCCESS) { + DHLOGE("Query all data from DB failed"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } - std::lock_guard lock(versionMutex_); - for (auto dhVersion : dhVersions) { - dhVersions_[dhVersion.first] = dhVersion.second; + for (const auto &data : dataVector) { + DHVersion dhVersion; + dhVersion.FromJsonString(data); + const std::string &deviceId = dhVersion.deviceId; + const std::string &localDeviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; + if (deviceId.compare(localDeviceId) == 0) { + DHLOGE("local device info not need sync from db"); + continue; + } + if (!DHContext::GetInstance().IsDeviceOnline(deviceId)) { + DHLOGE("offline device, no need sync to memory, deviceId : %s ", + GetAnonyString(deviceId).c_str()); + continue; + } + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed, uuid: %s", uuid.c_str()); + continue; + } + dhVersions_[uuid] = dhVersion; } + return DH_FWK_SUCCESS; } @@ -176,6 +267,42 @@ std::string VersionManager::GetLocalDeviceVersion() return DH_LOCAL_VERSION; } + +void VersionManager::CreateManualSyncCount(const std::string &deviceId) +{ + std::lock_guard lock(versionMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return; + } + dbAdapterPtr_->CreateManualSyncCount(deviceId); +} + +void VersionManager::RemoveManualSyncCount(const std::string &deviceId) +{ + std::lock_guard lock(versionMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return; + } + dbAdapterPtr_->RemoveManualSyncCount(deviceId); +} + +int32_t VersionManager::ManualSync(const std::string &networkId) +{ + DHLOGI("ManualSync start, networkId: %s", GetAnonyString(networkId).c_str()); + std::unique_lock lock(versionMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + if (dbAdapterPtr_->ManualSync(networkId) != DH_FWK_SUCCESS) { + DHLOGE("ManualSync failed"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + return DH_FWK_SUCCESS; +} + void VersionManager::OnChange(const DistributedKv::ChangeNotification &changeNotification) { DHLOGI("VersionManager: DB data OnChange"); @@ -203,7 +330,7 @@ void VersionManager::HandleVersionAddChange(const std::vector Date: Tue, 12 Jul 2022 19:03:38 +0800 Subject: [PATCH 20/36] =?UTF-8?q?=20=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../common/versionmanager/src/version_manager_test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp index 895c17f9..2a2511fa 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp @@ -146,13 +146,13 @@ HWTEST_F(VersionManagerTest, version_manager_test_004, TestSize.Level0) HWTEST_F(VersionManagerTest, version_manager_test_005, TestSize.Level0) { int32_t ret = VersionManager::GetInstance()->RemoveDHVersion(TEST_DEVICE_ID_2); - EXPECT_EQ(DH_FWK_SUCCESS, ret); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); ret = VersionManager::GetInstance()->RemoveDHVersion(TEST_DEVICE_ID_4); - EXPECT_EQ(ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST, ret); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); ret = VersionManager::GetInstance()->RemoveDHVersion(TEST_DEVICE_ID_2); - EXPECT_EQ(ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST, ret); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); ret = VersionManager::GetInstance()->RemoveDHVersion(TEST_DEVICE_ID_1); - EXPECT_EQ(DH_FWK_SUCCESS, ret); + EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); } } // namespace DistributedHardware } // namespace OHOS -- Gitee From f8558081e70118be0bf9c2688c3d2a773d8073fa Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Tue, 12 Jul 2022 19:45:42 +0800 Subject: [PATCH 21/36] =?UTF-8?q?=20=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../src/versionmanager/version_manager.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index e921825d..f33d2a85 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -235,7 +235,7 @@ int32_t VersionManager::SyncRemoteVersionInfos() } std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); if (uuid.empty()) { - DHLOGI("Find uuid failed, uuid: %s", uuid.c_str()); + DHLOGI("Find uuid failed, deviceId: %s", GetAnonyString(deviceId).c_str()); continue; } dhVersions_[uuid] = dhVersion; @@ -267,7 +267,6 @@ std::string VersionManager::GetLocalDeviceVersion() return DH_LOCAL_VERSION; } - void VersionManager::CreateManualSyncCount(const std::string &deviceId) { std::lock_guard lock(versionMutex_); @@ -330,7 +329,7 @@ void VersionManager::HandleVersionAddChange(const std::vector Date: Tue, 12 Jul 2022 20:13:16 +0800 Subject: [PATCH 22/36] =?UTF-8?q?=20=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../resourcemanager/version_info_adapter.h | 52 ----- .../resourcemanager/version_info_adapter.cpp | 205 ------------------ 2 files changed, 257 deletions(-) delete mode 100644 services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_adapter.h delete mode 100644 services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_adapter.cpp diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_adapter.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_adapter.h deleted file mode 100644 index c740605f..00000000 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_adapter.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_ADAPTER_H -#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_ADAPTER_H - -#include -#include -#include - -#include "db_adapter.h" -#include "impl_utils.h" -#include "single_instance.h" - -class DBAdapter; -namespace OHOS { -namespace DistributedHardware { -class VersionInfoAdapter { -DECLARE_SINGLE_INSTANCE(VersionInfoAdapter); - -public: - int32_t Init(); - int32_t UnInit(); - - int32_t AddVersion(const DHVersion &version); - int32_t GetVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion); - int32_t SyncRemoteVersionInfos(std::unordered_map &dhVersions); - int32_t RemoveVersionInfoInDB(const std::string &deviceId); - void CreateManualSyncCount(const std::string &deviceId); - void RemoveManualSyncCount(const std::string &deviceId); - int32_t ManualSync(const std::string &networkId); - - -private: - mutable std::mutex verAdapterMutex_; - std::shared_ptr dbAdapterPtr_; -}; -} // namespace DistributedHardware -} // namespace OHOS -#endif diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_adapter.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_adapter.cpp deleted file mode 100644 index 46e4d6da..00000000 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_adapter.cpp +++ /dev/null @@ -1,205 +0,0 @@ -/* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "version_info_adapter.h" - -#include "anonymous_string.h" -#include "constants.h" -#include "dh_context.h" -#include "dh_utils_tool.h" -#include "distributed_hardware_errno.h" -#include "distributed_hardware_log.h" -#include "event_bus.h" -#include "task_executor.h" -#include "task_factory.h" -#include "version_info_event.h" -#include "version_manager.h" - -class DBAdapter; -namespace OHOS { -namespace DistributedHardware { -#undef DH_LOG_TAG -#define DH_LOG_TAG "VersionInfoAdapter" - -IMPLEMENT_SINGLE_INSTANCE(VersionInfoAdapter); - -int32_t VersionInfoAdapter::Init() -{ - DHLOGI("VersionInfoAdapter instance init!"); - std::lock_guard lock(verAdapterMutex_); - dbAdapterPtr_ = std::make_shared(APP_ID, GLOBAL_VERSION_ID, VersionManager::GetInstance()); - if (dbAdapterPtr_->Init() != DH_FWK_SUCCESS) { - DHLOGE("Init dbAdapterPtr_ failed"); - return ERR_DH_FWK_RESOURCE_INIT_DB_FAILED; - } - - DHLOGI("VersionInfoAdapter instance init success"); - return DH_FWK_SUCCESS; -} - -int32_t VersionInfoAdapter::UnInit() -{ - DHLOGI("VersionInfoAdapter UnInit"); - std::lock_guard lock(verAdapterMutex_); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return ERR_DH_FWK_RESOURCE_UNINIT_DB_FAILED; - } - dbAdapterPtr_->UnInit(); - dbAdapterPtr_.reset(); - return DH_FWK_SUCCESS; -} - -int32_t VersionInfoAdapter::AddVersion(const DHVersion &version) -{ - std::lock_guard lock(verAdapterMutex_); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; - } - - std::string data(""); - dbAdapterPtr_->GetDataByKey(version.deviceId, data); - if (data.compare(version.ToJsonString()) == 0) { - DHLOGI("dhversion already stored, Key: %s", GetAnonyString(version.deviceId).c_str()); - return DH_FWK_SUCCESS; - } - - std::string key = version.deviceId; - std::string value = version.ToJsonString(); - DHLOGI("AddVersion, Key: %s", GetAnonyString(version.deviceId).c_str()); - if (dbAdapterPtr_->PutData(key, value) != DH_FWK_SUCCESS) { - DHLOGE("Fail to storage to kv"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; - } - return DH_FWK_SUCCESS; -} - -int32_t VersionInfoAdapter::GetVersionInfoFromDB(const std::string &deviceId, DHVersion &dhVersion) -{ - DHLOGI("Sync VersionInfo from DB, deviceId: %s", GetAnonyString(deviceId).c_str()); - std::lock_guard lock(verAdapterMutex_); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; - } - std::string data(""); - if (dbAdapterPtr_->GetDataByKey(deviceId, data) != DH_FWK_SUCCESS) { - DHLOGE("Query data from DB by deviceId failed, deviceId: %s", GetAnonyString(deviceId).c_str()); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; - } - - DHLOGI("Query data from DB by deviceId success, deviceId: %s", GetAnonyString(deviceId).c_str()); - dhVersion.FromJsonString(data); - return DH_FWK_SUCCESS; -} - -int32_t VersionInfoAdapter::SyncRemoteVersionInfos(std::unordered_map &dhVersions) -{ - DHLOGI("Sync full remote version info from DB"); - std::vector dataVector; - { - std::lock_guard lock(verAdapterMutex_); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; - } - - if (dbAdapterPtr_->GetDataByKeyPrefix("", dataVector) != DH_FWK_SUCCESS) { - DHLOGE("Query all data from DB failed"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; - } - } - - for (const auto &data : dataVector) { - DHVersion dhVersion; - dhVersion.FromJsonString(data); - const std::string &deviceId = dhVersion.deviceId; - const std::string &localDeviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; - if (deviceId.compare(localDeviceId) == 0) { - DHLOGE("local device info not need sync from db"); - continue; - } - if (!DHContext::GetInstance().IsDeviceOnline(deviceId)) { - DHLOGE("offline device, no need sync to memory, deviceId : %s ", - GetAnonyString(deviceId).c_str()); - continue; - } - std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); - if (uuid.empty()) { - DHLOGI("Find uuid failed"); - continue; - } - dhVersions.insert(std::pair(uuid, dhVersion)); - } - return DH_FWK_SUCCESS; -} - -int32_t VersionInfoAdapter::RemoveVersionInfoInDB(const std::string &deviceId) -{ - DHLOGI("Remove capability device info, deviceId: %s", GetAnonyString(deviceId).c_str()); - std::lock_guard lock(verAdapterMutex_); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; - } - if (deviceId.empty()) { - DHLOGE("RemoveCapabilityInfoInDB failed, deviceId is empty"); - return ERR_DH_FWK_PARA_INVALID; - } - - if (dbAdapterPtr_->RemoveDeviceData(deviceId) != DH_FWK_SUCCESS) { - DHLOGE("Remove version Device Data failed, deviceId: %s", GetAnonyString(deviceId).c_str()); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; - } - return DH_FWK_SUCCESS; -} - -void VersionInfoAdapter::CreateManualSyncCount(const std::string &deviceId) -{ - std::lock_guard lock(verAdapterMutex_); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return; - } - dbAdapterPtr_->CreateManualSyncCount(deviceId); -} - -void VersionInfoAdapter::RemoveManualSyncCount(const std::string &deviceId) -{ - std::lock_guard lock(verAdapterMutex_); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return; - } - dbAdapterPtr_->RemoveManualSyncCount(deviceId); -} - -int32_t VersionInfoAdapter::ManualSync(const std::string &networkId) -{ - DHLOGI("ManualSync start, networkId: %s", GetAnonyString(networkId).c_str()); - std::unique_lock lock(verAdapterMutex_); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; - } - if (dbAdapterPtr_->ManualSync(networkId) != DH_FWK_SUCCESS) { - DHLOGE("ManualSync failed"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; - } - return DH_FWK_SUCCESS; -} -} // namespace DistributedHardware -} // namespace OHOS \ No newline at end of file -- Gitee From 155ed1eace63d9021c87ada42e4a73b8e3ec062e Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Wed, 13 Jul 2022 09:10:13 +0800 Subject: [PATCH 23/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../include/versionmanager/version_manager.h | 2 +- .../src/versionmanager/version_manager.cpp | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h index 3ecade65..5a1bacd5 100644 --- a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h @@ -66,7 +66,7 @@ public: void OnEvent(VersionInfoEvent &ev) override; private: - VersionManager() = default; + VersionManager(); void HandleVersionAddChange(const std::vector &insertRecords); void HandleVersionUpdateChange(const std::vector &updateRecords); void HandleVersionDeleteChange(const std::vector &deleteRecords); diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index f33d2a85..559444ff 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -26,6 +26,9 @@ namespace DistributedHardware { #undef DH_LOG_TAG #define DH_LOG_TAG "VersionManager" +VersionManager::VersionManager() : dbAdapterPtr_(nullptr) +{} + VersionManager::~VersionManager() { DHLOGI("VersionManager Destruction!"); -- Gitee From b53cd219598b3aac86a998a3df6113fc416a24be Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 15 Jul 2022 15:50:37 +0800 Subject: [PATCH 24/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../include/distributed_hardware_errno.h | 1 + .../componentmanager/component_manager.h | 7 +- .../include/resourcemanager/version_info.h | 38 ++ .../resourcemanager/version_info_manager.h | 76 ++++ .../include/utils/impl_utils.h | 9 - .../include/versionmanager/version_manager.h | 33 +- .../src/componentloader/component_loader.cpp | 25 +- .../componentmanager/component_manager.cpp | 132 +++---- .../src/distributed_hardware_manager.cpp | 12 +- .../version_info.cpp} | 34 +- .../resourcemanager/version_info_manager.cpp | 326 ++++++++++++++++++ .../src/task/online_task.cpp | 6 +- .../src/versionmanager/version_manager.cpp | 302 +--------------- 13 files changed, 593 insertions(+), 408 deletions(-) create mode 100644 services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h create mode 100644 services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h rename services/distributedhardwarefwkserviceimpl/src/{utils/impl_utils.cpp => resourcemanager/version_info.cpp} (68%) create mode 100644 services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index 96a88bfe..6a5fa0a7 100644 --- a/common/utils/include/distributed_hardware_errno.h +++ b/common/utils/include/distributed_hardware_errno.h @@ -57,6 +57,7 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_RESOURCE_REGISTER_DB_FAILED = -10408; constexpr int32_t ERR_DH_FWK_RESOURCE_UNREGISTER_DB_FAILED = -10409; constexpr int32_t ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY = -10410; + constexpr int32_t ERR_DH_FWK_RESOURCE_SYNC_VERSIONINFO_FAIL = -10411; /* DistributedHardwareManager errno, range: [-10500, -10599] */ constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_ONLINE = -10500; diff --git a/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h b/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h index 68391247..6eff855d 100644 --- a/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h @@ -68,16 +68,15 @@ private: int32_t GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, DHType dhType, EnableParam ¶m); std::string GetSinkVersionFromVerMgr(const std::string &uuid, const DHType dhType); + std::string GetSinkVersionFromVerInfoMgr(const std::string &uuid, const DHType dhType); + std::string GetSinkVersionFromRPC(const std::string &networkId, const std::string &uuid, DHType dhType) std::string GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType); - std::string GetVersionFromCache(const std::string &uuid, DHType dhType); - int32_t UpdateVersionCache(const std::string &networkId, const std::string &uuid); + void UpdateVersionCache(const std::string &uuid, const std::unordered_map &versions) sptr GetRemoteDHMS(const std::string &networkId) const; private: std::map compSource_; std::map compSink_; - std::unordered_map> sinkVersions_; - std::mutex sinkVersionMutex_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h new file mode 100644 index 00000000..a8e45167 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_H +#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_H + +#include "nlohmann/json.hpp" + +#include "impl_utils.h" + +namespace OHOS { +namespace DistributedHardware { +struct VersionInfo { + std::string deviceId; + std::string dhVersion; + std::unordered_map compVersions; + + void FromJsonString(const std::string &jsonStr); + std::string ToJsonString() const; +}; + +void ToJson(nlohmann::json &jsonObject, const VersionInfo &dhVersion); +void FromJson(const nlohmann::json &jsonObject, VersionInfo &dhVersion); +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h new file mode 100644 index 00000000..a25acecf --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_H +#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_H + +#include +#include +#include + +#include "kvstore_observer.h" + +#include "db_adapter.h" +#include "event.h" +#include "eventbus_handler.h" +#include "event_bus.h" +#include "event_sender.h" +#include "impl_utils.h" +#include "single_instance.h" +#include "version_info.h" +#include "version_info_event.h" + +class DBAdapter; +namespace OHOS { +namespace DistributedHardware { +class VersionInfoManager : public std::enable_shared_from_this, + public EventSender, + public DistributedKv::KvStoreObserver, + public EventBusHandler { +public: + VersionInfoManager(const VersionInfoManager &) = delete; + VersionInfoManager &operator = (const VersionInfoManager &) = delete; + VersionInfoManager(VersionInfoManager &&) = delete; + VersionInfoManager &operator = (VersionInfoManager &&) = delete; + static std::shared_ptr GetInstance(); + virtual ~VersionInfoManager(); + + int32_t Init(); + int32_t UnInit(); + + int32_t AddVersion(const VersionInfo &versionInfo); + int32_t SyncVersionInfoFromDB(const std::string &deviceId); + int32_t SyncRemoteVersionInfos(); + + void CreateManualSyncCount(const std::string &deviceId); + void RemoveManualSyncCount(const std::string &deviceId); + int32_t ManualSync(const std::string &networkId); + + void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; + void OnEvent(VersionInfoEvent &ev) override; + +private: + VersionManager(); + void HandleVersionAddChange(const std::vector &insertRecords); + void HandleVersionUpdateChange(const std::vector &updateRecords); + void HandleVersionDeleteChange(const std::vector &deleteRecords); + +private: + mutable std::mutex verInfoMgrMutex_; + std::shared_ptr dbAdapterPtr_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h b/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h index f9d1ef63..632505cd 100644 --- a/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h +++ b/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h @@ -18,8 +18,6 @@ #include -#include "nlohmann/json.hpp" - #include "device_type.h" #include "constants.h" @@ -60,17 +58,10 @@ struct CompVersion { struct DHVersion { std::string uuid; - std::string deviceId; std::string dhVersion; std::unordered_map compVersions; - - void FromJsonString(const std::string &jsonStr); - std::string ToJsonString() const; }; -void ToJson(nlohmann::json &jsonObject, const DHVersion &dhVersion); -void FromJson(const nlohmann::json &jsonObject, DHVersion &dhVersion); - struct TaskParam { std::string networkId; std::string uuid; diff --git a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h index 5a1bacd5..c5fd2d39 100644 --- a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h @@ -34,17 +34,11 @@ namespace OHOS { namespace DistributedHardware { const std::string DH_LOCAL_VERSION = "1.0"; -class VersionManager : public std::enable_shared_from_this, - public EventSender, - public DistributedKv::KvStoreObserver, - public EventBusHandler { + DECLARE_SINGLE_INSTANCE_BASE(VersionManager); + public: - VersionManager(const VersionManager &) = delete; - VersionManager &operator = (const VersionManager &) = delete; - VersionManager(VersionManager &&) = delete; - VersionManager &operator = (VersionManager &&) = delete; - static std::shared_ptr GetInstance(); - virtual ~VersionManager(); + VersionManager() {} + ~VersionManager() {} int32_t Init(); void UnInit(); @@ -52,30 +46,13 @@ public: int32_t RemoveDHVersion(const std::string &uuid); int32_t GetDHVersion(const std::string &uuid, DHVersion &dhVersion); int32_t GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion); - int32_t SyncDHVersionFromDB(const std::string &uuid); - int32_t SyncRemoteVersionInfos(); + int32_t SyncDHVersionFromDB(const std::string &uuid, DHVersion &dhVersion); std::string GetLocalDeviceVersion(); - int32_t AddLocalVersion(); void ShowLocalVersion(const DHVersion &dhVersion) const; - void CreateManualSyncCount(const std::string &deviceId); - void RemoveManualSyncCount(const std::string &deviceId); - int32_t ManualSync(const std::string &networkId); - - void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; - void OnEvent(VersionInfoEvent &ev) override; - -private: - VersionManager(); - void HandleVersionAddChange(const std::vector &insertRecords); - void HandleVersionUpdateChange(const std::vector &updateRecords); - void HandleVersionDeleteChange(const std::vector &deleteRecords); - private: std::unordered_map dhVersions_; - std::shared_ptr dbAdapterPtr_; std::mutex versionMutex_; -}; } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp index b67e032a..b76270aa 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp @@ -22,10 +22,14 @@ #include "nlohmann/json.hpp" #include "constants.h" +#include "dh_context.h" #include "dh_utils_hitrace.h" -#include "distributed_hardware_log.h" #include "dh_utils_hisysevent.h" #include "hidump_helper.h" +#include "distributed_hardware_log.h" +#include "version_info.h" +#include "version_info_manager.h" +#include "version_manager.h" using nlohmann::json; @@ -72,6 +76,7 @@ int32_t ComponentLoader::Init() int32_t ret = ParseConfig(); DHTraceEnd(); + StoreLocalDHVersionToDB(); return ret; } @@ -137,6 +142,19 @@ int32_t ComponentLoader::GetLocalDHVersion(DHVersion &dhVersion) return DH_FWK_SUCCESS; } +void StoreLocalDHVersionToDB() +{ + if (!isLocalVersionInit_.load()) { + DHLOGE("Store local DHVersion fail"); + return; + } + VersionInfo versionInfo; + versionInfo.dhVersion = VersionManager::GetInstance().GetLocalDeviceVersion(); + versionInfo.deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; + versionInfo.compVersions = localDHVersion_.compVersions; + VersionInfoManager::GetInstance()->AddVersion(versionInfo); +} + void *ComponentLoader::GetHandler(const std::string &soName) { if (soName.length() <= 0) { @@ -257,8 +275,11 @@ int32_t ComponentLoader::ParseConfig() return ERR_DH_FWK_LOADER_COMPONENT_PROFILE_IS_EMPTY; } ret = GetCompPathAndVersion(jsonStr, dhtypeMap); + if (ret != DH_FWK_SUCCESS) { + return ret; + } GetAllHandler(dhtypeMap); - return ret; + return DH_FWK_SUCCESS; } int32_t ComponentLoader::ReleaseHandler(void *&handler) diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index 8cb9291b..82533e32 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -104,10 +104,7 @@ int32_t ComponentManager::UnInit() compSource_.clear(); compSink_.clear(); - { - std::lock_guard lock(sinkVersionMutex_); - sinkVersions_.clear(); - } + DHLOGI("Release component success"); return DH_FWK_SUCCESS; } @@ -119,7 +116,7 @@ ComponentManager::ActionResult ComponentManager::StartSource() std::string uuid = DHContext::GetInstance().GetDeviceInfo().uuid; for (const auto &item : compSource_) { CompVersion compversion; - VersionManager::GetInstance()->GetCompVersion(uuid, item.first, compversion); + VersionManager::GetInstance().GetCompVersion(uuid, item.first, compversion); auto params = compversion.sourceVersion; auto future = std::async(std::launch::async, [item, params]() { return item.second->InitSource(params); }); futures.emplace(item.first, future.share()); @@ -134,7 +131,7 @@ ComponentManager::ActionResult ComponentManager::StartSink() std::string uuid = DHContext::GetInstance().GetDeviceInfo().uuid; for (const auto &item : compSink_) { CompVersion compversion; - VersionManager::GetInstance()->GetCompVersion(uuid, item.first, compversion); + VersionManager::GetInstance().GetCompVersion(uuid, item.first, compversion); auto params = compversion.sinkVersion; auto future = std::async(std::launch::async, [item, params]() { return item.second->InitSink(params); }); futures.emplace(item.first, future.share()); @@ -339,76 +336,56 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std } param.attrs = capability->GetDHAttrs(); - param.version = GetSinkVersionFromVerMgr(uuid, dhType); - if (!param.version.empty()) { - DHLOGI("Get sink version from Version Mgr success. uuid = %s, dhId = %s, dhType = %#X, version = %s", - GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), dhType, param.version.c_str()); - return DH_FWK_SUCCESS; - } - param.version = GetSinkVersion(networkId, uuid, dhType); - if (!param.version.empty()) { - DHLOGI("Get sink version from rpc success. uuid = %s, dhId = %s, dhType = %#X, version = %s", - GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), dhType, param.version.c_str()); - return DH_FWK_SUCCESS; + if (param.version.empty()) { + DHLOGE("Get Sink Version failed, uuid = %s, dhId = %s, dhType = %#X,", GetAnonyString(uuid).c_str(), + GetAnonyString(dhId).c_str(), dhType); + return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED; } - DHLOGE("Get Sink Version failed, uuid = %s, dhId = %s, dhType = %#X,", GetAnonyString(uuid).c_str(), - GetAnonyString(dhId).c_str(), dhType); + DHLOGI("success. uuid =%s, dhId = %s, version = %s", GetAnonyString(uuid).c_str(), + GetAnonyString(dhId).c_str(), param.version.c_str()); + return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED; } std::string ComponentManager::GetSinkVersionFromVerMgr(const std::string &uuid, const DHType dhType) { CompVersion compversion; - auto ret = VersionManager::GetInstance()->GetCompVersion(uuid, dhType, compversion); + int32_t ret = VersionManager::GetInstance().GetCompVersion(uuid, dhType, compversion); if (ret != DH_FWK_SUCCESS) { - DHLOGE("Get version from Version Manager failed, uuid =%s, dhType = %#X, errCode = %d", + DHLOGE("Get sink version from Version Manager failed, uuid =%s, dhType = %#X, errCode = %d", GetAnonyString(uuid).c_str(), dhType, ret); return ""; } + DHLOGI("Get SinkVersion from version mgr success, sinkVersion = %s, uuid = %s, dhType = %#X", + compversion.sinkVersion, GetAnonyString(uuid).c_str(), dhType); return compversion.sinkVersion; } -std::string ComponentManager::GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType) -{ - DHLOGI("networkId = %s ", GetAnonyString(networkId).c_str()); - auto sinkVersion = GetVersionFromCache(uuid, dhType); - if (!sinkVersion.empty()) { - DHLOGI("GetVersionFromCache success, sinkVersion = %s, uuid = %s, dhType = %#X", sinkVersion.c_str(), - GetAnonyString(uuid).c_str(), dhType); - return sinkVersion; - } - - auto updateResult = UpdateVersionCache(networkId, uuid); - if (updateResult != DH_FWK_SUCCESS) { - DHLOGE("UpdateVersionCache failed, uuid = %s, errCode = %d", GetAnonyString(uuid).c_str(), updateResult); - return ""; - } - - sinkVersion = GetVersionFromCache(uuid, dhType); - return sinkVersion; -} - -std::string ComponentManager::GetVersionFromCache(const std::string &uuid, DHType dhType) +std::string ComponentManager::GetSinkVersionFromVerInfoMgr(const std::string &uuid, const DHType dhType) { - std::lock_guard lock(sinkVersionMutex_); - auto iter = sinkVersions_.find(uuid); - if (iter == sinkVersions_.end()) { - DHLOGE("can not find component version for uuid = %s", GetAnonyString(uuid).c_str()); + VersionInfo versionInfo; + int32_t ret = VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(GetDeviceIdByUUID(uuid), versionInfo); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Get sink version from Version info Manager failed, uuid =%s, dhType = %#X, errCode = %d", + GetAnonyString(uuid).c_str(), dhType, ret); return ""; } - - auto find = iter->second.find(dhType); - if (find == iter->second.end()) { - DHLOGE("can not find component version for uuid = %s, dhType = %#X", uuid.c_str(), dhType); + auto iter = versionInfo.compVersions.find(dhType); + if (iter == versionInfo.compVersions.end()) { + DHLOGE("can not find component version for dhType = %d", dhType); return ""; } - return find->second; + DHLOGI("Get SinkVersion from version info mgr success, sinkVersion = %s, uuid = %s, dhType = %#X", + iter->second.sinkVersion, GetAnonyString(uuid).c_str(), dhType); + return iter->second.sinkVersion; } -int32_t ComponentManager::UpdateVersionCache(const std::string &networkId, const std::string &uuid) +std::string ComponentManager::GetSinkVersionFromRPC(const std::string &networkId, const std::string &uuid, DHType dhType) { + DHLOGI("networkId = %s ", GetAnonyString(networkId).c_str()); + sptr dhms = GetRemoteDHMS(networkId); if (dhms == nullptr) { DHLOGI("GetRemoteDHMS failed, networkId = %s", GetAnonyString(networkId).c_str()); @@ -419,14 +396,53 @@ int32_t ComponentManager::UpdateVersionCache(const std::string &networkId, const auto ret = dhms->QuerySinkVersion(versions); if (ret != DH_FWK_SUCCESS) { DHLOGE("QuerySinkVersion failed, errCode = %d", ret); - return ret; + return ""; } - { - std::lock_guard lock(sinkVersionMutex_); - sinkVersions_.emplace(uuid, versions); + + auto iter = versions.find(dhType); + if (iter == versions.end()) { + DHLOGE("can not find component version for uuid = %s, dhType = %#X", + GetAnonyString(uuid).c_str(), dhType); + return ""; } - DHLOGI("QuerySinkVersion success"); - return DH_FWK_SUCCESS; + DHLOGI("QuerySinkVersion success, sinkVersion = %s, uuid = %s, dhType = %#X", + iter->second, GetAnonyString(uuid).c_str(), dhType); + UpdateVersionCache(uuid, versions); + return iter->second; +} + +std::string ComponentManager::GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType) +{ + std::string sinkVersion(""); + sinkVersion = GetSinkVersionFromVerMgr(uuid, dhType); + if (!sinkVersion.empty()) { + return sinkVersion; + } + + sinkVersion = GetSinkVersionFromVerInfoMgr(uuid, dhType); + if (!sinkVersion.empty()) { + return sinkVersion; + } + + sinkVersion = GetSinkVersionFromRPC(networkId, uuid, dhType); + if (!sinkVersion.empty()) { + return sinkVersion; + } + + return ""; +} + +void ComponentManager::UpdateVersionCache(const std::string &uuid, const std::unordered_map &versions) +{ + DHVersion dhVersion; + dhVersion.uuid = uuid; + for (auto versionPair : versions) { + CompVersion compVersion; + compVersion.dhType = versionPair.first; + compVersion.sinkVersion = versionPair.second; + dhVersion.compVersions.insert(std::pair(compVersion.dhType, compVersion); + } + VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); } sptr ComponentManager::GetRemoteDHMS(const std::string &networkId) const diff --git a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp index 7ee8f213..291595f5 100644 --- a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp @@ -54,11 +54,13 @@ int32_t DistributedHardwareManager::Initialize() DHLOGI("start"); CapabilityInfoManager::GetInstance()->Init(); + VersionInfoManager::GetInstance()->Init(); + ComponentLoader::GetInstance().Init(); LocalHardwareManager::GetInstance().Init(); - VersionManager::GetInstance()->Init(); + VersionManager::GetInstance().Init(); ComponentManager::GetInstance().Init(); @@ -72,12 +74,14 @@ int32_t DistributedHardwareManager::Release() ComponentManager::GetInstance().UnInit(); - VersionManager::GetInstance()->UnInit(); + VersionManager::GetInstance().UnInit(); LocalHardwareManager::GetInstance().UnInit(); ComponentLoader::GetInstance().UnInit(); + VersionInfoManager::GetInstance()->UnInit(); + CapabilityInfoManager::GetInstance()->UnInit(); return DH_FWK_SUCCESS; @@ -113,7 +117,7 @@ int32_t DistributedHardwareManager::SendOnLineEvent(const std::string &networkId TaskExecutor::GetInstance().PushTask(task); DHContext::GetInstance().AddOnlineDevice(uuid, networkId); CapabilityInfoManager::GetInstance()->CreateManualSyncCount(GetDeviceIdByUUID(uuid)); - VersionManager::GetInstance()->CreateManualSyncCount(GetDeviceIdByUUID(uuid)); + VersionInfoManager::GetInstance()->CreateManualSyncCount(GetDeviceIdByUUID(uuid)); return DH_FWK_SUCCESS; } @@ -155,7 +159,7 @@ int32_t DistributedHardwareManager::SendOffLineEvent(const std::string &networkI DHContext::GetInstance().RemoveOnlineDevice(realUUID); CapabilityInfoManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); - VersionManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); + VersionInofManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); HiSysEventWriteCompOfflineMsg(DHFWK_DEV_OFFLINE, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, GetAnonyString(networkId), "dhfwk device offline event."); diff --git a/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp similarity index 68% rename from services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp rename to services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp index 649eb947..d017cc02 100644 --- a/services/distributedhardwarefwkserviceimpl/src/utils/impl_utils.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp @@ -13,32 +13,40 @@ * limitations under the License. */ -#include "impl_utils.h" +#include "version_info.h" -#include "dh_utils_tool.h" +#include + +#include "nlohmann/json.hpp" + +#include "anonymous_string.h" +#include "constants.h" +#include "distributed_hardware_errno.h" namespace OHOS { namespace DistributedHardware { -void DHVersion::FromJsonString(const std::string &jsonStr) +#undef DH_LOG_TAG +#define DH_LOG_TAG "VersionInfo" +void VersionInfo::FromJsonString(const std::string &jsonStr) { nlohmann::json jsonObj = nlohmann::json::parse(jsonStr); FromJson(jsonObj, *this); } -std::string DHVersion::ToJsonString() const +std::string VersionInfo::ToJsonString() const { nlohmann::json jsonObj; ToJson(jsonObj, *this); return jsonObj.dump(); } -void ToJson(nlohmann::json &jsonObject, const DHVersion &dhVersion) +void ToJson(nlohmann::json &jsonObject, const VersionInfo &dhVersionInfo) { - jsonObject[DEV_ID] = dhVersion.deviceId; - jsonObject[DH_VER] = dhVersion.dhVersion; + jsonObject[DEV_ID] = dhVersionInfo.deviceId; + jsonObject[DH_VER] = dhVersionInfo.dhVersion; nlohmann::json compVers; - for (const auto &compVersion : dhVersion.compVersions) { + for (const auto &compVersion : dhVersionInfo.compVersions) { nlohmann::json compVer; compVer[NAME] = compVersion.second.name; compVer[TYPE] = compVersion.second.dhType; @@ -51,14 +59,14 @@ void ToJson(nlohmann::json &jsonObject, const DHVersion &dhVersion) jsonObject[COMP_VER] = compVers; } -void FromJson(const nlohmann::json &jsonObject, DHVersion &dhVersion) +void FromJson(const nlohmann::json &jsonObject, VersionInfo &dhVersionInfo) { if (jsonObject.find(DEV_ID) != jsonObject.end()) { - dhVersion.deviceId = jsonObject.at(DEV_ID).get(); + dhVersionInfo.deviceId = jsonObject.at(DEV_ID).get(); } if (jsonObject.find(DH_VER) != jsonObject.end()) { - dhVersion.dhVersion = jsonObject.at(DH_VER).get(); + dhVersionInfo.dhVersion = jsonObject.at(DH_VER).get(); } if (jsonObject.find(COMP_VER) != jsonObject.end()) { @@ -69,9 +77,9 @@ void FromJson(const nlohmann::json &jsonObject, DHVersion &dhVersion) compVer.handlerVersion = compVerObj.at(HANDLER).get(); compVer.sourceVersion = compVerObj.at(SOURCE_VER).get(); compVer.sinkVersion = compVerObj.at(SINK_VER).get(); - dhVersion.compVersions.insert(std::pair(compVer.dhType, compVer)); + dhVersionInfo.compVersions.insert(std::pair(compVer.dhType, compVer)); } } } } // namespace DistributedHardware -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp new file mode 100644 index 00000000..0bd6e51c --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp @@ -0,0 +1,326 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "version_info_manager.h" + +#include "anonymous_string.h" +#include "constants.h" +#include "dh_context.h" +#include "dh_utils_tool.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "event_bus.h" +#include "task_executor.h" +#include "task_factory.h" +#include "version_info_event.h" +#include "version_manager.h" + +class DBAdapter; +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "VersionInfoManager" + +VersionInfoManager::VersionInfoManager() : dbAdapterPtr_(nullptr) +{} + +VersionInfoManager::~VersionInfoManager() +{ + DHLOGI("VersionInfoManager Destruction!"); +} + +std::shared_ptr VersionInfoManager::GetInstance() +{ + static std::shared_ptr instance(new(std::nothrow) VersionInfoManager); + if (instance == nullptr) { + DHLOGE("instance is nullptr, because applying memory fail!"); + return nullptr; + } + return instance; +} + +int32_t VersionInfoManager::Init() +{ + DHLOGI("VersionInfoManager instance init!"); + std::lock_guard lock(verInfoMgrMutex_); + dbAdapterPtr_ = std::make_shared(APP_ID, GLOBAL_VERSION_ID, shared_from_this()); + if (dbAdapterPtr_->Init() != DH_FWK_SUCCESS) { + DHLOGE("Init dbAdapterPtr_ failed"); + return ERR_DH_FWK_RESOURCE_INIT_DB_FAILED; + } + VersionInfoEvent versionInfoEvent(*this); + DHContext::GetInstance().GetEventBus()->AddHandler(versionInfoEvent.GetType(), *this); + DHLOGI("VersionInfoManager instance init success"); + return DH_FWK_SUCCESS; +} + +int32_t VersionInfoManager::UnInit() +{ + DHLOGI("VersionInfoManager UnInit"); + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_UNINIT_DB_FAILED; + } + dbAdapterPtr_->UnInit(); + dbAdapterPtr_.reset(); + return DH_FWK_SUCCESS; +} + +int32_t VersionInfoManager::AddVersion(const VersionInfo &versionInfo) +{ + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + + std::string data(""); + dbAdapterPtr_->GetDataByKey(versionInfo.deviceId, data); + if (data.compare(versionInfo.ToJsonString()) == 0) { + DHLOGI("dhversion already stored, Key: %s", GetAnonyString(versionInfo.deviceId).c_str()); + return DH_FWK_SUCCESS; + } + + std::string key = versionInfo.deviceId; + std::string value = versionInfo.ToJsonString(); + DHLOGI("AddVersion, Key: %s", GetAnonyString(versionInfo.deviceId).c_str()); + if (dbAdapterPtr_->PutData(key, value) != DH_FWK_SUCCESS) { + DHLOGE("Fail to storage to kv"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + return DH_FWK_SUCCESS; +} + +int32_t VersionInfoManager::GetVersionInfoByDeviceId(const std::string &deviceId, VersionInfo &versionInfo) +{ + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + std::string data(""); + if (dbAdapterPtr_->GetDataByKey(deviceId, data) != DH_FWK_SUCCESS) { + DHLOGE("Query data from DB by deviceId failed, deviceId: %s", GetAnonyString(deviceId).c_str()); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + versionInfo.FromJsonString(data); + return DH_FWK_SUCCESS; +} + + +int32_t VersionInfoManager::SyncVersionInfoFromDB(const std::string &deviceId) +{ + DHLOGI("Sync VersionInfo from DB, deviceId: %s", GetAnonyString(deviceId).c_str()); + std::lock_guard lock(verInfoMgrMutex_); + { + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + std::string data(""); + if (dbAdapterPtr_->GetDataByKey(deviceId, data) != DH_FWK_SUCCESS) { + DHLOGE("Query data from DB by deviceId failed, deviceId: %s", GetAnonyString(deviceId).c_str()); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + } + DHLOGI("Query data from DB by deviceId success, deviceId: %s", GetAnonyString(deviceId).c_str()); + + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed"); + return ERR_DH_FWK_RESOURCE_SYNC_VERSIONINFO_FAIL; + } + VersionInfo versionInfo + versionInfo.FromJsonString(data); + DHVersion dhVersion; + dhVersion.uuid = uuid; + dhVersion.dhVersion = versionInfo.dhVersion; + dhVersion.compVersions = versionInfo.compVersions; + VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); + return DH_FWK_SUCCESS; +} + +int32_t VersionInfoManager::SyncRemoteVersionInfos() +{ + DHLOGI("Sync full remote version info from DB"); + std::vector dataVector; + std::lock_guard lock(verInfoMgrMutex_); + { + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + if (dbAdapterPtr_->GetDataByKeyPrefix("", dataVector) != DH_FWK_SUCCESS) { + DHLOGE("Query all data from DB failed"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + } + + for (const auto &data : dataVector) { + VersionInfo versionInfo; + versionInfo.FromJsonString(data); + const std::string &deviceId = versionInfo.deviceId; + const std::string &localDeviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; + if (deviceId.compare(localDeviceId) == 0) { + DHLOGE("local device info not need sync from db"); + continue; + } + if (!DHContext::GetInstance().IsDeviceOnline(deviceId)) { + DHLOGE("offline device, no need sync to memory, deviceId : %s ", + GetAnonyString(deviceId).c_str()); + continue; + } + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed"); + continue; + } + DHVersion dhVersion; + dhVersion.uuid = uuid; + dhVersion.dhVersion = versionInfo.dhVersion; + dhVersion.compVersions = versionInfo.compVersions; + VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); + } + return DH_FWK_SUCCESS; +} + +void VersionInfoManager::CreateManualSyncCount(const std::string &deviceId) +{ + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return; + } + dbAdapterPtr_->CreateManualSyncCount(deviceId); +} + +void VersionInfoManager::RemoveManualSyncCount(const std::string &deviceId) +{ + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return; + } + dbAdapterPtr_->RemoveManualSyncCount(deviceId); +} + +int32_t VersionInfoManager::ManualSync(const std::string &networkId) +{ + DHLOGI("ManualSync start, networkId: %s", GetAnonyString(networkId).c_str()); + std::unique_lock lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + if (dbAdapterPtr_->ManualSync(networkId) != DH_FWK_SUCCESS) { + DHLOGE("ManualSync failed"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + return DH_FWK_SUCCESS; +} + +void VersionInfoManager::OnChange(const DistributedKv::ChangeNotification &changeNotification) +{ + DHLOGI("VersionInfoManager: DB data OnChange"); + if (!changeNotification.GetInsertEntries().empty()) { + DHLOGI("Handle version data add change"); + HandleVersionAddChange(changeNotification.GetInsertEntries()); + } + if (!changeNotification.GetUpdateEntries().empty()) { + DHLOGI("Handle version data update change"); + HandleVersionUpdateChange(changeNotification.GetUpdateEntries()); + } + if (!changeNotification.GetDeleteEntries().empty()) { + DHLOGI("Handle version data delete change"); + HandleVersionDeleteChange(changeNotification.GetDeleteEntries()); + } +} + +void VersionInfoManager::HandleVersionAddChange(const std::vector &insertRecords) +{ + DHLOGI("VersionInfoManager: Version add change"); + for (const auto &item : insertRecords) { + const std::string value = item.value.ToString(); + VersionInfo versionInfo; + versionInfo.FromJsonString(value); + const std::string &deviceId = versionInfo.deviceId; + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed, deviceId: %s", GetAnonyString(deviceId).c_str()); + continue; + } + DHLOGI("Add Version, uuid: %s", GetAnonyString(uuid).c_str()); + DHVersion dhVersion; + dhVersion.uuid = uuid; + dhVersion.dhVersion = versionInfo.dhVersion; + dhVersion.compVersions = versionInfo.compVersions; + VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); + } +} + +void VersionInfoManager::HandleVersionUpdateChange(const std::vector &updateRecords) +{ + DHLOGI("VersionInfoManager: Version update change"); + for (const auto &item : updateRecords) { + const std::string value = item.value.ToString(); + VersionInfo versionInfo; + versionInfo.FromJsonString(value); + const std::string &deviceId = versionInfo.deviceId; + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed, deviceId: %s", GetAnonyString(deviceId).c_str()); + continue; + } + DHLOGI("Update Version ,uuid: %s", GetAnonyString(uuid).c_str()); + DHVersion dhVersion; + dhVersion.uuid = uuid; + dhVersion.dhVersion = versionInfo.dhVersion; + dhVersion.compVersions = versionInfo.compVersions; + VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); + } +} + +void VersionInfoManager::HandleVersionDeleteChange(const std::vector &deleteRecords) +{ + DHLOGI("VersionInfoManager: Version delete change"); + for (const auto &item : deleteRecords) { + const std::string value = item.value.ToString(); + VersionInfo dhVersion; + dhVersion.FromJsonString(value); + const std::string &deviceId = dhVersion.deviceId; + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed, deviceId: %s", GetAnonyString(deviceId).c_str()); + continue; + } + DHLOGI("Delete Version ,uuid: %s", GetAnonyString(uuid).c_str()); + VersionManager::GetInstance().RemoveDHVersion(uuid); + } +} + +void VersionInfoManager::OnEvent(VersionInfoEvent &ev) +{ + switch (ev.GetAction()) { + case VersionInfoEvent::EventType::RECOVER: + SyncRemoteVersionInfos(); + break; + default: + DHLOGE("Event is undefined, type is %d", ev.GetAction()); + break; + } +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp b/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp index 24728f83..c874439a 100644 --- a/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp @@ -23,7 +23,7 @@ #include "task_board.h" #include "task_executor.h" #include "task_factory.h" -#include "version_manager.h" +#include "version_info_manager.h" namespace OHOS { namespace DistributedHardware { @@ -75,7 +75,7 @@ void OnLineTask::DoSyncInfo() DHLOGW("ManualSync failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } - ret = VersionManager::GetInstance()->ManualSync(GetNetworkId()); + ret = VersionInfoManager::GetInstance()->ManualSync(GetNetworkId()); if (ret != DH_FWK_SUCCESS) { DHLOGW("ManualSync version failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } @@ -85,7 +85,7 @@ void OnLineTask::DoSyncInfo() DHLOGE("SyncDeviceInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } - ret = VersionManager::GetInstance()->SyncDHVersionFromDB(GetUUID()); + ret = VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(GetDeviceIdByUUID(GetUUID())); if (ret != DH_FWK_SUCCESS) { DHLOGE("SyncVersionInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index 559444ff..de642f56 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -18,7 +18,6 @@ #include "anonymous_string.h" #include "component_loader.h" #include "dh_context.h" -#include "dh_utils_tool.h" #include "distributed_hardware_log.h" namespace OHOS { @@ -26,44 +25,19 @@ namespace DistributedHardware { #undef DH_LOG_TAG #define DH_LOG_TAG "VersionManager" -VersionManager::VersionManager() : dbAdapterPtr_(nullptr) -{} - -VersionManager::~VersionManager() -{ - DHLOGI("VersionManager Destruction!"); -} - -std::shared_ptr VersionManager::GetInstance() -{ - static std::shared_ptr instance(new(std::nothrow) VersionManager); - if (instance == nullptr) { - DHLOGE("instance is nullptr, because applying memory fail!"); - return nullptr; - } - return instance; -} - int32_t VersionManager::Init() { DHLOGI("start"); - { - std::lock_guard lock(versionMutex_); - dbAdapterPtr_ = std::make_shared(APP_ID, GLOBAL_VERSION_ID, shared_from_this()); - if (dbAdapterPtr_->Init() != DH_FWK_SUCCESS) { - DHLOGE("Init dbAdapterPtr_ failed"); - return ERR_DH_FWK_RESOURCE_INIT_DB_FAILED; - } - } - - VersionInfoEvent versionInfoEvent(*this); - DHContext::GetInstance().GetEventBus()->AddHandler(versionInfoEvent.GetType(), *this); - int32_t ret = AddLocalVersion(); + DHVersion dhVersion; + int32_t ret = ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion); if (ret != DH_FWK_SUCCESS) { - DHLOGE("AddLocalVersion fail"); + DHLOGE("GetLocalDHVersion fail"); return ret; } - + dhVersion.dhVersion = GetLocalDeviceVersion(); + ShowLocalVersion(dhVersion); + std::string strUUID = DHContext::GetInstance().GetDeviceInfo().uuid; + AddDHVersion(strUUID, dhVersion); return DH_FWK_SUCCESS; } @@ -73,26 +47,6 @@ void VersionManager::UnInit() dhVersions_.clear(); } -int32_t VersionManager::AddLocalVersion() -{ - DHVersion dhVersion; - int32_t ret = ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion); - if (ret != DH_FWK_SUCCESS) { - DHLOGE("GetLocalDHVersion fail"); - return ret; - } - dhVersion.dhVersion = GetLocalDeviceVersion(); - dhVersion.uuid = DHContext::GetInstance().GetDeviceInfo().uuid; - dhVersion.deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; - ShowLocalVersion(dhVersion); - ret = AddDHVersion(dhVersion.uuid, dhVersion); - if (ret != DH_FWK_SUCCESS) { - DHLOGE("AddDHVersion fail"); - return ret; - } - return DH_FWK_SUCCESS; -} - void VersionManager::ShowLocalVersion(const DHVersion &dhVersion) const { for (const auto &item : dhVersion.compVersions) { @@ -108,50 +62,21 @@ int32_t VersionManager::AddDHVersion(const std::string &uuid, const DHVersion &d std::lock_guard lock(versionMutex_); dhVersions_[uuid] = dhVersion; - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; - } - - std::string data(""); - dbAdapterPtr_->GetDataByKey(dhVersion.deviceId, data); - if (data.compare(dhVersion.ToJsonString()) == 0) { - DHLOGI("dhversion already stored, Key: %s", GetAnonyString(dhVersion.deviceId).c_str()); - return DH_FWK_SUCCESS; - } - - std::string key = dhVersion.deviceId; - std::string value = dhVersion.ToJsonString(); - DHLOGI("add version to db, Key: %s", GetAnonyString(dhVersion.deviceId).c_str()); - if (dbAdapterPtr_->PutData(key, value) != DH_FWK_SUCCESS) { - DHLOGE("Fail to storage to kv"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; - } return DH_FWK_SUCCESS; } int32_t VersionManager::RemoveDHVersion(const std::string &uuid) { - DHLOGI("removeDHVersion uuid: %s", GetAnonyString(uuid).c_str()); + DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); std::lock_guard lock(versionMutex_); std::unordered_map::iterator iter = dhVersions_.find(uuid); if (iter == dhVersions_.end()) { - DHLOGE("there is no uuid: %s, remove in cache fail", GetAnonyString(uuid).c_str()); - } else { - dhVersions_.erase(iter); + DHLOGE("there is no uuid: %s, remove fail", GetAnonyString(uuid).c_str()); + return ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST; } + dhVersions_.erase(iter); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; - } - - if (dbAdapterPtr_->RemoveDeviceData(GetDeviceIdByUUID(uuid)) != DH_FWK_SUCCESS) { - DHLOGE("Remove version Device Data failed, deviceId: %s", - GetAnonyString(GetDeviceIdByUUID(uuid)).c_str()); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; - } return DH_FWK_SUCCESS; } @@ -160,91 +85,13 @@ int32_t VersionManager::GetDHVersion(const std::string &uuid, DHVersion &dhVersi DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); std::lock_guard lock(versionMutex_); std::unordered_map::iterator iter = dhVersions_.find(uuid); - if (iter != dhVersions_.end()) { + if (iter == dhVersions_.end()) { + DHLOGE("there is no uuid: %s, get version fail", GetAnonyString(uuid).c_str()); + return ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST; + } else { dhVersion = dhVersions_[uuid]; return DH_FWK_SUCCESS; } - DHLOGI("there is no uuid: %s in cache, get version in db", GetAnonyString(uuid).c_str()); - - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; - } - std::string data(""); - if (dbAdapterPtr_->GetDataByKey(GetDeviceIdByUUID(uuid), data) != DH_FWK_SUCCESS) { - DHLOGE("Query data from DB by deviceId failed, deviceId: %s", - GetAnonyString(GetDeviceIdByUUID(uuid)).c_str()); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; - } - - DHLOGI("Query data from DB by deviceId success, deviceId: %s", - GetAnonyString(GetDeviceIdByUUID(uuid)).c_str()); - dhVersion.FromJsonString(data); - return DH_FWK_SUCCESS; -} - -int32_t VersionManager::SyncDHVersionFromDB(const std::string &uuid) -{ - DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); - std::lock_guard lock(versionMutex_); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; - } - - std::string data(""); - if (dbAdapterPtr_->GetDataByKey(GetDeviceIdByUUID(uuid), data) != DH_FWK_SUCCESS) { - DHLOGE("Query data from DB by deviceId failed, deviceId: %s", - GetAnonyString(GetDeviceIdByUUID(uuid)).c_str()); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; - } - - DHLOGI("Query data from DB by deviceId success, deviceId: %s", - GetAnonyString(GetDeviceIdByUUID(uuid)).c_str()); - DHVersion dhVersion; - dhVersion.FromJsonString(data); - dhVersions_[uuid] = dhVersion; - return DH_FWK_SUCCESS; -} - -int32_t VersionManager::SyncRemoteVersionInfos() -{ - std::unordered_map dhVersions; - std::vector dataVector; - std::lock_guard lock(versionMutex_); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; - } - - if (dbAdapterPtr_->GetDataByKeyPrefix("", dataVector) != DH_FWK_SUCCESS) { - DHLOGE("Query all data from DB failed"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; - } - - for (const auto &data : dataVector) { - DHVersion dhVersion; - dhVersion.FromJsonString(data); - const std::string &deviceId = dhVersion.deviceId; - const std::string &localDeviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; - if (deviceId.compare(localDeviceId) == 0) { - DHLOGE("local device info not need sync from db"); - continue; - } - if (!DHContext::GetInstance().IsDeviceOnline(deviceId)) { - DHLOGE("offline device, no need sync to memory, deviceId : %s ", - GetAnonyString(deviceId).c_str()); - continue; - } - std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); - if (uuid.empty()) { - DHLOGI("Find uuid failed, deviceId: %s", GetAnonyString(deviceId).c_str()); - continue; - } - dhVersions_[uuid] = dhVersion; - } - - return DH_FWK_SUCCESS; } int32_t VersionManager::GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion) @@ -269,124 +116,5 @@ std::string VersionManager::GetLocalDeviceVersion() { return DH_LOCAL_VERSION; } - -void VersionManager::CreateManualSyncCount(const std::string &deviceId) -{ - std::lock_guard lock(versionMutex_); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return; - } - dbAdapterPtr_->CreateManualSyncCount(deviceId); -} - -void VersionManager::RemoveManualSyncCount(const std::string &deviceId) -{ - std::lock_guard lock(versionMutex_); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return; - } - dbAdapterPtr_->RemoveManualSyncCount(deviceId); -} - -int32_t VersionManager::ManualSync(const std::string &networkId) -{ - DHLOGI("ManualSync start, networkId: %s", GetAnonyString(networkId).c_str()); - std::unique_lock lock(versionMutex_); - if (dbAdapterPtr_ == nullptr) { - DHLOGE("dbAdapterPtr_ is null"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; - } - if (dbAdapterPtr_->ManualSync(networkId) != DH_FWK_SUCCESS) { - DHLOGE("ManualSync failed"); - return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; - } - return DH_FWK_SUCCESS; -} - -void VersionManager::OnChange(const DistributedKv::ChangeNotification &changeNotification) -{ - DHLOGI("VersionManager: DB data OnChange"); - if (!changeNotification.GetInsertEntries().empty()) { - DHLOGI("Handle version data add change"); - HandleVersionAddChange(changeNotification.GetInsertEntries()); - } - if (!changeNotification.GetUpdateEntries().empty()) { - DHLOGI("Handle version data update change"); - HandleVersionUpdateChange(changeNotification.GetUpdateEntries()); - } - if (!changeNotification.GetDeleteEntries().empty()) { - DHLOGI("Handle capability data delete change"); - HandleVersionDeleteChange(changeNotification.GetDeleteEntries()); - } -} - -void VersionManager::HandleVersionAddChange(const std::vector &insertRecords) -{ - DHLOGI("VersionManager: Version add change"); - for (const auto &item : insertRecords) { - const std::string value = item.value.ToString(); - DHVersion dhVersion; - dhVersion.FromJsonString(value); - const std::string &deviceId = dhVersion.deviceId; - std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); - if (uuid.empty()) { - DHLOGI("Find uuid failed, deviceId: %s", GetAnonyString(deviceId).c_str()); - continue; - } - DHLOGI("Add Version ,uuid: %s", GetAnonyString(uuid).c_str()); - AddDHVersion(uuid, dhVersion); - } -} - -void VersionManager::HandleVersionUpdateChange(const std::vector &updateRecords) -{ - DHLOGI("VersionManager: Version update change"); - for (const auto &item : updateRecords) { - const std::string value = item.value.ToString(); - DHVersion dhVersion; - dhVersion.FromJsonString(value); - const std::string &deviceId = dhVersion.deviceId; - std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); - if (uuid.empty()) { - DHLOGI("Find uuid failed, deviceId: %s", GetAnonyString(deviceId).c_str()); - continue; - } - DHLOGI("Update Version ,uuid: %s", GetAnonyString(uuid).c_str()); - AddDHVersion(uuid, dhVersion); - } -} - -void VersionManager::HandleVersionDeleteChange(const std::vector &deleteRecords) -{ - DHLOGI("VersionManager: Version delete change"); - for (const auto &item : deleteRecords) { - const std::string value = item.value.ToString(); - DHVersion dhVersion; - dhVersion.FromJsonString(value); - const std::string &deviceId = dhVersion.deviceId; - std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); - if (uuid.empty()) { - DHLOGI("Find uuid failed, deviceId: %s", GetAnonyString(deviceId).c_str()); - continue; - } - - DHLOGI("Delete Version ,uuid: %s", GetAnonyString(uuid).c_str()); - RemoveDHVersion(uuid); - } -} - -void VersionManager::OnEvent(VersionInfoEvent &ev) -{ - switch (ev.GetAction()) { - case VersionInfoEvent::EventType::RECOVER: - SyncRemoteVersionInfos(); - break; - default: - DHLOGE("Event is undefined, type is %d", ev.GetAction()); - break; - } -} } // namespace DistributedHardware } // namespace OHOS -- Gitee From 38ebdba4ffcfaa59e6e1c501907e78181bfd6521 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 15 Jul 2022 15:53:26 +0800 Subject: [PATCH 25/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../src/componentmanager/component_manager.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index 82533e32..2061a0e3 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -105,6 +105,7 @@ int32_t ComponentManager::UnInit() compSource_.clear(); compSink_.clear(); + DHLOGI("Release component success"); return DH_FWK_SUCCESS; } -- Gitee From 2bd695692243a87c3251d8b0b056d44b12619db9 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 15 Jul 2022 15:58:18 +0800 Subject: [PATCH 26/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- services/distributedhardwarefwkserviceimpl/BUILD.gn | 2 ++ .../src/componentmanager/component_manager.cpp | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/services/distributedhardwarefwkserviceimpl/BUILD.gn b/services/distributedhardwarefwkserviceimpl/BUILD.gn index cf5f3fe2..84d8911a 100644 --- a/services/distributedhardwarefwkserviceimpl/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/BUILD.gn @@ -52,6 +52,8 @@ ohos_shared_library("distributedhardwarefwksvr_impl") { "src/resourcemanager/capability_info_manager.cpp", "src/resourcemanager/capability_utils.cpp", "src/resourcemanager/db_adapter.cpp", + "src/resourcemanager/version_info.cpp", + "src/resourcemanager/version_info_manager.cpp", "src/task/disable_task.cpp", "src/task/enable_task.cpp", "src/task/offline_task.cpp", diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index 2061a0e3..82533e32 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -105,7 +105,6 @@ int32_t ComponentManager::UnInit() compSource_.clear(); compSink_.clear(); - DHLOGI("Release component success"); return DH_FWK_SUCCESS; } -- Gitee From e2936bc68f7b4735aff10f7bc62617123e4c82b6 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 15 Jul 2022 17:03:12 +0800 Subject: [PATCH 27/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- services/distributedhardwarefwkserviceimpl/BUILD.gn | 1 - .../include/componentloader/component_loader.h | 1 + .../include/componentmanager/component_manager.h | 4 ++-- .../include/resourcemanager/version_info_manager.h | 3 ++- .../include/versionmanager/version_manager.h | 13 ++++--------- .../src/componentloader/component_loader.cpp | 2 +- .../src/componentmanager/component_manager.cpp | 12 +++++++----- .../src/distributed_hardware_manager.cpp | 3 ++- .../src/resourcemanager/version_info.cpp | 1 + .../src/resourcemanager/version_info_manager.cpp | 5 +++-- .../src/versionmanager/version_manager.cpp | 1 + 11 files changed, 24 insertions(+), 22 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/BUILD.gn b/services/distributedhardwarefwkserviceimpl/BUILD.gn index 84d8911a..51b12dff 100644 --- a/services/distributedhardwarefwkserviceimpl/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/BUILD.gn @@ -63,7 +63,6 @@ ohos_shared_library("distributedhardwarefwksvr_impl") { "src/task/task_executor.cpp", "src/task/task_factory.cpp", "src/utils/dh_context.cpp", - "src/utils/impl_utils.cpp", "src/versionmanager/version_manager.cpp", ] diff --git a/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h b/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h index b0dca951..aba9b3e3 100644 --- a/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h +++ b/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h @@ -75,6 +75,7 @@ private: int32_t GetCompPathAndVersion(const std::string &jsonStr, std::map &dhtypeMap); CompVersion GetCompVersionFromComConfig(const CompConfig& cCfg); int32_t ParseConfig(); + void StoreLocalDHVersionToDB(); bool IsDHTypeExist(DHType dhType); std::string Readfile(const std::string &filePath); diff --git a/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h b/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h index 6eff855d..6eaedc92 100644 --- a/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h @@ -69,9 +69,9 @@ private: DHType dhType, EnableParam ¶m); std::string GetSinkVersionFromVerMgr(const std::string &uuid, const DHType dhType); std::string GetSinkVersionFromVerInfoMgr(const std::string &uuid, const DHType dhType); - std::string GetSinkVersionFromRPC(const std::string &networkId, const std::string &uuid, DHType dhType) + std::string GetSinkVersionFromRPC(const std::string &networkId, const std::string &uuid, DHType dhType); std::string GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType); - void UpdateVersionCache(const std::string &uuid, const std::unordered_map &versions) + void UpdateVersionCache(const std::string &uuid, const std::unordered_map &versions); sptr GetRemoteDHMS(const std::string &networkId) const; private: diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h index a25acecf..bba50cf1 100644 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h @@ -51,6 +51,7 @@ public: int32_t UnInit(); int32_t AddVersion(const VersionInfo &versionInfo); + int32_t GetVersionInfoByDeviceId(const std::string &deviceId, VersionInfo &versionInfo); int32_t SyncVersionInfoFromDB(const std::string &deviceId); int32_t SyncRemoteVersionInfos(); @@ -62,7 +63,7 @@ public: void OnEvent(VersionInfoEvent &ev) override; private: - VersionManager(); + VersionInfoManager(); void HandleVersionAddChange(const std::vector &insertRecords); void HandleVersionUpdateChange(const std::vector &updateRecords); void HandleVersionDeleteChange(const std::vector &deleteRecords); diff --git a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h index c5fd2d39..f056d897 100644 --- a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h @@ -20,26 +20,20 @@ #include #include -#include "kvstore_observer.h" - -#include "db_adapter.h" -#include "eventbus_handler.h" -#include "event_bus.h" -#include "event_sender.h" +#include "single_instance.h" #include "distributed_hardware_errno.h" #include "device_type.h" -#include "impl_utils.h" -#include "version_info_event.h" +#include "utils/impl_utils.h" namespace OHOS { namespace DistributedHardware { const std::string DH_LOCAL_VERSION = "1.0"; +class VersionManager { DECLARE_SINGLE_INSTANCE_BASE(VersionManager); public: VersionManager() {} ~VersionManager() {} - int32_t Init(); void UnInit(); int32_t AddDHVersion(const std::string &uuid, const DHVersion &dhVersion); @@ -53,6 +47,7 @@ public: private: std::unordered_map dhVersions_; std::mutex versionMutex_; +}; } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp index b76270aa..3cd27c01 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp @@ -142,7 +142,7 @@ int32_t ComponentLoader::GetLocalDHVersion(DHVersion &dhVersion) return DH_FWK_SUCCESS; } -void StoreLocalDHVersionToDB() +void ComponentLoader::StoreLocalDHVersionToDB() { if (!isLocalVersionInit_.load()) { DHLOGE("Store local DHVersion fail"); diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index 82533e32..e395ca14 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -34,6 +34,7 @@ #include "ipc_object_stub.h" #include "iservice_registry.h" #include "system_ability_definition.h" +#include "version_info_manager.h" #include "version_manager.h" namespace OHOS { @@ -359,7 +360,7 @@ std::string ComponentManager::GetSinkVersionFromVerMgr(const std::string &uuid, return ""; } DHLOGI("Get SinkVersion from version mgr success, sinkVersion = %s, uuid = %s, dhType = %#X", - compversion.sinkVersion, GetAnonyString(uuid).c_str(), dhType); + compversion.sinkVersion.c_str(), GetAnonyString(uuid).c_str(), dhType); return compversion.sinkVersion; } @@ -378,7 +379,7 @@ std::string ComponentManager::GetSinkVersionFromVerInfoMgr(const std::string &uu return ""; } DHLOGI("Get SinkVersion from version info mgr success, sinkVersion = %s, uuid = %s, dhType = %#X", - iter->second.sinkVersion, GetAnonyString(uuid).c_str(), dhType); + iter->second.sinkVersion.c_str(), GetAnonyString(uuid).c_str(), dhType); return iter->second.sinkVersion; } @@ -389,7 +390,7 @@ std::string ComponentManager::GetSinkVersionFromRPC(const std::string &networkId sptr dhms = GetRemoteDHMS(networkId); if (dhms == nullptr) { DHLOGI("GetRemoteDHMS failed, networkId = %s", GetAnonyString(networkId).c_str()); - return ERR_DH_FWK_COMPONENT_GET_REMOTE_SA_FAILED; + return ""; } std::unordered_map versions; @@ -406,8 +407,9 @@ std::string ComponentManager::GetSinkVersionFromRPC(const std::string &networkId return ""; } DHLOGI("QuerySinkVersion success, sinkVersion = %s, uuid = %s, dhType = %#X", - iter->second, GetAnonyString(uuid).c_str(), dhType); + iter->second.c_str(), GetAnonyString(uuid).c_str(), dhType); UpdateVersionCache(uuid, versions); + return iter->second; } @@ -440,7 +442,7 @@ void ComponentManager::UpdateVersionCache(const std::string &uuid, const std::un CompVersion compVersion; compVersion.dhType = versionPair.first; compVersion.sinkVersion = versionPair.second; - dhVersion.compVersions.insert(std::pair(compVersion.dhType, compVersion); + dhVersion.compVersions.insert(std::pair(compVersion.dhType, compVersion)); } VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); } diff --git a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp index 291595f5..0cc1e922 100644 --- a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp @@ -29,6 +29,7 @@ #include "task_board.h" #include "task_executor.h" #include "task_factory.h" +#include "version_info_manager.h" #include "version_manager.h" namespace OHOS { @@ -159,7 +160,7 @@ int32_t DistributedHardwareManager::SendOffLineEvent(const std::string &networkI DHContext::GetInstance().RemoveOnlineDevice(realUUID); CapabilityInfoManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); - VersionInofManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); + VersionInfoManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); HiSysEventWriteCompOfflineMsg(DHFWK_DEV_OFFLINE, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, GetAnonyString(networkId), "dhfwk device offline event."); diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp index d017cc02..dadb59d4 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp @@ -27,6 +27,7 @@ namespace OHOS { namespace DistributedHardware { #undef DH_LOG_TAG #define DH_LOG_TAG "VersionInfo" + void VersionInfo::FromJsonString(const std::string &jsonStr) { nlohmann::json jsonObj = nlohmann::json::parse(jsonStr); diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp index 0bd6e51c..2d12c87e 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp @@ -124,13 +124,14 @@ int32_t VersionInfoManager::GetVersionInfoByDeviceId(const std::string &deviceId int32_t VersionInfoManager::SyncVersionInfoFromDB(const std::string &deviceId) { DHLOGI("Sync VersionInfo from DB, deviceId: %s", GetAnonyString(deviceId).c_str()); + std::string data(""); std::lock_guard lock(verInfoMgrMutex_); { if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; } - std::string data(""); + if (dbAdapterPtr_->GetDataByKey(deviceId, data) != DH_FWK_SUCCESS) { DHLOGE("Query data from DB by deviceId failed, deviceId: %s", GetAnonyString(deviceId).c_str()); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; @@ -143,7 +144,7 @@ int32_t VersionInfoManager::SyncVersionInfoFromDB(const std::string &deviceId) DHLOGI("Find uuid failed"); return ERR_DH_FWK_RESOURCE_SYNC_VERSIONINFO_FAIL; } - VersionInfo versionInfo + VersionInfo versionInfo; versionInfo.FromJsonString(data); DHVersion dhVersion; dhVersion.uuid = uuid; diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index de642f56..7c97601c 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -24,6 +24,7 @@ namespace OHOS { namespace DistributedHardware { #undef DH_LOG_TAG #define DH_LOG_TAG "VersionManager" +IMPLEMENT_SINGLE_INSTANCE(VersionManager); int32_t VersionManager::Init() { -- Gitee From bb9108cbb90a0a29a3d8fb01f8d14ce483339c1a Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 15 Jul 2022 17:15:11 +0800 Subject: [PATCH 28/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../src/componentmanager/component_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index e395ca14..519e9d7c 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -347,7 +347,7 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std DHLOGI("success. uuid =%s, dhId = %s, version = %s", GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), param.version.c_str()); - return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED; + return DH_FWK_SUCCESS; } std::string ComponentManager::GetSinkVersionFromVerMgr(const std::string &uuid, const DHType dhType) -- Gitee From d00824201d3f44fc405dfb49c95b8c21190f06ed Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 15 Jul 2022 18:00:18 +0800 Subject: [PATCH 29/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../src/version_manager_test.cpp | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp index 2a2511fa..aa4fa8af 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp @@ -64,7 +64,7 @@ const std::string TEST_COMPONENT_NAME_3 = "distributed_mic"; */ HWTEST_F(VersionManagerTest, version_manager_test_001, TestSize.Level0) { - auto ret = VersionManager::GetInstance()->Init(); + auto ret = VersionManager::GetInstance().Init(); EXPECT_EQ(DH_FWK_SUCCESS, ret); } @@ -101,10 +101,10 @@ HWTEST_F(VersionManagerTest, version_manager_test_002, TestSize.Level0) dhVersion.compVersions.insert(std::make_pair(cVs1.dhType, cVs1)); dhVersion.compVersions.insert(std::make_pair(cVs2.dhType, cVs2)); dhVersion.compVersions.insert(std::make_pair(cVs3.dhType, cVs3)); - int32_t ret = VersionManager::GetInstance()->AddDHVersion(dhVersion.uuid, dhVersion); + int32_t ret = VersionManager::GetInstance().AddDHVersion(dhVersion.uuid, dhVersion); EXPECT_EQ(DH_FWK_SUCCESS, ret); dhVersion.uuid = TEST_DEVICE_ID_2; - ret = VersionManager::GetInstance()->AddDHVersion(dhVersion.uuid, dhVersion); + ret = VersionManager::GetInstance().AddDHVersion(dhVersion.uuid, dhVersion); EXPECT_EQ(DH_FWK_SUCCESS, ret); } @@ -116,7 +116,7 @@ HWTEST_F(VersionManagerTest, version_manager_test_002, TestSize.Level0) */ HWTEST_F(VersionManagerTest, version_manager_test_003, TestSize.Level0) { - std::string strVersion = VersionManager::GetInstance()->GetLocalDeviceVersion(); + std::string strVersion = VersionManager::GetInstance().GetLocalDeviceVersion(); EXPECT_EQ(DH_LOCAL_VERSION, strVersion); } @@ -129,11 +129,11 @@ HWTEST_F(VersionManagerTest, version_manager_test_003, TestSize.Level0) HWTEST_F(VersionManagerTest, version_manager_test_004, TestSize.Level0) { DHVersion dhVersion; - int32_t ret = VersionManager::GetInstance()->GetDHVersion(TEST_DEVICE_ID_2, dhVersion); + int32_t ret = VersionManager::GetInstance().GetDHVersion(TEST_DEVICE_ID_2, dhVersion); EXPECT_EQ(DH_FWK_SUCCESS, ret); EXPECT_EQ(TEST_HANDLER_VERSION_2, dhVersion.compVersions[DHType::SPEAKER].handlerVersion); EXPECT_EQ(TEST_DH_VERSION, dhVersion.dhVersion); - ret = VersionManager::GetInstance()->GetDHVersion(TEST_DEVICE_ID_3, dhVersion); + ret = VersionManager::GetInstance().GetDHVersion(TEST_DEVICE_ID_3, dhVersion); EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); } @@ -145,13 +145,13 @@ HWTEST_F(VersionManagerTest, version_manager_test_004, TestSize.Level0) */ HWTEST_F(VersionManagerTest, version_manager_test_005, TestSize.Level0) { - int32_t ret = VersionManager::GetInstance()->RemoveDHVersion(TEST_DEVICE_ID_2); + int32_t ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_2); EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); - ret = VersionManager::GetInstance()->RemoveDHVersion(TEST_DEVICE_ID_4); + ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_4); EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); - ret = VersionManager::GetInstance()->RemoveDHVersion(TEST_DEVICE_ID_2); + ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_2); EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); - ret = VersionManager::GetInstance()->RemoveDHVersion(TEST_DEVICE_ID_1); + ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_1); EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); } } // namespace DistributedHardware -- Gitee From 13782094acfdf02929989c4f22db3e5527664e9e Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 15 Jul 2022 18:23:50 +0800 Subject: [PATCH 30/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../common/versionmanager/src/version_manager_test.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp index aa4fa8af..874900c8 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp @@ -134,7 +134,7 @@ HWTEST_F(VersionManagerTest, version_manager_test_004, TestSize.Level0) EXPECT_EQ(TEST_HANDLER_VERSION_2, dhVersion.compVersions[DHType::SPEAKER].handlerVersion); EXPECT_EQ(TEST_DH_VERSION, dhVersion.dhVersion); ret = VersionManager::GetInstance().GetDHVersion(TEST_DEVICE_ID_3, dhVersion); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + EXPECT_EQ(ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST, ret); } /** @@ -146,13 +146,13 @@ HWTEST_F(VersionManagerTest, version_manager_test_004, TestSize.Level0) HWTEST_F(VersionManagerTest, version_manager_test_005, TestSize.Level0) { int32_t ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_2); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + EXPECT_EQ(DH_FWK_SUCCESS, ret); ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_4); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + EXPECT_EQ(ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST, ret); ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_2); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + EXPECT_EQ(ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST, ret); ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_1); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + EXPECT_EQ(DH_FWK_SUCCESS, ret); } } // namespace DistributedHardware } // namespace OHOS -- Gitee From 4ff5fd6f00a29ac880ef73ee824ceb6a4341795b Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 15 Jul 2022 18:32:59 +0800 Subject: [PATCH 31/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../common/versionmanager/src/version_manager_test.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp index aa4fa8af..874900c8 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp @@ -134,7 +134,7 @@ HWTEST_F(VersionManagerTest, version_manager_test_004, TestSize.Level0) EXPECT_EQ(TEST_HANDLER_VERSION_2, dhVersion.compVersions[DHType::SPEAKER].handlerVersion); EXPECT_EQ(TEST_DH_VERSION, dhVersion.dhVersion); ret = VersionManager::GetInstance().GetDHVersion(TEST_DEVICE_ID_3, dhVersion); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + EXPECT_EQ(ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST, ret); } /** @@ -146,13 +146,13 @@ HWTEST_F(VersionManagerTest, version_manager_test_004, TestSize.Level0) HWTEST_F(VersionManagerTest, version_manager_test_005, TestSize.Level0) { int32_t ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_2); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + EXPECT_EQ(DH_FWK_SUCCESS, ret); ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_4); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + EXPECT_EQ(ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST, ret); ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_2); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + EXPECT_EQ(ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST, ret); ret = VersionManager::GetInstance().RemoveDHVersion(TEST_DEVICE_ID_1); - EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret); + EXPECT_EQ(DH_FWK_SUCCESS, ret); } } // namespace DistributedHardware } // namespace OHOS -- Gitee From 7356e20b992b40297d863e51371eb30d6b4826bf Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 15 Jul 2022 18:41:48 +0800 Subject: [PATCH 32/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../src/componentmanager/component_manager.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index 3b94d8a8..81ba0e3f 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -385,7 +385,8 @@ std::string ComponentManager::GetSinkVersionFromVerInfoMgr(const std::string &uu return iter->second.sinkVersion; } -std::string ComponentManager::GetSinkVersionFromRPC(const std::string &networkId, const std::string &uuid, DHType dhType) +std::string ComponentManager::GetSinkVersionFromRPC(const std::string &networkId, + const std::string &uuid, DHType dhType) { DHLOGI("networkId = %s ", GetAnonyString(networkId).c_str()); @@ -436,7 +437,8 @@ std::string ComponentManager::GetSinkVersion(const std::string &networkId, const return ""; } -void ComponentManager::UpdateVersionCache(const std::string &uuid, const std::unordered_map &versions) +void ComponentManager::UpdateVersionCache(const std::string &uuid, + const std::unordered_map &versions) { DHVersion dhVersion; dhVersion.uuid = uuid; -- Gitee From c4606d5777b102d7df747c5a3f618ec7df5cb2ef Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Wed, 20 Jul 2022 09:15:27 +0800 Subject: [PATCH 33/36] =?UTF-8?q?=20=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- common/utils/include/device_type.h | 2 +- .../include/componentloader/component_loader.h | 4 ++-- .../include/resourcemanager/version_info.h | 2 +- .../include/resourcemanager/version_info_event.h | 2 +- .../include/resourcemanager/version_info_manager.h | 2 +- .../src/componentloader/component_loader.cpp | 4 ++-- .../src/componentmanager/component_manager.cpp | 2 +- .../src/resourcemanager/version_info.cpp | 2 +- .../src/task/monitor_task_timer.cpp | 2 +- 9 files changed, 11 insertions(+), 11 deletions(-) diff --git a/common/utils/include/device_type.h b/common/utils/include/device_type.h index 22a21013..aa205318 100644 --- a/common/utils/include/device_type.h +++ b/common/utils/include/device_type.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h b/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h index aba9b3e3..1ef1ea69 100644 --- a/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h +++ b/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -75,7 +75,7 @@ private: int32_t GetCompPathAndVersion(const std::string &jsonStr, std::map &dhtypeMap); CompVersion GetCompVersionFromComConfig(const CompConfig& cCfg); int32_t ParseConfig(); - void StoreLocalDHVersionToDB(); + void StoreLocalDHVersionInDB(); bool IsDHTypeExist(DHType dhType); std::string Readfile(const std::string &filePath); diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h index fb3e84be..0c2961ad 100644 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_event.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_event.h index d5db9b40..607b13ba 100644 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_event.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_event.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h index 7637d2d9..3871740d 100644 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp index 3cd27c01..05de0cb8 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp @@ -74,9 +74,9 @@ int32_t ComponentLoader::Init() DHLOGI("start"); DHTraceStart(COMPONENT_LOAD_START); int32_t ret = ParseConfig(); + StoreLocalDHVersionInDB(); DHTraceEnd(); - StoreLocalDHVersionToDB(); return ret; } @@ -142,7 +142,7 @@ int32_t ComponentLoader::GetLocalDHVersion(DHVersion &dhVersion) return DH_FWK_SUCCESS; } -void ComponentLoader::StoreLocalDHVersionToDB() +void ComponentLoader::StoreLocalDHVersionInDB() { if (!isLocalVersionInit_.load()) { DHLOGE("Store local DHVersion fail"); diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index 517e76c1..30fa447d 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -457,7 +457,7 @@ void ComponentManager::UpdateVersionCache(const std::string &uuid, { DHVersion dhVersion; dhVersion.uuid = uuid; - for (auto versionPair : versions) { + for (const auto &versionPair : versions) { CompVersion compVersion; compVersion.dhType = versionPair.first; compVersion.sinkVersion = versionPair.second; diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp index 1dc0d5a5..f1d6d4cf 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp @@ -90,7 +90,7 @@ void FromJson(const nlohmann::json &jsonObject, VersionInfo &dhVersionInfo) } if (jsonObject.find(COMP_VER) != jsonObject.end()) { - for (auto compVerObj : jsonObject.at(COMP_VER)) { + for (const auto &compVerObj : jsonObject.at(COMP_VER)) { CompVersion compVer; FromJson(compVerObj, compVer); dhVersionInfo.compVersions.insert(std::pair(compVer.dhType, compVer)); diff --git a/services/distributedhardwarefwkserviceimpl/src/task/monitor_task_timer.cpp b/services/distributedhardwarefwkserviceimpl/src/task/monitor_task_timer.cpp index 73ed9e14..b27f0c18 100644 --- a/services/distributedhardwarefwkserviceimpl/src/task/monitor_task_timer.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/task/monitor_task_timer.cpp @@ -58,7 +58,7 @@ void MonitorTaskTimer::StartTimer() void MonitorTaskTimer::StopTimer() { - DHLOGI("stop"); + DHLOGI("start"); if (monitorTaskTimerThread_.joinable()) { monitorTaskTimerThread_.join(); } -- Gitee From 0003a23fa21223562827de8a1094043c940119fa Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Wed, 20 Jul 2022 11:07:36 +0800 Subject: [PATCH 34/36] =?UTF-8?q?=20=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../include/distributed_hardware_errno.h | 1 - .../include/resourcemanager/version_info.h | 4 ++-- .../include/versionmanager/version_manager.h | 2 +- .../componentmanager/component_manager.cpp | 8 ++++---- .../src/resourcemanager/version_info.cpp | 19 ++++++++++--------- .../resourcemanager/version_info_manager.cpp | 16 ++++++++-------- .../src/task/online_task.cpp | 2 +- .../src/versionmanager/version_manager.cpp | 2 +- .../test/unittest/common/BUILD.gn | 2 +- .../test/unittest/common/task/BUILD.gn | 2 +- .../unittest/common/versionmanager/BUILD.gn | 2 +- 11 files changed, 30 insertions(+), 30 deletions(-) diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index a711339e..04fa2e52 100644 --- a/common/utils/include/distributed_hardware_errno.h +++ b/common/utils/include/distributed_hardware_errno.h @@ -45,7 +45,6 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_COMPONENT_GET_REMOTE_SA_FAILED = -10008; constexpr int32_t ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED = -10009; constexpr int32_t ERR_DH_FWK_COMPONENT_DHTYPE_NOT_FOUND = -10010; - constexpr int32_t ERR_DH_FWK_COMPONENT_GET_REMOTE_DHMS_FAILED = -10011; /* ResourceManager errno, range: [-10400, -10499] */ constexpr int32_t ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL = -10400; diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h index 0c2961ad..08909227 100644 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h @@ -31,9 +31,9 @@ struct VersionInfo { std::string ToJsonString() const; }; -void ToJson(nlohmann::json &jsonObject, const VersionInfo &dhVersion); +void ToJson(nlohmann::json &jsonObject, const VersionInfo &versionInfo); void FromJson(const nlohmann::json &jsonObject, CompVersion &compVer); -void FromJson(const nlohmann::json &jsonObject, VersionInfo &dhVersion); +void FromJson(const nlohmann::json &jsonObject, VersionInfo &versionInfo); } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h index f056d897..3d36bd1f 100644 --- a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index 30fa447d..5bb89fa8 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -342,7 +342,7 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std std::string sinkVersion(""); ret = GetSinkVersion(networkId, uuid, dhType, sinkVersion); if (ret != DH_FWK_SUCCESS) { - DHLOGE("Get Sink Version failed, uuid = %s, dhId = %s, dhType = %#X,", GetAnonyString(uuid).c_str(), + DHLOGE("Get sink version failed, uuid = %s, dhId = %s, dhType = %#X,", GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), dhType); return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED; } @@ -359,11 +359,11 @@ int32_t ComponentManager::GetSinkVersionFromVerMgr(const std::string &uuid, cons CompVersion compversion; int32_t ret = VersionManager::GetInstance().GetCompVersion(uuid, dhType, compversion); if (ret != DH_FWK_SUCCESS) { - DHLOGE("Get sink version from Version Manager failed, uuid =%s, dhType = %#X, errCode = %d", + DHLOGE("Get sink version from version Manager failed, uuid =%s, dhType = %#X, errCode = %d", GetAnonyString(uuid).c_str(), dhType, ret); return ret; } - DHLOGI("Get SinkVersion from version mgr success, sinkVersion = %s, uuid = %s, dhType = %#X", + DHLOGI("Get sink version from version mgr success, sinkVersion = %s, uuid = %s, dhType = %#X", compversion.sinkVersion.c_str(), GetAnonyString(uuid).c_str(), dhType); sinkVersion = compversion.sinkVersion; return DH_FWK_SUCCESS; @@ -398,7 +398,7 @@ int32_t ComponentManager::GetSinkVersionFromRPC(const std::string &networkId, co sptr dhms = GetRemoteDHMS(networkId); if (dhms == nullptr) { DHLOGI("GetRemoteDHMS failed, networkId = %s", GetAnonyString(networkId).c_str()); - return ERR_DH_FWK_COMPONENT_GET_REMOTE_DHMS_FAILED; + return ERR_DH_FWK_COMPONENT_GET_REMOTE_SA_FAILED; } std::unordered_map versions; diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp index f1d6d4cf..649f3519 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp @@ -22,6 +22,7 @@ #include "anonymous_string.h" #include "constants.h" #include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" namespace OHOS { namespace DistributedHardware { @@ -41,13 +42,13 @@ std::string VersionInfo::ToJsonString() const return jsonObj.dump(); } -void ToJson(nlohmann::json &jsonObject, const VersionInfo &dhVersionInfo) +void ToJson(nlohmann::json &jsonObject, const VersionInfo &versionInfo) { - jsonObject[DEV_ID] = dhVersionInfo.deviceId; - jsonObject[DH_VER] = dhVersionInfo.dhVersion; + jsonObject[DEV_ID] = versionInfo.deviceId; + jsonObject[DH_VER] = versionInfo.dhVersion; nlohmann::json compVers; - for (const auto &compVersion : dhVersionInfo.compVersions) { + for (const auto &compVersion : versionInfo.compVersions) { nlohmann::json compVer; compVer[NAME] = compVersion.second.name; compVer[TYPE] = compVersion.second.dhType; @@ -62,7 +63,7 @@ void ToJson(nlohmann::json &jsonObject, const VersionInfo &dhVersionInfo) void FromJson(const nlohmann::json &jsonObject, CompVersion &compVer) { - if (jsonObject.find(DEV_ID) != jsonObject.end()) { + if (jsonObject.find(NAME) != jsonObject.end()) { compVer.name = jsonObject.at(NAME).get(); } if (jsonObject.find(TYPE) != jsonObject.end()) { @@ -79,21 +80,21 @@ void FromJson(const nlohmann::json &jsonObject, CompVersion &compVer) } } -void FromJson(const nlohmann::json &jsonObject, VersionInfo &dhVersionInfo) +void FromJson(const nlohmann::json &jsonObject, VersionInfo &versionInfo) { if (jsonObject.find(DEV_ID) != jsonObject.end()) { - dhVersionInfo.deviceId = jsonObject.at(DEV_ID).get(); + versionInfo.deviceId = jsonObject.at(DEV_ID).get(); } if (jsonObject.find(DH_VER) != jsonObject.end()) { - dhVersionInfo.dhVersion = jsonObject.at(DH_VER).get(); + versionInfo.dhVersion = jsonObject.at(DH_VER).get(); } if (jsonObject.find(COMP_VER) != jsonObject.end()) { for (const auto &compVerObj : jsonObject.at(COMP_VER)) { CompVersion compVer; FromJson(compVerObj, compVer); - dhVersionInfo.compVersions.insert(std::pair(compVer.dhType, compVer)); + versionInfo.compVersions.insert(std::pair(compVer.dhType, compVer)); } } } diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp index 922920fc..28907dc4 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp @@ -136,7 +136,7 @@ void VersionInfoManager::UpdateVersionCache(const VersionInfo &versionInfo) int32_t VersionInfoManager::SyncVersionInfoFromDB(const std::string &deviceId) { - DHLOGI("Sync VersionInfo from DB, deviceId: %s", GetAnonyString(deviceId).c_str()); + DHLOGI("Sync versionInfo from DB, deviceId: %s", GetAnonyString(deviceId).c_str()); std::lock_guard lock(verInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { @@ -176,11 +176,11 @@ int32_t VersionInfoManager::SyncRemoteVersionInfos() const std::string &deviceId = versionInfo.deviceId; const std::string &localDeviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; if (deviceId.compare(localDeviceId) == 0) { - DHLOGE("local device info not need sync from db"); + DHLOGE("Local device info not need sync from db"); continue; } if (!DHContext::GetInstance().IsDeviceOnline(deviceId)) { - DHLOGE("offline device, no need sync to memory, deviceId : %s ", + DHLOGE("Offline device, no need sync to memory, deviceId : %s ", GetAnonyString(deviceId).c_str()); continue; } @@ -226,7 +226,7 @@ int32_t VersionInfoManager::ManualSync(const std::string &networkId) void VersionInfoManager::OnChange(const DistributedKv::ChangeNotification &changeNotification) { - DHLOGI("VersionInfoManager: DB data OnChange"); + DHLOGI("DB data OnChange"); if (!changeNotification.GetInsertEntries().empty()) { DHLOGI("Handle version data add change"); HandleVersionAddChange(changeNotification.GetInsertEntries()); @@ -243,7 +243,7 @@ void VersionInfoManager::OnChange(const DistributedKv::ChangeNotification &chang void VersionInfoManager::HandleVersionAddChange(const std::vector &insertRecords) { - DHLOGI("VersionInfoManager: Version add change"); + DHLOGI("Version add change"); for (const auto &item : insertRecords) { const std::string value = item.value.ToString(); VersionInfo versionInfo; @@ -254,7 +254,7 @@ void VersionInfoManager::HandleVersionAddChange(const std::vector &updateRecords) { - DHLOGI("VersionInfoManager: Version update change"); + DHLOGI("Version update change"); for (const auto &item : updateRecords) { const std::string value = item.value.ToString(); VersionInfo versionInfo; @@ -265,7 +265,7 @@ void VersionInfoManager::HandleVersionUpdateChange(const std::vector &deleteRecords) { - DHLOGI("VersionInfoManager: Version delete change"); + DHLOGI("Version delete change"); for (const auto &item : deleteRecords) { const std::string value = item.value.ToString(); VersionInfo dhVersion; @@ -276,7 +276,7 @@ void VersionInfoManager::HandleVersionDeleteChange(const std::vector Date: Mon, 25 Jul 2022 10:41:02 +0800 Subject: [PATCH 35/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../test/fuzztest/componentmanager_fuzzer/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/componentmanager_fuzzer/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/fuzztest/componentmanager_fuzzer/BUILD.gn index 9955a866..93a8ccd8 100644 --- a/services/distributedhardwarefwkserviceimpl/test/fuzztest/componentmanager_fuzzer/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/fuzztest/componentmanager_fuzzer/BUILD.gn @@ -28,6 +28,7 @@ ohos_fuzztest("ComponentmanagerFuzzTest") { "${services_path}/distributedhardwarefwkserviceimpl/include", "${services_path}/distributedhardwarefwkserviceimpl/include/componentmanager", "${services_path}/distributedhardwarefwkserviceimpl/include/utils", + "${services_path}/distributedhardwarefwkserviceimpl/include/resourcemanager", "${common_path}/utils/include", "${common_path}/log/include", "//utils/native/base/include", -- Gitee From 6f1f35eb7ba99f88379c7c557f7b7aea3e923819 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Mon, 25 Jul 2022 14:41:36 +0800 Subject: [PATCH 36/36] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E9=83=A8=E4=BB=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanderer-dl122 --- .../test/fuzztest/componentmanager_fuzzer/BUILD.gn | 3 ++- .../test/unittest/common/componentmanager/BUILD.gn | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/componentmanager_fuzzer/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/fuzztest/componentmanager_fuzzer/BUILD.gn index 93a8ccd8..ba5f1b9a 100644 --- a/services/distributedhardwarefwkserviceimpl/test/fuzztest/componentmanager_fuzzer/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/fuzztest/componentmanager_fuzzer/BUILD.gn @@ -27,10 +27,11 @@ ohos_fuzztest("ComponentmanagerFuzzTest") { "${utils_path}/include/log", "${services_path}/distributedhardwarefwkserviceimpl/include", "${services_path}/distributedhardwarefwkserviceimpl/include/componentmanager", - "${services_path}/distributedhardwarefwkserviceimpl/include/utils", "${services_path}/distributedhardwarefwkserviceimpl/include/resourcemanager", + "${services_path}/distributedhardwarefwkserviceimpl/include/utils", "${common_path}/utils/include", "${common_path}/log/include", + "//third_party/json/include", "//utils/native/base/include", ] cflags = [ diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/BUILD.gn index f37e19bd..2c886df0 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/BUILD.gn @@ -25,9 +25,11 @@ config("module_private_config") { "${utils_path}/include/log", "${services_path}/distributedhardwarefwkserviceimpl/include", "${services_path}/distributedhardwarefwkserviceimpl/include/componentmanager", + "${services_path}/distributedhardwarefwkserviceimpl/include/resourcemanager", "${services_path}/distributedhardwarefwkserviceimpl/include/utils", "${common_path}/utils/include", "${common_path}/log/include", + "//third_party/json/include", "//utils/native/base/include", ] } -- Gitee