refactored Connection to take a Network in the ctor
standardized variable names decoupled Connection and Client (they are no longer friends)
This commit is contained in:
parent
360e64b34b
commit
9eabe0587e
|
|
@ -34,7 +34,7 @@ void ClientPrivate::init(const QUrl &connectionString)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!connection_)
|
if (!connection_)
|
||||||
connection_ = new Connection(q);
|
connection_ = new Connection(network_, q);
|
||||||
network_->setMethodHandlerConnection(connection_);
|
network_->setMethodHandlerConnection(connection_);
|
||||||
|
|
||||||
auth_ = QSharedPointer<Authenticator>(
|
auth_ = QSharedPointer<Authenticator>(
|
||||||
|
|
@ -51,7 +51,9 @@ void ClientPrivate::init(const QUrl &connectionString)
|
||||||
|
|
||||||
void ClientPrivate::connect()
|
void ClientPrivate::connect()
|
||||||
{
|
{
|
||||||
sockConnect();
|
if (network_->state() != QAbstractSocket::UnconnectedState)
|
||||||
|
disconnect();
|
||||||
|
network_->connectTo(host, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientPrivate::parseConnectionString(const QUrl &connectionString)
|
void ClientPrivate::parseConnectionString(const QUrl &connectionString)
|
||||||
|
|
@ -71,13 +73,6 @@ void ClientPrivate::parseConnectionString(const QUrl &connectionString)
|
||||||
q->setVirtualHost(connectionString.path());
|
q->setVirtualHost(connectionString.path());
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientPrivate::sockConnect()
|
|
||||||
{
|
|
||||||
if (network_->state() != QAbstractSocket::UnconnectedState)
|
|
||||||
disconnect();
|
|
||||||
network_->connectTo(host, port);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClientPrivate::disconnect()
|
void ClientPrivate::disconnect()
|
||||||
{
|
{
|
||||||
if (network_->state() == QAbstractSocket::UnconnectedState) {
|
if (network_->state() == QAbstractSocket::UnconnectedState) {
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,6 @@ private:
|
||||||
Q_DECLARE_PRIVATE(Client)
|
Q_DECLARE_PRIVATE(Client)
|
||||||
QScopedPointer<ClientPrivate> d_ptr;
|
QScopedPointer<ClientPrivate> d_ptr;
|
||||||
|
|
||||||
friend class ConnectionPrivate;
|
|
||||||
friend class ChannelPrivate;
|
friend class ChannelPrivate;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,6 @@ public:
|
||||||
void connect();
|
void connect();
|
||||||
void disconnect();
|
void disconnect();
|
||||||
void parseConnectionString(const QUrl &connectionString);
|
void parseConnectionString(const QUrl &connectionString);
|
||||||
void sockConnect();
|
|
||||||
|
|
||||||
quint32 port;
|
quint32 port;
|
||||||
QString host;
|
QString host;
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
using namespace QAMQP;
|
using namespace QAMQP;
|
||||||
|
|
||||||
ConnectionPrivate::ConnectionPrivate(Connection *q)
|
ConnectionPrivate::ConnectionPrivate(Connection *q)
|
||||||
: closed_(false),
|
: closed(false),
|
||||||
connected(false),
|
connected(false),
|
||||||
q_ptr(q)
|
q_ptr(q)
|
||||||
{
|
{
|
||||||
|
|
@ -22,20 +22,11 @@ ConnectionPrivate::~ConnectionPrivate()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionPrivate::init(Client *parent)
|
|
||||||
{
|
|
||||||
Q_Q(Connection);
|
|
||||||
q->setParent(parent);
|
|
||||||
client_ = parent;
|
|
||||||
heartbeatTimer_ = new QTimer(parent);
|
|
||||||
QObject::connect(heartbeatTimer_, SIGNAL(timeout()), q, SLOT(_q_heartbeat()));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionPrivate::startOk()
|
void ConnectionPrivate::startOk()
|
||||||
{
|
{
|
||||||
Frame::Method frame(Frame::fcConnection, miStartOk);
|
Frame::Method frame(Frame::fcConnection, miStartOk);
|
||||||
QByteArray arguments_;
|
QByteArray arguments;
|
||||||
QDataStream stream(&arguments_, QIODevice::WriteOnly);
|
QDataStream stream(&arguments, QIODevice::WriteOnly);
|
||||||
|
|
||||||
Frame::TableField clientProperties;
|
Frame::TableField clientProperties;
|
||||||
clientProperties["version"] = QString(QAMQP_VERSION);
|
clientProperties["version"] = QString(QAMQP_VERSION);
|
||||||
|
|
@ -44,10 +35,11 @@ void ConnectionPrivate::startOk()
|
||||||
clientProperties.unite(customProperty);
|
clientProperties.unite(customProperty);
|
||||||
Frame::serialize(stream, clientProperties);
|
Frame::serialize(stream, clientProperties);
|
||||||
|
|
||||||
client_->d_func()->auth_->write(stream);
|
client->auth()->write(stream);
|
||||||
Frame::writeField('s', stream, "en_US");
|
Frame::writeField('s', stream, "en_US");
|
||||||
frame.setArguments(arguments_);
|
|
||||||
client_->d_func()->network_->sendFrame(frame);
|
frame.setArguments(arguments);
|
||||||
|
network->sendFrame(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionPrivate::secureOk()
|
void ConnectionPrivate::secureOk()
|
||||||
|
|
@ -57,30 +49,30 @@ void ConnectionPrivate::secureOk()
|
||||||
void ConnectionPrivate::tuneOk()
|
void ConnectionPrivate::tuneOk()
|
||||||
{
|
{
|
||||||
Frame::Method frame(Frame::fcConnection, miTuneOk);
|
Frame::Method frame(Frame::fcConnection, miTuneOk);
|
||||||
QByteArray arguments_;
|
QByteArray arguments;
|
||||||
QDataStream stream(&arguments_, QIODevice::WriteOnly);
|
QDataStream stream(&arguments, QIODevice::WriteOnly);
|
||||||
|
|
||||||
stream << qint16(0); //channel_max
|
stream << qint16(0); //channel_max
|
||||||
stream << qint32(FRAME_MAX); //frame_max
|
stream << qint32(FRAME_MAX); //frame_max
|
||||||
stream << qint16(heartbeatTimer_->interval() / 1000); //heartbeat
|
stream << qint16(heartbeatTimer->interval() / 1000); //heartbeat
|
||||||
|
|
||||||
frame.setArguments(arguments_);
|
frame.setArguments(arguments);
|
||||||
client_->d_func()->network_->sendFrame(frame);
|
network->sendFrame(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionPrivate::open()
|
void ConnectionPrivate::open()
|
||||||
{
|
{
|
||||||
Frame::Method frame(Frame::fcConnection, miOpen);
|
Frame::Method frame(Frame::fcConnection, miOpen);
|
||||||
QByteArray arguments_;
|
QByteArray arguments;
|
||||||
QDataStream stream(&arguments_, QIODevice::WriteOnly);
|
QDataStream stream(&arguments, QIODevice::WriteOnly);
|
||||||
|
|
||||||
Frame::writeField('s',stream, client_->virtualHost());
|
Frame::writeField('s',stream, client->virtualHost());
|
||||||
|
|
||||||
stream << qint8(0);
|
stream << qint8(0);
|
||||||
stream << qint8(0);
|
stream << qint8(0);
|
||||||
|
|
||||||
frame.setArguments(arguments_);
|
frame.setArguments(arguments);
|
||||||
client_->d_func()->network_->sendFrame(frame);
|
network->sendFrame(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionPrivate::start(const Frame::Method &frame)
|
void ConnectionPrivate::start(const Frame::Method &frame)
|
||||||
|
|
@ -133,12 +125,12 @@ void ConnectionPrivate::tune(const Frame::Method &frame)
|
||||||
qDebug(">> frame_max: %d", frame_max);
|
qDebug(">> frame_max: %d", frame_max);
|
||||||
qDebug(">> heartbeat: %d", heartbeat);
|
qDebug(">> heartbeat: %d", heartbeat);
|
||||||
|
|
||||||
if (heartbeatTimer_) {
|
if (heartbeatTimer) {
|
||||||
heartbeatTimer_->setInterval(heartbeat * 1000);
|
heartbeatTimer->setInterval(heartbeat * 1000);
|
||||||
if (heartbeatTimer_->interval())
|
if (heartbeatTimer->interval())
|
||||||
heartbeatTimer_->start();
|
heartbeatTimer->start();
|
||||||
else
|
else
|
||||||
heartbeatTimer_->stop();
|
heartbeatTimer->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
tuneOk();
|
tuneOk();
|
||||||
|
|
@ -173,32 +165,32 @@ void ConnectionPrivate::close(const Frame::Method &frame)
|
||||||
qDebug(">> class-id: %d", classId);
|
qDebug(">> class-id: %d", classId);
|
||||||
qDebug(">> method-id: %d", methodId);
|
qDebug(">> method-id: %d", methodId);
|
||||||
connected = false;
|
connected = false;
|
||||||
client_->d_func()->network_->error(QAbstractSocket::RemoteHostClosedError);
|
network->error(QAbstractSocket::RemoteHostClosedError);
|
||||||
QMetaObject::invokeMethod(q, "disconnected");
|
Q_EMIT q->disconnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionPrivate::close(int code, const QString &text, int classId, int methodId)
|
void ConnectionPrivate::close(int code, const QString &text, int classId, int methodId)
|
||||||
{
|
{
|
||||||
Frame::Method frame(Frame::fcConnection, miClose);
|
Frame::Method frame(Frame::fcConnection, miClose);
|
||||||
QByteArray arguments_;
|
QByteArray arguments;
|
||||||
QDataStream stream(&arguments_, QIODevice::WriteOnly);
|
QDataStream stream(&arguments, QIODevice::WriteOnly);
|
||||||
|
|
||||||
Frame::writeField('s',stream, client_->virtualHost());
|
Frame::writeField('s',stream, client->virtualHost());
|
||||||
|
|
||||||
stream << qint16(code);
|
stream << qint16(code);
|
||||||
Frame::writeField('s', stream, text);
|
Frame::writeField('s', stream, text);
|
||||||
stream << qint16(classId);
|
stream << qint16(classId);
|
||||||
stream << qint16(methodId);
|
stream << qint16(methodId);
|
||||||
|
|
||||||
frame.setArguments(arguments_);
|
frame.setArguments(arguments);
|
||||||
client_->d_func()->network_->sendFrame(frame);
|
network->sendFrame(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionPrivate::closeOk()
|
void ConnectionPrivate::closeOk()
|
||||||
{
|
{
|
||||||
Frame::Method frame(Frame::fcConnection, miCloseOk);
|
Frame::Method frame(Frame::fcConnection, miCloseOk);
|
||||||
connected = false;
|
connected = false;
|
||||||
client_->d_func()->network_->sendFrame(frame);
|
network->sendFrame(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionPrivate::closeOk(const Frame::Method &frame)
|
void ConnectionPrivate::closeOk(const Frame::Method &frame)
|
||||||
|
|
@ -207,24 +199,24 @@ void ConnectionPrivate::closeOk(const Frame::Method &frame)
|
||||||
Q_Q(Connection);
|
Q_Q(Connection);
|
||||||
|
|
||||||
connected = false;
|
connected = false;
|
||||||
QMetaObject::invokeMethod(q, "disconnected");
|
Q_EMIT q->disconnected();
|
||||||
if (heartbeatTimer_)
|
if (heartbeatTimer)
|
||||||
heartbeatTimer_->stop();
|
heartbeatTimer->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionPrivate::setQOS(qint32 prefetchSize, quint16 prefetchCount, int channel, bool global)
|
void ConnectionPrivate::setQOS(qint32 prefetchSize, quint16 prefetchCount, int channel, bool global)
|
||||||
{
|
{
|
||||||
Frame::Method frame(Frame::fcBasic, 10);
|
Frame::Method frame(Frame::fcBasic, 10);
|
||||||
frame.setChannel(channel);
|
frame.setChannel(channel);
|
||||||
QByteArray arguments_;
|
QByteArray arguments;
|
||||||
QDataStream out(&arguments_, QIODevice::WriteOnly);
|
QDataStream out(&arguments, QIODevice::WriteOnly);
|
||||||
|
|
||||||
out << prefetchSize;
|
out << prefetchSize;
|
||||||
out << prefetchCount;
|
out << prefetchCount;
|
||||||
out << qint8(global ? 1 : 0);
|
out << qint8(global ? 1 : 0);
|
||||||
|
|
||||||
frame.setArguments(arguments_);
|
frame.setArguments(arguments);
|
||||||
client_->d_func()->network_->sendFrame(frame);
|
network->sendFrame(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ConnectionPrivate::_q_method(const Frame::Method &frame)
|
bool ConnectionPrivate::_q_method(const Frame::Method &frame)
|
||||||
|
|
@ -235,7 +227,7 @@ bool ConnectionPrivate::_q_method(const Frame::Method &frame)
|
||||||
|
|
||||||
qDebug() << "Connection:";
|
qDebug() << "Connection:";
|
||||||
|
|
||||||
if (closed_) {
|
if (closed) {
|
||||||
if (frame.id() == miCloseOk)
|
if (frame.id() == miCloseOk)
|
||||||
closeOk(frame);
|
closeOk(frame);
|
||||||
|
|
||||||
|
|
@ -272,17 +264,20 @@ bool ConnectionPrivate::_q_method(const Frame::Method &frame)
|
||||||
void ConnectionPrivate::_q_heartbeat()
|
void ConnectionPrivate::_q_heartbeat()
|
||||||
{
|
{
|
||||||
Frame::Heartbeat frame;
|
Frame::Heartbeat frame;
|
||||||
client_->d_func()->network_->sendFrame(frame);
|
network->sendFrame(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
Connection::Connection(Client *parent)
|
Connection::Connection(Network *network, Client *client)
|
||||||
: QObject(parent),
|
: QObject(client),
|
||||||
d_ptr(new ConnectionPrivate(this))
|
d_ptr(new ConnectionPrivate(this))
|
||||||
{
|
{
|
||||||
Q_D(Connection);
|
Q_D(Connection);
|
||||||
d->init(parent);
|
d->client = client;
|
||||||
|
d->network = network;
|
||||||
|
d->heartbeatTimer = new QTimer(this);
|
||||||
|
connect(d->heartbeatTimer, SIGNAL(timeout()), this, SLOT(_q_heartbeat()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection::~Connection()
|
Connection::~Connection()
|
||||||
|
|
@ -323,7 +318,7 @@ void Connection::closeOk()
|
||||||
{
|
{
|
||||||
Q_D(Connection);
|
Q_D(Connection);
|
||||||
d->closeOk();
|
d->closeOk();
|
||||||
Q_EMIT disconnect();
|
Q_EMIT disconnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::openOk()
|
void Connection::openOk()
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ namespace QAMQP
|
||||||
{
|
{
|
||||||
|
|
||||||
class Client;
|
class Client;
|
||||||
|
class Network;
|
||||||
class ClientPrivate;
|
class ClientPrivate;
|
||||||
class ChannelPrivate;
|
class ChannelPrivate;
|
||||||
class ConnectionPrivate;
|
class ConnectionPrivate;
|
||||||
|
|
@ -39,7 +40,7 @@ Q_SIGNALS:
|
||||||
void connected();
|
void connected();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit Connection(Client *parent = 0);
|
explicit Connection(Network *network, Client *parent);
|
||||||
|
|
||||||
Q_DISABLE_COPY(Connection)
|
Q_DISABLE_COPY(Connection)
|
||||||
Q_DECLARE_PRIVATE(Connection)
|
Q_DECLARE_PRIVATE(Connection)
|
||||||
|
|
|
||||||
|
|
@ -46,11 +46,11 @@ public:
|
||||||
|
|
||||||
void setQOS(qint32 prefetchSize, quint16 prefetchCount, int channel, bool global);
|
void setQOS(qint32 prefetchSize, quint16 prefetchCount, int channel, bool global);
|
||||||
|
|
||||||
QPointer<Client> client_;
|
QPointer<Client> client;
|
||||||
bool closed_;
|
QPointer<Network> network;
|
||||||
|
bool closed;
|
||||||
bool connected;
|
bool connected;
|
||||||
QPointer<QTimer> heartbeatTimer_;
|
QPointer<QTimer> heartbeatTimer;
|
||||||
|
|
||||||
Frame::TableField customProperty;
|
Frame::TableField customProperty;
|
||||||
|
|
||||||
Q_DECLARE_PUBLIC(Connection)
|
Q_DECLARE_PUBLIC(Connection)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue