| 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 "jsonrpcclient.h" |
| 7 |
|
| 8 |
#include <QtCore/QJsonDocument> |
| 9 |
#include <QtCore/QDataStream> |
| 10 |
#include <QtCore/QTimer> |
| 11 |
#include <QtNetwork/QLocalSocket> |
| 12 |
|
| 13 |
namespace Avogadro::MoleQueue { |
| 14 |
|
| 15 |
JsonRpcClient::JsonRpcClient(QObject* parent_) |
| 16 |
: QObject(parent_), m_packetCounter(0), m_socket(nullptr) |
| 17 |
{ |
| 18 |
connect(this, SIGNAL(newPacket(QByteArray)), SLOT(readPacket(QByteArray)), |
| 19 |
Qt::QueuedConnection); |
| 20 |
} |
| 21 |
|
| 22 |
JsonRpcClient::~JsonRpcClient() |
| 23 |
{ |
| 24 |
flush(); |
| 25 |
} |
| 26 |
|
| 27 |
bool JsonRpcClient::isConnected() const |
| 28 |
{ |
| 29 |
if (!m_socket) |
| 30 |
return false; |
| 31 |
else |
| 32 |
return m_socket->isOpen(); |
| 33 |
} |
| 34 |
|
| 35 |
bool JsonRpcClient::connectToServer(const QString& serverName_) |
| 36 |
{ |
| 37 |
if (m_socket && m_socket->isOpen()) { |
| 38 |
if (m_socket->serverName() == serverName_) { |
| 39 |
return false; |
| 40 |
} else { |
| 41 |
m_socket->close(); |
| 42 |
delete m_socket; |
| 43 |
m_socket = nullptr; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// New connection. |
| 48 |
if (m_socket == nullptr) { |
| 49 |
m_socket = new QLocalSocket(this); |
| 50 |
connect(m_socket, SIGNAL(readyRead()), this, SLOT(readSocket())); |
| 51 |
} |
| 52 |
|
| 53 |
if (serverName_.isEmpty()) { |
| 54 |
return false; |
| 55 |
} else { |
| 56 |
m_socket->connectToServer(serverName_); |
| 57 |
return isConnected(); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
QString JsonRpcClient::serverName() const |
| 62 |
{ |
| 63 |
if (m_socket) |
| 64 |
return m_socket->serverName(); |
| 65 |
else |
| 66 |
return QString(); |
| 67 |
} |
| 68 |
|
| 69 |
void JsonRpcClient::flush() |
| 70 |
{ |
| 71 |
if (m_socket) |
| 72 |
m_socket->flush(); |
| 73 |
} |
| 74 |
|
| 75 |
QJsonObject JsonRpcClient::emptyRequest() |
| 76 |
{ |
| 77 |
QJsonObject request; |
| 78 |
request["jsonrpc"] = QLatin1String("2.0"); |
| 79 |
request["id"] = static_cast<int>(m_packetCounter++); |
| 80 |
return request; |
| 81 |
} |
| 82 |
|
| 83 |
bool JsonRpcClient::sendRequest(const QJsonObject& request) |
| 84 |
{ |
| 85 |
if (!m_socket) |
| 86 |
return false; |
| 87 |
|
| 88 |
QJsonDocument document(request); |
| 89 |
QDataStream stream(m_socket); |
| 90 |
stream.setVersion(QDataStream::Qt_4_8); |
| 91 |
stream << document.toJson(); |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
void JsonRpcClient::readPacket(const QByteArray message) |
| 96 |
{ |
| 97 |
// Read packet into a Json value |
| 98 |
QJsonParseError error; |
| 99 |
QJsonDocument reader = QJsonDocument::fromJson(message, &error); |
| 100 |
|
| 101 |
if (error.error != QJsonParseError::NoError) { |
| 102 |
emit badPacketReceived("Unparsable message received\n:" + |
| 103 |
error.errorString() + "\nContent: " + message); |
| 104 |
return; |
| 105 |
} else if (!reader.isObject()) { |
| 106 |
// We need a valid object, something bad happened. |
| 107 |
emit badPacketReceived("Packet did not contain a valid JSON object."); |
| 108 |
return; |
| 109 |
} else { |
| 110 |
QJsonObject root = reader.object(); |
| 111 |
if (root["method"] != QJsonValue::Null) { |
| 112 |
if (root["id"] != QJsonValue::Null) |
| 113 |
emit badPacketReceived("Received a request packet for the client."); |
| 114 |
else |
| 115 |
emit notificationReceived(root); |
| 116 |
} |
| 117 |
if (root["result"] != QJsonValue::Null) { |
| 118 |
// This is a result packet, and should emit a signal. |
| 119 |
emit resultReceived(root); |
| 120 |
} else if (root["error"] != QJsonValue::Null) { |
| 121 |
emit errorReceived(root); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
void JsonRpcClient::readSocket() |
| 127 |
{ |
| 128 |
if (m_socket->bytesAvailable() > 0) { |
| 129 |
QDataStream stream(m_socket); |
| 130 |
QByteArray json; |
| 131 |
stream >> json; |
| 132 |
emit newPacket(json); |
| 133 |
if (m_socket->bytesAvailable() > 0) |
| 134 |
QTimer::singleShot(0, this, SLOT(readSocket())); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
} // namespace Avogadro::MoleQueue |