115 lines
2.8 KiB
C++
115 lines
2.8 KiB
C++
#include <QApplication>
|
|
#include <QTextStream>
|
|
#include <QDebug>
|
|
|
|
#include <qxtglobalshortcut.h>
|
|
|
|
#include "qamqpclient.h"
|
|
#include "qamqpexchange.h"
|
|
#include "qamqpqueue.h"
|
|
|
|
class Receiver : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
Receiver(QObject *parent = 0) : QObject(parent) {
|
|
m_client.setAutoReconnect(true);
|
|
}
|
|
|
|
public Q_SLOTS:
|
|
void start() {
|
|
connect(&m_client, SIGNAL(connected()), this, SLOT(clientConnected()));
|
|
m_client.connectToHost();
|
|
}
|
|
|
|
void OneShot(void) {
|
|
m_client.connectToHost();
|
|
}
|
|
|
|
private Q_SLOTS:
|
|
void clientConnected() {
|
|
QAmqpQueue *queue = m_client.createQueue("hello");
|
|
disconnect(queue, 0, 0, 0); // in case this is a reconnect
|
|
connect(queue, SIGNAL(declared()), this, SLOT(queueDeclared()));
|
|
queue->declare();
|
|
}
|
|
|
|
void queueDeclared() {
|
|
QAmqpQueue *queue = qobject_cast<QAmqpQueue*>(sender());
|
|
if (!queue)
|
|
return;
|
|
|
|
connect(queue, SIGNAL(messageReceived()), this, SLOT(messageReceived()));
|
|
|
|
// queue->consume(QAmqpQueue::coNoAck);
|
|
// queue->consume(QAmqpQueue::coNoLocal);
|
|
|
|
qint32 sizeQueue = queue->messageCount();
|
|
while (sizeQueue--) {
|
|
queue->get(false);
|
|
}
|
|
|
|
qDebug() << Qt::endl << " [*] Waiting for messages. To exit press CTRL+C";
|
|
|
|
// queue->ack(3, false); // Acknowledgement the 3rd message.
|
|
|
|
// queue->reopen();
|
|
|
|
m_client.disconnectFromHost();
|
|
}
|
|
|
|
void messageReceived() {
|
|
QAmqpQueue *queue = qobject_cast<QAmqpQueue*>(sender());
|
|
if (!queue)
|
|
return;
|
|
|
|
QAmqpMessage message = queue->dequeue();
|
|
qDebug() << " [x] Received in" << message.payload();
|
|
|
|
int input=0;
|
|
// std::scanf("%d", &input);
|
|
qDebug() << " [x] Received out, " << message.deliveryTag() << " | " << message.payload() << " , input = " << input;
|
|
}
|
|
|
|
private:
|
|
QAmqpClient m_client;
|
|
|
|
};
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
qDebug() << " " << Qt::endl;
|
|
QApplication app(argc, argv);
|
|
|
|
QTextStream out(stdout);
|
|
QTextStream err(stderr);
|
|
|
|
const QKeySequence shortcut("Ctrl+Shift+Z");
|
|
const QxtGlobalShortcut globalShortcut(shortcut);
|
|
|
|
if ( !globalShortcut.isValid() ) {
|
|
err << QString("Error: Failed to set shortcut %1")
|
|
.arg(shortcut.toString()) << Qt::endl;
|
|
return 1;
|
|
}
|
|
|
|
out << QString("Press shortcut %1 (or CTRL+C to exit)").arg(shortcut.toString()) << Qt::endl;
|
|
|
|
Receiver receiver;
|
|
|
|
QObject::connect(
|
|
&globalShortcut, &QxtGlobalShortcut::activated, &globalShortcut,
|
|
[&]{
|
|
// out << QLatin1String("Shortcut pressed!") << Qt::endl;
|
|
receiver.OneShot();
|
|
// QApplication::quit();
|
|
});
|
|
|
|
|
|
receiver.start();
|
|
|
|
return app.exec();
|
|
}
|
|
|
|
#include "main.moc"
|