From d12a00b0ef751c6402de426b41a438569180c729 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 27 Feb 2024 15:16:15 +0800 Subject: [PATCH 1/6] feature:Support obtaining network types Signed-off-by: htt1997 --- .../src/device_manager_adapter.cpp | 82 ++++++++++++++++--- .../communicator/device_manager_adapter.h | 13 ++- .../service/cloud/cloud_service_impl.cpp | 3 +- .../service/cloud/cloud_service_impl.h | 2 +- .../service/kvdb/kvdb_service_impl.cpp | 3 + 5 files changed, 87 insertions(+), 16 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp index 6c69a4493..3e7e9efdb 100644 --- a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp +++ b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp @@ -31,6 +31,19 @@ using namespace OHOS::NetManagerStandard; using KvStoreUtils = OHOS::DistributedKv::KvStoreUtils; constexpr int32_t DM_OK = 0; constexpr const char *PKG_NAME = "ohos.distributeddata.service"; +static DeviceManagerAdapter::NetworkType Convert(NetManagerStandard::NetBearType bearType) +{ + switch (bearType) { + case NetManagerStandard::BEARER_WIFI: + return DeviceManagerAdapter::WIFI; + case NetManagerStandard::BEARER_CELLULAR: + return DeviceManagerAdapter::CELLULAR; + case NetManagerStandard::BEARER_ETHERNET: + return DeviceManagerAdapter::ETHERNET; + default: + return DeviceManagerAdapter::OTHER; + } +} class DataMgrDmStateCall final : public DistributedHardware::DeviceStateCallback { public: explicit DataMgrDmStateCall(DeviceManagerAdapter &dmAdapter) : dmAdapter_(dmAdapter) {} @@ -98,7 +111,6 @@ private: int32_t NetConnCallbackObserver::NetAvailable(sptr &netHandle) { ZLOGI("OnNetworkAvailable"); - dmAdapter_.SetNetAvailable(true); dmAdapter_.Online(dmAdapter_.cloudDmInfo); return DistributedKv::SUCCESS; } @@ -106,22 +118,33 @@ int32_t NetConnCallbackObserver::NetAvailable(sptr &netHandle, const sptr &netAllCap) { ZLOGI("OnNetCapabilitiesChange"); - return DistributedKv::SUCCESS; + if (netHandle == nullptr || netAllCap == nullptr) { + return 0; + } + if (netAllCap->netCaps_.count(NetManagerStandard::NET_CAPABILITY_VALIDATED) && !netAllCap->bearerTypes_.empty()) { + dmAdapter_.SetNetAvailable(true); + dmAdapter_.SetNetType(Convert(*netAllCap->bearerTypes_.begin())); + dmAdapter_.OnReady(dmAdapter_.cloudDmInfo); + } else { + dmAdapter_.SetNetAvailable(false); + dmAdapter_.SetNetType(DeviceManagerAdapter::NONE); + } + return 0; } int32_t NetConnCallbackObserver::NetConnectionPropertiesChange(sptr &netHandle, const sptr &info) { ZLOGI("OnNetConnectionPropertiesChange"); - return DistributedKv::SUCCESS; + return 0; } int32_t NetConnCallbackObserver::NetLost(sptr &netHandle) @@ -129,13 +152,13 @@ int32_t NetConnCallbackObserver::NetLost(sptr &netHandle) ZLOGI("OnNetLost"); dmAdapter_.SetNetAvailable(false); dmAdapter_.Offline(dmAdapter_.cloudDmInfo); - return DistributedKv::SUCCESS; + return 0; } int32_t NetConnCallbackObserver::NetBlockStatusChange(sptr &netHandle, bool blocked) { ZLOGI("OnNetBlockStatusChange"); - return DistributedKv::SUCCESS; + return 0; } DeviceManagerAdapter::DeviceManagerAdapter() @@ -625,16 +648,51 @@ bool DeviceManagerAdapter::IsNetworkAvailable() return isNetAvailable_; } } - NetHandle handle; - auto status = NetConnClient::GetInstance().GetDefaultNet(handle); - return SetNetAvailable(status == 0 && handle.GetNetId() != 0); + return refreshNet().first; } -bool DeviceManagerAdapter::SetNetAvailable(bool isNetAvailable) +void DeviceManagerAdapter::SetNetAvailable(bool isNetAvailable) { std::unique_lock lock(mutex_); isNetAvailable_ = isNetAvailable; expireTime_ = std::chrono::steady_clock::now() + std::chrono::milliseconds(EFFECTIVE_DURATION); - return isNetAvailable_; +} + +void DeviceManagerAdapter::SetNetType(NetworkType netWorkType) +{ + std::unique_lock lock(mutex_); + defaultNetwork_ = netWorkType; +} + +DeviceManagerAdapter::NetworkType DeviceManagerAdapter::GetNetworkType(bool retrieve) +{ + if (!retrieve) { + std::shared_lock lock(mutex_); + return defaultNetwork_; + } + return refreshNet().second; +} + +std::pair DeviceManagerAdapter::refreshNet() +{ + NetHandle handle; + auto status = NetConnClient::GetInstance().GetDefaultNet(handle); + if (status != 0 || handle.GetNetId() == 0) { + SetNetAvailable(false); + SetNetType(NONE); + return { false, NONE }; + } + NetAllCapabilities capabilities; + status = NetConnClient::GetInstance().GetNetCapabilities(handle, capabilities); + if (status != 0 || !capabilities.netCaps_.count(NetManagerStandard::NET_CAPABILITY_VALIDATED) || + capabilities.bearerTypes_.empty()) { + SetNetAvailable(false); + SetNetType(NONE); + return { false, NONE }; + } + SetNetAvailable(true); + auto type = Convert(*capabilities.bearerTypes_.begin()); + SetNetType(type); + return { true, type }; } } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h index 295033a5c..649d48aa9 100644 --- a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h +++ b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h @@ -37,6 +37,13 @@ public: DEVICE_ONREADY, DEVICE_BUTT }; + enum NetworkType { + NONE, + CELLULAR, + WIFI, + ETHERNET, + OTHER + }; using DmDeviceInfo = OHOS::DistributedHardware::DmDeviceInfo; using DeviceInfo = OHOS::AppDistributedKv::DeviceInfo; using PipeInfo = OHOS::AppDistributedKv::PipeInfo; @@ -66,6 +73,7 @@ public: std::string ToNetworkID(const std::string &id); void NotifyReadyEvent(const std::string &uuid); bool IsNetworkAvailable(); + NetworkType GetNetworkType(bool retrieve = false); friend class DataMgrDmStateCall; friend class NetConnCallbackObserver; @@ -74,7 +82,9 @@ private: ~DeviceManagerAdapter(); std::function RegDevCallback(); bool RegOnNetworkChange(); - bool SetNetAvailable(bool isNetAvailable); + void SetNetAvailable(bool isNetAvailable); + void SetNetType(NetworkType netWorkType); + std::pair refreshNet(); bool GetDeviceInfo(const DmDeviceInfo &dmInfo, DeviceInfo &dvInfo); void SaveDeviceInfo(const DeviceInfo &deviceInfo, const AppDistributedKv::DeviceChangeType &type); void InitDeviceInfo(bool onlyCache = true); @@ -100,6 +110,7 @@ private: static constexpr int32_t EFFECTIVE_DURATION = 30 * 1000; // ms Time expireTime_ = std::chrono::steady_clock::now(); bool isNetAvailable_ = false; + NetworkType defaultNetwork_ = NONE; ConcurrentMap> readyDevices_; }; } // namespace DistributedData diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 3e4eb147f..bdc2edd00 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -375,10 +375,9 @@ int32_t CloudServiceImpl::OnUserChange(uint32_t code, const std::string &user, c return E_OK; } -int32_t CloudServiceImpl::Online(const std::string &device) +int32_t CloudServiceImpl::OnReady(const std::string& device) { if (device != DeviceManagerAdapter::CLOUD_DEVICE_UUID) { - ZLOGI("Not network online"); return SUCCESS; } std::vector users; diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index 10c446d04..be13494f3 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -43,7 +43,7 @@ public: int32_t OnInitialize() override; int32_t OnBind(const BindInfo &info) override; int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; - int32_t Online(const std::string &device) override; + int32_t OnReady(const std::string &device) override; int32_t Offline(const std::string &device) override; std::pair> AllocResourceAndShare(const std::string& storeId, diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index 6f17ca9ea..7d2be643d 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -540,6 +540,9 @@ int32_t KVDBServiceImpl::OnUserChange(uint32_t code, const std::string &user, co int32_t KVDBServiceImpl::OnReady(const std::string &device) { + if (device == DeviceManagerAdapter::CLOUD_DEVICE_UUID) { + return SUCCESS; + } std::vector metaData; auto prefix = StoreMetaData::GetPrefix({ DMAdapter::GetInstance().GetLocalDevice().uuid }); if (!MetaDataManager::GetInstance().LoadMeta(prefix, metaData)) { -- Gitee From 16a34877fa939f02ac65a5012eb9e6fc402d7c72 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 27 Feb 2024 20:48:36 +0800 Subject: [PATCH 2/6] refactor:extract common storeInfo Signed-off-by: htt1997 --- .../framework/cloud/cloud_event.cpp | 4 +-- .../framework/cloud/make_query_event.cpp | 2 +- .../framework/include/cloud/cloud_event.h | 10 +----- .../include/snapshot/machine_status.h | 8 ----- .../framework/include/snapshot/snapshot.h | 1 + .../framework/include/store/store_Info.h | 31 +++++++++++++++++++ .../service/cloud/cloud_service_impl.cpp | 4 +-- .../service/cloud/cloud_service_impl.h | 3 +- .../service/cloud/sync_manager.cpp | 4 +-- .../service/cloud/sync_manager.h | 1 + .../service/object/object_asset_machine.h | 1 + .../service/rdb/rdb_service_impl.cpp | 12 +++---- .../service/rdb/rdb_service_impl.h | 2 +- .../service/test/cloud_data_test.cpp | 3 +- .../service/test/cloud_test.cpp | 2 +- 15 files changed, 54 insertions(+), 34 deletions(-) create mode 100644 services/distributeddataservice/framework/include/store/store_Info.h diff --git a/services/distributeddataservice/framework/cloud/cloud_event.cpp b/services/distributeddataservice/framework/cloud/cloud_event.cpp index 8146446d3..bfaf3c9eb 100644 --- a/services/distributeddataservice/framework/cloud/cloud_event.cpp +++ b/services/distributeddataservice/framework/cloud/cloud_event.cpp @@ -16,12 +16,12 @@ #include "cloud/cloud_event.h" namespace OHOS::DistributedData { -CloudEvent::CloudEvent(int32_t evtId, CloudEvent::StoreInfo storeInfo) +CloudEvent::CloudEvent(int32_t evtId, StoreInfo storeInfo) : Event(evtId), storeInfo_(std::move(storeInfo)) { } -const CloudEvent::StoreInfo& CloudEvent::GetStoreInfo() const +const StoreInfo& CloudEvent::GetStoreInfo() const { return storeInfo_; } diff --git a/services/distributeddataservice/framework/cloud/make_query_event.cpp b/services/distributeddataservice/framework/cloud/make_query_event.cpp index 1c1ee97ec..0ed24b9c8 100644 --- a/services/distributeddataservice/framework/cloud/make_query_event.cpp +++ b/services/distributeddataservice/framework/cloud/make_query_event.cpp @@ -16,7 +16,7 @@ #include "cloud/make_query_event.h" namespace OHOS::DistributedData { -MakeQueryEvent::MakeQueryEvent(CloudEvent::StoreInfo storeInfo, +MakeQueryEvent::MakeQueryEvent(StoreInfo storeInfo, std::shared_ptr predicates, const std::vector& columns, Callback callback) : CloudEvent(MAKE_QUERY, std::move(storeInfo)), predicates_(std::move(predicates)), columns_(columns), diff --git a/services/distributeddataservice/framework/include/cloud/cloud_event.h b/services/distributeddataservice/framework/include/cloud/cloud_event.h index 9ce38785c..19837945d 100644 --- a/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -18,6 +18,7 @@ #include #include "eventcenter/event.h" +#include "store/store_info.h" namespace OHOS::DistributedData { class API_EXPORT CloudEvent : public Event { @@ -33,15 +34,6 @@ public: CLOUD_BUTT }; - struct StoreInfo { - uint32_t tokenId = 0; - std::string bundleName; - std::string storeName; - int32_t instanceId = 0; - int32_t user = 0; - std::string deviceId; - }; - CloudEvent(int32_t evtId, StoreInfo storeInfo); ~CloudEvent() = default; const StoreInfo& GetStoreInfo() const; diff --git a/services/distributeddataservice/framework/include/snapshot/machine_status.h b/services/distributeddataservice/framework/include/snapshot/machine_status.h index 8d4aa0bf9..e7c83aff8 100644 --- a/services/distributeddataservice/framework/include/snapshot/machine_status.h +++ b/services/distributeddataservice/framework/include/snapshot/machine_status.h @@ -50,14 +50,6 @@ struct AssetBindInfo { std::string field; std::string assetName; }; - -struct StoreInfo { - uint32_t tokenId = 0; - int32_t instanceId = 0; - int32_t user; - std::string bundleName; - std::string storeName; -}; } // namespace DistributedData } // namespace OHOS #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_SNAPSHOT_MACHINE_STATUS_H diff --git a/services/distributeddataservice/framework/include/snapshot/snapshot.h b/services/distributeddataservice/framework/include/snapshot/snapshot.h index 6cfbbfcf9..8a9fa7eef 100644 --- a/services/distributeddataservice/framework/include/snapshot/snapshot.h +++ b/services/distributeddataservice/framework/include/snapshot/snapshot.h @@ -17,6 +17,7 @@ #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_SNAPSHOT_SNAPSHOT_H #include "machine_status.h" #include "store/general_value.h" +#include "store/store_info.h" #include "visibility.h" namespace OHOS { namespace DistributedData { diff --git a/services/distributeddataservice/framework/include/store/store_Info.h b/services/distributeddataservice/framework/include/store/store_Info.h new file mode 100644 index 000000000..8138955aa --- /dev/null +++ b/services/distributeddataservice/framework/include/store/store_Info.h @@ -0,0 +1,31 @@ +/* +* Copyright (c) 2024 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_DATA_SERVICES_FRAMEWORK_STORE_STORE_INFO_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_INFO_H + +#include + +namespace OHOS::DistributedData { +struct StoreInfo { + uint32_t tokenId = 0; + std::string bundleName; + std::string storeName; + int32_t instanceId = 0; + int32_t user = 0; + std::string deviceId; +}; +} // namespace OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_STORE_INFO_H diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index bdc2edd00..98549b23e 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -621,7 +621,7 @@ void CloudServiceImpl::CloudShare(const Event &event) } std::pair> CloudServiceImpl::PreShare( - const CloudEvent::StoreInfo& storeInfo, GenQuery& query) + const StoreInfo& storeInfo, GenQuery& query) { StoreMetaData meta; meta.bundleName = storeInfo.bundleName; @@ -776,7 +776,7 @@ std::pair> CloudServiceImpl::Alloc return { E_INVALID_ARGS, {} }; } auto memo = std::make_shared(predicates); - CloudEvent::StoreInfo storeInfo; + StoreInfo storeInfo; storeInfo.bundleName = hapInfo.bundleName; storeInfo.tokenId = tokenId; storeInfo.user = hapInfo.user; diff --git a/services/distributeddataservice/service/cloud/cloud_service_impl.h b/services/distributeddataservice/service/cloud/cloud_service_impl.h index be13494f3..3bd1101c1 100644 --- a/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -32,6 +32,7 @@ namespace OHOS::CloudData { class CloudServiceImpl : public CloudServiceStub { public: using StoreMetaData = DistributedData::StoreMetaData; + using StoreInfo = DistributedData::StoreInfo; CloudServiceImpl(); ~CloudServiceImpl() = default; int32_t EnableCloud(const std::string &id, const std::map &switches) override; @@ -129,7 +130,7 @@ private: void Execute(Task task); void CleanSubscription(Subscription &sub); int32_t DoClean(CloudInfo &cloudInfo, const std::map &actions); - std::pair> PreShare(const CloudEvent::StoreInfo& storeInfo, + std::pair> PreShare(const StoreInfo& storeInfo, DistributedData::GenQuery& query); std::vector ConvertCursor(std::shared_ptr cursor) const; int32_t CheckNotifyConditions(const std::string &id, const std::string &bundleName, CloudInfo &cloudInfo); diff --git a/services/distributeddataservice/service/cloud/sync_manager.cpp b/services/distributeddataservice/service/cloud/sync_manager.cpp index d739aae72..7b8f5e71d 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.cpp +++ b/services/distributeddataservice/service/cloud/sync_manager.cpp @@ -225,7 +225,7 @@ ExecutorPool::Task SyncManager::GetSyncTask(int32_t times, bool retry, RefCount if (!info.Contains(database.name)) { continue; } - CloudEvent::StoreInfo storeInfo = { 0, schema.bundleName, database.name, + StoreInfo storeInfo = { 0, schema.bundleName, database.name, cloud.apps[schema.bundleName].instanceId, cloud.user }; auto query = info.GenerateQuery(database.name, database.GetTableNames()); auto evt = std::make_unique(std::move(storeInfo), @@ -347,7 +347,7 @@ int32_t SyncManager::Compare(uint64_t syncId, int32_t user) void SyncManager::UpdateSchema(const SyncManager::SyncInfo &syncInfo) { - CloudEvent::StoreInfo storeInfo; + StoreInfo storeInfo; storeInfo.user = syncInfo.user_; storeInfo.bundleName = syncInfo.bundleName_; EventCenter::GetInstance().PostEvent(std::make_unique(CloudEvent::GET_SCHEMA, storeInfo)); diff --git a/services/distributeddataservice/service/cloud/sync_manager.h b/services/distributeddataservice/service/cloud/sync_manager.h index d68e7b57d..18b21c5eb 100644 --- a/services/distributeddataservice/service/cloud/sync_manager.h +++ b/services/distributeddataservice/service/cloud/sync_manager.h @@ -76,6 +76,7 @@ private: using Duration = ExecutorPool::Duration; using Retryer = std::function; using CloudInfo = DistributedData::CloudInfo; + using StoreInfo = DistributedData::StoreInfo; static constexpr ExecutorPool::Duration RETRY_INTERVAL = std::chrono::seconds(10); // second static constexpr ExecutorPool::Duration LOCKED_INTERVAL = std::chrono::seconds(30); // second diff --git a/services/distributeddataservice/service/object/object_asset_machine.h b/services/distributeddataservice/service/object/object_asset_machine.h index fef1ccee2..14283191b 100644 --- a/services/distributeddataservice/service/object/object_asset_machine.h +++ b/services/distributeddataservice/service/object/object_asset_machine.h @@ -18,6 +18,7 @@ #define DISTRIBUTEDDATAMGR_OBJECT_ASSET_MACHINE_H #include "snapshot/machine_status.h" +#include "store/store_info.h" namespace OHOS { namespace DistributedObject { diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 1ba52d4c7..a26d1e21a 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -428,7 +428,7 @@ int RdbServiceImpl::DoSync(const RdbSyncerParam ¶m, const RdbService::Option void RdbServiceImpl::DoCompensateSync(const BindEvent& event) { auto bindInfo = event.GetBindInfo(); - CloudEvent::StoreInfo storeInfo; + StoreInfo storeInfo; storeInfo.bundleName = bindInfo.bundleName; storeInfo.tokenId = bindInfo.tokenId; storeInfo.user = bindInfo.user; @@ -454,7 +454,7 @@ void RdbServiceImpl::DoCompensateSync(const BindEvent& event) void RdbServiceImpl::DoCloudSync(const RdbSyncerParam ¶m, const RdbService::Option &option, const PredicatesMemo &predicates, const AsyncDetail &async) { - CloudEvent::StoreInfo storeInfo; + StoreInfo storeInfo; storeInfo.bundleName = param.bundleName_; storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); storeInfo.user = AccountDelegate::GetInstance()->GetUserByToken(storeInfo.tokenId); @@ -628,7 +628,7 @@ std::pair> RdbServiceImpl::Query auto rdbQuery = std::make_shared(); rdbQuery->MakeQuery(predicates); rdbQuery->SetColumns(columns); - CloudEvent::StoreInfo storeInfo; + StoreInfo storeInfo; storeInfo.bundleName = param.bundleName_; storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); storeInfo.user = AccountDelegate::GetInstance()->GetUserByToken(storeInfo.tokenId); @@ -642,7 +642,7 @@ std::pair> RdbServiceImpl::Query return { RDB_OK, std::make_shared(cursor) }; } -std::pair> RdbServiceImpl::AllocResource(CloudEvent::StoreInfo& storeInfo, +std::pair> RdbServiceImpl::AllocResource(StoreInfo& storeInfo, std::shared_ptr rdbQuery) { std::pair> result; @@ -668,7 +668,7 @@ int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) } if (executors_ != nullptr) { - CloudEvent::StoreInfo storeInfo; + StoreInfo storeInfo; storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); storeInfo.bundleName = param.bundleName_; storeInfo.storeName = RemoveSuffix(param.storeName_); @@ -959,7 +959,7 @@ int32_t RdbServiceImpl::NotifyDataChange(const RdbSyncerParam ¶m, const RdbC Anonymous::Change(param.storeName_).c_str()); return RDB_ERROR; } - CloudEvent::StoreInfo storeInfo; + StoreInfo storeInfo; storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); storeInfo.bundleName = param.bundleName_; storeInfo.storeName = RemoveSuffix(param.storeName_); diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index 5c21e6f7b..309bf4707 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -43,7 +43,7 @@ public: using SecretKeyMetaData = DistributedData::SecretKeyMetaData; using DetailAsync = DistributedData::GeneralStore::DetailAsync; using Handler = std::function> &)>; - using StoreInfo = DistributedData::CloudEvent::StoreInfo; + using StoreInfo = DistributedData::StoreInfo; RdbServiceImpl(); virtual ~RdbServiceImpl(); diff --git a/services/distributeddataservice/service/test/cloud_data_test.cpp b/services/distributeddataservice/service/test/cloud_data_test.cpp index 6b8c965a2..09f631376 100644 --- a/services/distributeddataservice/service/test/cloud_data_test.cpp +++ b/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -28,6 +28,7 @@ #include "metadata/store_meta_data.h" #include "metadata/store_meta_data_local.h" #include "mock/db_store_mock.h" +#include "store/store_info.h" #include "rdb_types.h" using namespace testing::ext; using namespace OHOS::DistributedData; @@ -188,7 +189,7 @@ HWTEST_F(CloudDataTest, GetSchema, TestSize.Level0) SchemaMeta schemaMeta; ASSERT_FALSE( MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), schemaMeta, true)); - CloudEvent::StoreInfo storeInfo { OHOS::IPCSkeleton::GetCallingTokenID(), TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, 0 }; + StoreInfo storeInfo { OHOS::IPCSkeleton::GetCallingTokenID(), TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, 0 }; auto event = std::make_unique(CloudEvent::GET_SCHEMA, storeInfo); EventCenter::GetInstance().PostEvent(std::move(event)); ASSERT_FALSE( diff --git a/services/distributeddataservice/service/test/cloud_test.cpp b/services/distributeddataservice/service/test/cloud_test.cpp index fd87a48e5..b1edd4951 100644 --- a/services/distributeddataservice/service/test/cloud_test.cpp +++ b/services/distributeddataservice/service/test/cloud_test.cpp @@ -55,7 +55,7 @@ HWTEST_F(CloudTest, EventInfo, TestSize.Level1) SyncEvent::EventInfo eventInfo1(mode, wait, retry, query, async); SyncEvent::EventInfo eventInfo2(std::move(eventInfo1)); SyncEvent::EventInfo eventInfo3 = std::move(eventInfo2); - CloudEvent::StoreInfo storeInfo{ IPCSkeleton::GetCallingTokenID(), testCloudBundle, testCloudStore, 0 }; + StoreInfo storeInfo{ IPCSkeleton::GetCallingTokenID(), testCloudBundle, testCloudStore, 0 }; SyncEvent evt(storeInfo, eventInfo3); EXPECT_EQ(evt.GetMode(), mode); EXPECT_EQ(evt.GetWait(), wait); -- Gitee From f747757807f599ea79ecca9b21354cf1793616eb Mon Sep 17 00:00:00 2001 From: htt1997 Date: Fri, 8 Mar 2024 18:20:23 +0800 Subject: [PATCH 3/6] refactor Signed-off-by: htt1997 --- .../src/device_manager_adapter.cpp | 49 +++++++++---------- .../communicator/device_manager_adapter.h | 3 +- .../store/{store_Info.h => store_info.h} | 0 3 files changed, 25 insertions(+), 27 deletions(-) rename services/distributeddataservice/framework/include/store/{store_Info.h => store_info.h} (100%) diff --git a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp index 3e7e9efdb..7dfa44800 100644 --- a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp +++ b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp @@ -111,14 +111,13 @@ private: int32_t NetConnCallbackObserver::NetAvailable(sptr &netHandle) { ZLOGI("OnNetworkAvailable"); - dmAdapter_.Online(dmAdapter_.cloudDmInfo); return DistributedKv::SUCCESS; } int32_t NetConnCallbackObserver::NetUnavailable() { ZLOGI("OnNetworkUnavailable"); - dmAdapter_.SetNetType(DeviceManagerAdapter::NONE); + dmAdapter_.SetNet(false, DeviceManagerAdapter::NONE); return 0; } @@ -130,12 +129,9 @@ int32_t NetConnCallbackObserver::NetCapabilitiesChange(sptr &netHandl return 0; } if (netAllCap->netCaps_.count(NetManagerStandard::NET_CAPABILITY_VALIDATED) && !netAllCap->bearerTypes_.empty()) { - dmAdapter_.SetNetAvailable(true); - dmAdapter_.SetNetType(Convert(*netAllCap->bearerTypes_.begin())); - dmAdapter_.OnReady(dmAdapter_.cloudDmInfo); + dmAdapter_.SetNet(true, Convert(*netAllCap->bearerTypes_.begin())); } else { - dmAdapter_.SetNetAvailable(false); - dmAdapter_.SetNetType(DeviceManagerAdapter::NONE); + dmAdapter_.SetNet(false, DeviceManagerAdapter::NONE); } return 0; } @@ -150,8 +146,7 @@ int32_t NetConnCallbackObserver::NetConnectionPropertiesChange(sptr & int32_t NetConnCallbackObserver::NetLost(sptr &netHandle) { ZLOGI("OnNetLost"); - dmAdapter_.SetNetAvailable(false); - dmAdapter_.Offline(dmAdapter_.cloudDmInfo); + dmAdapter_.SetNet(false, DeviceManagerAdapter::NONE); return 0; } @@ -651,17 +646,24 @@ bool DeviceManagerAdapter::IsNetworkAvailable() return refreshNet().first; } -void DeviceManagerAdapter::SetNetAvailable(bool isNetAvailable) +void DeviceManagerAdapter::SetNet(bool isNetAvailable, NetworkType netWorkType) { - std::unique_lock lock(mutex_); - isNetAvailable_ = isNetAvailable; - expireTime_ = std::chrono::steady_clock::now() + std::chrono::milliseconds(EFFECTIVE_DURATION); -} - -void DeviceManagerAdapter::SetNetType(NetworkType netWorkType) -{ - std::unique_lock lock(mutex_); - defaultNetwork_ = netWorkType; + bool ready = false; + bool offline = false; + { + std::unique_lock lock(mutex_); + ready = !isNetAvailable_ && isNetAvailable; + offline = isNetAvailable_ && !isNetAvailable; + isNetAvailable_ = isNetAvailable; + defaultNetwork_ = netWorkType; + expireTime_ = std::chrono::steady_clock::now() + std::chrono::milliseconds(EFFECTIVE_DURATION); + } + if (ready) { + OnReady(cloudDmInfo); + } + if (offline) { + Offline(cloudDmInfo); + } } DeviceManagerAdapter::NetworkType DeviceManagerAdapter::GetNetworkType(bool retrieve) @@ -678,21 +680,18 @@ std::pair DeviceManagerAdapter::refresh NetHandle handle; auto status = NetConnClient::GetInstance().GetDefaultNet(handle); if (status != 0 || handle.GetNetId() == 0) { - SetNetAvailable(false); - SetNetType(NONE); + SetNet(false, DeviceManagerAdapter::NONE); return { false, NONE }; } NetAllCapabilities capabilities; status = NetConnClient::GetInstance().GetNetCapabilities(handle, capabilities); if (status != 0 || !capabilities.netCaps_.count(NetManagerStandard::NET_CAPABILITY_VALIDATED) || capabilities.bearerTypes_.empty()) { - SetNetAvailable(false); - SetNetType(NONE); + SetNet(false, DeviceManagerAdapter::NONE); return { false, NONE }; } - SetNetAvailable(true); auto type = Convert(*capabilities.bearerTypes_.begin()); - SetNetType(type); + SetNet(true, type); return { true, type }; } } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h index 649d48aa9..133e9fc1c 100644 --- a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h +++ b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h @@ -82,8 +82,7 @@ private: ~DeviceManagerAdapter(); std::function RegDevCallback(); bool RegOnNetworkChange(); - void SetNetAvailable(bool isNetAvailable); - void SetNetType(NetworkType netWorkType); + void SetNet(bool isNetAvailable, NetworkType netWorkType); std::pair refreshNet(); bool GetDeviceInfo(const DmDeviceInfo &dmInfo, DeviceInfo &dvInfo); void SaveDeviceInfo(const DeviceInfo &deviceInfo, const AppDistributedKv::DeviceChangeType &type); diff --git a/services/distributeddataservice/framework/include/store/store_Info.h b/services/distributeddataservice/framework/include/store/store_info.h similarity index 100% rename from services/distributeddataservice/framework/include/store/store_Info.h rename to services/distributeddataservice/framework/include/store/store_info.h -- Gitee From 57d6da1ce61a3caf6ea150e256567ffff951f584 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Sat, 9 Mar 2024 10:20:44 +0800 Subject: [PATCH 4/6] refactor:Determine the duration of network disconnection Signed-off-by: htt1997 --- .../communicator/src/device_manager_adapter.cpp | 12 ++++++++---- .../include/communicator/device_manager_adapter.h | 4 +++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp index 7dfa44800..b1a39a1fe 100644 --- a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp +++ b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp @@ -643,7 +643,7 @@ bool DeviceManagerAdapter::IsNetworkAvailable() return isNetAvailable_; } } - return refreshNet().first; + return RefreshNet().first; } void DeviceManagerAdapter::SetNet(bool isNetAvailable, NetworkType netWorkType) @@ -652,8 +652,12 @@ void DeviceManagerAdapter::SetNet(bool isNetAvailable, NetworkType netWorkType) bool offline = false; { std::unique_lock lock(mutex_); - ready = !isNetAvailable_ && isNetAvailable; + ready = !isNetAvailable_ && isNetAvailable && + (std::chrono::steady_clock::now() - netLostTime_) > std::chrono::milliseconds(NET_LOST_DURATION); offline = isNetAvailable_ && !isNetAvailable; + if (offline) { + netLostTime_ = std::chrono::steady_clock::now(); + } isNetAvailable_ = isNetAvailable; defaultNetwork_ = netWorkType; expireTime_ = std::chrono::steady_clock::now() + std::chrono::milliseconds(EFFECTIVE_DURATION); @@ -672,10 +676,10 @@ DeviceManagerAdapter::NetworkType DeviceManagerAdapter::GetNetworkType(bool retr std::shared_lock lock(mutex_); return defaultNetwork_; } - return refreshNet().second; + return RefreshNet().second; } -std::pair DeviceManagerAdapter::refreshNet() +std::pair DeviceManagerAdapter::RefreshNet() { NetHandle handle; auto status = NetConnClient::GetInstance().GetDefaultNet(handle); diff --git a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h index 133e9fc1c..14caa90a7 100644 --- a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h +++ b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h @@ -83,7 +83,7 @@ private: std::function RegDevCallback(); bool RegOnNetworkChange(); void SetNet(bool isNetAvailable, NetworkType netWorkType); - std::pair refreshNet(); + std::pair RefreshNet(); bool GetDeviceInfo(const DmDeviceInfo &dmInfo, DeviceInfo &dvInfo); void SaveDeviceInfo(const DeviceInfo &deviceInfo, const AppDistributedKv::DeviceChangeType &type); void InitDeviceInfo(bool onlyCache = true); @@ -107,7 +107,9 @@ private: std::shared_ptr executors_; mutable std::shared_mutex mutex_; static constexpr int32_t EFFECTIVE_DURATION = 30 * 1000; // ms + static constexpr int32_t NET_LOST_DURATION = 10 * 1000; // ms Time expireTime_ = std::chrono::steady_clock::now(); + Time netLostTime_ = std::chrono::steady_clock::now(); bool isNetAvailable_ = false; NetworkType defaultNetwork_ = NONE; ConcurrentMap> readyDevices_; -- Gitee From 63270c58f8f4222406715abab789be96bb74c857 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Mon, 11 Mar 2024 22:03:36 +0800 Subject: [PATCH 5/6] fix: don't unwatch when Busy Signed-off-by: htt1997 --- services/distributeddataservice/framework/store/auto_cache.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributeddataservice/framework/store/auto_cache.cpp b/services/distributeddataservice/framework/store/auto_cache.cpp index b4f8647c6..b7a5b7d1d 100644 --- a/services/distributeddataservice/framework/store/auto_cache.cpp +++ b/services/distributeddataservice/framework/store/auto_cache.cpp @@ -227,11 +227,11 @@ bool AutoCache::Delegate::Close() { std::unique_lock lock(mutex_); if (store_ != nullptr) { - store_->Unwatch(Origin::ORIGIN_ALL, *this); auto status = store_->Close(); if (status == Error::E_BUSY) { return false; } + store_->Unwatch(Origin::ORIGIN_ALL, *this); } return true; } -- Gitee From b3b2d1d6465ba0934caf822b1c1f1fa8bc4e3651 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 12 Mar 2024 20:57:20 +0800 Subject: [PATCH 6/6] refactor:delete lock Signed-off-by: htt1997 --- .../src/device_manager_adapter.cpp | 55 +++++++------------ .../communicator/device_manager_adapter.h | 16 ++++-- 2 files changed, 31 insertions(+), 40 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp index b1a39a1fe..94b0e5f96 100644 --- a/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp +++ b/services/distributeddataservice/adapter/communicator/src/device_manager_adapter.cpp @@ -117,7 +117,7 @@ int32_t NetConnCallbackObserver::NetAvailable(sptr &netHandl return 0; } if (netAllCap->netCaps_.count(NetManagerStandard::NET_CAPABILITY_VALIDATED) && !netAllCap->bearerTypes_.empty()) { - dmAdapter_.SetNet(true, Convert(*netAllCap->bearerTypes_.begin())); + dmAdapter_.SetNet(Convert(*netAllCap->bearerTypes_.begin())); } else { - dmAdapter_.SetNet(false, DeviceManagerAdapter::NONE); + dmAdapter_.SetNet(DeviceManagerAdapter::NONE); } return 0; } @@ -146,7 +146,7 @@ int32_t NetConnCallbackObserver::NetConnectionPropertiesChange(sptr & int32_t NetConnCallbackObserver::NetLost(sptr &netHandle) { ZLOGI("OnNetLost"); - dmAdapter_.SetNet(false, DeviceManagerAdapter::NONE); + dmAdapter_.SetNet(DeviceManagerAdapter::NONE); return 0; } @@ -637,65 +637,52 @@ bool DeviceManagerAdapter::RegOnNetworkChange() bool DeviceManagerAdapter::IsNetworkAvailable() { - { - std::shared_lock lock(mutex_); - if (isNetAvailable_ || expireTime_ > std::chrono::steady_clock::now()) { - return isNetAvailable_; - } + if (defaultNetwork_ != NONE || expireTime_ > GetTimeStamp()) { + return defaultNetwork_ != NONE; } - return RefreshNet().first; + return RefreshNet() != NONE; } -void DeviceManagerAdapter::SetNet(bool isNetAvailable, NetworkType netWorkType) +DeviceManagerAdapter::NetworkType DeviceManagerAdapter::SetNet(NetworkType netWorkType) { - bool ready = false; - bool offline = false; - { - std::unique_lock lock(mutex_); - ready = !isNetAvailable_ && isNetAvailable && - (std::chrono::steady_clock::now() - netLostTime_) > std::chrono::milliseconds(NET_LOST_DURATION); - offline = isNetAvailable_ && !isNetAvailable; - if (offline) { - netLostTime_ = std::chrono::steady_clock::now(); - } - isNetAvailable_ = isNetAvailable; - defaultNetwork_ = netWorkType; - expireTime_ = std::chrono::steady_clock::now() + std::chrono::milliseconds(EFFECTIVE_DURATION); + auto oldNet = defaultNetwork_; + bool ready = oldNet == NONE && netWorkType != NONE && (GetTimeStamp() - netLostTime_) > NET_LOST_DURATION; + bool offline = oldNet != NONE && netWorkType == NONE; + if (offline) { + netLostTime_ = GetTimeStamp(); } + defaultNetwork_ = netWorkType; + expireTime_ = GetTimeStamp() + EFFECTIVE_DURATION; if (ready) { OnReady(cloudDmInfo); } if (offline) { Offline(cloudDmInfo); } + return netWorkType; } DeviceManagerAdapter::NetworkType DeviceManagerAdapter::GetNetworkType(bool retrieve) { if (!retrieve) { - std::shared_lock lock(mutex_); return defaultNetwork_; } - return RefreshNet().second; + return RefreshNet(); } -std::pair DeviceManagerAdapter::RefreshNet() +DeviceManagerAdapter::NetworkType DeviceManagerAdapter::RefreshNet() { NetHandle handle; auto status = NetConnClient::GetInstance().GetDefaultNet(handle); if (status != 0 || handle.GetNetId() == 0) { - SetNet(false, DeviceManagerAdapter::NONE); - return { false, NONE }; + return SetNet(NONE); } NetAllCapabilities capabilities; status = NetConnClient::GetInstance().GetNetCapabilities(handle, capabilities); if (status != 0 || !capabilities.netCaps_.count(NetManagerStandard::NET_CAPABILITY_VALIDATED) || capabilities.bearerTypes_.empty()) { - SetNet(false, DeviceManagerAdapter::NONE); - return { false, NONE }; + return SetNet(NONE); } - auto type = Convert(*capabilities.bearerTypes_.begin()); - SetNet(true, type); - return { true, type }; + return SetNet(Convert(*capabilities.bearerTypes_.begin())); } } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h index 14caa90a7..30a9b393e 100644 --- a/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h +++ b/services/distributeddataservice/adapter/include/communicator/device_manager_adapter.h @@ -82,8 +82,8 @@ private: ~DeviceManagerAdapter(); std::function RegDevCallback(); bool RegOnNetworkChange(); - void SetNet(bool isNetAvailable, NetworkType netWorkType); - std::pair RefreshNet(); + NetworkType SetNet(NetworkType netWorkType); + NetworkType RefreshNet(); bool GetDeviceInfo(const DmDeviceInfo &dmInfo, DeviceInfo &dvInfo); void SaveDeviceInfo(const DeviceInfo &deviceInfo, const AppDistributedKv::DeviceChangeType &type); void InitDeviceInfo(bool onlyCache = true); @@ -95,6 +95,12 @@ private: void OnReady(const DmDeviceInfo &info); void TimeOut(const std::string uuid); std::vector GetObservers(); + static inline uint64_t GetTimeStamp() + { + return std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()) + .count(); + } std::mutex devInfoMutex_ {}; DeviceInfo localInfo_ {}; @@ -105,12 +111,10 @@ private: static constexpr int32_t SYNC_TIMEOUT = 60 * 1000; // ms ConcurrentMap syncTask_ {}; std::shared_ptr executors_; - mutable std::shared_mutex mutex_; static constexpr int32_t EFFECTIVE_DURATION = 30 * 1000; // ms static constexpr int32_t NET_LOST_DURATION = 10 * 1000; // ms - Time expireTime_ = std::chrono::steady_clock::now(); - Time netLostTime_ = std::chrono::steady_clock::now(); - bool isNetAvailable_ = false; + uint64_t expireTime_ = GetTimeStamp(); + uint64_t netLostTime_ = GetTimeStamp(); NetworkType defaultNetwork_ = NONE; ConcurrentMap> readyDevices_; }; -- Gitee