| 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_JSONRPCCLIENT_H |
| 7 |
#define AVOGADRO_MOLEQUEUE_JSONRPCCLIENT_H |
| 8 |
|
| 9 |
#include "avogadromolequeueexport.h" |
| 10 |
|
| 11 |
#include <QtCore/QJsonObject> |
| 12 |
#include <QtCore/QObject> |
| 13 |
|
| 14 |
class QLocalSocket; |
| 15 |
|
| 16 |
namespace Avogadro { |
| 17 |
namespace MoleQueue { |
| 18 |
|
| 19 |
/** |
| 20 |
* @class JsonRpcClient jsonrpcclient.h <molequeue/client/jsonrpcclient.h> |
| 21 |
* @brief The JsonRpcClient class is used by clients to submit calls to an RPC |
| 22 |
* server using JSON-RPC 2.0. |
| 23 |
* @author Marcus D. Hanwell |
| 24 |
* |
| 25 |
* Provides a simple Qt C++ API to make JSON-RPC 2.0 calls to an RPC server. To |
| 26 |
* create a client connection and call a method the following should be done: |
| 27 |
* |
| 28 |
@code |
| 29 |
#include <molequeue/client/jsonrpcclient.h> |
| 30 |
|
| 31 |
MoleQueue::JsonRpcClient *client = new MoleQueue::JsonRpcClient(this); |
| 32 |
client->connectToServer("MyRpcServer"); |
| 33 |
QJsonObject request(client->emptyRequest()); |
| 34 |
request["method"] = QLatin1String("listQueues"); |
| 35 |
client->sendRequest(request); |
| 36 |
@endcode |
| 37 |
* |
| 38 |
* You should connect to the appropriate signals in order to act on results, |
| 39 |
* notifications and errors received in response to requests set using the |
| 40 |
* client connection. |
| 41 |
*/ |
| 42 |
|
| 43 |
class AVOGADROMOLEQUEUE_EXPORT JsonRpcClient : public QObject |
| 44 |
{ |
| 45 |
Q_OBJECT |
| 46 |
|
| 47 |
public: |
| 48 |
explicit JsonRpcClient(QObject* parent_ = nullptr); |
| 49 |
~JsonRpcClient() override; |
| 50 |
|
| 51 |
/** |
| 52 |
* Query if the client is connected to a server. |
| 53 |
* @return True if connected, false if not. |
| 54 |
*/ |
| 55 |
bool isConnected() const; |
| 56 |
|
| 57 |
/** |
| 58 |
* @return The server name that the client is connected to. |
| 59 |
*/ |
| 60 |
QString serverName() const; |
| 61 |
|
| 62 |
public slots: |
| 63 |
/** |
| 64 |
* Connect to the server. |
| 65 |
* @param serverName Name of the socket to connect to. |
| 66 |
*/ |
| 67 |
bool connectToServer(const QString& serverName); |
| 68 |
|
| 69 |
/** |
| 70 |
* @brief flush Flush all pending messages to the server. |
| 71 |
* @warning This should not need to be called if used in an event loop, as Qt |
| 72 |
* will start writing to the socket as soon as control returns to the event |
| 73 |
* loop. |
| 74 |
*/ |
| 75 |
void flush(); |
| 76 |
|
| 77 |
/** |
| 78 |
* Use this function to construct an empty JSON-RPC 2.0 request, with a valid |
| 79 |
* request id, JSON-RPC 2.0 key etc. |
| 80 |
* @return a standard empty JSON-RPC 2.0 packet, the method etc is empty. |
| 81 |
*/ |
| 82 |
QJsonObject emptyRequest(); |
| 83 |
|
| 84 |
/** |
| 85 |
* Send the Json request to the RPC server. |
| 86 |
* @param request The JSON-RPC 2.0 request object. |
| 87 |
* @return True on success, false on failure. |
| 88 |
*/ |
| 89 |
bool sendRequest(const QJsonObject& request); |
| 90 |
|
| 91 |
protected slots: |
| 92 |
/** |
| 93 |
* Read incoming packets of data from the server. |
| 94 |
*/ |
| 95 |
void readPacket(const QByteArray message); |
| 96 |
|
| 97 |
/** |
| 98 |
* Read incoming data, interpret JSON stream. |
| 99 |
*/ |
| 100 |
void readSocket(); |
| 101 |
|
| 102 |
signals: |
| 103 |
/** |
| 104 |
* Emitted when the connection state changes. |
| 105 |
*/ |
| 106 |
void connectionStateChanged(); |
| 107 |
|
| 108 |
/** |
| 109 |
* Emitted when a result is received. |
| 110 |
*/ |
| 111 |
void resultReceived(QJsonObject message); |
| 112 |
|
| 113 |
/** |
| 114 |
* Emitted when a notification is received. |
| 115 |
*/ |
| 116 |
void notificationReceived(QJsonObject message); |
| 117 |
|
| 118 |
/** |
| 119 |
* Emitted when an error response is received. |
| 120 |
*/ |
| 121 |
void errorReceived(QJsonObject message); |
| 122 |
|
| 123 |
/** |
| 124 |
* Emitted when a bad packet was received that the client could not parse. |
| 125 |
*/ |
| 126 |
void badPacketReceived(QString error); |
| 127 |
|
| 128 |
/** |
| 129 |
* Emitted when a new packet of data is received. This is handled internally, |
| 130 |
* other classes should listen to resultReceived, notificationReceived, |
| 131 |
* errorReceived, and badPacketReceived. |
| 132 |
*/ |
| 133 |
void newPacket(const QByteArray& packet); |
| 134 |
|
| 135 |
protected: |
| 136 |
unsigned int m_packetCounter; |
| 137 |
QLocalSocket* m_socket; |
| 138 |
}; |
| 139 |
|
| 140 |
} // End namespace MoleQueue |
| 141 |
} // End namespace Avogadro |
| 142 |
|
| 143 |
#endif // AVOGADRO_MOLEQUEUE_JSONRPCCLIENT_H |