qamqp/tutorials/topics/emit_log_topic/main.cpp

66 lines
1.7 KiB
C++
Raw Normal View History

#include <QCoreApplication>
2014-06-19 23:04:04 +08:00
#include <QStringList>
#include <QDebug>
#include "qamqpclient.h"
#include "qamqpexchange.h"
#include "qamqpqueue.h"
2014-06-19 23:04:04 +08:00
using namespace QAMQP;
class TopicLogEmitter : public QObject
{
Q_OBJECT
public:
TopicLogEmitter(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 *topic_logs = m_client.createExchange("topic_logs");
connect(topic_logs, SIGNAL(declared()), this, SLOT(exchangeDeclared()));
topic_logs->declare(Exchange::Topic);
}
void exchangeDeclared() {
Exchange *topic_logs = qobject_cast<Exchange*>(sender());
if (!topic_logs)
return;
QStringList args = qApp->arguments();
args.takeFirst(); // remove executable name
QString routingKey = (args.isEmpty() ? "anonymous.info" : args.first());
QString message;
if (args.size() > 1) {
args.takeFirst();
message = args.join(" ");
} else {
message = "Hello World!";
}
topic_logs->publish(message, routingKey);
qDebug(" [x] Sent %s:%s", routingKey.toLatin1().constData(), message.toLatin1().constData());
m_client.disconnectFromHost();
}
private:
Client m_client;
};
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
2014-06-19 23:04:04 +08:00
TopicLogEmitter logEmitter;
logEmitter.start();
return app.exec();
}
2014-06-19 23:04:04 +08:00
#include "main.moc"