2014-07-31 18:58:13 +08:00
|
|
|
/**
|
|
|
|
|
* DeferredGet.cpp
|
|
|
|
|
*
|
|
|
|
|
* Implementation of the DeferredGet call
|
|
|
|
|
*
|
|
|
|
|
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
|
|
|
|
|
* @copyright 2014 Copernica BV
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Dependencies
|
|
|
|
|
*/
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set up namespace
|
|
|
|
|
*/
|
|
|
|
|
namespace AMQP {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Report success, a get message succeeded and the message is expected soon
|
|
|
|
|
* @param messageCount Message count
|
|
|
|
|
* @return Deferred
|
|
|
|
|
*/
|
|
|
|
|
Deferred *DeferredGet::reportSuccess(uint32_t messageCount) const
|
|
|
|
|
{
|
|
|
|
|
// make copies of the callbacks
|
|
|
|
|
auto messageCallback = _messageCallback;
|
|
|
|
|
auto finalizeCallback = _finalizeCallback;
|
2014-08-01 16:05:02 +08:00
|
|
|
auto *channel = _channel;
|
2014-08-28 16:02:01 +08:00
|
|
|
|
|
|
|
|
// install a monitor because the channel could be destructed
|
|
|
|
|
Monitor monitor(channel);
|
|
|
|
|
|
|
|
|
|
// report the size (technically, the channel object could be destructed now, but we ignore that case)
|
|
|
|
|
if (_sizeCallback) _sizeCallback(messageCount);
|
2014-07-31 18:58:13 +08:00
|
|
|
|
|
|
|
|
// we now know the name, so we can install the message callback on the channel
|
2014-08-01 16:05:02 +08:00
|
|
|
_channel->install("", [channel, messageCallback, finalizeCallback](const Message &message, uint64_t deliveryTag, bool redelivered) {
|
|
|
|
|
|
2014-08-04 19:44:17 +08:00
|
|
|
// install a monitor to deal with the case that the channel is removed
|
|
|
|
|
Monitor monitor(channel);
|
|
|
|
|
|
2014-07-31 18:58:13 +08:00
|
|
|
// call the callbacks
|
|
|
|
|
if (messageCallback) messageCallback(message, deliveryTag, redelivered);
|
|
|
|
|
|
|
|
|
|
// call the finalize callback
|
|
|
|
|
if (finalizeCallback) finalizeCallback();
|
2014-08-04 19:44:17 +08:00
|
|
|
|
|
|
|
|
// we can remove the callback now from the channel
|
|
|
|
|
if (monitor.valid()) channel->uninstall("");
|
2014-07-31 18:58:13 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// return next object
|
|
|
|
|
return _next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Report success, although no message could be get
|
|
|
|
|
*/
|
|
|
|
|
Deferred *DeferredGet::reportSuccess() const
|
|
|
|
|
{
|
2014-08-28 16:02:01 +08:00
|
|
|
// report the size
|
|
|
|
|
if (_sizeCallback) _sizeCallback(0);
|
|
|
|
|
|
2014-07-31 18:58:13 +08:00
|
|
|
// check if a callback was set
|
|
|
|
|
if (_emptyCallback) _emptyCallback();
|
|
|
|
|
|
|
|
|
|
// call finalize callback
|
|
|
|
|
if (_finalizeCallback) _finalizeCallback();
|
|
|
|
|
|
|
|
|
|
// return next object
|
|
|
|
|
return _next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End of namespace
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|