diff --git a/services/service/include/device_manager_service_listener.h b/services/service/include/device_manager_service_listener.h index 9bad1b5615399c562b1e5d1d4470a9e0722e7358..7112ecf7021ee80bd0004e3a65a77216a8d777ea 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 62ee556a15b4bfce3c09f2338a6e9710db2d47a9..326326716a05e02ff8cb237486dd3c25752d1fad 100644 --- a/services/service/src/device_manager_service_listener.cpp +++ b/services/service/src/device_manager_service_listener.cpp @@ -44,9 +44,8 @@ namespace OHOS { namespace DistributedHardware { -std::mutex DeviceManagerServiceListener::alreadyOnlineSetLock_; -std::unordered_set DeviceManagerServiceListener::alreadyOnlineSet_ = {}; -const int32_t LAST_APP_ONLINE_NUMS = 8; +std::mutex DeviceManagerServiceListener::alreadyOnlinePkgNameLock_; +std::unordered_map DeviceManagerServiceListener::alreadyOnlinePkgName_ = {}; void DeviceManagerServiceListener::ConvertDeviceInfoToDeviceBasicInfo(const std::string &pkgName, const DmDeviceInfo &info, DmDeviceBasicInfo &deviceBasicInfo) { @@ -123,12 +122,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 +133,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 +164,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 +184,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 +535,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 028bbcf415623ec30c6bd033f7d0f36e610486d0..ec3edbe60808f53332654a9686761a80f202be0f 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(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 3eb06df90288657e4036982c05aa3a641cad74ea..53590b7a87930b984071992a9bf9bb357643dda5 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(const 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