1 /*
2 * Copyright (c) 2023 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 #include "network/softbus/softbus_session_pool.h"
16
17 #include "session.h"
18 #include <algorithm>
19
20 namespace OHOS {
21 namespace Storage {
22 namespace DistributedFile {
23 constexpr int32_t SESSION_COUNT = 10;
GetInstance()24 SoftBusSessionPool &SoftBusSessionPool::GetInstance()
25 {
26 static SoftBusSessionPool instance;
27 return instance;
28 }
29
GenerateSessionName()30 std::string SoftBusSessionPool::GenerateSessionName()
31 {
32 std::lock_guard<std::mutex> createSessionMutex(sessionMutex_);
33 for (int32_t i = 0; i < SESSION_COUNT; i++) {
34 std::string sessionName = std::string(SESSION_NAME_PREFIX) + std::to_string(i);
35 auto it = sessionMap_.find(sessionName);
36 if (it == sessionMap_.end()) {
37 return sessionName;
38 }
39 }
40 return "";
41 }
42
AddSessionInfo(const std::string & sessionName,const SessionInfo & sessionInfo)43 void SoftBusSessionPool::AddSessionInfo(const std::string &sessionName, const SessionInfo &sessionInfo)
44 {
45 std::lock_guard<std::mutex> createSessionMutex(sessionMutex_);
46 sessionMap_.insert(std::pair<std::string, SessionInfo>(sessionName, sessionInfo));
47 }
48
DeleteSessionInfo(const std::string & sessionName)49 void SoftBusSessionPool::DeleteSessionInfo(const std::string &sessionName)
50 {
51 std::lock_guard<std::mutex> createSessionMutex(sessionMutex_);
52 sessionMap_.erase(sessionName);
53 }
54
GetSessionInfo(const std::string & sessionName,SessionInfo & sessionInfo)55 bool SoftBusSessionPool::GetSessionInfo(const std::string &sessionName, SessionInfo &sessionInfo)
56 {
57 for (const auto &it : sessionMap_) {
58 if (it.first == sessionName) {
59 sessionInfo = it.second;
60 return true;
61 }
62 }
63 return false;
64 }
65 } // namespace DistributedFile
66 } // namespace Storage
67 } // namespace OHOS