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 
OnBind(int32_t socket,PeerSocketInfo info)33         void OnBind(int32_t socket, PeerSocketInfo info) override
34         {
35             std::shared_ptr<SoftbusDistributedDataManager> manager = ptr_.lock();
36             if (manager != nullptr) {
37                 manager->SessionOpened(socket, info);
38             }
39         }
40 
OnShutdown(int32_t socket,ShutdownReason reason)41         void OnShutdown(int32_t socket, ShutdownReason reason) override
42         {
43             std::shared_ptr<SoftbusDistributedDataManager> manager = ptr_.lock();
44             if (manager != nullptr) {
45                 manager->SessionClosed(socket);
46             }
47         }
48 
OnMessage(int32_t socket,const void * data,int32_t dataLen)49         void OnMessage(int32_t socket, const void *data, int32_t dataLen) override
50         {
51             std::shared_ptr<SoftbusDistributedDataManager> manager = ptr_.lock();
52             std::string msg = std::string(static_cast<const char*>(data), dataLen);
53             if (manager != nullptr) {
54                 manager->MessageReceived(socket, msg);
55             }
56         }
57 
OnBytes(int32_t socket,const void * data,int32_t dataLen)58         void OnBytes(int32_t socket, const void *data, int32_t dataLen) override
59         {
60             std::shared_ptr<SoftbusDistributedDataManager> manager = ptr_.lock();
61             std::string msg = std::string(static_cast<const char*>(data), dataLen);
62             if (manager != nullptr) {
63                 manager->BytesReceived(socket, msg);
64             }
65         }
66 
67         std::weak_ptr<SoftbusDistributedDataManager> ptr_;
68     };
69 
70 public:
71     SoftbusDistributedDataManager();
72 
73     ~SoftbusDistributedDataManager();
74 
75     void Init();
76 
77     void InitSessionServer(const std::string &pkg);
78 
79     void CreateServer(const std::shared_ptr<SoftbusSessionServer> &server);
80 
81     void DestroySessionServer(const std::string &pkg);
82 
83     void ReleaseServer(const std::shared_ptr<SoftbusSessionServer> &server);
84 
85 private:
86     void SessionOpened(int32_t socket, PeerSocketInfo info);
87     void SessionClosed(int32_t socket);
88     void MessageReceived(int32_t socket, const std::string &data);
89     void BytesReceived(int32_t socket, const std::string &data);
90     void OnSessionServerOpened();
91     void OnSessionServerClosed(int32_t socket);
92     void OnMessageHandleReceived(int32_t socket, const std::string &data);
93     void OnBytesServerReceived(const std::string &data);
94 
95     std::map<int32_t, std::shared_ptr<SoftbusSessionServer>> serverMap_;
96     std::shared_ptr<SSListener> ssListener_;
97     std::recursive_mutex softbusDistributedDataLock_;
98 
99     static constexpr const int MESSAGE_CODE_CONNECT_SERVER = 1;
100 
101     PeerSocketInfo peerSocketInfo = {
102         .name = nullptr,
103         .networkId = nullptr,
104         .pkgName = nullptr,
105         .dataType = DATA_TYPE_BYTES,
106     };
107 
108     std::map<const std::string, int32_t> mMap_;
109 };
110 } // namespace OHOS::AVSession
111 
112 #endif // SOFTBUS_DISTRIBUTED_DATA_MANAGER_H