PDA

View Full Version : ZMQ bindings with Qt



ashboi
30th September 2013, 09:07
I'm currently trying to work with implementing a socket client in my qt app so accept emitted strings as signals from buttons, and sending these strings to the socket server on another device and wait until another signal is emitted and repeat the procedure. I've got a separate client script working with zeroMQ, but I'm not sure how to go about breaking it down into being event driven and integrating it with Qt.

I was wondering has anyone managed to implement zeromq with sockets in qt? I've looked at zmq bindings for qt (nzmqt & qzmq), but there isn't much examples available on them. I hope someone could provide some short examples on how to enable zeromq to work with being event driven.

ashboi
30th September 2013, 15:19
I've made some progress with writing up a script for nzmqt bindings, but I got so many errors that I have no idea what I'm doing wrong. Currently, what I've done is declare 3 different types of sockets 1 PUSH to send messages to the server and 2 SUB to get reply from the server.

The error message I got:

In file included from main.cpp:2:0:
socket.h:26:22: error: ‘ZMQContext’ has not been declared
socket.h:26:96: error: ISO C++ forbids declaration of ‘Ventilator’ with no type [-fpermissive]
socket.h:26:96: error: only declarations of constructors can be ‘explicit’
In file included from main.cpp:2:0:
socket.h:28:26: error: ‘ZMQContext’ has not been declared
socket.h:28:112: error: ISO C++ forbids declaration of ‘Subscriber’ with no type [-fpermissive]
socket.h:28:112: error: only declarations of constructors can be ‘explicit’
socket.h:30:23: error: ‘ZMQContext’ has not been declared
socket.h:30:109: error: ISO C++ forbids declaration of ‘Subscriber2’ with no type [-fpermissive]
socket.h:30:109: error: only declarations of constructors can be ‘explicit’
socket.h:53:5: error: ‘ZMQSocket’ does not name a type
socket.h:54:5: error: ‘ZMQSocket’ does not name a type
socket.h:55:5: error: ‘ZMQSocket’ does not name a type
In file included from main.cpp:2:0:
socket.h: In member function ‘int Socket::Ventilator(int&, const QString&, QObject*)’:
socket.h:26:99: error: only constructors take member initialisers
In file included from main.cpp:2:0:
socket.h:26:155: error: class ‘Socket’ does not have any field named ‘ventilator_’
socket.h:26:168: error: expected ‘{’ at end of input
socket.h:26:168: warning: no return statement in function returning non-void [-Wreturn-type]
socket.h: At global scope:
socket.h:26:11: warning: unused parameter ‘context’ [-Wunused-parameter]
make: *** [main.o] Error 1



Here is my current code:

Socket.cpp

#include "socket.h"
#include <QByteArray>
#include <QList>

using namespace std;

explicit Socket::Subscriber(ZMQContext& context, const QString& address, const QString& topic, QObject *parent = 0)
: super(parent)
, address_("127.0.0.1:5558"), topic_("")
, socket_(0)
{
socket_ = context.createSocket(ZMQSocket::TYP_SUB, this);
socket_->setObjectName("Subscriber.Socket.socket(SUB)");
connect(socket_, SIGNAL(recvString(const QList<QByteArray>&)), SLOT(recvString(const QList<QByteArray>&)));
}

explicit Socket::Subscriber2(ZMQContext& context, const QString& address, const QString& topic, QObject *parent = 0)
: super(parent)
, address2_("127.0.0.1:5559"), topic2_("")
, socket2_(0)
{
socket2_ = context.createSocket(ZMQSocket::TYP_SUB, this);
socket2_->setObjectName("Subscriber2.Socket.socket(SUB)");
connect(socket2_, SIGNAL(recvString(const QList<QByteArray>&)), SLOT(recvString(const QList<QByteArray>&)));
}

explicit Socket::Ventilator(ZMQContext& context, const QString& ventilatorAddress, QObject* parent = 0)
: super(parent)
, ventilatorAddress_("127.0.0.1:5557"), ventilator_(0)
{
ventilator_ = context.createSocket(ZMQSocket::TYP_PUSH, this);
ventilator_->setObjectName("Ventilator.Socket.ventilator(PUSH)");
}

void Socket::startImpl()
{
socket_->subscribeTo(topic_);
socket_->connectTo(address_);

socket2_->subscribeTo(topic2_);
socket2_->connectTo(address2_);

ventilator_->connectTo(ventilatorAddress_);
}

void Socket::send(QString message)
{
this->message = message;
}

void Socket::sendString()
{
ventilator_->sendMessage(send);
}

void Socket::recvString(const QList<QByteArray>& message)
{
qDebug() << "Subscriber> " << message;
}


Socket.h

#ifndef SOCKET_H
#define SOCKET_H

#include <nzmqt/nzmqt.hpp>

#include <QByteArray>
#include <QDateTime>
#include <QList>
#include <QTimer>
#include <QThread>

namespace nzmqt
{
class Socket;
}

class Socket : public QThread
{
Q_OBJECT

public:
explicit Ventilator(ZMQContext& context, const QString& ventilatorAddress, QObject* parent = 0): QThread(parent), ventilatorAddress_(ventilatorAddress), ventilator_(0);

explicit Subscriber(ZMQContext& context, const QString& address, const QString& topic, QObject *parent = 0);

explicit Subscriber2(ZMQContext& context, const QString& address, const QString& topic, QObject *parent = 0);

signals:
void stringRecv(const QList<QByteArray>& message);

public slots:
void sendsig(QString signal);

protected:
void startImpl();

protected slots:
void recvString();
void sendString();

private:
QString signal;
QString address_;
QString topic_;
QString address2_;
QString topic2_;
QString ventilatorAddress_;

ZMQSocket* socket_;
ZMQSocket* socket2_;
ZMQSocket* ventilator_;
};
#endif // SOCKET_H

wysota
30th September 2013, 15:44
You are likely missing an include for ZMQContext.