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