void TestQJsonRpcMessage::testMessageTypes()
{
    QJsonRpcMessage invalid;
    QCOMPARE(invalid.type(), QJsonRpcMessage::Invalid);

    QJsonRpcMessage request = QJsonRpcMessage::createRequest("testMethod");
    QCOMPARE(request.type(), QJsonRpcMessage::Request);

    QJsonRpcMessage response = request.createResponse("testResponse");
    QCOMPARE(response.type(), QJsonRpcMessage::Response);

    QJsonRpcMessage error = request.createErrorResponse(QJsonRpc::NoError);
    QCOMPARE(error.type(), QJsonRpcMessage::Error);

    QJsonRpcMessage notification = QJsonRpcMessage::createNotification("testNotification");
    QCOMPARE(notification.type(), QJsonRpcMessage::Notification);
}
Example #2
0
int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
    QStringList args = app.arguments();
    QString appName = args.takeFirst();
    bool notification = args.contains("-n");
    if (notification)
        args.removeAll("-n");

    if (args.size() < 2) {
        qDebug("usage: %s [-n] <service> <method> <arguments>", appName.toLocal8Bit().data());
        return -1;
    }

    // try to process socket
    QIODevice *device = 0;
    QScopedPointer<QIODevice> devicePtr(device);
    QString service = args.takeFirst();
    QUrl serviceUrl = QUrl::fromUserInput(service);
    QHostAddress serviceAddress(serviceUrl.host());
    if (serviceAddress.isNull()) {
        QLocalSocket *localSocket = new QLocalSocket;
        device = localSocket;
        localSocket->connectToServer(service);
        if (!localSocket->waitForConnected(5000)) {
            qDebug("could not connect to service: %s", service.toLocal8Bit().data());
            return -1;
        }
    } else {
        QTcpSocket *tcpSocket = new QTcpSocket;
        device = tcpSocket;
        int servicePort = serviceUrl.port() ? serviceUrl.port() : 5555;
        tcpSocket->connectToHost(serviceAddress, servicePort);
        if (!tcpSocket->waitForConnected(5000)) {
            qDebug("could not connect to host at %s:%d", serviceUrl.host().toLocal8Bit().data(),
                   servicePort);
            return -1;
        }
    }

    QJsonRpcSocket socket(device);
    QString method = args.takeFirst();
    QVariantList arguments;
    foreach (QString arg, args)
        arguments.append(arg);

    QJsonRpcMessage request = notification ? QJsonRpcMessage::createNotification(method, arguments) :
                                             QJsonRpcMessage::createRequest(method, arguments);
    QJsonRpcMessage response = socket.sendMessageBlocking(request, 5000);
    if (response.type() == QJsonRpcMessage::Error) {
        qDebug("error(%d): %s", response.errorCode(), response.errorMessage().toLocal8Bit().data());
        return -1;
    }

    qDebug() << response.result();
}
void TestQJsonRpcMessage::testInvalidDataResponseWithoutId()
{
    // invalid without id
    const char *invalid = "{\"jsonrpc\": \"2.0\", \"params\": []}";
    QJsonRpcMessage request(invalid);
    QJsonRpcMessage error    = request.createErrorResponse(QJsonRpc::NoError, QString());
    QJsonRpcMessage response = request.createResponse(QString());
    QCOMPARE(request.type(), QJsonRpcMessage::Invalid);
    QCOMPARE(response.type(), QJsonRpcMessage::Invalid);    
    QCOMPARE(error.id(), 0);
}
Example #4
0
QDebug operator<<(QDebug dbg, const QJsonRpcMessage &msg)
{
    dbg.nospace() << "QJsonRpcMessage(type=" << msg.type();
    if (msg.type() != QJsonRpcMessage::Notification) {
        dbg.nospace() << ", id=" << msg.id();
    }

    if (msg.type() == QJsonRpcMessage::Request ||
        msg.type() == QJsonRpcMessage::Notification) {
        dbg.nospace() << ", method=" << msg.method()
                      << ", params=" << msg.params();
    } else if (msg.type() == QJsonRpcMessage::Response) {
        dbg.nospace() << ", result=" << msg.result();
    } else if (msg.type() == QJsonRpcMessage::Error) {
        dbg.nospace() << ", code=" << msg.errorCode()
                      << ", message=" << msg.errorMessage()
                      << ", data=" << msg.errorData();
    }
    dbg.nospace() << ")";
    return dbg.space();
}
Example #5
0
bool QJsonRpcMessage::operator==(const QJsonRpcMessage &message) const
{
    if (message.d == d)
        return true;

    if (message.type() == type()) {
        if (message.type() == QJsonRpcMessage::Error) {
            return (message.errorCode() == errorCode() &&
                    message.errorMessage() == errorMessage() &&
                    message.errorData() == errorData());
        } else {
            if (message.type() == QJsonRpcMessage::Notification) {
                return (message.method() == method() &&
                        message.params() == params());
            } else {
                return (message.id() == id() &&
                        message.method() == method() &&
                        message.params() == params());
            }
        }
    }

    return false;
}
Example #6
0
bool DlvClient::callBlocked(const QString &method, const JsonDataIn *in, JsonDataOut *out) const
{
    QVariantMap param;
    in->toMap(param);
    QJsonRpcMessage request = QJsonRpcMessage::createRequest("RPCServer."+method, QJsonValue::fromVariant(param));

    QJsonRpcMessage response = m_dlv->sendMessageBlocking(request,m_callTimeout);
    if (response.type() == QJsonRpcMessage::Error) {
        //qDebug("error(%d): %s", response.errorCode(), response.errorMessage().toLocal8Bit().data());//
        return false;
    }
    m_lastJsonData = response.result().toVariant();
    out->fromMap(response.result().toVariant().toMap());
    return true;
}