Debian Codegraph @

avogadro/molequeue/client/client.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "client.h"
7
8 #include "jobobject.h"
9 #include "jsonrpcclient.h"
10
11 #include <QtCore/QJsonDocument>
12
13 namespace Avogadro::MoleQueue {
14
15 Client::Client(QObject* parent_) : QObject(parent_), m_jsonRpcClient(nullptr) {}
16
17 Client::~Client() {}
18
19 bool Client::isConnected() const
20 {
21 if (!m_jsonRpcClient)
22 return false;
23 else
24 return m_jsonRpcClient->isConnected();
25 }
26
27 bool Client::connectToServer(const QString& serverName)
28 {
29 if (!m_jsonRpcClient) {
30 m_jsonRpcClient = new JsonRpcClient(this);
31 connect(m_jsonRpcClient, SIGNAL(resultReceived(QJsonObject)),
32 SLOT(processResult(QJsonObject)));
33 connect(m_jsonRpcClient, SIGNAL(notificationReceived(QJsonObject)),
34 SLOT(processNotification(QJsonObject)));
35 connect(m_jsonRpcClient, SIGNAL(errorReceived(QJsonObject)),
36 SLOT(processError(QJsonObject)));
37 connect(m_jsonRpcClient, SIGNAL(connectionStateChanged()),
38 SIGNAL(connectionStateChanged()));
39 }
40
41 return m_jsonRpcClient->connectToServer(serverName);
42 }
43
44 int Client::requestQueueList()
45 {
46 if (!m_jsonRpcClient)
47 return -1;
48
49 QJsonObject packet = m_jsonRpcClient->emptyRequest();
50 packet["method"] = QLatin1String("listQueues");
51 if (!m_jsonRpcClient->sendRequest(packet))
52 return -1;
53
54 int localId = static_cast<int>(packet["id"].toDouble());
55 m_requests[localId] = ListQueues;
56 return localId;
57 }
58
59 int Client::submitJob(const JobObject& job)
60 {
61 if (!m_jsonRpcClient)
62 return -1;
63
64 QJsonObject packet = m_jsonRpcClient->emptyRequest();
65 packet["method"] = QLatin1String("submitJob");
66 packet["params"] = job.json();
67 if (!m_jsonRpcClient->sendRequest(packet))
68 return -1;
69
70 int localId = static_cast<int>(packet["id"].toDouble());
71 m_requests[localId] = SubmitJob;
72 return localId;
73 }
74
75 int Client::lookupJob(unsigned int moleQueueId)
76 {
77 if (!m_jsonRpcClient)
78 return -1;
79
80 QJsonObject packet = m_jsonRpcClient->emptyRequest();
81 packet["method"] = QLatin1String("lookupJob");
82 QJsonObject params;
83 params["moleQueueId"] = static_cast<int>(moleQueueId);
84 packet["params"] = params;
85 if (!m_jsonRpcClient->sendRequest(packet))
86 return -1;
87
88 int localId = static_cast<int>(packet["id"].toDouble());
89 m_requests[localId] = LookupJob;
90 return localId;
91 }
92
93 int Client::cancelJob(unsigned int moleQueueId)
94 {
95 if (!m_jsonRpcClient)
96 return -1;
97
98 QJsonObject packet = m_jsonRpcClient->emptyRequest();
99 packet["method"] = QLatin1String("cancelJob");
100 QJsonObject params;
101 params["moleQueueId"] = static_cast<int>(moleQueueId);
102 packet["params"] = params;
103 if (!m_jsonRpcClient->sendRequest(packet))
104 return -1;
105
106 int localId = static_cast<int>(packet["id"].toDouble());
107 m_requests[localId] = CancelJob;
108 return localId;
109 }
110
111 int Client::registerOpenWith(const QString& name, const QString& executable,
112 const QList<QRegularExpression>& filePatterns)
113 {
114 if (!m_jsonRpcClient)
115 return -1;
116
117 QJsonObject method;
118 method["executable"] = executable;
119
120 QJsonObject packet(buildRegisterOpenWithRequest(name, filePatterns, method));
121
122 if (!m_jsonRpcClient->sendRequest(packet))
123 return -1;
124
125 int localId = static_cast<int>(packet["id"].toDouble());
126 m_requests[localId] = RegisterOpenWith;
127 return localId;
128 }
129
130 int Client::registerOpenWith(const QString& name, const QString& rpcServer,
131 const QString& rpcMethod,
132 const QList<QRegularExpression>& filePatterns)
133 {
134 if (!m_jsonRpcClient)
135 return -1;
136
137 QJsonObject method;
138 method["rpcServer"] = rpcServer;
139 method["rpcMethod"] = rpcMethod;
140
141 QJsonObject packet(buildRegisterOpenWithRequest(name, filePatterns, method));
142
143 if (!m_jsonRpcClient->sendRequest(packet))
144 return -1;
145
146 int localId = static_cast<int>(packet["id"].toDouble());
147 m_requests[localId] = RegisterOpenWith;
148 return localId;
149 }
150
151 int Client::listOpenWithNames()
152 {
153 if (!m_jsonRpcClient)
154 return -1;
155
156 QJsonObject packet = m_jsonRpcClient->emptyRequest();
157 packet["method"] = QLatin1String("listOpenWithNames");
158 if (!m_jsonRpcClient->sendRequest(packet))
159 return -1;
160
161 int localId = static_cast<int>(packet["id"].toDouble());
162 m_requests[localId] = ListOpenWithNames;
163 return localId;
164 }
165
166 int Client::unregisterOpenWith(const QString& handlerName)
167 {
168 if (!m_jsonRpcClient)
169 return -1;
170
171 QJsonObject packet = m_jsonRpcClient->emptyRequest();
172 packet["method"] = QLatin1String("unregisterOpenWith");
173 QJsonObject params;
174 params["name"] = handlerName;
175 packet["params"] = params;
176 if (!m_jsonRpcClient->sendRequest(packet))
177 return -1;
178
179 int localId = static_cast<int>(packet["id"].toDouble());
180 m_requests[localId] = UnregisterOpenWith;
181 return localId;
182 }
183
184 void Client::flush()
185 {
186 if (m_jsonRpcClient)
187 m_jsonRpcClient->flush();
188 }
189
190 void Client::processResult(const QJsonObject& response)
191 {
192 if (response["id"] != QJsonValue::Null &&
193 m_requests.contains(static_cast<int>(response["id"].toDouble()))) {
194 int localId = static_cast<int>(response["id"].toDouble());
195 switch (m_requests[localId]) {
196 case ListQueues:
197 emit queueListReceived(response["result"].toObject());
198 break;
199 case SubmitJob:
200 emit submitJobResponse(
201 localId, static_cast<unsigned int>(
202 response["result"].toObject()["moleQueueId"].toDouble()));
203 break;
204 case LookupJob:
205 emit lookupJobResponse(localId, response["result"].toObject());
206 break;
207 case CancelJob:
208 emit cancelJobResponse(static_cast<unsigned int>(
209 response["result"].toObject()["moleQueueId"].toDouble()));
210 break;
211 case RegisterOpenWith:
212 emit registerOpenWithResponse(localId);
213 break;
214 case ListOpenWithNames:
215 emit listOpenWithNamesResponse(localId, response["result"].toArray());
216 break;
217 case UnregisterOpenWith:
218 emit unregisterOpenWithResponse(localId);
219 break;
220 default:
221 break;
222 }
223 }
224 }
225
226 void Client::processNotification(const QJsonObject& notification)
227 {
228 if (notification["method"].toString() == "jobStateChanged") {
229 QJsonObject params = notification["params"].toObject();
230 emit jobStateChanged(
231 static_cast<unsigned int>(params["moleQueueId"].toDouble()),
232 params["oldState"].toString(), params["newState"].toString());
233 }
234 }
235
236 void Client::processError(const QJsonObject& error)
237 {
238 int localId = static_cast<int>(error["id"].toDouble());
239 int errorCode = -1;
240 QString errorMessage = tr("No message specified.");
241 QJsonValue errorData;
242
243 const QJsonValue& errorValue = error.value(QLatin1String("error"));
244 if (errorValue.isObject()) {
245 const QJsonObject errorObject = errorValue.toObject();
246 if (errorObject.value("code").isDouble())
247 errorCode = static_cast<int>(errorObject.value("code").toDouble());
248 if (errorObject.value("message").isString())
249 errorMessage = errorObject.value("message").toString();
250 if (errorObject.contains("data"))
251 errorData = errorObject.value("data");
252 }
253 emit errorReceived(localId, errorCode, errorMessage, errorData);
254 }
255
256 QJsonObject Client::buildRegisterOpenWithRequest(
257 const QString& name, const QList<QRegularExpression>& filePatterns,
258 const QJsonObject& handlerMethod)
259 {
260 QJsonArray patterns;
261 foreach (const QRegularExpression& regex, filePatterns) {
262 QJsonObject pattern;
263 pattern["regex"] = regex.pattern();
264 pattern["caseSensitive"] = regex.patternOptions().testFlag(
265 QRegularExpression::CaseInsensitiveOption);
266 patterns.append(pattern);
267 }
268
269 QJsonObject params;
270 params["name"] = name;
271 params["method"] = handlerMethod;
272 params["patterns"] = patterns;
273
274 QJsonObject packet = m_jsonRpcClient->emptyRequest();
275 packet["method"] = QLatin1String("registerOpenWith");
276 packet["params"] = params;
277
278 return packet;
279 }
280
281 } // namespace Avogadro::MoleQueue