2014-05-29 00:25:28 +08:00
|
|
|
#include "amqp_exchange.h"
|
|
|
|
|
#include "amqp_exchange_p.h"
|
|
|
|
|
#include "amqp_queue.h"
|
2014-05-29 01:52:27 +08:00
|
|
|
#include "amqp_global.h"
|
2014-05-29 00:25:28 +08:00
|
|
|
|
|
|
|
|
#include <QDataStream>
|
2014-05-29 01:52:27 +08:00
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
using namespace QAMQP;
|
2014-05-29 00:25:28 +08:00
|
|
|
|
|
|
|
|
Exchange::Exchange(int channelNumber, Client *parent)
|
|
|
|
|
: Channel(new ExchangePrivate(this), parent)
|
|
|
|
|
{
|
2014-05-29 01:52:27 +08:00
|
|
|
Q_D(Exchange);
|
2014-05-29 00:25:28 +08:00
|
|
|
d->init(channelNumber, parent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Exchange::~Exchange()
|
|
|
|
|
{
|
|
|
|
|
remove();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Exchange::onOpen()
|
|
|
|
|
{
|
2014-05-29 01:52:27 +08:00
|
|
|
Q_D(Exchange);
|
2014-05-29 00:25:28 +08:00
|
|
|
if (d->delayedDeclare)
|
|
|
|
|
d->declare();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Exchange::onClose()
|
|
|
|
|
{
|
2014-05-29 01:52:27 +08:00
|
|
|
Q_D(Exchange);
|
2014-05-29 00:25:28 +08:00
|
|
|
d->remove(true, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Exchange::ExchangeOptions Exchange::option() const
|
|
|
|
|
{
|
2014-05-29 01:52:27 +08:00
|
|
|
Q_D(const Exchange);
|
2014-05-29 00:25:28 +08:00
|
|
|
return d->options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Exchange::type() const
|
|
|
|
|
{
|
2014-05-29 01:52:27 +08:00
|
|
|
Q_D(const Exchange);
|
2014-05-29 00:25:28 +08:00
|
|
|
return d->type;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-29 01:52:27 +08:00
|
|
|
void Exchange::declare(const QString &type, ExchangeOptions option , const Frame::TableField &arg)
|
2014-05-29 00:25:28 +08:00
|
|
|
{
|
2014-05-29 01:52:27 +08:00
|
|
|
Q_D(Exchange);
|
2014-05-29 00:25:28 +08:00
|
|
|
d->options = option;
|
|
|
|
|
d->type = type;
|
|
|
|
|
d->arguments = arg;
|
|
|
|
|
d->declare();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Exchange::remove(bool ifUnused, bool noWait)
|
|
|
|
|
{
|
2014-05-29 01:52:27 +08:00
|
|
|
Q_D(Exchange);
|
2014-05-29 00:25:28 +08:00
|
|
|
d->remove(ifUnused, noWait);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-29 01:52:27 +08:00
|
|
|
void Exchange::bind(Queue *queue)
|
2014-05-29 00:25:28 +08:00
|
|
|
{
|
2014-05-29 01:52:27 +08:00
|
|
|
Q_D(Exchange);
|
2014-05-29 00:25:28 +08:00
|
|
|
queue->bind(this, d->name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Exchange::bind(const QString &queueName)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(queueName);
|
|
|
|
|
qWarning("Not implemented");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Exchange::bind(const QString &queueName, const QString &key)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(queueName);
|
|
|
|
|
Q_UNUSED(key);
|
|
|
|
|
qWarning("Not implemented");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Exchange::publish(const QString &message, const QString &key, const MessageProperties &prop)
|
|
|
|
|
{
|
2014-05-29 01:52:27 +08:00
|
|
|
Q_D(Exchange);
|
2014-05-29 00:25:28 +08:00
|
|
|
d->publish(message.toUtf8(), key, QLatin1String("text.plain"), QVariantHash(), prop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Exchange::publish(const QByteArray &message, const QString &key,
|
|
|
|
|
const QString &mimeType, const MessageProperties &prop)
|
|
|
|
|
{
|
2014-05-29 01:52:27 +08:00
|
|
|
Q_D(Exchange);
|
2014-05-29 00:25:28 +08:00
|
|
|
d->publish(message, key, mimeType, QVariantHash(), prop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Exchange::publish(const QByteArray &message, const QString &key,
|
|
|
|
|
const QVariantHash &headers, const QString &mimeType,
|
|
|
|
|
const MessageProperties &prop)
|
|
|
|
|
{
|
2014-05-29 01:52:27 +08:00
|
|
|
Q_D(Exchange);
|
2014-05-29 00:25:28 +08:00
|
|
|
d->publish(message, key, mimeType, headers, prop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2014-05-30 23:12:09 +08:00
|
|
|
ExchangePrivate::ExchangePrivate(Exchange *q)
|
2014-05-29 00:25:28 +08:00
|
|
|
: ChannelPrivate(q),
|
|
|
|
|
delayedDeclare(false),
|
|
|
|
|
declared(false)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExchangePrivate::~ExchangePrivate()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-29 01:52:27 +08:00
|
|
|
bool ExchangePrivate::_q_method(const Frame::Method &frame)
|
2014-05-29 00:25:28 +08:00
|
|
|
{
|
|
|
|
|
if (ChannelPrivate::_q_method(frame))
|
|
|
|
|
return true;
|
|
|
|
|
|
2014-05-29 01:52:27 +08:00
|
|
|
if (frame.methodClass() != Frame::fcExchange)
|
2014-05-29 00:25:28 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
switch(frame.id()) {
|
|
|
|
|
case miDeclareOk:
|
|
|
|
|
declareOk(frame);
|
|
|
|
|
break;
|
|
|
|
|
case miDelete:
|
|
|
|
|
deleteOk(frame);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-29 01:52:27 +08:00
|
|
|
void ExchangePrivate::declareOk(const Frame::Method &frame)
|
2014-05-29 00:25:28 +08:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(frame)
|
2014-05-29 01:52:27 +08:00
|
|
|
Q_Q(Exchange);
|
2014-05-29 00:25:28 +08:00
|
|
|
qDebug() << "Declared exchange: " << name;
|
|
|
|
|
declared = true;
|
|
|
|
|
QMetaObject::invokeMethod(q, "declared");
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-29 01:52:27 +08:00
|
|
|
void ExchangePrivate::deleteOk(const Frame::Method &frame)
|
2014-05-29 00:25:28 +08:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(frame)
|
2014-05-29 01:52:27 +08:00
|
|
|
Q_Q(Exchange);
|
2014-05-29 00:25:28 +08:00
|
|
|
qDebug() << "Deleted exchange: " << name;
|
|
|
|
|
declared = false;
|
|
|
|
|
QMetaObject::invokeMethod(q, "removed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExchangePrivate::declare()
|
|
|
|
|
{
|
|
|
|
|
if (!opened) {
|
|
|
|
|
delayedDeclare = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (name.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2014-05-29 01:52:27 +08:00
|
|
|
Frame::Method frame(Frame::fcExchange, miDeclare);
|
2014-05-29 00:25:28 +08:00
|
|
|
frame.setChannel(number);
|
|
|
|
|
QByteArray arguments_;
|
|
|
|
|
QDataStream stream(&arguments_, QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
|
|
stream << qint16(0); //reserver 1
|
2014-05-29 01:52:27 +08:00
|
|
|
Frame::writeField('s', stream, name);
|
|
|
|
|
Frame::writeField('s', stream, type);
|
2014-05-29 00:25:28 +08:00
|
|
|
stream << qint8(options);
|
2014-05-29 01:52:27 +08:00
|
|
|
Frame::writeField('F', stream, ExchangePrivate::arguments);
|
2014-05-29 00:25:28 +08:00
|
|
|
|
|
|
|
|
frame.setArguments(arguments_);
|
|
|
|
|
sendFrame(frame);
|
|
|
|
|
delayedDeclare = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExchangePrivate::remove(bool ifUnused, bool noWait)
|
|
|
|
|
{
|
2014-05-29 01:52:27 +08:00
|
|
|
Frame::Method frame(Frame::fcExchange, miDelete);
|
2014-05-29 00:25:28 +08:00
|
|
|
frame.setChannel(number);
|
|
|
|
|
QByteArray arguments_;
|
|
|
|
|
QDataStream stream(&arguments_, QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
|
|
stream << qint16(0); //reserver 1
|
2014-05-29 01:52:27 +08:00
|
|
|
Frame::writeField('s', stream, name);
|
2014-05-29 00:25:28 +08:00
|
|
|
|
|
|
|
|
qint8 flag = 0;
|
|
|
|
|
|
|
|
|
|
flag |= (ifUnused ? 0x1 : 0);
|
|
|
|
|
flag |= (noWait ? 0x2 : 0);
|
|
|
|
|
|
|
|
|
|
stream << flag; //reserver 1
|
|
|
|
|
|
|
|
|
|
frame.setArguments(arguments_);
|
|
|
|
|
sendFrame(frame);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExchangePrivate::publish(const QByteArray &message, const QString &key,
|
2014-05-30 23:12:09 +08:00
|
|
|
const QString &mimeType, const QVariantHash &headers,
|
2014-05-29 00:25:28 +08:00
|
|
|
const Exchange::MessageProperties &prop)
|
|
|
|
|
{
|
2014-05-29 01:52:27 +08:00
|
|
|
Frame::Method frame(Frame::fcBasic, bmPublish);
|
2014-05-29 00:25:28 +08:00
|
|
|
frame.setChannel(number);
|
|
|
|
|
QByteArray arguments_;
|
|
|
|
|
QDataStream out(&arguments_, QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
|
|
out << qint16(0); //reserver 1
|
2014-05-29 01:52:27 +08:00
|
|
|
Frame::writeField('s', out, name);
|
|
|
|
|
Frame::writeField('s', out, key);
|
2014-05-29 00:25:28 +08:00
|
|
|
out << qint8(0);
|
|
|
|
|
|
|
|
|
|
frame.setArguments(arguments_);
|
|
|
|
|
sendFrame(frame);
|
|
|
|
|
|
2014-05-29 01:52:27 +08:00
|
|
|
Frame::Content content(Frame::fcBasic);
|
2014-05-29 00:25:28 +08:00
|
|
|
content.setChannel(number);
|
2014-05-29 01:52:27 +08:00
|
|
|
content.setProperty(Frame::Content::cpContentType, mimeType);
|
|
|
|
|
content.setProperty(Frame::Content::cpContentEncoding, "utf-8");
|
|
|
|
|
content.setProperty(Frame::Content::cpHeaders, headers);
|
|
|
|
|
content.setProperty(Frame::Content::cpMessageId, "0");
|
2014-05-29 00:25:28 +08:00
|
|
|
|
2014-05-30 23:12:09 +08:00
|
|
|
Exchange::MessageProperties::ConstIterator it;
|
|
|
|
|
Exchange::MessageProperties::ConstIterator itEnd = prop.constEnd();
|
|
|
|
|
for (it = prop.constBegin(); it != itEnd; ++it)
|
|
|
|
|
content.setProperty(it.key(), it.value());
|
2014-05-29 00:25:28 +08:00
|
|
|
|
|
|
|
|
content.setBody(message);
|
|
|
|
|
sendFrame(content);
|
|
|
|
|
|
|
|
|
|
int fullSize = message.size();
|
|
|
|
|
for (int sended_ = 0; sended_ < fullSize; sended_+= (FRAME_MAX - 7)) {
|
2014-05-29 01:52:27 +08:00
|
|
|
Frame::ContentBody body;
|
2014-05-29 00:25:28 +08:00
|
|
|
QByteArray partition_ = message.mid(sended_, (FRAME_MAX - 7));
|
|
|
|
|
body.setChannel(number);
|
|
|
|
|
body.setBody(partition_);
|
|
|
|
|
sendFrame(body);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExchangePrivate::_q_disconnected()
|
|
|
|
|
{
|
|
|
|
|
ChannelPrivate::_q_disconnected();
|
|
|
|
|
qDebug() << "Exchange " << name << " disconnected";
|
|
|
|
|
delayedDeclare = false;
|
|
|
|
|
declared = false;
|
|
|
|
|
}
|