Lightweight RPC
-
I was searching for a small RPC framework and found "qjsonrpc...":https://bitbucket.org/devonit/qjsonrpc/overview. I like it because there are no other dependencies, just qt. The interfaces dont't need to be defined in another language, they are written in plain c++ and qt.
Here is an example taken from a "blog...":http://mbroadst.blogspot.de/2012/03/qjsonrpc-round-2.html
The server:
@
class TestService : public QJsonRpcService {
Q_OBJECT
Q_CLASSINFO("serviceName", "service")
public:
TestService(QObject *parent = 0) : QJsonRpcService(parent) {}
public Q_SLOTS:
QString currentTime() { return QTime::currentTime().toString(); }
};int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QJsonRpcLocalServiceProvider rpcServer;
rpcServer.addService(new TestService);
if (!rpcServer.listen("/tmp/testservice")) {
qDebug() << "can't start local server: " << rpcServer.errorString();
return -1;
}
return app.exec();
}
@
The client:
@
#include <QCoreApplication>
#include <QLocalSocket>
#include "qjsonrpcservice.h"
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QLocalSocket socket;
socket.connectToServer("/tmp/testservice");
if (!socket.waitForConnected()) {
qDebug() << "couldn't connect to local server: " << socket.errorString();
return -1;
}
QEventLoop loop;
QJsonRpcServiceSocket service(&socket);
QJsonRpcServiceReply *reply = service.invokeRemoteMethod("service.currentTime");
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
qDebug() << "response: " << reply->response();
}
@
That's all :)