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