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