Debian Codegraph @

avogadro/molequeue/client/client.h

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 #ifndef AVOGADRO_MOLEQUEUE_CLIENT_H
7 #define AVOGADRO_MOLEQUEUE_CLIENT_H
8
9 #include "avogadromolequeueexport.h"
10
11 #include <QRegularExpression>
12 #include <QtCore/QHash>
13 #include <QtCore/QJsonArray>
14 #include <QtCore/QJsonObject>
15 #include <QtCore/QObject>
16
17 namespace Avogadro {
18 namespace MoleQueue {
19
20 class JsonRpcClient;
21 class JobObject;
22
23 /**
24 * @class Client client.h <molequeue/client/client.h>
25 * @brief The Client class is used by clients to submit jobs to a running
26 * MoleQueue server.
27 * @author Marcus D. Hanwell
28 *
29 * Provides a simple Qt C++ API to use the MoleQueue JSON-RPC calls to submit
30 * and query the state of submitted jobs.
31 */
32
33 class AVOGADROMOLEQUEUE_EXPORT Client : public QObject
34 {
35 Q_OBJECT
36
37 public:
38 explicit Client(QObject* parent_ = nullptr);
39 ~Client() override;
40
41 /**
42 * Query if the client is connected to a server.
43 * @return True if connected, false if not.
44 */
45 bool isConnected() const;
46
47 public slots:
48 /**
49 * Connect to the server.
50 * @param serverName Name of the socket to connect to, the default of
51 * "MoleQueue" is usually correct when connecting to the running MoleQueue.
52 */
53 bool connectToServer(const QString& serverName = "MoleQueue");
54
55 /**
56 * Request the list of queues and programs from the server. The signal
57 * queueListReceived() will be emitted once this has been received.
58 * @return The local ID of the job submission request.
59 */
60 int requestQueueList();
61
62 /**
63 * Submit a job to MoleQueue. If the returned local ID is retained the signal
64 * for a job submission will provide the MoleQueue ID along with the local ID.
65 * @param job The job specification to be submitted to MoleQueue.
66 * @return The local ID of the job submission request.
67 */
68 int submitJob(const JobObject& job);
69
70 /**
71 * Request information about a job. You should supply the MoleQueue ID that
72 * was received in response to a job submission.
73 * @param moleQueueId The MoleQueue ID for the job.
74 * @return The local ID of the job submission request.
75 */
76 int lookupJob(unsigned int moleQueueId);
77
78 /**
79 * Cancel a job that was submitted.
80 * @param moleQueueId The MoleQueue ID for the job.
81 * @return The local ID of the job submission request.
82 */
83 int cancelJob(unsigned int moleQueueId);
84
85 /**
86 * Register an executable file handler with MoleQueue.
87 * @param name GUI name of the file handler.
88 * @param executable Executable to call with the filename as the first
89 * argument. If the full path to the exectuble is not specified, it must be
90 * in the user's $PATH.
91 * @param filePatterns A list of QRegularExpression objects that the handler
92 * can open. The QRegularExpression objects may only use the new
93 * QRegularExpression pattern syntax.
94 * @return The local ID of the request.
95 * @note The executable is expected to use the following calling convention
96 * to open files:
97 ~~~
98 executable /absolute/path/to/selected/fileName
99 ~~~
100 */
101 int registerOpenWith(const QString& name, const QString& executable,
102 const QList<QRegularExpression>& filePatterns);
103
104 /**
105 * Register a JSON-RPC 2.0 local socket file handler with MoleQueue.
106 * @param name GUI name of the file handler.
107 * @param rpcServer Name of the local socket that the server is listening on.
108 * @param rpcMethod JSON-RPC 2.0 request method to use.
109 * @param filePatterns A list of QRegularExpression objects that the handler
110 * can open. The QRegularExpression objects may only use the new
111 * QRegularExpression pattern syntax.
112 * @return The local ID of the request.
113 * @note The following JSON-RPC 2.0 request is sent to the server when the
114 * handler is activated:
115 ~~~
116 {
117 "jsonrpc": "2.0",
118 "method": "<rpcMethod>",
119 "params": {
120 "fileName": "/absolute/path/to/selected/fileName"
121 }
122 },
123 "id": "XXX"
124 }
125 ~~~
126 * where <rpcMethod> is replaced by the @a rpcMethod argument.
127 */
128 int registerOpenWith(const QString& name, const QString& rpcServer,
129 const QString& rpcMethod,
130 const QList<QRegularExpression>& filePatterns);
131
132 /**
133 * @brief Request a list of all file handler names.
134 * @return The local ID of the request.
135 */
136 int listOpenWithNames();
137
138 /**
139 * @brief Unregister the indicated file handler from the molequeue server.
140 * @param handlerName Name of the file handler to remove.
141 * @return The local ID of the request.
142 * @sa listOpenWithNames
143 */
144 int unregisterOpenWith(const QString& handlerName);
145
146 /**
147 * @brief flush Flush all pending messages to the server.
148 * @warning This should not need to be called if used in an event loop, as Qt
149 * will start writing to the socket as soon as control returns to the event
150 * loop.
151 */
152 void flush();
153
154 signals:
155 /**
156 * Emitted when the connection state changes.
157 */
158 void connectionStateChanged();
159
160 /**
161 * Emitted when the remote queue list is received. This gives a list of lists,
162 * the primary key is the queue name, and that contains a list of available
163 * programs for each queue.
164 * @param queues A JSON object containing the names of the queues and the
165 * programs each queue have available.
166 */
167 void queueListReceived(QJsonObject queues);
168
169 /**
170 * Emitted when the job request response is received.
171 * @param localId The local ID the job submission response is in reply to.
172 * @param moleQueueId The remote MoleQueue ID for the job submission (can be
173 * used to perform further actions on the job).
174 */
175 void submitJobResponse(int localId, unsigned int moleQueueId);
176
177 /**
178 * Emitted when a job lookup response is received.
179 * @param localId The local ID the job submission response is in reply to.
180 * @param jobInfo A Json object containing all available job information.
181 */
182 void lookupJobResponse(int localId, QJsonObject jobInfo);
183
184 /**
185 * Emitted when a job is successfully cancelled.
186 */
187 void cancelJobResponse(unsigned int moleQueueId);
188
189 /**
190 * Emitted when the job state changes.
191 */
192 void jobStateChanged(unsigned int moleQueueId, QString oldState,
193 QString newState);
194
195 /**
196 * Emitted when a successful registerOpenWith response is received.
197 */
198 void registerOpenWithResponse(int localId);
199
200 /**
201 * Emitted when a successful listOpenWithNames response is received.
202 */
203 void listOpenWithNamesResponse(int localId, QJsonArray handlerNames);
204
205 /**
206 * Emitted when a successful unregisterOpenWith response is received.
207 */
208 void unregisterOpenWithResponse(int localId);
209
210 /**
211 * Emitted when an error response is received.
212 */
213 void errorReceived(int localId, int errorCode, QString errorMessage,
214 QJsonValue errorData);
215
216 protected slots:
217 /** Parse the response object and emit the appropriate signal(s). */
218 void processResult(const QJsonObject& response);
219
220 /** Parse a notification object and emit the appropriate signal(s). */
221 void processNotification(const QJsonObject& notification);
222
223 /** Parse an error object and emit the appropriate signal(s). */
224 void processError(const QJsonObject& notification);
225
226 protected:
227 enum MessageType
228 {
229 Invalid = -1,
230 ListQueues,
231 SubmitJob,
232 CancelJob,
233 LookupJob,
234 RegisterOpenWith,
235 ListOpenWithNames,
236 UnregisterOpenWith
237 };
238
239 JsonRpcClient* m_jsonRpcClient;
240 QHash<unsigned int, MessageType> m_requests;
241
242 private:
243 QJsonObject buildRegisterOpenWithRequest(
244 const QString& name, const QList<QRegularExpression>& filePatterns,
245 const QJsonObject& handlerMethod);
246 };
247
248 } // End namespace MoleQueue
249 } // End namespace Avogadro
250
251 #endif // AVOGADRO_MOLEQUEUE_CLIENT_H