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/softbus/softbus_session_dispatcher.h"
17
18 #include <sstream>
19
20 #include "dm_device_info.h"
21 #include "network/softbus/softbus_agent.h"
22 #include "utils_log.h"
23
24 namespace OHOS {
25 namespace Storage {
26 namespace DistributedFile {
27 using namespace std;
28 mutex SoftbusSessionDispatcher::idMapMutex_;
29 map<int32_t, std::pair<std::string, std::string>> SoftbusSessionDispatcher::idMap_;
30 mutex SoftbusSessionDispatcher::softbusAgentMutex_;
31 map<string, weak_ptr<SoftbusAgent>> SoftbusSessionDispatcher::busNameToAgent_;
32
RegisterSessionListener(const string busName,weak_ptr<SoftbusAgent> softbusAgent)33 void SoftbusSessionDispatcher::RegisterSessionListener(const string busName, weak_ptr<SoftbusAgent> softbusAgent)
34 {
35 if (busName == "") {
36 stringstream ss;
37 ss << "Failed to register session to softbus";
38 LOGE("%{public}s", ss.str().c_str());
39 throw runtime_error(ss.str());
40 }
41 lock_guard<mutex> lock(softbusAgentMutex_);
42 auto agent = busNameToAgent_.find(busName);
43 if (agent != busNameToAgent_.end()) {
44 stringstream ss;
45 ss << "this softbusAgent is exist, busName: " << busName.c_str();
46 LOGE("%{public}s", ss.str().c_str());
47 throw runtime_error(ss.str());
48 } else {
49 busNameToAgent_.insert(make_pair(busName, softbusAgent));
50 }
51 LOGI("RegisterSessionListener Success, busName:%{public}s", busName.c_str());
52 }
UnregisterSessionListener(const string busName)53 void SoftbusSessionDispatcher::UnregisterSessionListener(const string busName)
54 {
55 lock_guard<mutex> lock(softbusAgentMutex_);
56 auto agent = busNameToAgent_.find(busName);
57 if (agent != busNameToAgent_.end()) {
58 busNameToAgent_.erase(busName);
59 } else {
60 stringstream ss;
61 ss << "this softbusAgent is not exist, busName: " << busName.c_str();
62 LOGE("%{public}s", ss.str().c_str());
63 throw runtime_error(ss.str());
64 }
65 LOGI("UnregisterSessionListener Success, busName:%{public}s", busName.c_str());
66 }
GetAgent(int32_t sessionId,std::string peerSessionName)67 weak_ptr<SoftbusAgent> SoftbusSessionDispatcher::GetAgent(int32_t sessionId, std::string peerSessionName)
68 {
69 if (peerSessionName.empty()) {
70 return {};
71 }
72 lock_guard<mutex> lock(softbusAgentMutex_);
73 auto agent = busNameToAgent_.find(string(peerSessionName));
74 if (agent != busNameToAgent_.end()) {
75 LOGI("Get softbus Agent Success, busName:%{public}s", peerSessionName.c_str());
76 return agent->second;
77 }
78 LOGE("Get Session Agent fail, not exist! sessionId:%{public}d", sessionId);
79 return {};
80 }
OnSessionOpened(int32_t sessionId,PeerSocketInfo info)81 void SoftbusSessionDispatcher::OnSessionOpened(int32_t sessionId, PeerSocketInfo info)
82 {
83 LOGI("OnSessionOpened Enter sessionId = %{public}d", sessionId);
84 if (!SoftbusAgent::IsSameAccount(info.networkId)) {
85 LOGI("The source and sink device is not same account, not support.");
86 Shutdown(sessionId);
87 return;
88 }
89 std::string peerSessionName(info.name);
90 std::string peerDevId = info.networkId;
91 {
92 std::lock_guard<std::mutex> lock(idMapMutex_);
93 idMap_[sessionId] = std::make_pair(peerDevId, peerSessionName);
94 }
95 auto agent = GetAgent(sessionId, peerSessionName);
96 if (auto spt = agent.lock()) {
97 spt->OnSessionOpened(sessionId, info);
98 } else {
99 LOGE("session not exist!, session id is %{public}d", sessionId);
100 return;
101 }
102 }
OnSessionClosed(int32_t sessionId,ShutdownReason reason)103 void SoftbusSessionDispatcher::OnSessionClosed(int32_t sessionId, ShutdownReason reason)
104 {
105 LOGI("OnSessionClosed Enter sessionId = %{public}d", sessionId);
106 (void)reason;
107 std::string peerSessionName = "";
108 std::string peerDevId = "";
109 {
110 std::lock_guard<std::mutex> lock(idMapMutex_);
111 auto it = idMap_.find(sessionId);
112 if (it != idMap_.end()) {
113 peerDevId = it->second.first;
114 peerSessionName = it->second.second;
115 idMap_.erase(it);
116 }
117 }
118
119 auto agent = GetAgent(sessionId, peerSessionName);
120 if (auto spt = agent.lock()) {
121 spt->OnSessionClosed(sessionId, peerDevId);
122 } else {
123 LOGE("session not exist!, session id is %{public}d", sessionId);
124 }
125 }
126 } // namespace DistributedFile
127 } // namespace Storage
128 } // namespace OHOS
129