Revert "订阅DB SA启动"

This reverts commit d049679241.

Signed-off-by: wangzhaohao <wangzhaohao@huawei.com>
This commit is contained in:
wangzhaohao 2024-06-08 15:57:15 +08:00
parent a1d805abe9
commit 911a72c9c2
12 changed files with 155 additions and 72 deletions

View File

@ -34,6 +34,7 @@ int32_t LnnDeleteDBDataByPrefix(int32_t dbId, const char *keyPrefix, int32_t key
int32_t LnnGetDBData(int32_t dbId, const char *key, int32_t keyLen, char **value);
int32_t LnnCloudSync(int32_t dbId);
void LnnClearRedundancyCache(void);
bool LnnSubcribeKvStoreService(void);
#ifdef __cplusplus
};
#endif

View File

@ -26,6 +26,9 @@
#include "softbus_errcode.h"
#include "softbus_def.h"
#include "softbus_utils.h"
#include "iservice_registry.h"
#include "lnn_kv_store_launch_listener.h"
#include "system_ability_definition.h"
using namespace OHOS;
using namespace OHOS::DistributedKv;
@ -38,6 +41,7 @@ std::mutex g_kvAdapterWrapperMutex;
} // namespace
static int32_t g_dbId = 1;
static bool g_isSubscribed = false;
static std::map<int32_t, std::shared_ptr<OHOS::KVAdapter>> g_dbID2KvAdapter;
static void BasicCloudSyncInfoToMap(const CloudSyncInfo *localInfo, std::map<std::string, std::string> &values,
const uint64_t &nowTime);
@ -52,6 +56,14 @@ int32_t LnnCreateKvAdapter(int32_t *dbId, const char *appId, int32_t appIdLen, c
LNN_LOGE(LNN_LEDGER, "invalid param");
return SOFTBUS_INVALID_PARAM;
}
{
std::lock_guard<std::mutex> lock(g_kvAdapterWrapperMutex);
if (!g_dbID2KvAdapter.empty()) {
*dbId = g_dbID2KvAdapter.begin()->first;
LNN_LOGI(LNN_LEDGER, "kvAdapter is exist, dbId=%{public}d", *dbId);
return SOFTBUS_OK;
}
}
std::string appIdStr(appId, appIdLen);
std::string storeIdStr(storeId, storeIdLen);
std::shared_ptr<KVAdapter> kvAdapter = nullptr;
@ -376,28 +388,31 @@ static void ComplexCloudSyncInfoToMap(const CloudSyncInfo *localInfo, std::map<s
void LnnRegisterDataChangeListener(int32_t dbId, const char *appId, int32_t appIdLen, const char *storeId,
int32_t storeIdLen)
{
int32_t status;
{
std::lock_guard<std::mutex> lock(g_kvAdapterWrapperMutex);
if (dbId < MIN_DBID_COUNT || dbId >= g_dbId || appId == nullptr || appIdLen < MIN_STRING_LEN ||
appIdLen > MAX_STRING_LEN || storeId == nullptr || storeIdLen < MIN_STRING_LEN ||
storeIdLen > MAX_STRING_LEN) {
LNN_LOGE(LNN_LEDGER, "invalid param");
return;
}
std::string appIdStr(appId, appIdLen);
std::string storeIdStr(storeId, storeIdLen);
auto kvAdapter = FindKvStorePtr(dbId);
if (kvAdapter == nullptr) {
LNN_LOGE(LNN_LEDGER, "kvAdapter is not exist, dbId=%{public}d", dbId);
return;
}
status = kvAdapter->RegisterDataChangeListener(std::make_shared<KvDataChangeListener>(appIdStr, storeIdStr));
std::lock_guard<std::mutex> lock(g_kvAdapterWrapperMutex);
if (dbId < MIN_DBID_COUNT || dbId >= g_dbId || appId == nullptr || appIdLen < MIN_STRING_LEN ||
appIdLen > MAX_STRING_LEN || storeId == nullptr || storeIdLen < MIN_STRING_LEN ||
storeIdLen > MAX_STRING_LEN) {
LNN_LOGE(LNN_LEDGER, "invalid param");
return;
}
if (g_isSubscribed) {
LNN_LOGI(LNN_LEDGER, "DataChangeListener is already registered");
return;
}
std::string appIdStr(appId, appIdLen);
std::string storeIdStr(storeId, storeIdLen);
auto kvAdapter = FindKvStorePtr(dbId);
if (kvAdapter == nullptr) {
LNN_LOGE(LNN_LEDGER, "kvAdapter is not exist, dbId=%{public}d", dbId);
return;
}
int32_t status = kvAdapter->RegisterDataChangeListener(std::make_shared<KvDataChangeListener>(appIdStr,
storeIdStr));
if (status != SOFTBUS_OK) {
LNN_LOGE(LNN_LEDGER, "RegisterDataChangeListener failed");
return;
}
g_isSubscribed = true;
LNN_LOGI(LNN_LEDGER, "RegisterDataChangeListener success");
}
@ -413,10 +428,36 @@ void LnnUnRegisterDataChangeListener(int32_t dbId)
LNN_LOGE(LNN_LEDGER, "kvAdapter is not exist, dbId=%{public}d", dbId);
return;
}
kvAdapter->DeRegisterDataChangeListener();
if (kvAdapter->DeRegisterDataChangeListener() != SOFTBUS_OK) {
LNN_LOGE(LNN_LEDGER, "DeRegisterDataChangeListener failed");
return;
}
g_isSubscribed = false;
LNN_LOGI(LNN_LEDGER, "DeRegisterDataChangeListener success");
}
void LnnClearRedundancyCache(void)
{
KvDataChangeListener::ClearCache();
}
bool LnnSubcribeKvStoreService(void)
{
auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (abilityManager == nullptr) {
LNN_LOGE(LNN_LEDGER, "abilityManager is nullptr");
return false;
}
auto listener = new (std::nothrow) KvStoreStatusChangeListener();
if (listener == nullptr) {
LNN_LOGE(LNN_LEDGER, "failed to create listener");
return false;
}
int32_t ret = abilityManager->SubscribeSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID, listener);
if (ret != ERR_OK) {
LNN_LOGE(LNN_LEDGER, "subscribe system ability failed, ret=%{public}d", ret);
return false;
}
LNN_LOGI(LNN_LEDGER, "subscribe kv store service success");
return true;
}

