From 69a572bd3daac77bac9b26134b832e642ed84be1 Mon Sep 17 00:00:00 2001 From: caochao <2930650069@qq.com> Date: Fri, 14 Jan 2022 12:50:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=94=A8=E6=88=B7=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E6=96=B0=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cao_liu_chao --- services/devicemanagerservice/BUILD.gn | 2 +- ...ager_adapt.h => dm_common_event_manager.h} | 39 ++--- .../commonevent/dm_common_event_manager.cpp | 152 ++++++++++++++++++ .../commonevent/event_manager_adapt.cpp | 150 ----------------- 4 files changed, 165 insertions(+), 178 deletions(-) rename services/devicemanagerservice/include/dependency/commonevent/{event_manager_adapt.h => dm_common_event_manager.h} (68%) create mode 100644 services/devicemanagerservice/src/dependency/commonevent/dm_common_event_manager.cpp delete mode 100644 services/devicemanagerservice/src/dependency/commonevent/event_manager_adapt.cpp diff --git a/services/devicemanagerservice/BUILD.gn b/services/devicemanagerservice/BUILD.gn index 3b6f201b..0b95c557 100644 --- a/services/devicemanagerservice/BUILD.gn +++ b/services/devicemanagerservice/BUILD.gn @@ -68,7 +68,7 @@ if (defined(ohos_lite)) { "src/authentication/auth_ui.cpp", "src/authentication/dm_auth_manager.cpp", "src/config/config_manager.cpp", - "src/dependency/commonevent/event_manager_adapt.cpp", + "src/dependency/commonevent/dm_common_event_manager.cpp", "src/dependency/hichain/hichain_connector.cpp", "src/dependency/softbus/softbus_connector.cpp", "src/dependency/softbus/softbus_session.cpp", diff --git a/services/devicemanagerservice/include/dependency/commonevent/event_manager_adapt.h b/services/devicemanagerservice/include/dependency/commonevent/dm_common_event_manager.h similarity index 68% rename from services/devicemanagerservice/include/dependency/commonevent/event_manager_adapt.h rename to services/devicemanagerservice/include/dependency/commonevent/dm_common_event_manager.h index f4433f46..3c7879ea 100644 --- a/services/devicemanagerservice/include/dependency/commonevent/event_manager_adapt.h +++ b/services/devicemanagerservice/include/dependency/commonevent/dm_common_event_manager.h @@ -26,55 +26,40 @@ #include "common_event_subscriber.h" #include "dm_log.h" #include "matching_skills.h" +#include "single_instance.h" namespace OHOS { namespace DistributedHardware { using CommomEventCallback = void (*)(void); class DmCommonEventManager { -public: - static DmCommonEventManager &GetInstance(); - +DECLARE_SINGLE_INSTANCE_BASE(DmCommonEventManager); public: bool SubscribeServiceEvent(const std::string &event, CommomEventCallback callback); bool UnsubscribeServiceEvent(const std::string &event); - + void Test(const std::string &event); class EventSubscriber : public EventFwk::CommonEventSubscriber { public: - explicit EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo) - : EventFwk::CommonEventSubscriber(subscribeInfo) - { - } - + EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo, CommomEventCallback callback, + std::string event) : EventFwk::CommonEventSubscriber(subscribeInfo), callback_(callback), event_(event) {} void OnReceiveEvent(const EventFwk::CommonEventData &data); - void addEventCallback(const std::string &event, CommomEventCallback callback); - void deleteEventCallback(const std::string &event); - + void TestCommonEvent(const std::string &event); private: - std::mutex callbackLock_; - std::map dmEventCallback_; + CommomEventCallback callback_; + std::string event_; }; - private: - DmCommonEventManager() = default; - virtual ~DmCommonEventManager(); - DmCommonEventManager(const DmCommonEventManager &) = delete; - DmCommonEventManager &operator=(const DmCommonEventManager &) = delete; - DmCommonEventManager(DmCommonEventManager &&) = delete; - DmCommonEventManager &operator=(DmCommonEventManager &&) = delete; - + DmCommonEventManager(); + ~DmCommonEventManager(); private: static void DealCallback(void); - static void InitCallbackThread(void); - private: - static std::once_flag onceFlag_; - static std::list callbackQueue_; static std::mutex callbackQueueMutex_; + static std::mutex eventSubscriberMutex_; static std::condition_variable notEmpty_; + static std::list callbackQueue_; std::map> dmEventSubscriber_; }; } // namespace DistributedHardware } // namespace OHOS - #endif // OHOS_EVENT_MANAGER_ADAPT_H \ No newline at end of file diff --git a/services/devicemanagerservice/src/dependency/commonevent/dm_common_event_manager.cpp b/services/devicemanagerservice/src/dependency/commonevent/dm_common_event_manager.cpp new file mode 100644 index 00000000..ab66d98e --- /dev/null +++ b/services/devicemanagerservice/src/dependency/commonevent/dm_common_event_manager.cpp @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2021 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 "dm_common_event_manager.h" + +#include + +#include "dm_constants.h" + +using namespace OHOS::EventFwk; + +namespace OHOS { +namespace DistributedHardware { +std::mutex DmCommonEventManager::callbackQueueMutex_; +std::mutex DmCommonEventManager::eventSubscriberMutex_; +std::condition_variable DmCommonEventManager::notEmpty_; +std::list DmCommonEventManager::callbackQueue_; + +DmCommonEventManager &DmCommonEventManager::GetInstance() +{ + static DmCommonEventManager instance; + return instance; +} + +DmCommonEventManager::DmCommonEventManager() +{ + std::thread th(DealCallback); + th.detach(); +} + +DmCommonEventManager::~DmCommonEventManager() +{ + std::unique_lock mutexLock(eventSubscriberMutex_); + for (auto iter = dmEventSubscriber_.begin(); iter != dmEventSubscriber_.end(); iter++) { + if (!CommonEventManager::UnSubscribeCommonEvent(iter->second)) { + LOGI("Unsubscribe service event failed: %s", iter->first.c_str()); + } + } +} + +void DmCommonEventManager::DealCallback(void) +{ + while (1) { + std::unique_lock callbackQueueMutexLock(callbackQueueMutex_); + notEmpty_.wait(callbackQueueMutexLock, [] { return !callbackQueue_.empty(); }); + CommomEventCallback funcPrt = callbackQueue_.front(); + funcPrt(); + callbackQueue_.pop_front(); + } +} + +bool DmCommonEventManager::SubscribeServiceEvent(const std::string &event, CommomEventCallback callback) +{ + LOGI("Subscribe event: %s", event.c_str()); + if (dmEventSubscriber_.find(event) != dmEventSubscriber_.end() || callback == nullptr) { + LOGE("Subscribe event:%s has been exist or callback is nullptr", event.c_str()); + return false; + } + + MatchingSkills matchingSkills; + matchingSkills.AddEvent(event); + CommonEventSubscribeInfo subscriberInfo(matchingSkills); + std::shared_ptr subscriber = + std::make_shared(subscriberInfo, callback, event); + if (subscriber == nullptr) { + LOGE("subscriber is nullptr %s", event.c_str()); + return false; + } + + if (!CommonEventManager::SubscribeCommonEvent(subscriber)) { + LOGE("Subscribe service event failed: %s", event.c_str()); + return false; + } + + std::unique_lock mutexLock(eventSubscriberMutex_); + dmEventSubscriber_[event] = subscriber; + return true; +} + +bool DmCommonEventManager::UnsubscribeServiceEvent(const std::string &event) +{ + LOGI("UnSubscribe event: %s", event.c_str()); + if (dmEventSubscriber_.find(event) == dmEventSubscriber_.end()) { + LOGE("UnSubscribe event: %s not been exist", event.c_str()); + return false; + } + + if (!CommonEventManager::UnSubscribeCommonEvent(dmEventSubscriber_[event])) { + LOGE("Unsubscribe service event failed: %s", event.c_str()); + return false; + } + + std::unique_lock mutexLock(eventSubscriberMutex_); + dmEventSubscriber_.erase(event); + return true; +} + +void DmCommonEventManager::Test(const std::string &event) +{ + std::unique_lock mutexLock(eventSubscriberMutex_); + if (dmEventSubscriber_.find(event) != dmEventSubscriber_.end()) { + dmEventSubscriber_[event]->TestCommonEvent(event); + } +} + +void DmCommonEventManager::EventSubscriber::TestCommonEvent(const std::string &event) +{ + if (event != event_) { + LOGE("Received event is error"); + return; + } + + std::unique_lock callbackQueueMutexLock(callbackQueueMutex_); + if (callbackQueue_.size() > COMMON_CALLBACK_MAX_SIZE) { + LOGE("event callback Queue is too long"); + return; + } + callbackQueue_.push_back(callback_); + notEmpty_.notify_one(); +} + +void DmCommonEventManager::EventSubscriber::OnReceiveEvent(const CommonEventData &data) +{ + std::string receiveEvent = data.GetWant().GetAction(); + LOGI("Received event: %s", receiveEvent.c_str()); + if (receiveEvent != event_) { + LOGE("Received event is error"); + return; + } + + std::unique_lock callbackQueueMutexLock(callbackQueueMutex_); + if (callbackQueue_.size() > COMMON_CALLBACK_MAX_SIZE) { + LOGE("event callback Queue is too long"); + return; + } + callbackQueue_.push_back(callback_); + notEmpty_.notify_one(); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/devicemanagerservice/src/dependency/commonevent/event_manager_adapt.cpp b/services/devicemanagerservice/src/dependency/commonevent/event_manager_adapt.cpp deleted file mode 100644 index 4a280649..00000000 --- a/services/devicemanagerservice/src/dependency/commonevent/event_manager_adapt.cpp +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright (c) 2021 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 "event_manager_adapt.h" - -#include - -#include "dm_constants.h" - -using namespace OHOS::EventFwk; - -namespace OHOS { -namespace DistributedHardware { -std::once_flag DmCommonEventManager::onceFlag_; -std::list DmCommonEventManager::callbackQueue_; -std::mutex DmCommonEventManager::callbackQueueMutex_; -std::condition_variable DmCommonEventManager::notEmpty_; - -DmCommonEventManager &DmCommonEventManager::GetInstance() -{ - static DmCommonEventManager instance; - std::call_once(onceFlag_, [] { - std::thread th(DealCallback); - th.detach(); - }); - return instance; -} - -void DmCommonEventManager::DealCallback(void) -{ - while (1) { - std::unique_lock callbackQueueLock(callbackQueueMutex_); - notEmpty_.wait(callbackQueueLock, [] { return !callbackQueue_.empty(); }); - CommomEventCallback funcPrt = callbackQueue_.front(); - funcPrt(); - callbackQueue_.pop_front(); - } -} - -bool DmCommonEventManager::SubscribeServiceEvent(const std::string &event, CommomEventCallback callback) -{ - LOGI("Subscribe event: %s", event.c_str()); - if (dmEventSubscriber_.find(event) != dmEventSubscriber_.end()) { - LOGE("Subscribe event:%s has been added", event.c_str()); - return false; - } - - MatchingSkills matchingSkills; - matchingSkills.AddEvent(event); - CommonEventSubscribeInfo subscriberInfo(matchingSkills); - std::shared_ptr subscriber = std::make_shared(subscriberInfo); - if (subscriber == nullptr) { - LOGE("subscriber is nullptr %s", event.c_str()); - return false; - } - subscriber->addEventCallback(event, callback); - - bool subscribeResult = CommonEventManager::SubscribeCommonEvent(subscriber); - if (subscribeResult) { - LOGE("Subscribe service event success: %s", event.c_str()); - dmEventSubscriber_[event] = subscriber; - return subscribeResult; - } else { - LOGE("Subscribe service event failed: %s", event.c_str()); - return false; - } -} - -bool DmCommonEventManager::UnsubscribeServiceEvent(const std::string &event) -{ - LOGI("UnSubscribe event: %s", event.c_str()); - if (dmEventSubscriber_.find(event) != dmEventSubscriber_.end()) { - LOGE("UnSubscribe event: %s not been exist", event.c_str()); - return false; - } - - bool unsubscribeResult = CommonEventManager::UnSubscribeCommonEvent(dmEventSubscriber_[event]); - if (unsubscribeResult) { - LOGI("Unsubscribe service event success: %s", event.c_str()); - dmEventSubscriber_[event]->deleteEventCallback(event); - dmEventSubscriber_.erase(event); - return unsubscribeResult; - } else { - LOGE("Unsubscribe service event failed: %s", event.c_str()); - return false; - } -} - -DmCommonEventManager::~DmCommonEventManager() -{ - for (auto iter = dmEventSubscriber_.begin(); iter != dmEventSubscriber_.end(); iter++) { - bool unsubscribeResult = CommonEventManager::UnSubscribeCommonEvent(iter->second); - if (unsubscribeResult) { - LOGI("Unsubscribe service event success: %s", iter->first.c_str()); - } - } -} - -void DmCommonEventManager::EventSubscriber::OnReceiveEvent(const CommonEventData &data) -{ - std::string event = data.GetWant().GetAction(); - LOGI("Received event: %s, value: %d", event.c_str()); - - std::unique_lock callbackLock(callbackLock_); - auto iter = dmEventCallback_.find(event); - if (iter != dmEventCallback_.end()) { - CommomEventCallback funcPrt = iter->second; - callbackLock_.unlock(); - - std::unique_lock callbackQueueLock(callbackQueueMutex_); - if (callbackQueue_.size() <= COMMON_CALLBACK_MAX_SIZE) { - callbackQueue_.push_back(funcPrt); - notEmpty_.notify_one(); - } else { - LOGE("event callback Queue is too long"); - } - } -} - -void DmCommonEventManager::EventSubscriber::addEventCallback(const std::string &event, CommomEventCallback callback) -{ - std::unique_lock callbackLock(callbackLock_); - if (dmEventCallback_.find(event) == dmEventCallback_.end()) { - dmEventCallback_[event] = callback; - LOGI("add event success: %s", event.c_str()); - } -} - -void DmCommonEventManager::EventSubscriber::deleteEventCallback(const std::string &event) -{ - std::unique_lock callbackLock(callbackLock_); - if (dmEventCallback_.find(event) != dmEventCallback_.end()) { - dmEventCallback_.erase(event); - LOGI("delete event failed: %s", event.c_str()); - } -} -} // namespace DistributedHardware -} // namespace OHOS