From 5b9c2bdcfc3bc6e1af8b04000d6f250889de2087 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sat, 28 Sep 2024 10:49:06 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=8A=E4=B8=8B?= =?UTF-8?q?=E7=BA=BF=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- .../include/device_manager_service_listener.h | 7 +-- .../src/device_manager_service_listener.cpp | 50 +++++++++++-------- .../softbuscache/include/dm_softbus_cache.h | 1 + .../softbuscache/src/dm_softbus_cache.cpp | 15 ++++++ 4 files changed, 49 insertions(+), 24 deletions(-) diff --git a/services/service/include/device_manager_service_listener.h b/services/service/include/device_manager_service_listener.h index 9bad1b561..7112ecf70 100644 --- a/services/service/include/device_manager_service_listener.h +++ b/services/service/include/device_manager_service_listener.h @@ -18,7 +18,7 @@ #include #include -#include +#include #include #include "dm_device_info.h" @@ -85,6 +85,7 @@ private: std::string ComposeOnlineKey(const std::string &pkgName, const std::string &devId); void SetDeviceScreenInfo(std::shared_ptr pReq, const std::string &pkgName, const DmDeviceInfo &deviceInfo); + void RemoveOnlinePkgName(const DmDeviceInfo &info); #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) int32_t ConvertUdidHashToAnoyAndSave(const std::string &pkgName, DmDeviceInfo &deviceInfo); int32_t ConvertUdidHashToAnoyDeviceId(const std::string &pkgName, const std::string &udidHash, @@ -93,8 +94,8 @@ private: private: #if !defined(__LITEOS_M__) IpcServerListener ipcServerListener_; - static std::mutex alreadyOnlineSetLock_; - static std::unordered_set alreadyOnlineSet_; + static std::mutex alreadyOnlinePkgNameLock_; + static std::unordered_map alreadyOnlinePkgName_; #endif }; } // namespace DistributedHardware diff --git a/services/service/src/device_manager_service_listener.cpp b/services/service/src/device_manager_service_listener.cpp index 62ee556a1..bdc1027a5 100644 --- a/services/service/src/device_manager_service_listener.cpp +++ b/services/service/src/device_manager_service_listener.cpp @@ -44,8 +44,8 @@ namespace OHOS { namespace DistributedHardware { -std::mutex DeviceManagerServiceListener::alreadyOnlineSetLock_; -std::unordered_set DeviceManagerServiceListener::alreadyOnlineSet_ = {}; +std::mutex DeviceManagerServiceListener::alreadyOnlinePkgNameLock_; +std::unordered_map DeviceManagerServiceListener::alreadyOnlinePkgName_ = {}; const int32_t LAST_APP_ONLINE_NUMS = 8; void DeviceManagerServiceListener::ConvertDeviceInfoToDeviceBasicInfo(const std::string &pkgName, const DmDeviceInfo &info, DmDeviceBasicInfo &deviceBasicInfo) @@ -123,12 +123,8 @@ void DeviceManagerServiceListener::ProcessDeviceStateChange(const DmDeviceState std::shared_ptr pRsp = std::make_shared(); std::vector PkgNameVec = ipcServerListener_.GetAllPkgName(); if (state == DEVICE_STATE_OFFLINE) { + RemoveOnlinePkgName(info); for (const auto &it : PkgNameVec) { - std::string notifyKey = ComposeOnlineKey(it, std::string(info.deviceId)); - { - std::lock_guard autoLock(alreadyOnlineSetLock_); - alreadyOnlineSet_.erase(notifyKey); - } SetDeviceInfo(pReq, it, state, info, deviceBasicInfo); ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); } @@ -138,11 +134,11 @@ void DeviceManagerServiceListener::ProcessDeviceStateChange(const DmDeviceState std::string notifyKey = ComposeOnlineKey(it, std::string(info.deviceId)); DmDeviceState notifyState = state; { - std::lock_guard autoLock(alreadyOnlineSetLock_); - if (alreadyOnlineSet_.find(notifyKey) != alreadyOnlineSet_.end()) { + std::lock_guard autoLock(alreadyOnlinePkgNameLock_); + if (alreadyOnlinePkgName_.find(notifyKey) != alreadyOnlinePkgName_.end()) { notifyState = DmDeviceState::DEVICE_INFO_CHANGED; } else { - alreadyOnlineSet_.insert(notifyKey); + alreadyOnlinePkgName_[notifyKey] = info; } } SetDeviceInfo(pReq, it, notifyState, info, deviceBasicInfo); @@ -169,22 +165,19 @@ void DeviceManagerServiceListener::ProcessAppStateChange(const std::string &pkgN for (const auto &it : notifyPkgnames) { std::string notifyKey = it + "_" + info.deviceId; { - std::lock_guard autoLock(alreadyOnlineSetLock_); - if (alreadyOnlineSet_.find(notifyKey) != alreadyOnlineSet_.end()) { + std::lock_guard autoLock(alreadyOnlinePkgNameLock_); + if (alreadyOnlinePkgName_.find(notifyKey) != alreadyOnlinePkgName_.end()) { continue; } - alreadyOnlineSet_.insert(notifyKey); + alreadyOnlinePkgName_[notifyKey] = info; } SetDeviceInfo(pReq, it, state, info, deviceBasicInfo); ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); } } if (state == DEVICE_STATE_OFFLINE) { - if (alreadyOnlineSet_.size() == LAST_APP_ONLINE_NUMS) { - { - std::lock_guard autoLock(alreadyOnlineSetLock_); - alreadyOnlineSet_.clear(); - } + if (!SoftbusCache::GetInstance().CheckIsOnline(std::string(info.deviceId))) { + RemoveOnlinePkgName(info); for (const auto &it : notifyPkgnames) { SetDeviceInfo(pReq, it, state, info, deviceBasicInfo); ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); @@ -192,9 +185,9 @@ void DeviceManagerServiceListener::ProcessAppStateChange(const std::string &pkgN } else { std::string notifyKey = pkgName + "_" + info.deviceId; { - std::lock_guard autoLock(alreadyOnlineSetLock_); - if (alreadyOnlineSet_.find(notifyKey) != alreadyOnlineSet_.end()) { - alreadyOnlineSet_.erase(notifyKey); + std::lock_guard autoLock(alreadyOnlinePkgNameLock_); + if (alreadyOnlinePkgName_.find(notifyKey) != alreadyOnlinePkgName_.end()) { + alreadyOnlinePkgName_.erase(notifyKey); } } SetDeviceInfo(pReq, pkgName, state, info, deviceBasicInfo); @@ -543,5 +536,20 @@ void DeviceManagerServiceListener::OnDeviceScreenStateChange(const std::string & } } } + +void DeviceManagerServiceListener::RemoveOnlinePkgName(const DmDeviceInfo &info) +{ + LOGI("udidHash: %{public}s.", GetAnonyString(info.deviceId).c_str()); + { + std::lock_guard autoLock(alreadyOnlinePkgNameLock_); + for (auto item = alreadyOnlinePkgName_.begin(); item != alreadyOnlinePkgName_.end();) { + if (std::string(item.second.deviceId) == std::string(info.deviceId)) { + item = alreadyOnlinePkgName_.erase(item); + } else { + ++item; + } + } + } +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/softbuscache/include/dm_softbus_cache.h b/services/softbuscache/include/dm_softbus_cache.h index 028bbcf41..240f45288 100644 --- a/services/softbuscache/include/dm_softbus_cache.h +++ b/services/softbuscache/include/dm_softbus_cache.h @@ -47,6 +47,7 @@ public: void UpDataLocalDevInfo(); int32_t GetUdidByUdidHash(const std::string &udidHash, std::string &udid); int32_t GetUuidByUdid(const std::string &udid, std::string &uuid); + bool CheckIsOnline(std::string &udidHash); private: int32_t GetUdidByNetworkId(const char *networkId, std::string &udid); int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid); diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index 3eb06df90..e990ed2e4 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -408,5 +408,20 @@ int32_t SoftbusCache::GetUuidByUdid(const std::string &udid, std::string &uuid) } return ERR_DM_FAILED; } + +bool SoftbusCache::CheckIsOnline(std::string &udidHash) +{ + LOGI("udidHash %{public}s.", GetAnonyString(udidHash).c_str()); + { + std::lock_guard mutexLock(deviceInfosMutex_); + for (const auto &item : deviceInfo_) { + if (std::string(item.second.second.deviceId) == udidHash) { + LOGI("CheckIsOnline is true."); + return true; + } + } + } + return false; +} } // namespace DistributedHardware } // namespace OHOS -- Gitee From 023977f4298bd8f3fc460acb059765c82c7ae7f5 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sat, 28 Sep 2024 11:20:03 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=8A=E4=B8=8B?= =?UTF-8?q?=E7=BA=BF=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/service/src/device_manager_service_listener.cpp | 2 +- services/softbuscache/include/dm_softbus_cache.h | 2 +- services/softbuscache/src/dm_softbus_cache.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/services/service/src/device_manager_service_listener.cpp b/services/service/src/device_manager_service_listener.cpp index bdc1027a5..2a7339c2d 100644 --- a/services/service/src/device_manager_service_listener.cpp +++ b/services/service/src/device_manager_service_listener.cpp @@ -543,7 +543,7 @@ void DeviceManagerServiceListener::RemoveOnlinePkgName(const DmDeviceInfo &info) { std::lock_guard autoLock(alreadyOnlinePkgNameLock_); for (auto item = alreadyOnlinePkgName_.begin(); item != alreadyOnlinePkgName_.end();) { - if (std::string(item.second.deviceId) == std::string(info.deviceId)) { + if (std::string(item->second.deviceId) == std::string(info.deviceId)) { item = alreadyOnlinePkgName_.erase(item); } else { ++item; diff --git a/services/softbuscache/include/dm_softbus_cache.h b/services/softbuscache/include/dm_softbus_cache.h index 240f45288..ec3edbe60 100644 --- a/services/softbuscache/include/dm_softbus_cache.h +++ b/services/softbuscache/include/dm_softbus_cache.h @@ -47,7 +47,7 @@ public: void UpDataLocalDevInfo(); int32_t GetUdidByUdidHash(const std::string &udidHash, std::string &udid); int32_t GetUuidByUdid(const std::string &udid, std::string &uuid); - bool CheckIsOnline(std::string &udidHash); + bool CheckIsOnline(const std::string &udidHash); private: int32_t GetUdidByNetworkId(const char *networkId, std::string &udid); int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid); diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index e990ed2e4..53590b7a8 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -409,7 +409,7 @@ int32_t SoftbusCache::GetUuidByUdid(const std::string &udid, std::string &uuid) return ERR_DM_FAILED; } -bool SoftbusCache::CheckIsOnline(std::string &udidHash) +bool SoftbusCache::CheckIsOnline(const std::string &udidHash) { LOGI("udidHash %{public}s.", GetAnonyString(udidHash).c_str()); { -- Gitee From 1441afffd629e294ada9b59b8ec7517e69b58be1 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sat, 28 Sep 2024 15:29:38 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=8A=E4=B8=8B?= =?UTF-8?q?=E7=BA=BF=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/service/src/device_manager_service_listener.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/service/src/device_manager_service_listener.cpp b/services/service/src/device_manager_service_listener.cpp index 2a7339c2d..326326716 100644 --- a/services/service/src/device_manager_service_listener.cpp +++ b/services/service/src/device_manager_service_listener.cpp @@ -46,7 +46,6 @@ namespace OHOS { namespace DistributedHardware { std::mutex DeviceManagerServiceListener::alreadyOnlinePkgNameLock_; std::unordered_map DeviceManagerServiceListener::alreadyOnlinePkgName_ = {}; -const int32_t LAST_APP_ONLINE_NUMS = 8; void DeviceManagerServiceListener::ConvertDeviceInfoToDeviceBasicInfo(const std::string &pkgName, const DmDeviceInfo &info, DmDeviceBasicInfo &deviceBasicInfo) { -- Gitee