View File

@ -98,3 +98,8 @@ void LnnUnRegisterDataChangeListener(int32_t dbId)
void LnnClearRedundancyCache(void)
{
}
bool LnnSubcribeKvStoreService(void)
{
return false;
}

View File

@ -35,9 +35,12 @@ if (dsoftbus_feature_lnn_cloud_sync) {
"$dsoftbus_root_path/core/adapter/bus_center/src/lnn_kv_adapter_wrapper.cpp",
"$dsoftbus_root_path/core/adapter/kv_store/src/lnn_kv_adapter.cpp",
"$dsoftbus_root_path/core/adapter/kv_store/src/lnn_kv_data_change_listener.cpp",
"$dsoftbus_root_path/core/adapter/subscribe_kv_store_sa/src/lnn_kv_store_launch_listener.cpp",
]
bus_center_core_adapter_inc += [
"$dsoftbus_root_path/core/adapter/kv_store/include",
"$dsoftbus_root_path/core/adapter/subscribe_kv_store_sa/include",
]
bus_center_core_adapter_inc +=
[ "$dsoftbus_root_path/core/adapter/kv_store/include" ]
} else {
bus_center_core_adapter_src += [ "$dsoftbus_root_path/core/adapter/bus_center/src/lnn_kv_adapter_wrapper_virtual.cpp" ]
}

View File

@ -39,7 +39,7 @@ public:
int32_t DeleteKvStore();
int32_t CloudSync();
int32_t RegisterDataChangeListener(const std::shared_ptr<DistributedKv::KvStoreObserver> &dataChangeListener);
void DeRegisterDataChangeListener();
int32_t DeRegisterDataChangeListener();
static void CloudSyncCallback(DistributedKv::ProgressDetail &&detail);
private:

View File

@ -392,11 +392,17 @@ void KVAdapter::CloudSyncCallback(DistributedKv::ProgressDetail &&detail)
}
}
void KVAdapter::DeRegisterDataChangeListener()
int32_t KVAdapter::DeRegisterDataChangeListener()
{
LNN_LOGI(LNN_LEDGER, "call!");
UnRegisterDataChangeListener();
int32_t ret = UnRegisterDataChangeListener();
if (ret != SOFTBUS_OK) {
LNN_LOGE(LNN_LEDGER, "UnRegisterDataChangeListener failed, ret=%{public}d", ret);
return ret;
}
DeleteDataChangeListener();
LNN_LOGI(LNN_LEDGER, "DeRegisterDataChangeListener success");
return SOFTBUS_OK;
}
} // namespace OHOS

View File

@ -85,7 +85,6 @@ std::vector<DistributedKv::Entry> KvDataChangeListener::ConvertCloudChangeDataTo
std::vector<DistributedKv::Entry> entries;
if (keys.empty()) {
LNN_LOGE(LNN_LEDGER, "keys empty");
LnnDestroyKvAdapter(dbId);
return entries;
}
for (const auto &key : keys) {
@ -103,7 +102,6 @@ std::vector<DistributedKv::Entry> KvDataChangeListener::ConvertCloudChangeDataTo
entries.emplace_back(entry);
SoftBusFree(value);
}
LnnDestroyKvAdapter(dbId);
return entries;
}

View File

