• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "softbus_distributed_data_manager.h"
17 
18 #include "avsession_log.h"
19 #include "avsession_errors.h"
20 #include "migrate_avsession_constant.h"
21 #include "softbus_session_utils.h"
22 
23 
24 namespace OHOS::AVSession {
SoftbusDistributedDataManager()25 SoftbusDistributedDataManager::SoftbusDistributedDataManager() {}
26 
~SoftbusDistributedDataManager()27 SoftbusDistributedDataManager::~SoftbusDistributedDataManager() {}
28 
Init()29 void SoftbusDistributedDataManager::Init()
30 {
31     std::weak_ptr<SoftbusDistributedDataManager> managerWeak(shared_from_this());
32     ssListener_ = std::make_shared<SSListener>(managerWeak);
33     SoftbusSessionManager::GetInstance().AddSessionListener(ssListener_);
34 }
35 
SessionOpened(int32_t sessionId)36 void SoftbusDistributedDataManager::SessionOpened(int32_t sessionId)
37 {
38     std::string sessionName;
39     int32_t ret = SoftbusSessionManager::GetInstance().GetPeerSessionName(sessionId, sessionName);
40     CHECK_AND_RETURN_LOG(ret == AVSESSION_SUCCESS, "GetPeerSessionName failed");
41     if (sessionName != CONFIG_SOFTBUS_SESSION_TAG) {
42         SLOGE("onSessionOpened: the group id is not match the media session group. sessionName is %{public}s",
43             sessionName.c_str());
44         return;
45     }
46     if (SoftbusSessionManager::GetInstance().IsServerSide(sessionId)) {
47         OnSessionServerOpened(sessionId);
48     }
49 }
50 
SessionClosed(int32_t sessionId)51 void SoftbusDistributedDataManager::SessionClosed(int32_t sessionId)
52 {
53     std::string sessionName;
54     int32_t ret = SoftbusSessionManager::GetInstance().GetPeerSessionName(sessionId, sessionName);
55     CHECK_AND_RETURN_LOG(ret == AVSESSION_SUCCESS, "GetPeerSessionName failed");
56     if (sessionName != CONFIG_SOFTBUS_SESSION_TAG) {
57         SLOGE("onSessionOpened: the group id is not match the media session group.");
58         return;
59     }
60     std::string deviceId;
61     ret = SoftbusSessionManager::GetInstance().ObtainPeerDeviceId(sessionId, deviceId);
62     CHECK_AND_RETURN_LOG(ret == AVSESSION_SUCCESS, "ObtainPeerDeviceId failed");
63     if (SoftbusSessionManager::GetInstance().IsServerSide(sessionId)) {
64         OnSessionServerClosed(sessionId, deviceId);
65     }
66 }
67 
MessageReceived(int32_t sessionId,const std::string & data)68 void SoftbusDistributedDataManager::MessageReceived(int32_t sessionId, const std::string &data)
69 {
70     std::string sessionName;
71     int32_t ret = SoftbusSessionManager::GetInstance().GetPeerSessionName(sessionId, sessionName);
72     CHECK_AND_RETURN_LOG(ret == AVSESSION_SUCCESS, "GetPeerSessionName failed");
73     if (sessionName != CONFIG_SOFTBUS_SESSION_TAG) {
74         SLOGE("onSessionOpened: the group id is not match the media session group. sessionName is %{public}s",
75             sessionName.c_str());
76         return;
77     }
78     OnMessageHandleReceived(sessionId, data);
79 }
80 
BytesReceived(int32_t sessionId,const std::string & data)81 void SoftbusDistributedDataManager::BytesReceived(int32_t sessionId, const std::string &data)
82 {
83     std::string sessionName;
84     int32_t ret = SoftbusSessionManager::GetInstance().GetPeerSessionName(sessionId, sessionName);
85     CHECK_AND_RETURN_LOG(ret == AVSESSION_SUCCESS, "GetPeerSessionName failed");
86     if (sessionName != CONFIG_SOFTBUS_SESSION_TAG) {
87         SLOGE("onSessionOpened: the group id is not match the media session group. sessionName is %{public}s",
88             sessionName.c_str());
89         return;
90     }
91     if (SoftbusSessionManager::GetInstance().IsServerSide(sessionId)) {
92         OnBytesServerReceived(sessionId, data);
93     }
94 }
95 
InitSessionServer(const std::string & pkg)96 void SoftbusDistributedDataManager::InitSessionServer(const std::string &pkg)
97 {
98     SLOGI("init session server...");
99     SoftbusSessionManager::GetInstance().CreateSessionServer(pkg);
100 }
101 
CreateServer(const std::shared_ptr<SoftbusSessionServer> & server)102 void SoftbusDistributedDataManager::CreateServer(const std::shared_ptr<SoftbusSessionServer> &server)
103 {
104     if (server == nullptr) {
105         SLOGE("createServer fail for server is null.");
106         return;
107     }
108     int characteristic = server->GetCharacteristic();
109     std::lock_guard lockGuard(softbusDistributedDataLock_);
110     serverMap_.insert({ characteristic, server });
111 }
112 
DestroySessionServer(const std::string & pkg)113 void SoftbusDistributedDataManager::DestroySessionServer(const std::string &pkg)
114 {
115     SLOGI("destroy session server...");
116     std::lock_guard lockGuard(softbusDistributedDataLock_);
117     for (auto it = serverMap_.begin(); it != serverMap_.end();) {
118         it->second->DisconnectAllProxy();
119         serverMap_.erase(it++);
120     }
121     SoftbusSessionManager::GetInstance().RemoveSessionServer(pkg);
122 }
123 
ReleaseServer(const std::shared_ptr<SoftbusSessionServer> & server)124 void SoftbusDistributedDataManager::ReleaseServer(const std::shared_ptr<SoftbusSessionServer> &server)
125 {
126     int characteristic = server->GetCharacteristic();
127     std::lock_guard lockGuard(softbusDistributedDataLock_);
128     auto iter = serverMap_.find(characteristic);
129     if (iter != serverMap_.end() && iter->second == server) {
130         server->DisconnectAllProxy();
131         serverMap_.erase(characteristic);
132     }
133 }
134 
OnSessionServerOpened(int32_t sessionId)135 void SoftbusDistributedDataManager::OnSessionServerOpened(int32_t sessionId)
136 {
137     std::string deviceId;
138     SoftbusSessionManager::GetInstance().ObtainPeerDeviceId(sessionId, deviceId);
139     SLOGI("OnSessionServerOpened: the peer device id is %{public}s.",
140         SoftbusSessionUtils::AnonymizeDeviceId(deviceId).c_str());
141 }
142 
OnSessionServerClosed(int32_t sessionId,const std::string & deviceId)143 void SoftbusDistributedDataManager::OnSessionServerClosed(int32_t sessionId, const std::string &deviceId)
144 {
145     SLOGI("OnSessionServerClosed: the peer device id is %{public}s.",
146         SoftbusSessionUtils::AnonymizeDeviceId(deviceId).c_str());
147     std::lock_guard lockGuard(softbusDistributedDataLock_);
148     for (auto it = serverMap_.begin(); it != serverMap_.end(); it++) {
149         it->second->DisconnectProxy(sessionId);
150     }
151 }
152 
OnMessageHandleReceived(int32_t sessionId,const std::string & data)153 void SoftbusDistributedDataManager::OnMessageHandleReceived(int32_t sessionId, const std::string &data)
154 {
155     std::string deviceId;
156     SoftbusSessionManager::GetInstance().ObtainPeerDeviceId(sessionId, deviceId);
157     std::string anonymizeDeviceId = SoftbusSessionUtils::AnonymizeDeviceId(deviceId);
158     SLOGI("onMessageHandleReceived: %{public}s", anonymizeDeviceId.c_str());
159     if (data.length() > 1 && data[0] == MESSAGE_CODE_CONNECT_SERVER) {
160         std::lock_guard lockGuard(softbusDistributedDataLock_);
161         auto iter = serverMap_.find(data[1]);
162         if (iter == serverMap_.end()) {
163             SLOGE("onMessageHandleReceived: server is invalid deviceId %{public}s", anonymizeDeviceId.c_str());
164             return;
165         }
166         iter->second->ConnectProxy(sessionId);
167     }
168 }
169 
OnBytesServerReceived(int32_t sessionId,const std::string & data)170 void SoftbusDistributedDataManager::OnBytesServerReceived(int32_t sessionId, const std::string &data)
171 {
172     std::string deviceId;
173     SoftbusSessionManager::GetInstance().ObtainPeerDeviceId(sessionId, deviceId);
174     std::string anonymizeDeviceId = SoftbusSessionUtils::AnonymizeDeviceId(deviceId);
175     SLOGI("onBytesServerReceived: %{public}s", anonymizeDeviceId.c_str());
176     if (data.length() > 0) {
177         std::lock_guard lockGuard(softbusDistributedDataLock_);
178         auto iter = serverMap_.find(data[0]);
179         if (iter == serverMap_.end()) {
180             SLOGE("onBytesServerReceived: server is invalid deviceId %{public}s", anonymizeDeviceId.c_str());
181             return;
182         }
183         iter->second->OnBytesReceived(deviceId, data);
184     }
185 }
186 } // namespace OHOS::AVSession