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