适配事件监听、查询接口demo

Signed-off-by: m30043719 <maxiaodong25@huawei.com>
This commit is contained in:
m30043719 2024-06-02 08:29:01 +08:00
parent ff19bced46
commit 55ae9c72ca
8 changed files with 112 additions and 60 deletions

View File

@ -26,15 +26,14 @@ using namespace std;
Business g_business;
namespace {
DmsHandler &dmsSourceHandlerdemo = DmsHandler::GetInstance();
DSchedEventType g_type = DMS_CONTINUE;
sptr<IDSchedEventListener> listener = sptr<IDSchedEventListener>(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<EventNotify> 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;

View File

@ -26,9 +26,10 @@ namespace DistributedSchedule {
class Business : public DSchedEventListenerStub {
public:
void DSchedEventNotify(EventNotify &notify) override;
void Register();
void UnRegister();
void GetContinueInfo();
void Register(DSchedEventType type);
void UnRegister(DSchedEventType type);
void GetContinueDeviceInfo();
void GetDSchedEventInfo(DSchedEventType type);
};
}
}

View File

@ -17,6 +17,7 @@
#define OHOS_DISTRIBUTED_DATA_STORAGE_H
#include <atomic>
#include <future>
#include <memory>
#include <mutex>
#include <shared_mutex>
@ -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<OHOS::DistributedKv::Status> &resultStatusSignal) const;
mutable std::shared_mutex initLock_;
std::shared_ptr<AppExecFwk::EventHandler> dmsDataStorageHandler_;
@ -111,6 +113,7 @@ private:
std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_; // protected by initLock_
std::unique_ptr<DistributedDataChangeListener> distributedDataChangeListener_;
sptr<IRemoteObject::DeathRecipient> kvStoreDeathRecipient_;
int32_t waittingTime_ = 180; // 3 s
};
} // DistributedSchedule
} // OHOS

View File

@ -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<std::string> networkIdList;
networkIdList.push_back(deviceInfo.networkId);
DmsBmStorage::GetInstance()->PullOtherDistributedData(networkIdList);
DmsBmStorage::GetInstance()->PushOtherDistributedData(networkIdList);
}
bool DnetworkAdapter::AddDeviceChangeListener(const std::shared_ptr<DeviceListener>& listener)

View File

@ -2461,8 +2461,8 @@ int32_t DistributedSchedService::SetMissionContinueState(int32_t missionId, cons
int32_t DistributedSchedService::RegisterDSchedEventListener(const DSchedEventType& type,
const sptr<IRemoteObject>& 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<IRemoteObject>& 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;

View File

@ -70,7 +70,7 @@ std::shared_ptr<DmsBmStorage> 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<std::string> 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<std::string> 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<std::string> 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<OHOS::DistributedKv::Status> &r
void DmsBmStorage::GetEntries(const std::string &networkId, const Key &allEntryKeyPrefix,
std::promise<OHOS::DistributedKv::Status> &resultStatusSignal, std::vector<Entry> &allEntries)
{
HILOGI("called.");
kvStorePtr_->GetEntries(allEntryKeyPrefix, networkId,
[&resultStatusSignal, &allEntries](Status innerStatus, std::vector<Entry> 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<std::string> 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()) {

View File

@ -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<shared_mutex> 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<OHOS::DistributedKv::Status> 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<OHOS::DistributedKv::Status> &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;

View File

@ -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";