From 5b7aafffa60ce30cc7d74e5d584518fa0a82b49f Mon Sep 17 00:00:00 2001 From: li-tiangang4 Date: Mon, 12 Aug 2024 14:51:52 +0800 Subject: [PATCH] delete event_bus Signed-off-by: li-tiangang4 --- bundle.json | 7 +- .../include/resourcemanager/db_adapter.h | 2 - .../resourcemanager/version_info_event.h | 56 ---- .../resourcemanager/version_info_manager.h | 3 +- .../resourcemanager/version_info_manager.cpp | 1 - .../src/resource_manager_test.cpp | 1 - .../src/version_info_manager_test.cpp | 1 - utils/BUILD.gn | 1 - utils/include/eventbus/event.h | 49 ---- utils/include/eventbus/event_bus.h | 275 ------------------ utils/include/eventbus/event_registration.h | 50 ---- utils/include/eventbus/event_sender.h | 27 -- utils/include/eventbus/eventbus_handler.h | 39 --- 13 files changed, 2 insertions(+), 510 deletions(-) delete mode 100644 services/distributedhardwarefwkservice/include/resourcemanager/version_info_event.h delete mode 100644 utils/include/eventbus/event.h delete mode 100644 utils/include/eventbus/event_bus.h delete mode 100644 utils/include/eventbus/event_registration.h delete mode 100644 utils/include/eventbus/event_sender.h delete mode 100644 utils/include/eventbus/eventbus_handler.h diff --git a/bundle.json b/bundle.json index 0dcfcb8b..555edf47 100644 --- a/bundle.json +++ b/bundle.json @@ -113,12 +113,7 @@ "dh_utils_hitrace.h", "dh_utils_tool.h", "histreamer_ability_parser.h", - "histreamer_query_tool.h", - "eventbus/event_bus.h", - "eventbus/event_registration.h", - "eventbus/event_sender.h", - "eventbus/event.h", - "eventbus/eventbus_handler.h" + "histreamer_query_tool.h" ], "header_base": "//foundation/distributedhardware/distributed_hardware_fwk/utils/include" } diff --git a/services/distributedhardwarefwkservice/include/resourcemanager/db_adapter.h b/services/distributedhardwarefwkservice/include/resourcemanager/db_adapter.h index 825cbd62..35063d8a 100644 --- a/services/distributedhardwarefwkservice/include/resourcemanager/db_adapter.h +++ b/services/distributedhardwarefwkservice/include/resourcemanager/db_adapter.h @@ -27,12 +27,10 @@ #include "kvstore_observer.h" #include "capability_info.h" -#include "event_sender.h" namespace OHOS { namespace DistributedHardware { class DBAdapter : public std::enable_shared_from_this, - public EventSender, public DistributedKv::KvStoreDeathRecipient { public: DBAdapter(const std::string &appId, const std::string &storeId, diff --git a/services/distributedhardwarefwkservice/include/resourcemanager/version_info_event.h b/services/distributedhardwarefwkservice/include/resourcemanager/version_info_event.h deleted file mode 100644 index 607b13ba..00000000 --- a/services/distributedhardwarefwkservice/include/resourcemanager/version_info_event.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2022 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 OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_EVENT_H -#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_EVENT_H - -#include - -#include "distributed_hardware_log.h" -#include "event.h" -#include "event_sender.h" - -namespace OHOS { -namespace DistributedHardware { -class VersionInfoEvent : public Event { - TYPEINDENT(VersionInfoEvent) - -public: - enum class EventType : uint32_t { - UNDEFINED = 0, - RECOVER = 1, - }; - -public: - explicit VersionInfoEvent(EventSender &sender) : Event(sender) - { - action_ = EventType::UNDEFINED; - } - - VersionInfoEvent(EventSender &sender, EventType action) : Event(sender), action_(action) {} - - virtual ~VersionInfoEvent() {} - - EventType GetAction() const - { - return action_; - } - -private: - EventType action_; -}; -} // namespace DistributedHardware -} // namespace OHOS -#endif diff --git a/services/distributedhardwarefwkservice/include/resourcemanager/version_info_manager.h b/services/distributedhardwarefwkservice/include/resourcemanager/version_info_manager.h index 427d4bd4..7badc0a1 100644 --- a/services/distributedhardwarefwkservice/include/resourcemanager/version_info_manager.h +++ b/services/distributedhardwarefwkservice/include/resourcemanager/version_info_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -27,7 +27,6 @@ #include "impl_utils.h" #include "single_instance.h" #include "version_info.h" -#include "version_info_event.h" class DBAdapter; namespace OHOS { diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp index 77248e7e..f24327cc 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp @@ -23,7 +23,6 @@ #include "distributed_hardware_log.h" #include "task_executor.h" #include "task_factory.h" -#include "version_info_event.h" #include "version_manager.h" class DBAdapter; diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/src/resource_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/src/resource_manager_test.cpp index 29d4a7bf..e914fc7b 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/src/resource_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/src/resource_manager_test.cpp @@ -26,7 +26,6 @@ #include "local_capability_info_manager.h" #include "meta_capability_info.h" #include "meta_info_manager.h" -#include "version_info_event.h" #include "dh_context.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/src/version_info_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/src/version_info_manager_test.cpp index 10ea8481..cd713bcf 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/src/version_info_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/src/version_info_manager_test.cpp @@ -21,7 +21,6 @@ #include "version_info_manager.h" #include "version_manager.h" -#include "version_info_event.h" #include "dh_context.h" #include "distributed_hardware_log.h" diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 9bbe50af..e46bb03f 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -39,7 +39,6 @@ ohos_shared_library("distributedhardwareutils") { "${common_path}/utils/include", "include", "include/log", - "include/eventbus", ] sources = [ diff --git a/utils/include/eventbus/event.h b/utils/include/eventbus/event.h deleted file mode 100644 index 879e9e99..00000000 --- a/utils/include/eventbus/event.h +++ /dev/null @@ -1,49 +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. - */ - -#ifndef OHOS_DISTRIBUTED_HARDWARE_EVENT_H -#define OHOS_DISTRIBUTED_HARDWARE_EVENT_H - -#include - -#include "event_sender.h" - -namespace OHOS { -namespace DistributedHardware { -#define TYPEINDENT(className) public: \ - virtual std::string GetType() const \ - { \ - return #className; \ - } - -class Event { -TYPEINDENT(Event) - -public: - explicit Event(EventSender &sender) : sender_(sender) {} - - virtual ~Event() = default; - - const EventSender &GetSender() const - { - return sender_; - } - -private: - EventSender &sender_; -}; -} // namespace DistributedHardware -} // namespace OHOS -#endif diff --git a/utils/include/eventbus/event_bus.h b/utils/include/eventbus/event_bus.h deleted file mode 100644 index 0328c99a..00000000 --- a/utils/include/eventbus/event_bus.h +++ /dev/null @@ -1,275 +0,0 @@ -/* - * Copyright (c) 2021-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 OHOS_DISTRIBUTED_HARDWARE_EVENT_BUS_H -#define OHOS_DISTRIBUTED_HARDWARE_EVENT_BUS_H - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "event_handler.h" - -#include "constants.h" -#include "dh_log.h" -#include "anonymous_string.h" -#include "distributed_hardware_errno.h" -#include "distributed_hardware_log.h" -#include "event.h" -#include "eventbus_handler.h" -#include "event_registration.h" - -#ifndef DH_LOG_TAG -#define DH_LOG_TAG "DHEventBus" -#endif - -namespace OHOS { -namespace DistributedHardware { -enum POSTMODE : uint32_t { - POST_ASYNC = 0, - POST_SYNC, -}; - -class EventBus final { -public: - EventBus() - { - ULOGI("ctor EventBus"); - if (eventbusHandler_ == nullptr) { - eventThread_ = std::thread([this]() { this->StartEvent(); }); - std::unique_lock lock(eventMutex_); - eventCon_.wait(lock, [this] { - return eventbusHandler_ != nullptr; - }); - } - } - - explicit EventBus(const std::string &threadName) - { - ULOGI("ctor EventBus threadName: %{public}s", threadName.c_str()); - if (eventbusHandler_ == nullptr) { - eventThread_ = std::thread([this]() { this->StartEvent(threadName); }); - std::unique_lock lock(eventMutex_); - eventCon_.wait(lock, [this] { - return eventbusHandler_ != nullptr; - }); - } - } - - ~EventBus() - { - ULOGI("dtor EventBus"); - if ((eventbusHandler_ != nullptr) && (eventbusHandler_->GetEventRunner() != nullptr)) { - eventbusHandler_->GetEventRunner()->Stop(); - } - eventThread_.join(); - eventbusHandler_ = nullptr; - } - - template - std::shared_ptr AddHandler(const std::string &typeId, - DistributedHardware::EventBusHandler &handler) - { - std::lock_guard lock(handlerMtx); - Registrations *registrations = handlers[typeId]; - - if (registrations == nullptr) { - registrations = new(std::nothrow) EventRegistration::Registrations(); - if (registrations == nullptr) { - ULOGE("registrations is null, because applying memory fail!"); - return nullptr; - } - handlers[typeId] = registrations; - } - - for (auto ® : *registrations) { - if (reg->GetHandler() == static_cast(&handler) && reg->GetSender() == nullptr) { - return reg; - } - } - - std::shared_ptr registration = - std::make_shared(static_cast(&handler), nullptr); - registrations->insert(registration); - - return registration; - } - - template - std::shared_ptr AddHandler(const std::string &typeId, EventBusHandler &handler, - EventSender &sender) - { - std::lock_guard lock(handlerMtx); - Registrations *registrations = handlers[typeId]; - - if (registrations == nullptr) { - registrations = new(std::nothrow) EventRegistration::Registrations(); - if (registrations == nullptr) { - ULOGE("registrations is null, because applying memory fail!"); - return nullptr; - } - handlers[typeId] = registrations; - } - - for (auto ® : *registrations) { - if (reg->GetHandler() == static_cast(&handler) && reg->GetSender() == &sender) { - return reg; - } - } - - std::shared_ptr registration = - std::make_shared(static_cast(&handler), &sender); - registrations->insert(registration); - - return registration; - } - - template - bool RemoveHandler(const std::string &typeId, std::shared_ptr &EvenReg) - { - std::lock_guard lock(handlerMtx); - Registrations *registrations = handlers[typeId]; - if (registrations == nullptr) { - return false; - } - - bool ret = false; - auto regIter = registrations->find(EvenReg); - if (regIter != registrations->end()) { - registrations->erase(regIter); - ret = true; - } - - return ret; - } - - template - void PostEvent(T &e, POSTMODE mode = POSTMODE::POST_ASYNC) - { - if (mode == POSTMODE::POST_SYNC) { - PostEventInner(e); - } else { - auto eventFunc = [this, e]() mutable { - PostEventInner(e); - }; - if (!(eventbusHandler_ && eventbusHandler_->PostTask(eventFunc))) { - ULOGE("Eventbus::PostEvent Async PostTask fail"); - } - } - } - - template - void PostEvent(T &e, int64_t delayTime) - { - auto eventFunc = [this, e]() mutable { - PostEventInner(e); - }; - if (!(eventbusHandler_ && eventbusHandler_->PostTask(eventFunc, e->getType(), delayTime))) { - ULOGE("Eventbus::PostEvent Async PostTask fail"); - } - } - - template - void RemoveEvent(T &e) - { - if (!(eventbusHandler_ && eventbusHandler_->RemoveTask(e->getType()))) { - ULOGE("Eventbus::RemoveEvent fail"); - } - } - - void PostTask(const OHOS::AppExecFwk::InnerEvent::Callback& callback, - const std::string& name, - int64_t delayTimeInMs = 0) - { - ULOGI("Eventbus::PostTask Async PostTask, taskName:%{public}s.", GetAnonyString(name).c_str()); - if (eventbusHandler_ != nullptr) { - eventbusHandler_->PostTask(callback, name, delayTimeInMs); - } - } - - void RemoveTask(const std::string& name) - { - ULOGI("Eventbus::RemoveTask, taskName:%{public}s.", GetAnonyString(name).c_str()); - if (eventbusHandler_ != nullptr) { - eventbusHandler_->RemoveTask(name); - } - } - -private: - template - void PostEventInner(T &e) - { - std::lock_guard lock(handlerMtx); - Registrations *registrations = handlers[e.GetType()]; - if (registrations == nullptr) { - return; - } - - for (auto ® : *registrations) { - if (reg->GetHandler() == nullptr) { - continue; - } - if ((reg->GetSender() == nullptr) || (reg->GetSender() == &e.GetSender())) { - static_cast *>(const_cast(reg->GetHandler()))->Dispatch(e); - } - } - } - - void StartEvent() - { - int32_t ret = pthread_setname_np(pthread_self(), START_EVENT); - if (ret != DH_FWK_SUCCESS) { - DHLOGE("StartEvent setname failed."); - } - auto busRunner = AppExecFwk::EventRunner::Create(false); - { - std::lock_guard lock(eventMutex_); - eventbusHandler_ = std::make_shared(busRunner); - } - eventCon_.notify_all(); - busRunner->Run(); - } - - void StartEventWithName(const std::string &threadName) - { - prctl(PR_SET_NAME, threadName.c_str()); - auto busRunner = AppExecFwk::EventRunner::Create(false); - { - std::lock_guard lock(eventMutex_); - eventbusHandler_ = std::make_shared(busRunner); - } - eventCon_.notify_all(); - busRunner->Run(); - } - -private: - std::shared_ptr eventbusHandler_; - - using Registrations = std::set>; - std::mutex handlerMtx; - using TypeMap = std::unordered_map> *>; - TypeMap handlers; - std::thread eventThread_; - std::condition_variable eventCon_; - std::mutex eventMutex_; -}; -} // namespace DistributedHardware -} // namespace OHOS -#endif diff --git a/utils/include/eventbus/event_registration.h b/utils/include/eventbus/event_registration.h deleted file mode 100644 index d726389f..00000000 --- a/utils/include/eventbus/event_registration.h +++ /dev/null @@ -1,50 +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. - */ - -#ifndef OHOS_DISTRIBUTED_HARDWARE_EVENT_REGISTRATION_H -#define OHOS_DISTRIBUTED_HARDWARE_EVENT_REGISTRATION_H - -#include -#include - -#include "event_sender.h" - -namespace OHOS { -namespace DistributedHardware { -class EventRegistration { -public: - using Registrations = std::set>; - - EventRegistration(void * const handler, EventSender * const sender) : handler(handler), sender(sender) {} - - virtual ~EventRegistration() {} - - const void *GetHandler() - { - return handler; - } - - const EventSender *GetSender() - { - return sender; - } - -private: - void * const handler; - EventSender * const sender; -}; -} // namespace DistributedHardware -} // namespace OHOS -#endif diff --git a/utils/include/eventbus/event_sender.h b/utils/include/eventbus/event_sender.h deleted file mode 100644 index 72f3bc59..00000000 --- a/utils/include/eventbus/event_sender.h +++ /dev/null @@ -1,27 +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. - */ - -#ifndef OHOS_DISTRIBUTED_HARDWARE_EVENT_SENDER_H -#define OHOS_DISTRIBUTED_HARDWARE_EVENT_SENDER_H - -namespace OHOS { -namespace DistributedHardware { -class EventSender { -public: - virtual ~EventSender() = default; -}; -} // namespace DistributedHardware -} // namespace OHOS -#endif diff --git a/utils/include/eventbus/eventbus_handler.h b/utils/include/eventbus/eventbus_handler.h deleted file mode 100644 index c7b702e0..00000000 --- a/utils/include/eventbus/eventbus_handler.h +++ /dev/null @@ -1,39 +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. - */ - -#ifndef OHOS_DISTRIBUTED_HARDWARE_EVENTBUSHANDLER_H -#define OHOS_DISTRIBUTED_HARDWARE_EVENTBUSHANDLER_H - -namespace OHOS { -namespace DistributedHardware { -template -class EventBusHandler { -public: - EventBusHandler() - { - } - - virtual ~EventBusHandler() = default; - - virtual void OnEvent(T &e) = 0; - - void Dispatch(T &e) - { - OnEvent(e); - } -}; -} // namespace DistributedHardware -} // namespace OHOS -#endif -- Gitee