1 /*
2  * Copyright (c) 2022 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 GRAPHIC_LITE_TCP_SOCKET_CLIENT_MANGER_H
17 #define GRAPHIC_LITE_TCP_SOCKET_CLIENT_MANGER_H
18 
19 #include <queue>
20 #include <QMutex>
21 
22 #include "common/task.h"
23 #include "gfx_utils/list.h"
24 #include "tcp_socket.h"
25 #include "ui_test_message.h"
26 
27 namespace OHOS {
28 class TcpScoketClient;
29 using DispatchFunc = std::function<void(std::vector<std::shared_ptr<TestMsgInfo>>)>;
30 using DispatchConfigFunc = std::function<void(std::shared_ptr<TestConfigInfo>)>;
31 using DispatchCompleteFunc = std::function<void()>;
32 class TcpSocketClientManager : public QObject {
33     Q_OBJECT
34 public:
35     TcpSocketClientManager() = default;
36     ~TcpSocketClientManager();
37     static TcpSocketClientManager* GetInstance();
38 
39     void InitSocket();
40     void ConnectSocket();
41     void DispatchMsg();
42     void RecvMsg(QByteArray recv);
43     void SetDispatchFuncCallBack(DispatchFunc dispatchFunc);
44     void SetDispatchConfigFuncCallBack(DispatchConfigFunc dispatchConfigFunc);
45     void SetDispatchCompleteFuncCallBack(DispatchCompleteFunc dispatchCompleteFunc);
46 
GetTcpSocket()47     inline TcpScoketClient *GetTcpSocket()
48     {
49         return tcpSocket_;
50     }
51 
52 signals:
53     void SendMsgSignal(size_t mainID);
54 public:
SendMsg(size_t mainID)55     void SendMsg(size_t mainID)
56     {
57         emit SendMsgSignal(mainID);
58     }
59 
60 private:
61     void OnRunRecvMsg();
62     void OnGetMsgInfo(const std::shared_ptr<QByteArray> recv);
63     void OnGetConfigInfo(const QJsonObject object);
64     void OnGetTestInfo(QJsonObject object);
65     void OnGetTestInfo(QJsonObject arrobj, std::shared_ptr<TestMsgInfo>& msgInfo);
66 
67     void OnGetPageNav(QJsonArray array, std::vector<std::string>& pageNav);
68     void OnGetTestSetps(QJsonArray array, std::vector<TestSteps>& steps);
69     void OnGetEventValue(QJsonArray array, std::vector<int>& values);
70     void OnPrintTestInfo(const std::vector<std::shared_ptr<TestMsgInfo>> testMsgInfo) const;
71 private:
72     TcpScoketClient *tcpSocket_;
73     DispatchFunc dispatchFunc_;
74     DispatchConfigFunc dispatchConfigFunc_;
75     DispatchCompleteFunc dispatchCompleteFunc_;
76     std::deque<std::shared_ptr<QByteArray>> recv_;
77     QMutex mutex_;
78 };
79 } // namespace OHOS
80 
81 #endif  // GRAPHIC_LITE_TCP_SOCKET_CLIENT_MANGER_H
82