1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #ifndef BASELINEPROTOCOL_H |
30 | #define BASELINEPROTOCOL_H |
31 | |
32 | #include <QDataStream> |
33 | #include <QTcpSocket> |
34 | #include <QImage> |
35 | #include <QVector> |
36 | #include <QMap> |
37 | #include <QPointer> |
38 | #include <QStringList> |
39 | |
40 | #define QLS QLatin1String |
41 | #define QLC QLatin1Char |
42 | |
43 | #define FileFormat "png" |
44 | |
45 | extern const QString PI_Project; |
46 | extern const QString PI_TestCase; |
47 | extern const QString PI_HostName; |
48 | extern const QString PI_HostAddress; |
49 | extern const QString PI_OSName; |
50 | extern const QString PI_OSVersion; |
51 | extern const QString PI_QtVersion; |
52 | extern const QString PI_QtBuildMode; |
53 | extern const QString PI_GitCommit; |
54 | extern const QString PI_QMakeSpec; |
55 | extern const QString PI_PulseGitBranch; |
56 | extern const QString PI_PulseTestrBranch; |
57 | |
58 | class PlatformInfo : public QMap<QString, QString> |
59 | { |
60 | public: |
61 | PlatformInfo(); |
62 | PlatformInfo(const PlatformInfo &other); |
63 | ~PlatformInfo() |
64 | {} |
65 | PlatformInfo &operator=(const PlatformInfo &other); |
66 | |
67 | static PlatformInfo localHostInfo(); |
68 | |
69 | void addOverride(const QString& key, const QString& value); |
70 | QStringList overrides() const; |
71 | bool isAdHocRun() const; |
72 | void setAdHocRun(bool isAdHoc); |
73 | |
74 | private: |
75 | QStringList orides; |
76 | bool adHoc; |
77 | friend QDataStream & operator<< (QDataStream &stream, const PlatformInfo &pi); |
78 | friend QDataStream & operator>> (QDataStream &stream, PlatformInfo& pi); |
79 | }; |
80 | QDataStream & operator<< (QDataStream &stream, const PlatformInfo &pi); |
81 | QDataStream & operator>> (QDataStream &stream, PlatformInfo& pi); |
82 | |
83 | |
84 | struct ImageItem |
85 | { |
86 | public: |
87 | ImageItem() |
88 | : status(Ok), itemChecksum(0) |
89 | {} |
90 | ImageItem(const ImageItem &other) |
91 | { *this = other; } |
92 | ~ImageItem() |
93 | {} |
94 | ImageItem &operator=(const ImageItem &other); |
95 | |
96 | static quint64 computeChecksum(const QImage& image); |
97 | |
98 | enum ItemStatus { |
99 | Ok = 0, |
100 | BaselineNotFound = 1, |
101 | IgnoreItem = 2, |
102 | Mismatch = 3, |
103 | FuzzyMatch = 4, |
104 | Error = 5 |
105 | }; |
106 | |
107 | QString testFunction; |
108 | QString itemName; |
109 | ItemStatus status; |
110 | QImage image; |
111 | QList<quint64> imageChecksums; |
112 | quint16 itemChecksum; |
113 | QByteArray misc; |
114 | |
115 | void writeImageToStream(QDataStream &stream) const; |
116 | void readImageFromStream(QDataStream &stream); |
117 | }; |
118 | QDataStream & operator<< (QDataStream &stream, const ImageItem &ii); |
119 | QDataStream & operator>> (QDataStream &stream, ImageItem& ii); |
120 | |
121 | Q_DECLARE_METATYPE(ImageItem); |
122 | |
123 | typedef QVector<ImageItem> ImageItemList; |
124 | |
125 | |
126 | class BaselineProtocol |
127 | { |
128 | public: |
129 | BaselineProtocol(); |
130 | ~BaselineProtocol(); |
131 | |
132 | static BaselineProtocol *instance(QObject *parent = 0); |
133 | |
134 | // **************************************************** |
135 | // Important constants here |
136 | // **************************************************** |
137 | enum Constant { |
138 | ProtocolVersion = 5, |
139 | ServerPort = 54129, |
140 | Timeout = 15000 |
141 | }; |
142 | |
143 | enum Command { |
144 | UnknownError = 0, |
145 | // Queries |
146 | AcceptPlatformInfo = 1, |
147 | RequestBaselineChecksums = 2, |
148 | AcceptMatch = 3, |
149 | AcceptNewBaseline = 4, |
150 | AcceptMismatch = 5, |
151 | // Responses |
152 | Ack = 128, |
153 | Abort = 129, |
154 | DoDryRun = 130, |
155 | FuzzyMatch = 131 |
156 | }; |
157 | |
158 | // For client: |
159 | |
160 | // For advanced client: |
161 | bool connect(const QString &testCase, bool *dryrun = 0, const PlatformInfo& clientInfo = PlatformInfo()); |
162 | bool disconnect(); |
163 | bool requestBaselineChecksums(const QString &testFunction, ImageItemList *itemList); |
164 | bool submitMatch(const ImageItem &item, QByteArray *serverMsg); |
165 | bool submitNewBaseline(const ImageItem &item, QByteArray *serverMsg); |
166 | bool submitMismatch(const ImageItem &item, QByteArray *serverMsg, bool *fuzzyMatch = 0); |
167 | |
168 | // For server: |
169 | bool acceptConnection(PlatformInfo *pi); |
170 | |
171 | QString errorMessage(); |
172 | |
173 | private: |
174 | bool sendItem(Command cmd, const ImageItem &item); |
175 | |
176 | bool sendBlock(Command cmd, const QByteArray &block); |
177 | bool receiveBlock(Command *cmd, QByteArray *block); |
178 | void sysSleep(int ms); |
179 | |
180 | QString errMsg; |
181 | QTcpSocket socket; |
182 | |
183 | friend class BaselineThread; |
184 | friend class BaselineHandler; |
185 | }; |
186 | |
187 | |
188 | #endif // BASELINEPROTOCOL_H |
189 | |