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