1 /*
2 * Copyright (c) 2021-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 "network/session_pool.h"
17 #include "dm_device_info.h"
18 #include "device/device_manager_agent.h"
19 #include "dfs_error.h"
20 #include "ipc/i_daemon.h"
21
22 namespace OHOS {
23 namespace Storage {
24 namespace DistributedFile {
25 using namespace std;
26
HoldSession(shared_ptr<BaseSession> session,const std::string backStage)27 void SessionPool::HoldSession(shared_ptr<BaseSession> session, const std::string backStage)
28 {
29 if (talker_ == nullptr) {
30 LOGE("talker_ is nullptr.");
31 return;
32 }
33 lock_guard lock(sessionPoolLock_);
34 talker_->SinkSessionTokernel(session, backStage);
35 AddSessionToPool(session);
36 }
37
ReleaseSession(const int32_t fd)38 void SessionPool::ReleaseSession(const int32_t fd)
39 {
40 lock_guard lock(sessionPoolLock_);
41 LOGI("ReleaseSession start, fd=%{public}d", fd);
42 for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end(); ++iter) {
43 if ((*iter)->GetHandle() == fd) {
44 (*iter)->Release();
45 iter = usrSpaceSessionPool_.erase(iter);
46 break;
47 }
48 }
49 }
50
CheckIfGetSession(const int32_t fd)51 bool SessionPool::CheckIfGetSession(const int32_t fd)
52 {
53 lock_guard lock(sessionPoolLock_);
54 std::shared_ptr<BaseSession> session = nullptr;
55 for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end(); ++iter) {
56 if ((*iter)->GetHandle() == fd) {
57 session = *iter;
58 break;
59 }
60 }
61 if (session == nullptr) {
62 return false;
63 }
64 return !session->IsFromServer();
65 }
66
SinkOffline(const std::string & cid)67 void SessionPool::SinkOffline(const std::string &cid)
68 {
69 lock_guard lock(sessionPoolLock_);
70 if (!FindCid(cid) && talker_ != nullptr) {
71 talker_->SinkOfflineCmdToKernel(cid);
72 }
73 }
74
FindSocketId(int32_t socketId)75 bool SessionPool::FindSocketId(int32_t socketId)
76 {
77 lock_guard lock(sessionPoolLock_);
78 for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end(); ++iter) {
79 if ((*iter)->GetSessionId() == socketId) {
80 return true;
81 }
82 }
83 return false;
84 }
85
ReleaseSession(const std::string & cid,bool isReleaseAll)86 void SessionPool::ReleaseSession(const std::string &cid, bool isReleaseAll)
87 {
88 lock_guard lock(sessionPoolLock_);
89 std::vector<std::shared_ptr<BaseSession>> sessions;
90 LOGI("ReleaseSession, cid:%{public}s", Utils::GetAnonyString(cid).c_str());
91 for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end();) {
92 if ((*iter)->GetCid() != cid || ((*iter)->IsFromServer() && !isReleaseAll)) {
93 ++iter;
94 continue;
95 }
96 sessions.push_back(*iter);
97 iter = usrSpaceSessionPool_.erase(iter);
98 }
99 for (const auto &session : sessions) {
100 session->Release();
101 }
102
103 if (talker_ == nullptr) {
104 LOGE("talker_ is nullptr.");
105 return;
106 }
107 if (!FindCid(cid)) {
108 talker_->SinkOfflineCmdToKernel(cid);
109 }
110 }
111
FindCid(const std::string & cid)112 bool SessionPool::FindCid(const std::string &cid)
113 {
114 for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end(); ++iter) {
115 if ((*iter)->GetCid() == cid) {
116 return true;
117 }
118 }
119 return false;
120 }
121
ReleaseAllSession()122 void SessionPool::ReleaseAllSession()
123 {
124 if (talker_ == nullptr) {
125 LOGE("talker_ is nullptr.");
126 return;
127 }
128 lock_guard lock(sessionPoolLock_);
129 for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end();) {
130 talker_->SinkOfflineCmdToKernel((*iter)->GetCid());
131 /* device offline, session release by softbus */
132 iter = usrSpaceSessionPool_.erase(iter);
133 }
134 }
135
AddSessionToPool(shared_ptr<BaseSession> session)136 void SessionPool::AddSessionToPool(shared_ptr<BaseSession> session)
137 {
138 lock_guard lock(sessionPoolLock_);
139 usrSpaceSessionPool_.push_back(session);
140 }
141 } // namespace DistributedFile
142 } // namespace Storage
143 } // namespace OHOS
144