events/main.cpp

115 lines
2.8 KiB
C++
Raw Normal View History

#include <QApplication>
#include <QTextStream>
2025-08-25 21:10:21 +08:00
#include <QDebug>
#include <qxtglobalshortcut.h>
2025-08-25 21:10:21 +08:00
#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);
2025-08-25 21:10:21 +08:00
}
public Q_SLOTS:
void start() {
connect(&m_client, SIGNAL(connected()), this, SLOT(clientConnected()));
m_client.connectToHost();
}
void OneShot(void) {
m_client.connectToHost();
}
2025-08-25 21:10:21 +08:00
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";
2025-08-25 21:10:21 +08:00
// queue->ack(3, false); // Acknowledgement the 3rd message.
2025-08-25 21:10:21 +08:00
// queue->reopen();
2025-08-25 21:10:21 +08:00
m_client.disconnectFromHost();
2025-08-25 21:10:21 +08:00
}
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;
2025-08-25 21:10:21 +08:00
Receiver receiver;
QObject::connect(
&globalShortcut, &QxtGlobalShortcut::activated, &globalShortcut,
[&]{
// out << QLatin1String("Shortcut pressed!") << Qt::endl;
receiver.OneShot();
// QApplication::quit();
});
2025-08-25 21:10:21 +08:00
receiver.start();
2025-08-25 21:10:21 +08:00
return app.exec();
}
#include "main.moc"