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 Stop(ITunnelClient &tunnel,
75         bool isUnchained, CooperateMessageCallback callback,
76         bool isCheckPermission = false);
77     int32_t GetCooperateState(ITunnelClient &tunnel,
78         const std::string &networkId, CooperateStateCallback callback,
79         bool isCheckPermission = false);
80     int32_t GetCooperateState(ITunnelClient &tunnel, const std::string &udId, bool &state);
81     int32_t RegisterEventListener(ITunnelClient &tunnel, const std::string &networkId,
82         MouseLocationListenerPtr listener);
83     int32_t UnregisterEventListener(ITunnelClient &tunnel, const std::string &networkId,
84         MouseLocationListenerPtr listener = nullptr);
85     int32_t AddHotAreaListener(ITunnelClient &tunnel, HotAreaListenerPtr listener);
86     int32_t RemoveHotAreaListener(ITunnelClient &tunnel, HotAreaListenerPtr listener = nullptr);
87 
88     int32_t OnCoordinationListener(const StreamClient &client, NetPacket &pkt);
89     int32_t OnCoordinationMessage(const StreamClient &client, NetPacket &pkt);
90     int32_t OnCoordinationState(const StreamClient &client, NetPacket &pkt);
91     int32_t OnHotAreaListener(const StreamClient &client, NetPacket &pkt);
92     int32_t OnMouseLocationListener(const StreamClient &client, NetPacket &pkt);
93 
94 private:
95     int32_t GenerateRequestID();
96     void OnDevCooperateListener(const std::string &networkId, CoordinationMessage msg);
97     void OnCooperateMessageEvent(int32_t userData, const std::string &networkId, const CoordinationMsgInfo &msgInfo);
98     void OnCooperateStateEvent(int32_t userData, bool state);
99     void OnDevHotAreaListener(int32_t displayX, int32_t displayY, HotAreaType type, bool isEdge);
100     void OnDevMouseLocationListener(const std::string &networkId, const Event &event);
101 #ifdef ENABLE_PERFORMANCE_CHECK
102     void StartTrace(int32_t userData);
103     void FinishTrace(int32_t userData, CoordinationMessage msg);
104     int32_t GetFirstSuccessIndex();
105     void DumpPerformanceInfo();
106 #endif // ENABLE_PERFORMANCE_CHECK
107 
108     std::list<CooperateListenerPtr> devCooperateListener_;
109     std::map<std::string, std::set<MouseLocationListenerPtr>> eventListener_;
110     std::list<HotAreaListenerPtr> devHotAreaListener_;
111     std::map<int32_t, CooperateEvent> devCooperateEvent_;
112     mutable std::mutex mtx_;
113     std::atomic_bool isListeningProcess_ { false };
114 
115 #ifdef ENABLE_PERFORMANCE_CHECK
116     struct PerformanceInfo {
117         std::map<int32_t, std::chrono::time_point<std::chrono::steady_clock>> traces_;
118         int32_t activateNum { 0 };
119         int32_t successNum { -1 };
120         int32_t failNum { -1 };
121         float successRate { 0.0f };
122         int32_t averageDuration { -1 };
123         int32_t failBeforeSuccess { -1 };
124         int32_t firstSuccessDuration { -1 };
125         int32_t maxDuration { std::numeric_limits<int32_t>::min() };
126         int32_t minDuration { std::numeric_limits<int32_t>::max() };
127         std::vector<int32_t> durationList;
128     };
129     std::mutex performanceLock_;
130     PerformanceInfo performanceInfo_;
131 #endif // ENABLE_PERFORMANCE_CHECK
132 };
133 } // namespace DeviceStatus
134 } // namespace Msdp
135 } // namespace OHOS
136 #endif // COOPERATE_CLIENT_H
137