• 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/softbus/softbus_session_dispatcher.h"
17 
18 #include <sstream>
19 
20 #include "network/softbus/softbus_agent.h"
21 #include "session.h"
22 #include "utils_log.h"
23 
24 namespace OHOS {
25 namespace Storage {
26 namespace DistributedFile {
27 using namespace std;
28 
29 constexpr int32_t SESSION_NAME_SIZE_MAX = 256;
30 
31 mutex SoftbusSessionDispatcher::softbusAgentMutex_;
32 map<string, weak_ptr<SoftbusAgent>> SoftbusSessionDispatcher::busNameToAgent_;
33 
RegisterSessionListener(const string busName,weak_ptr<SoftbusAgent> softbusAgent)34 void SoftbusSessionDispatcher::RegisterSessionListener(const string busName, weak_ptr<SoftbusAgent> softbusAgent)
35 {
36     if (busName == "") {
37         stringstream ss;
38         ss << "Failed to register session to softbus";
39         LOGE("%{public}s", ss.str().c_str());
40         throw runtime_error(ss.str());
41     }
42     lock_guard<mutex> lock(softbusAgentMutex_);
43     auto agent = busNameToAgent_.find(busName);
44     if (agent != busNameToAgent_.end()) {
45         stringstream ss;
46         ss << "this softbusAgent is not exist, busName: " << busName.c_str();
47         LOGE("%{public}s", ss.str().c_str());
48         throw runtime_error(ss.str());
49     } else {
50         busNameToAgent_.insert(make_pair(busName, softbusAgent));
51     }
52     LOGD("RegisterSessionListener SUCCESS, busName:%{public}s", busName.c_str());
53 }
UnregisterSessionListener(const string busName)54 void SoftbusSessionDispatcher::UnregisterSessionListener(const string busName)
55 {
56     lock_guard<mutex> lock(softbusAgentMutex_);
57     auto agent = busNameToAgent_.find(busName);
58     if (agent != busNameToAgent_.end()) {
59         busNameToAgent_.erase(busName);
60     } else {
61         stringstream ss;
62         ss << "this softbusAgent is not exist, busName: " << busName.c_str();
63         LOGE("%{public}s", ss.str().c_str());
64         throw runtime_error(ss.str());
65     }
66     LOGD("UnregisterSessionListener SUCCESS, busName:%{public}s", busName.c_str());
67 }
GetAgent(int sessionId)68 weak_ptr<SoftbusAgent> SoftbusSessionDispatcher::GetAgent(int sessionId)
69 {
70     char peeSessionName[SESSION_NAME_SIZE_MAX];
71     int ret = GetPeerSessionName(sessionId, peeSessionName, sizeof(peeSessionName));
72     if (ret != 0) {
73         LOGE("Get my peer session name failed, session id is %{public}d.", sessionId);
74         return {};
75     }
76     auto agent = busNameToAgent_.find(string(peeSessionName));
77     if (agent != busNameToAgent_.end()) {
78         LOGD("Get softbus Agent Success, busName:%{public}s", peeSessionName);
79         return agent->second;
80     }
81     LOGE("Get Session Agent fail, not exist! sessionId:%{public}d, busName:%{public}s", sessionId, peeSessionName);
82     return {};
83 }
OnSessionOpened(int sessionId,int result)84 int SoftbusSessionDispatcher::OnSessionOpened(int sessionId, int result)
85 {
86     auto agent = GetAgent(sessionId);
87     if (auto spt = agent.lock()) {
88         return spt->OnSessionOpened(sessionId, result);
89     } else {
90         LOGE("session not exist!, session id is %{public}d", sessionId);
91         return -1;
92     }
93 }
OnSessionClosed(int sessionId)94 void SoftbusSessionDispatcher::OnSessionClosed(int sessionId)
95 {
96     auto agent = GetAgent(sessionId);
97     if (auto spt = agent.lock()) {
98         spt->OnSessionClosed(sessionId);
99     } else {
100         LOGE("session not exist!, session id is %{public}d", sessionId);
101     }
102 }
103 } // namespace DistributedFile
104 } // namespace Storage
105 } // namespace OHOS
106