1 /* 2 * Copyright (c) 2021 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 DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H 17 #define DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H 18 19 #include <concurrent_map.h> 20 #include <condition_variable> 21 #include <functional> 22 #include <map> 23 #include <memory> 24 #include <mutex> 25 #include <set> 26 #include <tuple> 27 #include <vector> 28 29 #include "app_data_change_listener.h" 30 #include "app_device_change_listener.h" 31 #include "block_data.h" 32 #include "executor_pool.h" 33 #include "socket.h" 34 #include "softbus_client.h" 35 36 namespace OHOS { 37 namespace AppDistributedKv { 38 class SoftBusAdapter { 39 public: 40 struct ServerSocketInfo { 41 std::string name; /**< Peer socket name */ 42 std::string networkId; /**< Peer network ID */ 43 std::string pkgName; /**< Peer package name */ 44 }; 45 46 SoftBusAdapter(); 47 ~SoftBusAdapter(); 48 static std::shared_ptr<SoftBusAdapter> GetInstance(); 49 50 // add DataChangeListener to watch data change; 51 Status StartWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo); 52 53 // stop DataChangeListener to watch data change; 54 Status StopWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo); 55 56 // Send data to other device, function will be called back after sent to notify send result. 57 std::pair<Status, int32_t> SendData(const PipeInfo &pipeInfo, const DeviceId &deviceId, 58 const DataInfo &dataInfo, uint32_t length, const MessageInfo &info); 59 60 bool IsSameStartedOnPeer(const struct PipeInfo &pipeInfo, const struct DeviceId &peer); 61 62 void SetMessageTransFlag(const PipeInfo &pipeInfo, bool flag); 63 64 int CreateSessionServerAdapter(const std::string &sessionName); 65 66 int RemoveSessionServerAdapter(const std::string &sessionName) const; 67 68 void NotifyDataListeners(const uint8_t *data, int size, const std::string &deviceId, const PipeInfo &pipeInfo); 69 70 std::string OnClientShutdown(int32_t socket, bool isForce = true); 71 72 void OnBind(int32_t socket, PeerSocketInfo info); 73 74 void OnServerShutdown(int32_t socket); 75 76 bool GetPeerSocketInfo(int32_t socket, ServerSocketInfo &info); 77 78 Status Broadcast(const PipeInfo &pipeInfo, const LevelInfo &levelInfo); 79 void OnBroadcast(const DeviceId &device, const LevelInfo &levelInfo); 80 int32_t ListenBroadcastMsg(const PipeInfo &pipeInfo, 81 std::function<void(const std::string &, const LevelInfo &)> listener); 82 83 uint32_t GetMtuSize(const DeviceId &deviceId); 84 uint32_t GetTimeout(const DeviceId &deviceId); 85 86 Status ReuseConnect(const PipeInfo &pipeInfo, const DeviceId &deviceId, const ExtraDataInfo &extraInfo); 87 private: 88 using Time = std::chrono::steady_clock::time_point; 89 using Duration = std::chrono::steady_clock::duration; 90 using Task = ExecutorPool::Task; 91 92 std::string DelConnect(int32_t socket, bool isForce); 93 void StartCloseSessionTask(const std::string &deviceId); 94 Task GetCloseSessionTask(); 95 bool CloseSession(const std::string &networkId); 96 void GetExpireTime(std::shared_ptr<SoftBusClient> &conn); 97 std::pair<Status, int32_t> OpenConnect(const std::shared_ptr<SoftBusClient> &conn, const DeviceId &deviceId); 98 bool ConfigSessionAccessInfo(const ExtraDataInfo &extraInfo, SessionAccessInfo &sessionAccessInfo); 99 std::shared_ptr<SoftBusClient> GetConnect(const PipeInfo &pipeInfo, const DeviceId &deviceId, 100 const ExtraDataInfo &extraInfo); 101 102 static constexpr Time INVALID_NEXT = std::chrono::steady_clock::time_point::max(); 103 104 static std::shared_ptr<SoftBusAdapter> instance_; 105 ConcurrentMap<std::string, const AppDataChangeListener *> dataChangeListeners_{}; 106 ConcurrentMap<std::string, std::vector<std::shared_ptr<SoftBusClient>>> connects_; 107 bool flag_ = true; // only for br flag 108 std::function<void(const std::string &, const LevelInfo &)> onBroadcast_; 109 std::mutex taskMutex_; 110 ExecutorPool::TaskId taskId_ = ExecutorPool::INVALID_TASK_ID; 111 Time next_ = INVALID_NEXT; 112 113 ConcurrentMap<int32_t, ServerSocketInfo> peerSocketInfos_; 114 ISocketListener clientListener_{}; 115 ISocketListener serverListener_{}; 116 int32_t socket_ = 0; 117 }; 118 } // namespace AppDistributedKv 119 } // namespace OHOS 120 #endif /* DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H */