• 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 #ifndef SOFTBUS_DISTRIBUTED_DATA_MANAGER_H
17 #define SOFTBUS_DISTRIBUTED_DATA_MANAGER_H
18 
19 #include <mutex>
20 
21 #include "softbus_session_manager.h"
22 #include "softbus_session_server.h"
23 
24 namespace OHOS::AVSession {
25 class SoftbusDistributedDataManager : public std::enable_shared_from_this<SoftbusDistributedDataManager> {
26     class SSListener : public SoftbusSessionListener {
27     public:
SSListener(std::weak_ptr<SoftbusDistributedDataManager> ptr)28         explicit SSListener(std::weak_ptr<SoftbusDistributedDataManager> ptr)
29         {
30             ptr_ = ptr;
31         }
32 
OnSessionOpened(int32_t sessionId)33         void OnSessionOpened(int32_t sessionId) override
34         {
35             std::shared_ptr<SoftbusDistributedDataManager> manager = ptr_.lock();
36             if (manager != nullptr) {
37                 manager->SessionOpened(sessionId);
38             }
39         }
40 
OnSessionClosed(int32_t sessionId)41         void OnSessionClosed(int32_t sessionId) override
42         {
43             std::shared_ptr<SoftbusDistributedDataManager> manager = ptr_.lock();
44             if (manager != nullptr) {
45                 manager->SessionClosed(sessionId);
46             }
47         }
48 
OnMessageReceived(int32_t sessionId,const std::string & data)49         void OnMessageReceived(int32_t sessionId, const std::string &data) override
50         {
51             std::shared_ptr<SoftbusDistributedDataManager> manager = ptr_.lock();
52             if (manager != nullptr) {
53                 manager->MessageReceived(sessionId, data);
54             }
55         }
56 
OnBytesReceived(int32_t sessionId,const std::string & data)57         void OnBytesReceived(int32_t sessionId, const std::string &data) override
58         {
59             std::shared_ptr<SoftbusDistributedDataManager> manager = ptr_.lock();
60             if (manager != nullptr) {
61                 manager->BytesReceived(sessionId, data);
62             }
63         }
64 
65         std::weak_ptr<SoftbusDistributedDataManager> ptr_;
66     };
67 
68 public:
69     SoftbusDistributedDataManager();
70 
71     ~SoftbusDistributedDataManager();
72 
73     void Init();
74 
75     void InitSessionServer(const std::string &pkg);
76 
77     void CreateServer(const std::shared_ptr<SoftbusSessionServer> &server);
78 
79     void DestroySessionServer(const std::string &pkg);
80 
81     void ReleaseServer(const std::shared_ptr<SoftbusSessionServer> &server);
82 
83 private:
84     void SessionOpened(int32_t sessionId);
85     void SessionClosed(int32_t sessionId);
86     void MessageReceived(int32_t sessionId, const std::string &data);
87     void BytesReceived(int32_t sessionId, const std::string &data);
88     void OnSessionServerOpened(int32_t sessionId);
89     void OnSessionServerClosed(int32_t sessionId, const std::string &deviceId);
90     void OnMessageHandleReceived(int32_t sessionId, const std::string &data);
91     void OnBytesServerReceived(int32_t sessionId, const std::string &data);
92 
93     std::map<int32_t, std::shared_ptr<SoftbusSessionServer>> serverMap_;
94     std::shared_ptr<SSListener> ssListener_;
95     std::recursive_mutex softbusDistributedDataLock_;
96 
97     static constexpr const int MESSAGE_CODE_CONNECT_SERVER = 1;
98 };
99 } // namespace OHOS::AVSession
100 
101 #endif // SOFTBUS_DISTRIBUTED_DATA_MANAGER_H