!14 多用户代码的新修改

Merge pull request !14 from 曹刘超/master

Signed-off-by: puhui <puhui1@huawei.com>
This commit is contained in:
蒲辉
2022-01-14 06:30:30 +00:00
committed by Gitee
4 changed files with 165 additions and 178 deletions
+1 -1
View File
@@ -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",
@@ -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<std::string, CommomEventCallback> 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<CommomEventCallback> callbackQueue_;
static std::mutex callbackQueueMutex_;
static std::mutex eventSubscriberMutex_;
static std::condition_variable notEmpty_;
static std::list<CommomEventCallback> callbackQueue_;
std::map<std::string, std::shared_ptr<EventSubscriber>> dmEventSubscriber_;
};
} // namespace DistributedHardware
} // namespace OHOS
#endif // OHOS_EVENT_MANAGER_ADAPT_H
@@ -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 <thread>
#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<CommomEventCallback> DmCommonEventManager::callbackQueue_;
DmCommonEventManager &DmCommonEventManager::GetInstance()
{
static DmCommonEventManager instance;
return instance;
}
DmCommonEventManager::DmCommonEventManager()
{
std::thread th(DealCallback);
th.detach();
}
DmCommonEventManager::~DmCommonEventManager()
{
std::unique_lock<std::mutex> 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<std::mutex> 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<EventSubscriber> subscriber =
std::make_shared<EventSubscriber>(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<std::mutex> 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<std::mutex> mutexLock(eventSubscriberMutex_);
dmEventSubscriber_.erase(event);
return true;
}
void DmCommonEventManager::Test(const std::string &event)
{
std::unique_lock<std::mutex> 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<std::mutex> 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<std::mutex> 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
@@ -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 <thread>
#include "dm_constants.h"
using namespace OHOS::EventFwk;
namespace OHOS {
namespace DistributedHardware {
std::once_flag DmCommonEventManager::onceFlag_;
std::list<CommomEventCallback> 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<std::mutex> 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<EventSubscriber> subscriber = std::make_shared<EventSubscriber>(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<std::mutex> callbackLock(callbackLock_);
auto iter = dmEventCallback_.find(event);
if (iter != dmEventCallback_.end()) {
CommomEventCallback funcPrt = iter->second;
callbackLock_.unlock();
std::unique_lock<std::mutex> 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<std::mutex> 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<std::mutex> callbackLock(callbackLock_);
if (dmEventCallback_.find(event) != dmEventCallback_.end()) {
dmEventCallback_.erase(event);
LOGI("delete event failed: %s", event.c_str());
}
}
} // namespace DistributedHardware
} // namespace OHOS