• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2026 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef METWORKSLICE_MSGCENTER_H
17 #define METWORKSLICE_MSGCENTER_H
18 
19 #include <map>
20 #include <shared_mutex>
21 #include "singleton.h"
22 #include "event_handler.h"
23 #include "networkslice_submodule.h"
24 #include "networkslice_event.h"
25 #include "netmgr_ext_log_wrapper.h"
26 
27 namespace OHOS {
28 namespace NetManagerStandard {
29 class NetworkSliceMsgCenter final {
30     DECLARE_SINGLETON(NetworkSliceMsgCenter);
31 public:
32     void registHandler(NetworkSliceSubModule moduleId, std::shared_ptr<AppExecFwk::EventHandler> handler);
33     void unRegistHandler(NetworkSliceSubModule moduleId, std::shared_ptr<AppExecFwk::EventHandler> handler);
34     void Subscribe(NetworkSliceEvent event, NetworkSliceSubModule moduleId);
35     void UnSubscribe(NetworkSliceEvent event, NetworkSliceSubModule moduleId);
36 
37     template<typename... Args>
38     void Publish(NetworkSliceEvent event, Args... args);
39     template<typename... Args>
40     void PublishDelay(NetworkSliceEvent event, int64_t delayTime, Args... args);
41 private:
42     static std::shared_timed_mutex mutex_;
43     std::map<NetworkSliceSubModule,
44         std::list<std::shared_ptr<AppExecFwk::EventHandler>>> moduleIdHandlerListMap_;
45     std::map<NetworkSliceEvent, uint64_t> eventHandlerMap_;
46     std::map<uint64_t, uint64_t> eventSubscribeMap_;
47 };
48 
49 template<typename... Args>
Publish(NetworkSliceEvent event,Args...args)50 void NetworkSliceMsgCenter::Publish(NetworkSliceEvent event, Args... args)
51 {
52     NETMGR_EXT_LOG_I("enter event:%{public}d", event);
53     std::unique_lock<std::shared_timed_mutex> lock(mutex_);
54     auto it = eventHandlerMap_.find(event);
55     if (it == eventHandlerMap_.end()) {
56         NETMGR_EXT_LOG_I("event:%{public}d, no module subscribed!", event);
57         return;
58     }
59     for (int i = MODULE_COMMON; i < MODULE_BUTT; i++) {
60         if ((it->second & (1 << i)) == 0) {
61             continue;
62         }
63         auto itHandlerList = moduleIdHandlerListMap_.find(static_cast<NetworkSliceSubModule>(i));
64         if (itHandlerList == moduleIdHandlerListMap_.end()) {
65             continue;
66         }
67         for (auto itHandler : itHandlerList->second) {
68             if (sizeof...(args) > 0) {
69                 itHandler->SendEvent(event, args..., 0);
70             } else {
71                 itHandler->SendEvent(event);
72             }
73         }
74     }
75 }
76 
77 template<typename... Args>
PublishDelay(NetworkSliceEvent event,int64_t delayTime,Args...args)78 void NetworkSliceMsgCenter::PublishDelay(NetworkSliceEvent event, int64_t delayTime, Args... args)
79 {
80     NETMGR_EXT_LOG_I("enter event:%{public}d", event);
81     std::unique_lock<std::shared_timed_mutex> lock(mutex_);
82     auto it = eventHandlerMap_.find(event);
83     if (it == eventHandlerMap_.end()) {
84         NETMGR_EXT_LOG_I("event:%{public}d, not fined!", event);
85         return;
86     }
87     for (int i = MODULE_COMMON; i < MODULE_BUTT; i++) {
88         if ((it->second & (1 << i)) == 0) {
89             continue;
90         }
91         auto itHandlerList = moduleIdHandlerListMap_.find(static_cast<NetworkSliceSubModule>(i));
92         if (itHandlerList == moduleIdHandlerListMap_.end()) {
93             continue;
94         }
95         NETMGR_EXT_LOG_I("module:%{public}d SendEvent!", i);
96         for (auto itHandler : itHandlerList->second) {
97             itHandler->SendEvent(event, args..., delayTime);
98         }
99     }
100 }
101 
102 }
103 }
104 #endif
105