• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
18 namespace OHOS {
19 namespace Storage {
20 namespace DistributedFile {
21 using namespace std;
22 
OccupySession(int sessionId,uint8_t linkType)23 void SessionPool::OccupySession(int sessionId, uint8_t linkType)
24 {
25     lock_guard lock(sessionPoolLock_);
26     occupySession_.insert(std::pair<int, uint8_t>(sessionId, linkType));
27 }
28 
FindSession(int sessionId)29 bool SessionPool::FindSession(int sessionId)
30 {
31     lock_guard lock(sessionPoolLock_);
32     auto linkTypeIter = occupySession_.find(sessionId);
33     if (linkTypeIter != occupySession_.end()) {
34         return true;
35     } else {
36         return false;
37     }
38 }
39 
HoldSession(shared_ptr<BaseSession> session)40 void SessionPool::HoldSession(shared_ptr<BaseSession> session)
41 {
42     lock_guard lock(sessionPoolLock_);
43     talker_->SinkSessionTokernel(session);
44     AddSessionToPool(session);
45 }
46 
ReleaseSession(const int32_t fd)47 uint8_t SessionPool::ReleaseSession(const int32_t fd)
48 {
49     uint8_t linkType = 0;
50     lock_guard lock(sessionPoolLock_);
51     for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end();) {
52         if ((*iter)->GetHandle() == fd) {
53             auto linkTypeIter = occupySession_.find((*iter)->GetSessionId());
54             if (linkTypeIter != occupySession_.end()) {
55                 linkType = linkTypeIter->second;
56                 occupySession_.erase(linkTypeIter);
57                 (*iter)->Release();
58                 iter = usrSpaceSessionPool_.erase(iter);
59                 break;
60             }
61         }
62         ++iter;
63     }
64     return linkType;
65 }
66 
ReleaseSession(const string & cid,const uint8_t linkType)67 void SessionPool::ReleaseSession(const string &cid, const uint8_t linkType)
68 {
69     uint8_t mlinkType = 0;
70     lock_guard lock(sessionPoolLock_);
71     for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end();) {
72         if ((*iter)->GetCid() == cid) {
73             auto linkTypeIter = occupySession_.find((*iter)->GetSessionId());
74             if (linkTypeIter != occupySession_.end()) {
75                 mlinkType = (linkTypeIter->second == 0) ? linkType : linkTypeIter->second;
76             }
77             if (mlinkType == linkType) {
78                 (*iter)->Release();
79                 occupySession_.erase(linkTypeIter);
80                 iter = usrSpaceSessionPool_.erase(iter);
81                 mlinkType = 0;
82                 continue;
83             }
84         }
85         ++iter;
86     }
87 }
88 
ReleaseAllSession()89 void SessionPool::ReleaseAllSession()
90 {
91     lock_guard lock(sessionPoolLock_);
92     for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end();) {
93         talker_->SinkOfflineCmdToKernel((*iter)->GetCid());
94         /* device offline, session release by softbus */
95         iter = usrSpaceSessionPool_.erase(iter);
96     }
97 }
98 
AddSessionToPool(shared_ptr<BaseSession> session)99 void SessionPool::AddSessionToPool(shared_ptr<BaseSession> session)
100 {
101     usrSpaceSessionPool_.push_back(session);
102 }
103 } // namespace DistributedFile
104 } // namespace Storage
105 } // namespace OHOS
106