implemented pubsub tutorial
This commit is contained in:
parent
7ff7719518
commit
64c371ebb0
|
|
@ -1,9 +1,58 @@
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
#include <QStringList>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
|
#include "amqp_client.h"
|
||||||
|
#include "amqp_exchange.h"
|
||||||
|
#include "amqp_queue.h"
|
||||||
|
using namespace QAMQP;
|
||||||
|
|
||||||
|
class LogEmitter : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
LogEmitter(QObject *parent = 0) : QObject(parent) {}
|
||||||
|
|
||||||
|
public Q_SLOTS:
|
||||||
|
void start() {
|
||||||
|
connect(&m_client, SIGNAL(connected()), this, SLOT(clientConnected()));
|
||||||
|
connect(&m_client, SIGNAL(disconnected()), qApp, SLOT(quit()));
|
||||||
|
m_client.connectToHost();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Q_SLOTS:
|
||||||
|
void clientConnected() {
|
||||||
|
Exchange *exchange = m_client.createExchange("logs");
|
||||||
|
connect(exchange, SIGNAL(declared()), this, SLOT(exchangeDeclared()));
|
||||||
|
exchange->declare(Exchange::FanOut);
|
||||||
|
}
|
||||||
|
|
||||||
|
void exchangeDeclared() {
|
||||||
|
Exchange *exchange = qobject_cast<Exchange*>(sender());
|
||||||
|
if (!exchange)
|
||||||
|
return;
|
||||||
|
|
||||||
|
QString message;
|
||||||
|
if (qApp->arguments().size() < 2)
|
||||||
|
message = "info: Hello World!";
|
||||||
|
else
|
||||||
|
message = qApp->arguments().at(1);
|
||||||
|
exchange->publish(message, "");
|
||||||
|
qDebug() << " [x] Sent " << message;
|
||||||
|
m_client.disconnectFromHost();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Client m_client;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
QCoreApplication app(argc, argv);
|
QCoreApplication app(argc, argv);
|
||||||
qDebug() << "testing";
|
LogEmitter logEmitter;
|
||||||
return EXIT_SUCCESS;
|
logEmitter.start();
|
||||||
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "main.moc"
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,76 @@
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
#include <QTimer>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
|
#include "amqp_client.h"
|
||||||
|
#include "amqp_exchange.h"
|
||||||
|
#include "amqp_queue.h"
|
||||||
|
using namespace QAMQP;
|
||||||
|
|
||||||
|
class LogReceiver : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
LogReceiver(QObject *parent = 0) : QObject(parent) {}
|
||||||
|
|
||||||
|
public Q_SLOTS:
|
||||||
|
void start() {
|
||||||
|
connect(&m_client, SIGNAL(connected()), this, SLOT(clientConnected()));
|
||||||
|
m_client.connectToHost();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Q_SLOTS:
|
||||||
|
void clientConnected() {
|
||||||
|
Exchange *exchange = m_client.createExchange("logs");
|
||||||
|
connect(exchange, SIGNAL(declared()), this, SLOT(exchangeDeclared()));
|
||||||
|
exchange->declare(Exchange::FanOut);
|
||||||
|
}
|
||||||
|
|
||||||
|
void exchangeDeclared() {
|
||||||
|
Queue *temporaryQueue = m_client.createQueue();
|
||||||
|
connect(temporaryQueue, SIGNAL(declared()), this, SLOT(queueDeclared()));
|
||||||
|
connect(temporaryQueue, SIGNAL(bound()), this, SLOT(queueBound()));
|
||||||
|
connect(temporaryQueue, SIGNAL(messageReceived()), this, SLOT(messageReceived()));
|
||||||
|
temporaryQueue->declare(Queue::Exclusive);
|
||||||
|
}
|
||||||
|
|
||||||
|
void queueDeclared() {
|
||||||
|
Queue *temporaryQueue = qobject_cast<Queue*>(sender());
|
||||||
|
if (!temporaryQueue)
|
||||||
|
return;
|
||||||
|
|
||||||
|
temporaryQueue->bind("logs", temporaryQueue->name());
|
||||||
|
}
|
||||||
|
|
||||||
|
void queueBound() {
|
||||||
|
Queue *temporaryQueue = qobject_cast<Queue*>(sender());
|
||||||
|
if (!temporaryQueue)
|
||||||
|
return;
|
||||||
|
|
||||||
|
qDebug() << " [*] Waiting for logs. To exit press CTRL+C";
|
||||||
|
temporaryQueue->consume(Queue::coNoAck);
|
||||||
|
}
|
||||||
|
|
||||||
|
void messageReceived() {
|
||||||
|
Queue *temporaryQueue = qobject_cast<Queue*>(sender());
|
||||||
|
if (!temporaryQueue)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Message message = temporaryQueue->dequeue();
|
||||||
|
qDebug() << " [x] " << message.payload();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Client m_client;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
QCoreApplication app(argc, argv);
|
QCoreApplication app(argc, argv);
|
||||||
qDebug() << "testing";
|
LogReceiver logReceiver;
|
||||||
return EXIT_SUCCESS;
|
logReceiver.start();
|
||||||
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "main.moc"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue