diff --git a/tutorials/pubsub/emit_log/main.cpp b/tutorials/pubsub/emit_log/main.cpp index 9c5f00c..42bb135 100644 --- a/tutorials/pubsub/emit_log/main.cpp +++ b/tutorials/pubsub/emit_log/main.cpp @@ -1,9 +1,58 @@ #include +#include #include +#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(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) { QCoreApplication app(argc, argv); - qDebug() << "testing"; - return EXIT_SUCCESS; + LogEmitter logEmitter; + logEmitter.start(); + return app.exec(); } + +#include "main.moc" diff --git a/tutorials/pubsub/receive_logs/main.cpp b/tutorials/pubsub/receive_logs/main.cpp index 9c5f00c..4143cda 100644 --- a/tutorials/pubsub/receive_logs/main.cpp +++ b/tutorials/pubsub/receive_logs/main.cpp @@ -1,9 +1,76 @@ #include +#include #include +#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(sender()); + if (!temporaryQueue) + return; + + temporaryQueue->bind("logs", temporaryQueue->name()); + } + + void queueBound() { + Queue *temporaryQueue = qobject_cast(sender()); + if (!temporaryQueue) + return; + + qDebug() << " [*] Waiting for logs. To exit press CTRL+C"; + temporaryQueue->consume(Queue::coNoAck); + } + + void messageReceived() { + Queue *temporaryQueue = qobject_cast(sender()); + if (!temporaryQueue) + return; + + Message message = temporaryQueue->dequeue(); + qDebug() << " [x] " << message.payload(); + } + +private: + Client m_client; + +}; + int main(int argc, char **argv) { QCoreApplication app(argc, argv); - qDebug() << "testing"; - return EXIT_SUCCESS; + LogReceiver logReceiver; + logReceiver.start(); + return app.exec(); } + +#include "main.moc"