From 55ae9c72ca60ab7db117534aad9b9f0baa3ad819 Mon Sep 17 00:00:00 2001 From: m30043719 Date: Sun, 2 Jun 2024 08:29:01 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=E4=BA=8B=E4=BB=B6=E7=9B=91?= =?UTF-8?q?=E5=90=AC=E3=80=81=E6=9F=A5=E8=AF=A2=E6=8E=A5=E5=8F=A3demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: m30043719 --- interfaces/innerkits/tests/dms_sdk_demo.cpp | 62 +++++++++++++++---- interfaces/innerkits/tests/dms_sdk_demo.h | 7 ++- .../mission/distributed_data_storage.h | 5 +- .../src/adapter/dnetwork_adapter.cpp | 5 -- .../src/distributed_sched_service.cpp | 11 ++-- .../src/mission/distributed_bm_storage.cpp | 38 +++++------- .../src/mission/distributed_data_storage.cpp | 42 +++++++++---- .../src/mission/dms_continue_recv_manager.cpp | 2 +- 8 files changed, 112 insertions(+), 60 deletions(-) diff --git a/interfaces/innerkits/tests/dms_sdk_demo.cpp b/interfaces/innerkits/tests/dms_sdk_demo.cpp index d00eef25..18e6c246 100644 --- a/interfaces/innerkits/tests/dms_sdk_demo.cpp +++ b/interfaces/innerkits/tests/dms_sdk_demo.cpp @@ -26,15 +26,14 @@ using namespace std; Business g_business; namespace { DmsHandler &dmsSourceHandlerdemo = DmsHandler::GetInstance(); -DSchedEventType g_type = DMS_CONTINUE; sptr listener = sptr(new Business()); ContinueInfo g_continueInfo; } -void Business::Register() +void Business::Register(DSchedEventType type) { int32_t result = 0; - result = dmsSourceHandlerdemo.RegisterDSchedEventListener(g_type, listener); + result = dmsSourceHandlerdemo.RegisterDSchedEventListener(type, listener); if (result < 0) { cout << "RegisterDSchedEventListener failed.CODE = " << result << endl; } else { @@ -42,10 +41,10 @@ void Business::Register() } } -void Business::UnRegister() +void Business::UnRegister(DSchedEventType type) { int32_t result = 0; - result = dmsSourceHandlerdemo.UnRegisterDSchedEventListener(g_type, listener); + result = dmsSourceHandlerdemo.UnRegisterDSchedEventListener(type, listener); if (result < 0) { cout << "UnRegisterDSchedEventListener failed.CODE = " << result << endl; } else { @@ -53,7 +52,7 @@ void Business::UnRegister() } } -void Business::GetContinueInfo() +void Business::GetContinueDeviceInfo() { int32_t result = 0; result = dmsSourceHandlerdemo.GetContinueInfo(g_continueInfo); @@ -65,6 +64,31 @@ void Business::GetContinueInfo() } } +void Business::GetDSchedEventInfo(DSchedEventType type) +{ + vector notifys; + int32_t result = dmsSourceHandlerdemo.GetDSchedEventInfo(type, notifys); + if (result < 0) { + cout << "GetContinueInfo failed.CODE = " << result << endl; + } else { + for (auto notify : notifys) { + cout << endl << "DSchedEventInfo:" << endl; + cout << "eventResult: " << notify.eventResult_ << endl; + cout << "srcNetworkId: " << notify.srcNetworkId_ << endl; + cout << "dstNetworkId: " << notify.dstNetworkId_ << endl; + cout << "srcBundleName: " << notify.srcBundleName_ << endl; + cout << "srcModuleName: " << notify.srcModuleName_ << endl; + cout << "srcAbilityName: " << notify.srcAbilityName_ << endl; + cout << "destBundleName: " << notify.destBundleName_ << endl; + cout << "destModuleName: " << notify.destModuleName_ << endl; + cout << "destAbilityName: " << notify.destAbilityName_ << endl; + cout << "dSchedEventType: " << notify.dSchedEventType_ << endl; + cout << "state: " << notify.state_ << endl << endl; + } + } + notifys.clear(); +} + void Business::DSchedEventNotify(EventNotify& notify) { cout << endl << "DSchedEventNotify Start." << endl; @@ -81,11 +105,13 @@ void Business::DSchedEventNotify(EventNotify& notify) cout << "state: " << notify.state_ << endl; cout << "DSchedEventNotify Success." << endl; } - int main() { cout << "Please select an option to test the interface:" << endl; - cout << "A.RegisterDSchedEventListener B.UnRegisterDSchedEventListener C.GetContinueInfo X.exit" << endl; + cout << "A.RegisterContinueListener B.UnRegisterContinueListener C.GetContinueInfo" << endl; + cout << "D.RegisterCollaborationListener E.UnRegisterCollaborationListener F.GetCollaborationInfo" << endl; + cout << "G.RegisterAllListener H.UnRegisterAllListener I.GetAllInfo" << endl; + cout << "J.GetContinueDeviceInfo X.exit" << endl; cout << "\n" << endl; char cmd; @@ -94,11 +120,25 @@ int main() cmd = cmd + 'A' - 'a'; } switch (cmd) { - case 'A' : g_business.Register(); + case 'A' : g_business.Register(DMS_CONTINUE); break; - case 'B' : g_business.UnRegister(); + case 'B' : g_business.UnRegister(DMS_CONTINUE); break; - case 'C' : g_business.GetContinueInfo(); + case 'C' : g_business.GetDSchedEventInfo(DMS_CONTINUE); + break; + case 'D' : g_business.Register(DMS_COLLABORATION); + break; + case 'E' : g_business.UnRegister(DMS_COLLABORATION); + break; + case 'F' : g_business.GetDSchedEventInfo(DMS_COLLABORATION); + break; + case 'G' : g_business.Register(DMS_ALL); + break; + case 'H' : g_business.UnRegister(DMS_ALL); + break; + case 'I' : g_business.GetDSchedEventInfo(DMS_ALL); + break; + case 'J' : g_business.GetContinueDeviceInfo(); break; case 'X' : return 0; diff --git a/interfaces/innerkits/tests/dms_sdk_demo.h b/interfaces/innerkits/tests/dms_sdk_demo.h index 6529fa5c..ec9fb775 100644 --- a/interfaces/innerkits/tests/dms_sdk_demo.h +++ b/interfaces/innerkits/tests/dms_sdk_demo.h @@ -26,9 +26,10 @@ namespace DistributedSchedule { class Business : public DSchedEventListenerStub { public: void DSchedEventNotify(EventNotify ¬ify) override; - void Register(); - void UnRegister(); - void GetContinueInfo(); + void Register(DSchedEventType type); + void UnRegister(DSchedEventType type); + void GetContinueDeviceInfo(); + void GetDSchedEventInfo(DSchedEventType type); }; } } diff --git a/services/dtbschedmgr/include/mission/distributed_data_storage.h b/services/dtbschedmgr/include/mission/distributed_data_storage.h index 89b943de..1901f7fe 100644 --- a/services/dtbschedmgr/include/mission/distributed_data_storage.h +++ b/services/dtbschedmgr/include/mission/distributed_data_storage.h @@ -17,6 +17,7 @@ #define OHOS_DISTRIBUTED_DATA_STORAGE_H #include +#include #include #include #include @@ -99,9 +100,10 @@ private: bool InsertInnerLocked(const std::string& uuid, int32_t missionId, const uint8_t* byteStream, size_t len); bool DeleteInnerLocked(const std::string& uuid, int32_t missionId); bool FuzzyDeleteInnerLocked(const std::string& networkId); - bool QueryInnerLocked(const std::string& uuid, int32_t missionId, DistributedKv::Value& value) const; + bool QueryInnerLocked(const std::string& networkId, int32_t missionId, DistributedKv::Value& value) const; static void GenerateKey(const std::string& uuid, int32_t missionId, DistributedKv::Key& key); static void GenerateValue(const uint8_t* byteStream, size_t len, DistributedKv::Value& value); + OHOS::DistributedKv::Status GetResultSatus(std::promise &resultStatusSignal) const; mutable std::shared_mutex initLock_; std::shared_ptr dmsDataStorageHandler_; @@ -111,6 +113,7 @@ private: std::shared_ptr kvStorePtr_; // protected by initLock_ std::unique_ptr distributedDataChangeListener_; sptr kvStoreDeathRecipient_; + int32_t waittingTime_ = 180; // 3 s }; } // DistributedSchedule } // OHOS diff --git a/services/dtbschedmgr/src/adapter/dnetwork_adapter.cpp b/services/dtbschedmgr/src/adapter/dnetwork_adapter.cpp index f5d95ede..44c8568e 100644 --- a/services/dtbschedmgr/src/adapter/dnetwork_adapter.cpp +++ b/services/dtbschedmgr/src/adapter/dnetwork_adapter.cpp @@ -26,7 +26,6 @@ #include "distributed_sched_utils.h" #include "dtbschedmgr_device_info_storage.h" #include "dtbschedmgr_log.h" -#include "mission/distributed_bm_storage.h" namespace OHOS { namespace DistributedSchedule { @@ -106,10 +105,6 @@ void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceChanged(const DmDeviceInfo void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceReady(const DmDeviceInfo& deviceInfo) { HILOGI("called"); - std::vector networkIdList; - networkIdList.push_back(deviceInfo.networkId); - DmsBmStorage::GetInstance()->PullOtherDistributedData(networkIdList); - DmsBmStorage::GetInstance()->PushOtherDistributedData(networkIdList); } bool DnetworkAdapter::AddDeviceChangeListener(const std::shared_ptr& listener) diff --git a/services/dtbschedmgr/src/distributed_sched_service.cpp b/services/dtbschedmgr/src/distributed_sched_service.cpp index 22705222..cdb32990 100644 --- a/services/dtbschedmgr/src/distributed_sched_service.cpp +++ b/services/dtbschedmgr/src/distributed_sched_service.cpp @@ -2461,8 +2461,8 @@ int32_t DistributedSchedService::SetMissionContinueState(int32_t missionId, cons int32_t DistributedSchedService::RegisterDSchedEventListener(const DSchedEventType& type, const sptr& callback) { - if (dschedContinuation_ == nullptr) { - HILOGE("continuation object null!"); + if (dschedContinuation_ == nullptr || collaborateCbMgr_ == nullptr) { + HILOGE("object null!"); return INVALID_PARAMETERS_ERR; } bool ret = false; @@ -2492,8 +2492,8 @@ int32_t DistributedSchedService::RegisterDSchedEventListener(const DSchedEventTy int32_t DistributedSchedService::UnRegisterDSchedEventListener(const DSchedEventType& type, const sptr& callback) { - if (dschedContinuation_ == nullptr) { - HILOGE("continuation object null!"); + if (dschedContinuation_ == nullptr || collaborateCbMgr_ == nullptr) { + HILOGE("object null!"); return INVALID_PARAMETERS_ERR; } bool result = 0; @@ -2502,9 +2502,10 @@ int32_t DistributedSchedService::UnRegisterDSchedEventListener(const DSchedEvent result = dschedContinuation_->CleanupCallback(callback); break; case DMS_COLLABORATION: + result = collaborateCbMgr_->CleanupCallback(callback); break; case DMS_ALL: - result = dschedContinuation_->CleanupCallback(callback); + result = dschedContinuation_->CleanupCallback(callback) && collaborateCbMgr_->CleanupCallback(callback); break; default: break; diff --git a/services/dtbschedmgr/src/mission/distributed_bm_storage.cpp b/services/dtbschedmgr/src/mission/distributed_bm_storage.cpp index 4889e550..72b0b13c 100644 --- a/services/dtbschedmgr/src/mission/distributed_bm_storage.cpp +++ b/services/dtbschedmgr/src/mission/distributed_bm_storage.cpp @@ -70,7 +70,7 @@ std::shared_ptr DmsBmStorage::GetInstance() bool DmsBmStorage::SaveStorageDistributeInfo(const std::string &bundleName) { - HILOGD("called."); + HILOGI("called."); if (!CheckKvStore()) { HILOGE("kvStore is nullptr"); return false; @@ -98,9 +98,7 @@ bool DmsBmStorage::SaveStorageDistributeInfo(const std::string &bundleName) HILOGW("InnerSaveStorageDistributeInfo:%{public}s failed", bundleName.c_str()); return false; } - std::vector networkIdList = DtbschedmgrDeviceInfoStorage::GetInstance().GetNetworkIdList(); - PushOtherDistributedData(networkIdList); - HILOGD("end."); + HILOGI("end."); return true; } @@ -181,16 +179,14 @@ bool DmsBmStorage::DeleteStorageDistributeInfo(const std::string &bundleName) return false; } DelBundleNameId(bundleName); - std::vector networkIdList = DtbschedmgrDeviceInfoStorage::GetInstance().GetNetworkIdList(); - PushOtherDistributedData(networkIdList); - HILOGD("delete value to kvStore success"); + HILOGI("delete value to kvStore success"); return true; } bool DmsBmStorage::GetStorageDistributeInfo(const std::string &networkId, const std::string &bundleName, DmsBundleInfo &info) { - HILOGD("called."); + HILOGI("called."); if (!CheckKvStore()) { HILOGE("kvStore is nullptr"); return false; @@ -218,7 +214,7 @@ bool DmsBmStorage::GetStorageDistributeInfo(const std::string &networkId, info.FromJsonString(value.ToString()); return true; } - HILOGD("end."); + HILOGI("end."); return false; } @@ -253,32 +249,27 @@ bool DmsBmStorage::DealGetBundleName(const std::string &networkId, const uint16_ DmsBundleInfo distributedBundleInfo; if (distributedBundleInfo.FromJsonString(value) && distributedBundleInfo.bundleNameId == bundleNameId) { bundleName = distributedBundleInfo.bundleName; - HILOGD("end."); + HILOGI("end."); return true; } } - HILOGE("get bundleName failed"); return false; } bool DmsBmStorage::GetDistributedBundleName(const std::string &networkId, const uint16_t& bundleNameId, std::string &bundleName) { - HILOGD("networkId: %{public}s bundleNameId: %{public}d", GetAnonymStr(networkId).c_str(), bundleNameId); + HILOGI("networkId: %{public}s bundleNameId: %{public}d", GetAnonymStr(networkId).c_str(), bundleNameId); bool ret = DealGetBundleName(networkId, bundleNameId, bundleName); if (!ret) { - HILOGW("GetDistributedBundleName error and try to call again"); - std::vector networkIdList; - networkIdList.push_back(networkId); - PullOtherDistributedData(networkIdList); + HILOGW("get bundleName failed and try to call again"); ret = DealGetBundleName(networkId, bundleNameId, bundleName); } if (bundleName == "") { HILOGE("GetBundleName fail"); - DeleteStorageDistributeInfo(bundleName); return false; } - HILOGD("end."); + HILOGI("end."); return true; } @@ -295,6 +286,7 @@ Status DmsBmStorage::GetResultSatus(std::promise &r void DmsBmStorage::GetEntries(const std::string &networkId, const Key &allEntryKeyPrefix, std::promise &resultStatusSignal, std::vector &allEntries) { + HILOGI("called."); kvStorePtr_->GetEntries(allEntryKeyPrefix, networkId, [&resultStatusSignal, &allEntries](Status innerStatus, std::vector innerAllEntries) { HILOGI("GetEntries, result = %{public}d", innerStatus); @@ -303,6 +295,7 @@ void DmsBmStorage::GetEntries(const std::string &networkId, const Key &allEntryK } resultStatusSignal.set_value(innerStatus); }); + HILOGI("end."); } bool DmsBmStorage::GetBundleNameId(const std::string& bundleName, uint16_t &bundleNameId) @@ -549,7 +542,7 @@ bool IsContinuable(AppExecFwk::BundleInfo bundleInfo) void DmsBmStorage::UpdateDistributedData() { - HILOGD("called."); + HILOGI("called."); std::string udid; DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalUdid(udid); if (udid == "") { @@ -589,10 +582,7 @@ void DmsBmStorage::UpdateDistributedData() HILOGW("UpdateDistributedData SaveStorageDistributeInfo:%{public}s failed", bundleInfo.name.c_str()); } } - std::vector networkIdList = DtbschedmgrDeviceInfoStorage::GetInstance().GetNetworkIdList(); - PushOtherDistributedData(networkIdList); - PullOtherDistributedData(networkIdList); - HILOGD("end."); + HILOGI("end."); } void DmsBmStorage::AddBundleNameId(const uint16_t &bundleNameId, const std::string &bundleName) @@ -665,6 +655,7 @@ std::string FindContinueType(const DmsBundleInfo& distributedBundleInfo, uint8_t std::string DmsBmStorage::GetContinueType(const std::string &networkId, std::string &bundleName, uint8_t continueTypeId) { + HILOGI("called."); HILOGD("networkId: %{public}s, bundleName: %{public}s, continueTypeId: %{public}d", GetAnonymStr(networkId).c_str(), bundleName.c_str(), continueTypeId); if (!CheckKvStore()) { @@ -730,6 +721,7 @@ std::string FindModuleName(const DmsBundleInfo& distributedBundleInfo, std::stri std::string DmsBmStorage::GetAbilityName(const std::string &networkId, std::string &bundleName, std::string &continueType) { + HILOGI("called."); HILOGD("networkId: %{public}s, bundleName: %{public}s, continueTypeId: %{public}s", GetAnonymStr(networkId).c_str(), bundleName.c_str(), continueType.c_str()); if (!CheckKvStore()) { diff --git a/services/dtbschedmgr/src/mission/distributed_data_storage.cpp b/services/dtbschedmgr/src/mission/distributed_data_storage.cpp index f9432d70..0422829a 100644 --- a/services/dtbschedmgr/src/mission/distributed_data_storage.cpp +++ b/services/dtbschedmgr/src/mission/distributed_data_storage.cpp @@ -144,7 +144,7 @@ Status DistributedDataStorage::GetKvStore() Options options = { .createIfMissing = true, .encrypt = false, - .autoSync = true, + .autoSync = false, .securityLevel = DistributedKv::SecurityLevel::S2, .area = 1, .kvStoreType = KvStoreType::SINGLE_VERSION, @@ -370,24 +370,20 @@ bool DistributedDataStorage::Query(const string& networkId, int32_t missionId, V HILOGW("missionId is invalid!"); return false; } - string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(networkId); - if (uuid.empty()) { - HILOGW("uuid is empty!"); - return false; - } { shared_lock readLock(initLock_); - bool ret = QueryInnerLocked(uuid, missionId, value); + bool ret = QueryInnerLocked(networkId, missionId, value); if (!ret) { - HILOGE("Query uuid: %{public}s, missionId: %{public}d fail.", GetAnonymStr(uuid).c_str(), missionId); + HILOGE("Query networkId: %{public}s, missionId: %{public}d fail.", + GetAnonymStr(networkId).c_str(), missionId); return false; } } - HILOGI("Query uuid: %{public}s, missionId: %{public}d success.", GetAnonymStr(uuid).c_str(), missionId); + HILOGI("Query networkId: %{public}s, missionId: %{public}d success.", GetAnonymStr(networkId).c_str(), missionId); return true; } -bool DistributedDataStorage::QueryInnerLocked(const string& uuid, int32_t missionId, Value& value) const +bool DistributedDataStorage::QueryInnerLocked(const string& networkId, int32_t missionId, Value& value) const { HILOGD("called."); int64_t begin = GetTickCount(); @@ -395,9 +391,23 @@ bool DistributedDataStorage::QueryInnerLocked(const string& uuid, int32_t missio HILOGW("kvStorePtr is null!"); return false; } + string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(networkId); + if (uuid.empty()) { + HILOGW("uuid is empty!"); + return false; + } Key key; GenerateKey(uuid, missionId, key); - auto status = kvStorePtr_->Get(key, value); + std::promise resultStatusSignal; + kvStorePtr_->Get(key, networkId, + [&value, &resultStatusSignal](Status innerStatus, Value innerValue) { + HILOGI("The get, result = %{public}d", innerStatus); + if (innerStatus == Status::SUCCESS) { + value = innerValue; + } + resultStatusSignal.set_value(innerStatus); + }); + Status status = GetResultSatus(resultStatusSignal); HILOGI("[PerformanceTest] Get Snapshot spend %{public}" PRId64 " ms", GetTickCount() - begin); if (status != Status::SUCCESS) { HILOGE("kvStorePtr Get failed! status = %{public}d.", status); @@ -406,6 +416,16 @@ bool DistributedDataStorage::QueryInnerLocked(const string& uuid, int32_t missio return true; } +Status DistributedDataStorage::GetResultSatus(std::promise &resultStatusSignal) const +{ + auto future = resultStatusSignal.get_future(); + if (future.wait_for(std::chrono::seconds(waittingTime_)) == std::future_status::ready) { + Status status = future.get(); + return status; + } + return Status::ERROR; +} + void DistributedDataStorage::GenerateKey(const string& uuid, int32_t missionId, Key& key) { string keyString; diff --git a/services/dtbschedmgr/src/mission/dms_continue_recv_manager.cpp b/services/dtbschedmgr/src/mission/dms_continue_recv_manager.cpp index 5b11653d..6b8aef76 100644 --- a/services/dtbschedmgr/src/mission/dms_continue_recv_manager.cpp +++ b/services/dtbschedmgr/src/mission/dms_continue_recv_manager.cpp @@ -38,7 +38,7 @@ constexpr int32_t ACTIVE = 0; constexpr int32_t INACTIVE = 1; constexpr int32_t INDEX_2 = 2; constexpr int32_t INDEX_3 = 3; -constexpr int32_t DBMS_RETRY_MAX_TIME = 5; +constexpr int32_t DBMS_RETRY_MAX_TIME = 0; constexpr int32_t DBMS_RETRY_DELAY = 1000; const std::string TAG = "DMSContinueRecvMgr"; const std::string DBMS_RETRY_TASK = "retry_on_boradcast_task";