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 COOPERATE_CLIENT_H 17 #define COOPERATE_CLIENT_H 18 19 #include <functional> 20 #include <list> 21 #include <map> 22 #include <mutex> 23 #include <set> 24 #ifdef ENABLE_PERFORMANCE_CHECK 25 #include <chrono> 26 #include <vector> 27 #endif // ENABLE_PERFORMANCE_CHECK 28 29 #include "nocopyable.h" 30 31 #include "coordination_message.h" 32 #include "i_coordination_listener.h" 33 #include "i_event_listener.h" 34 #include "i_hotarea_listener.h" 35 #include "i_tunnel_client.h" 36 #include "net_packet.h" 37 #include "socket_client.h" 38 #include "stream_client.h" 39 40 namespace OHOS { 41 namespace Msdp { 42 namespace DeviceStatus { 43 class CooperateClient final { 44 public: 45 using CooperateMessageCallback = std::function<void(const std::string&, const CoordinationMsgInfo&)>; 46 using CooperateStateCallback = std::function<void(bool)>; 47 using CooperateListenerPtr = std::shared_ptr<ICoordinationListener>; 48 using HotAreaListenerPtr = std::shared_ptr<IHotAreaListener>; 49 using MouseLocationListenerPtr = std::shared_ptr<IEventListener>; 50 51 struct CooperateEvent { CooperateEventCooperateEvent52 CooperateEvent(CooperateMessageCallback callback) : msgCb(callback) {} CooperateEventCooperateEvent53 CooperateEvent(CooperateStateCallback callback) : stateCb(callback) {} 54 55 CooperateMessageCallback msgCb; 56 CooperateStateCallback stateCb; 57 }; 58 59 CooperateClient() = default; 60 ~CooperateClient() = default; 61 DISALLOW_COPY_AND_MOVE(CooperateClient); 62 63 int32_t RegisterListener(ITunnelClient &tunnel, 64 CooperateListenerPtr listener, bool isCheckPermission = false); 65 int32_t UnregisterListener(ITunnelClient &tunnel, 66 CooperateListenerPtr listener, bool isCheckPermission = false); 67 int32_t Enable(ITunnelClient &tunnel, 68 CooperateMessageCallback callback, bool isCheckPermission = false); 69 int32_t Disable(ITunnelClient &tunnel, 70 CooperateMessageCallback callback, bool isCheckPermission = false); 71 int32_t Start(ITunnelClient &tunnel, 72 const std::string &remoteNetworkId, int32_t startDeviceId, 73 CooperateMessageCallback callback, bool isCheckPermission = false); 74 int32_t StartWithOptions(ITunnelClient &tunnel, const std::string &remoteNetworkId, 75 int32_t startDeviceId, CooperateMessageCallback callback, const CooperateOptions &options); 76 int32_t Stop(ITunnelClient &tunnel, 77 bool isUnchained, CooperateMessageCallback callback, 78 bool isCheckPermission = false); 79 int32_t GetCooperateState(ITunnelClient &tunnel, 80 const std::string &networkId, CooperateStateCallback callback, 81 bool isCheckPermission = false); 82 int32_t GetCooperateState(ITunnelClient &tunnel, const std::string &udId, bool &state); 83 int32_t RegisterEventListener(ITunnelClient &tunnel, const std::string &networkId, 84 MouseLocationListenerPtr listener); 85 int32_t UnregisterEventListener(ITunnelClient &tunnel, const std::string &networkId, 86 MouseLocationListenerPtr listener = nullptr); 87 int32_t SetDamplingCoefficient(ITunnelClient &tunnel, uint32_t direction, double coefficient); 88 int32_t AddHotAreaListener(ITunnelClient &tunnel, HotAreaListenerPtr listener); 89 int32_t RemoveHotAreaListener(ITunnelClient &tunnel, HotAreaListenerPtr listener = nullptr); 90 91 int32_t OnCoordinationListener(const StreamClient &client, NetPacket &pkt); 92 int32_t OnCoordinationMessage(const StreamClient &client, NetPacket &pkt); 93 int32_t OnCoordinationState(const StreamClient &client, NetPacket &pkt); 94 int32_t OnHotAreaListener(const StreamClient &client, NetPacket &pkt); 95 int32_t OnMouseLocationListener(const StreamClient &client, NetPacket &pkt); 96 97 private: 98 int32_t GenerateRequestID(); 99 void OnDevCooperateListener(const std::string &networkId, CoordinationMessage msg); 100 void OnCooperateMessageEvent(int32_t userData, const std::string &networkId, const CoordinationMsgInfo &msgInfo); 101 void OnCooperateStateEvent(int32_t userData, bool state); 102 void OnDevHotAreaListener(int32_t displayX, int32_t displayY, HotAreaType type, bool isEdge); 103 void OnDevMouseLocationListener(const std::string &networkId, const Event &event); 104 #ifdef ENABLE_PERFORMANCE_CHECK 105 void StartTrace(int32_t userData); 106 void FinishTrace(int32_t userData, CoordinationMessage msg); 107 int32_t GetFirstSuccessIndex(); 108 void DumpPerformanceInfo(); 109 #endif // ENABLE_PERFORMANCE_CHECK 110 111 std::list<CooperateListenerPtr> devCooperateListener_; 112 std::map<std::string, std::set<MouseLocationListenerPtr>> eventListener_; 113 std::list<HotAreaListenerPtr> devHotAreaListener_; 114 std::map<int32_t, CooperateEvent> devCooperateEvent_; 115 mutable std::mutex mtx_; 116 std::atomic_bool isListeningProcess_ { false }; 117 118 #ifdef ENABLE_PERFORMANCE_CHECK 119 struct PerformanceInfo { 120 std::map<int32_t, std::chrono::time_point<std::chrono::steady_clock>> traces_; 121 int32_t activateNum { 0 }; 122 int32_t successNum { -1 }; 123 int32_t failNum { -1 }; 124 float successRate { 0.0f }; 125 int32_t averageDuration { -1 }; 126 int32_t failBeforeSuccess { -1 }; 127 int32_t firstSuccessDuration { -1 }; 128 int32_t maxDuration { std::numeric_limits<int32_t>::min() }; 129 int32_t minDuration { std::numeric_limits<int32_t>::max() }; 130 std::vector<int32_t> durationList; 131 }; 132 std::mutex performanceLock_; 133 PerformanceInfo performanceInfo_; 134 #endif // ENABLE_PERFORMANCE_CHECK 135 }; 136 } // namespace DeviceStatus 137 } // namespace Msdp 138 } // namespace OHOS 139 #endif // COOPERATE_CLIENT_H 140