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 "session_manager.h"
17 
18 #include <chrono>
19 #include <memory>
20 
21 #include "dfs_error.h"
22 #include "softbus_adapter.h"
23 #include "softbus_session.h"
24 #include "utils_log.h"
25 
26 namespace OHOS::FileManagement::CloudSync {
27 using namespace std;
28 const string SERVICE_NAME = "OHOS.Filemanagement.Dfs.ICloudSyncService";
29 /* need config softbus_trans_permission.json */
30 const string SESSION_NAME = "DistributedFileService_edge2edge_Session";
31 
~SessionManager()32 SessionManager::~SessionManager()
33 {
34     RemoveServer();
35 }
36 
Init()37 void SessionManager::Init()
38 {
39     CreateServer();
40 }
41 
CreateServer()42 void SessionManager::CreateServer()
43 {
44     auto ret = SoftbusAdapter::GetInstance().CreateSessionServer(SERVICE_NAME.c_str(), SESSION_NAME.c_str());
45     if (ret != E_OK) {
46         LOGE("create session server failed");
47         return;
48     }
49     SoftbusAdapter::GetInstance().RegisterSessionListener(SESSION_NAME.c_str(), shared_from_this());
50 }
51 
OnUserUnlocked()52 void SessionManager::OnUserUnlocked()
53 {
54     if (SetFileRecvListenerFlag_) {
55         LOGI("file recive listener has been successfully set up");
56         return;
57     }
58     /* setfileReceiveListener again when user unlocked to avoid register fall when user locked */
59     LOGI("UserUnlocked, SetFileReceiveListener again");
60 }
61 
RemoveServer()62 void SessionManager::RemoveServer()
63 {
64     auto ret = SoftbusAdapter::GetInstance().RemoveSessionServer(SERVICE_NAME.c_str(), SESSION_NAME.c_str());
65     if (ret != E_OK) {
66         LOGE("create session server failed");
67         return;
68     }
69 
70     SoftbusAdapter::GetInstance().UnRegisterSessionListener(SESSION_NAME.c_str());
71 }
72 
SendData(int sessionId,const void * data,uint32_t dataLen)73 int32_t SessionManager::SendData(int sessionId, const void *data, uint32_t dataLen)
74 {
75     return SoftbusAdapter::GetInstance().SendBytes(sessionId, data, dataLen);
76 }
77 
SendData(const std::string & peerNetworkId,const void * data,uint32_t dataLen)78 int32_t SessionManager::SendData(const std::string &peerNetworkId, const void *data, uint32_t dataLen)
79 {
80     auto sendSession = CreateSession(SoftbusSession::DataType::TYPE_BYTES, peerNetworkId);
81     if (sendSession == nullptr) {
82         return E_CREATE_SESSION;
83     }
84     return sendSession->SendData(data, dataLen);
85 }
86 
SendFile(const std::string & peerNetworkId,const std::vector<std::string> & sFileList,const std::vector<std::string> & dFileList)87 int32_t SessionManager::SendFile(const std::string &peerNetworkId,
88                                  const std::vector<std::string> &sFileList,
89                                  const std::vector<std::string> &dFileList)
90 {
91     auto sendSession = CreateSession(SoftbusSession::DataType::TYPE_FILE, peerNetworkId);
92     if (sendSession == nullptr) {
93         return E_CREATE_SESSION;
94     }
95     return sendSession->SendFile(sFileList, dFileList);
96 }
97 
CreateSession(SoftbusSession::DataType type,const std::string & peerDeviceId)98 std::shared_ptr<SoftbusSession> SessionManager::CreateSession(SoftbusSession::DataType type,
99                                                               const std::string &peerDeviceId)
100 {
101     if (!IsDeviceIdVailid(peerDeviceId)) {
102         LOGE("peer DeviceId is Invalid");
103         return nullptr;
104     }
105 
106     lock_guard<mutex> lock(sessionVecMutex_);
107     for (auto session : sendSessionVec_) {
108         if ((session->GetDataType() == type) && (session->GetPeerDeviceId() == peerDeviceId)) {
109             return session;
110         }
111     }
112 
113     auto sendSession = make_shared<SoftbusSession>(peerDeviceId, SESSION_NAME.c_str(), type);
114     auto ret = sendSession->Start();
115     if (ret != E_OK) {
116         LOGE("create session failed");
117         return nullptr;
118     }
119 
120     CacheSendSession(sendSession);
121     return sendSession;
122 }
123 
IsDeviceIdVailid(const std::string & peerDeviceId)124 bool SessionManager::IsDeviceIdVailid(const std::string &peerDeviceId)
125 {
126     return true;
127 }
128 
ReleaseSession(SoftbusSession::DataType type,const std::string & peerDeviceId)129 void SessionManager::ReleaseSession(SoftbusSession::DataType type, const std::string &peerDeviceId)
130 {
131     auto sendSession = GetSendSession(type, peerDeviceId);
132     if (sendSession == nullptr) {
133         LOGE("session not exist");
134         return;
135     }
136 
137     sendSession->Stop();
138 }
139 
OnSessionOpened(int socket,int result)140 void SessionManager::OnSessionOpened(int socket, int result)
141 {
142     if (result != E_OK) {
143         LOGE("OnSessionOpened failed:%{public}d", result);
144     }
145 }
146 
OnSessionClosed(int32_t socket)147 void SessionManager::OnSessionClosed(int32_t socket)
148 {
149     RemoveSendSession(socket);
150     if (dataHandler_ != nullptr) {
151         dataHandler_->OnSessionClosed();
152     }
153 }
154 
OnDataReceived(const std::string & senderNetworkId,int receiverSessionId,const void * data,unsigned int dataLen)155 void SessionManager::OnDataReceived(const std::string &senderNetworkId,
156                                     int receiverSessionId,
157                                     const void *data,
158                                     unsigned int dataLen)
159 {
160     if (dataHandler_ != nullptr) {
161         dataHandler_->OnMessageHandle(senderNetworkId, receiverSessionId, data, dataLen);
162     }
163 }
164 
OnFileReceived(const std::string & senderNetworkId,const char * filePath,int result)165 void SessionManager::OnFileReceived(const std::string &senderNetworkId, const char *filePath, int result)
166 {
167     if (dataHandler_ != nullptr) {
168         dataHandler_->OnFileRecvHandle(senderNetworkId, filePath, result);
169     }
170 }
171 
RegisterDataHandler(std::shared_ptr<RecieveDataHandler> handler)172 void SessionManager::RegisterDataHandler(std::shared_ptr<RecieveDataHandler> handler)
173 {
174     dataHandler_ = handler;
175 }
176 
GetSendSession(SoftbusSession::DataType type,const std::string & peerDeviceId)177 std::shared_ptr<SoftbusSession> SessionManager::GetSendSession(SoftbusSession::DataType type,
178                                                                const std::string &peerDeviceId)
179 {
180     lock_guard<mutex> lock(sessionVecMutex_);
181     for (auto session : sendSessionVec_) {
182         if ((session->GetDataType() == type) && (session->GetPeerDeviceId() == peerDeviceId)) {
183             return session;
184         }
185     }
186     return nullptr;
187 }
188 
CacheSendSession(std::shared_ptr<SoftbusSession> session)189 void SessionManager::CacheSendSession(std::shared_ptr<SoftbusSession> session)
190 {
191     sendSessionVec_.push_back(session);
192 }
193 
RemoveSendSession(int sessionId)194 void SessionManager::RemoveSendSession(int sessionId)
195 {
196     lock_guard<mutex> lock(sessionVecMutex_);
197     for (auto iter = sendSessionVec_.begin(); iter != sendSessionVec_.end();) {
198         if ((*iter)->GetSessionId() == sessionId) {
199             iter = sendSessionVec_.erase(iter);
200         } else {
201             ++iter;
202         }
203     }
204 }
205 } // namespace OHOS::FileManagement::CloudSync