• 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 #include "networkslicemsgcenter.h"
17 
18 namespace OHOS {
19 namespace NetManagerStandard {
20 NetworkSliceMsgCenter::NetworkSliceMsgCenter() = default;
21 NetworkSliceMsgCenter::~NetworkSliceMsgCenter() = default;
22 
23 std::shared_timed_mutex NetworkSliceMsgCenter::mutex_ = {};
24 
registHandler(NetworkSliceSubModule moduleId,std::shared_ptr<AppExecFwk::EventHandler> handler)25 void NetworkSliceMsgCenter::registHandler(NetworkSliceSubModule moduleId,
26     std::shared_ptr<AppExecFwk::EventHandler> handler)
27 {
28     std::unique_lock<std::shared_timed_mutex> lock(mutex_);
29     auto itHandlerList = moduleIdHandlerListMap_.find(moduleId);
30     if (itHandlerList != moduleIdHandlerListMap_.end()) {
31         auto &handleList = itHandlerList->second;
32         if (std::find(handleList.begin(), handleList.end(), handler) == handleList.end()) {
33             handleList.push_back(handler);
34         }
35     } else {
36         moduleIdHandlerListMap_[moduleId] = { handler };
37     }
38 }
39 
unRegistHandler(NetworkSliceSubModule moduleId,std::shared_ptr<AppExecFwk::EventHandler> handler)40 void NetworkSliceMsgCenter::unRegistHandler(NetworkSliceSubModule moduleId,
41     std::shared_ptr<AppExecFwk::EventHandler> handler)
42 {
43     std::unique_lock<std::shared_timed_mutex> lock(mutex_);
44     auto itHandlerList = moduleIdHandlerListMap_.find(moduleId);
45     if (itHandlerList != moduleIdHandlerListMap_.end()) {
46         itHandlerList->second.remove(handler);
47     }
48     if (itHandlerList == moduleIdHandlerListMap_.end() || itHandlerList->second.empty()) {
49         for (auto it = eventHandlerMap_.begin(); it != eventHandlerMap_.end(); it++) {
50             it->second &= ~(1 << moduleId);
51             uint64_t key = (static_cast<uint64_t>(it->first) << 32) &  static_cast<uint64_t>(moduleId);
52             eventSubscribeMap_.erase(key);
53         }
54         moduleIdHandlerListMap_.erase(moduleId);
55     }
56 }
57 
Subscribe(NetworkSliceEvent event,NetworkSliceSubModule moduleId)58 void NetworkSliceMsgCenter::Subscribe(NetworkSliceEvent event, NetworkSliceSubModule moduleId)
59 {
60     NETMGR_EXT_LOG_I("event:%{public}d, module:%{public}d", event, moduleId);
61     std::unique_lock<std::shared_timed_mutex> lock(mutex_);
62     auto it = eventHandlerMap_.find(event);
63     if (it != eventHandlerMap_.end()) {
64         it->second |= (1 << moduleId);
65     } else {
66         eventHandlerMap_.insert(std::make_pair(event, 1 << moduleId));
67     }
68     uint64_t key = (static_cast<uint64_t>(event) << 32) &  static_cast<uint64_t>(moduleId);
69     eventSubscribeMap_[key]++;
70 }
71 
UnSubscribe(NetworkSliceEvent event,NetworkSliceSubModule moduleId)72 void NetworkSliceMsgCenter::UnSubscribe(NetworkSliceEvent event, NetworkSliceSubModule moduleId)
73 {
74     uint64_t key = (static_cast<uint64_t>(event) << 32) &  static_cast<uint64_t>(moduleId);
75     auto it = eventSubscribeMap_.find(key);
76     if (it == eventSubscribeMap_.end() || it->second <= 1) {
77         auto it = eventHandlerMap_.find(event);
78         if (it != eventHandlerMap_.end()) {
79             it->second &= ~(1 << moduleId);
80         }
81         eventSubscribeMap_.erase(key);
82     } else {
83         eventSubscribeMap_[key]--;
84     }
85 }
86 
87 }
88 }
89