@ -0,0 +1,35 @@
/*
* 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 LNN_KV_STORE_LAUNCH_LISTENER_H
#define LNN_KV_STORE_LAUNCH_LISTENER_H
#include <cstdint>
#include <string>
#include "system_ability_status_change_stub.h"
namespace OHOS {
class KvStoreStatusChangeListener : public SystemAbilityStatusChangeStub {
public:
KvStoreStatusChangeListener() = default;
~KvStoreStatusChangeListener() = default;
void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId);
void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId);
};
} // namespace OHOS
#endif //LNN_KV_STORE_LAUNCH_LISTENER_H

View File

@ -0,0 +1,38 @@
/*
* 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.
*/
#include "lnn_kv_store_launch_listener.h"
#include "lnn_log.h"
#include "system_ability_definition.h"
#include "lnn_data_cloud_sync.h"
namespace OHOS {
void KvStoreStatusChangeListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
{
if (systemAbilityId == DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID) {
LNN_LOGI(LNN_LEDGER, "kv store SA launch.");
LnnInitCloudSyncModule();
}
}
void KvStoreStatusChangeListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
{
if (systemAbilityId == DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID) {
LNN_LOGI(LNN_LEDGER, "kv store SA shutdown.");
}
}
} // namespace OHOS

View File

@ -170,53 +170,6 @@ HWTEST_F(KVAdapterWrapperTest, LnnGet001, TestSize.Level1)
EXPECT_EQ(LnnGetDBData(dbId, keyPtr, MIN_STRING_LEN - 1, &value), SOFTBUS_INVALID_PARAM);
}
/**
* @tc.name: LnnPutDBDataBatch
* @tc.desc: LnnPutDBDataBatch
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(KVAdapterWrapperTest, LnnPutBatch001, TestSize.Level1)
{
int32_t dbId = g_dbId;
CloudSyncInfo info {
.networkId = "networkId",
.deviceName = "deviceName",
.unifiedName = "unifiedName",
.unifiedDefaultName = "unifiedDefaultName",
.nickName = "nickName",
.deviceTypeId = 1,
.deviceUdid = "deviceUdid",
.uuid = "uuid",
.softBusVersion = "softBusVersion",
.isBleP2p = true,
.supportedProtocols = 1,
.pkgVersion = "pkgVersion",
.wifiVersion = 1,
.bleVersion = 1,
.macAddr = "macAddr",
.accountId = 1,
.feature = 1,
.connSubFeature = 1,
.authCapacity = 1,
.osType = 1,
.osVersion = "osVersion",
.stateVersion = 1,
.p2pMac = "p2pMac",
.peerIrk = {1},
.publicAddress = {1},
.remotePtk = "remotePtk",
.broadcastCipherKey = NULL,
.lifeTotal = 1,
.curBeginTime = 1,
.currentIndex = 1,
.cipherKey = {1},
.cipherIv = {1},
.distributedSwitch = true
};
EXPECT_EQ(LnnPutDBDataBatch(dbId, &info), SOFTBUS_OK);
}
/**
* @tc.name: LnnDestroyKvAdapter
* @tc.desc: LnnDestroyKvAdapter

View File

@ -334,6 +334,7 @@ ohos_unittest("LNNNetBuilderMockTest") {
sources = [
"$dsoftbus_root_path/adapter/common/net/wifi/common/softbus_wifi_api_adapter_virtual.c",
"$dsoftbus_root_path/core/adapter/bus_center/src/lnn_deviceinfo_to_profile_virtual.c",
"$dsoftbus_root_path/core/adapter/bus_center/src/lnn_kv_adapter_wrapper_virtual.cpp",
"$dsoftbus_root_path/core/adapter/bus_center/src/lnn_link_finder_virtul.c",
"$dsoftbus_root_path/core/adapter/bus_center/src/lnn_ohos_account_virtual.cpp",
"$dsoftbus_root_path/core/authentication/src/auth_deviceprofile_virtual.cpp",

View File

@ -187,6 +187,7 @@ public:
virtual int32_t AuthStartVerify(const AuthConnInfo *connInfo, uint32_t requestId,
const AuthVerifyCallback *callback, AuthVerifyModule module, bool isFastAuth);
virtual bool IsSupportLpFeature(void);
virtual bool LnnSubcribeKvStoreService(void);
};
class NetBuilderDepsInterfaceMock : public NetBuilderDepsInterface {
public:
@ -297,6 +298,7 @@ public:
MOCK_METHOD2(LnnUnregisterEventHandler, void (LnnEventType, LnnEventHandler));
MOCK_METHOD1(LnnNotifyDeviceVerified, void (const char *));
MOCK_METHOD0(LnnInitBusCenterEvent, int32_t ());
MOCK_METHOD0(LnnSubcribeKvStoreService, bool ());
MOCK_METHOD0(LnnInitBatteryInfo, int32_t ());
MOCK_METHOD0(LnnDeinitBatteryInfo, void ());
MOCK_METHOD3(LnnSetLocalByteInfo, int32_t (InfoKey, const uint8_t *, uint32_t));