1 /*
2 * Copyright (c) 2024 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 "connect_count/connect_count.h"
17
18 #include "dfs_error.h"
19 #include "utils_directory.h"
20 #include "utils_log.h"
21
22 namespace OHOS {
23 namespace Storage {
24 namespace DistributedFile {
25 std::shared_ptr<ConnectCount> ConnectCount::instance_ = nullptr;
26 static const int32_t ON_STATUS_OFFLINE = 13900046;
GetInstance()27 std::shared_ptr<ConnectCount> ConnectCount::GetInstance()
28 {
29 static std::once_flag once;
30 std::call_once(once, [&]() {
31 instance_ = std::make_shared<ConnectCount>();
32 });
33 return instance_;
34 }
35
AddConnect(uint32_t callingTokenId,const std::string & networkId,sptr<IFileDfsListener> & listener)36 void ConnectCount::AddConnect(uint32_t callingTokenId, const std::string &networkId, sptr<IFileDfsListener> &listener)
37 {
38 std::lock_guard lock(connectMutex_);
39 for (auto &connect : connectList_) {
40 if (connect->callingTokenId == callingTokenId && connect->networkId == networkId) {
41 connect->num++;
42 return;
43 }
44 }
45 auto connect = std::make_shared<Connect>(callingTokenId, networkId, 1, listener);
46 connectList_.insert(connect);
47 }
48
CheckCount(const std::string & networkId)49 bool ConnectCount::CheckCount(const std::string &networkId)
50 {
51 std::lock_guard lock(connectMutex_);
52 for (const auto &connect : connectList_) {
53 if (connect->networkId == networkId) {
54 return true;
55 }
56 }
57 return false;
58 }
59
RemoveAllConnect()60 void ConnectCount::RemoveAllConnect()
61 {
62 std::lock_guard lock(connectMutex_);
63 for (const auto &connect : connectList_) {
64 if (connect->listener != nullptr) {
65 connect->listener->OnStatus(connect->networkId, ON_STATUS_OFFLINE);
66 }
67 }
68 connectList_.clear();
69 }
70
RemoveConnect(uint32_t callingTokenId)71 std::vector<std::string> ConnectCount::RemoveConnect(uint32_t callingTokenId)
72 {
73 std::lock_guard lock(connectMutex_);
74 auto networkIds = GetNetworkIds(callingTokenId);
75 for (auto iter = connectList_.begin(); iter != connectList_.end();) {
76 if ((*iter)->callingTokenId == callingTokenId) {
77 iter = connectList_.erase(iter);
78 continue;
79 }
80 iter++;
81 }
82 return networkIds;
83 }
84
RemoveConnect(const std::string & networkId)85 void ConnectCount::RemoveConnect(const std::string &networkId)
86 {
87 std::lock_guard lock(connectMutex_);
88 for (auto iter = connectList_.begin(); iter != connectList_.end();) {
89 if ((*iter)->networkId == networkId) {
90 iter = connectList_.erase(iter);
91 continue;
92 }
93 iter++;
94 }
95 }
96
RemoveConnect(uint32_t callingTokenId,const std::string & networkId)97 void ConnectCount::RemoveConnect(uint32_t callingTokenId, const std::string &networkId)
98 {
99 std::lock_guard lock(connectMutex_);
100 for (auto iter = connectList_.begin(); iter != connectList_.end();) {
101 if ((*iter)->callingTokenId == callingTokenId && (*iter)->networkId == networkId) {
102 iter = connectList_.erase(iter);
103 continue;
104 }
105 iter++;
106 }
107 }
108
GetNetworkIds(uint32_t callingTokenId)109 std::vector<std::string> ConnectCount::GetNetworkIds(uint32_t callingTokenId)
110 {
111 std::lock_guard lock(connectMutex_);
112 std::vector<std::string> networkIds;
113 for (const auto &connect : connectList_) {
114 if (connect->callingTokenId == callingTokenId) {
115 networkIds.push_back(connect->networkId);
116 }
117 }
118 return networkIds;
119 }
120
NotifyRemoteReverseObj(const std::string & networkId,int32_t status)121 void ConnectCount::NotifyRemoteReverseObj(const std::string &networkId, int32_t status)
122 {
123 std::lock_guard lock(connectMutex_);
124 for (const auto &connect : connectList_) {
125 if (connect->networkId == networkId && connect->listener != nullptr) {
126 connect->listener->OnStatus(networkId, status);
127 LOGI("NotifyRemoteReverseObj, networkId: %{public}s, callingTokenId: %{public}u",
128 Utils::GetAnonyString(networkId).c_str(), connect->callingTokenId);
129 }
130 }
131 }
132
FindCallingTokenIdForListerner(const sptr<IRemoteObject> & listener,uint32_t & callingTokenId)133 int32_t ConnectCount::FindCallingTokenIdForListerner(const sptr<IRemoteObject> &listener, uint32_t &callingTokenId)
134 {
135 std::lock_guard lock(connectMutex_);
136 for (const auto &connect : connectList_) {
137 if (connect->listener != nullptr && (connect->listener)->AsObject() == listener) {
138 callingTokenId = connect->callingTokenId;
139 return FileManagement::E_OK;
140 }
141 }
142 return FileManagement::ERR_BAD_VALUE;
143 }
144 } // namespace DistributedFile
145 } // namespace Storage
146 } // namespace OHOS
147