2014-07-31 18:58:13 +08:00
|
|
|
/**
|
|
|
|
|
* DeferredGet.h
|
|
|
|
|
*
|
|
|
|
|
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
|
|
|
|
|
* @copyright 2014 Copernica BV
|
|
|
|
|
*/
|
|
|
|
|
|
2016-04-06 22:49:39 +08:00
|
|
|
/**
|
|
|
|
|
* Include guard
|
|
|
|
|
*/
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2016-06-23 20:42:50 +08:00
|
|
|
/**
|
|
|
|
|
* Dependencies
|
|
|
|
|
*/
|
|
|
|
|
#include "deferredconsumerbase.h"
|
|
|
|
|
|
2014-07-31 18:58:13 +08:00
|
|
|
/**
|
|
|
|
|
* Set up namespace
|
|
|
|
|
*/
|
|
|
|
|
namespace AMQP {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class definition
|
2016-06-23 20:42:50 +08:00
|
|
|
*
|
2014-09-02 16:32:55 +08:00
|
|
|
* This class implements the 'shared_from_this' functionality, because
|
|
|
|
|
* it grabs a self-pointer when the callback is running, otherwise the onFinalize()
|
|
|
|
|
* is called before the actual message is consumed.
|
2014-07-31 18:58:13 +08:00
|
|
|
*/
|
2016-06-23 20:42:50 +08:00
|
|
|
class DeferredGet : public DeferredConsumerBase
|
2014-07-31 18:58:13 +08:00
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
/**
|
|
|
|
|
* Callback in case the queue is empty
|
|
|
|
|
* @var EmptyCallback
|
|
|
|
|
*/
|
|
|
|
|
EmptyCallback _emptyCallback;
|
2016-06-23 20:42:50 +08:00
|
|
|
|
2014-08-28 16:02:01 +08:00
|
|
|
/**
|
|
|
|
|
* Callback with the number of messages still in the queue
|
|
|
|
|
* @var SizeCallback
|
|
|
|
|
*/
|
|
|
|
|
SizeCallback _sizeCallback;
|
2014-07-31 18:58:13 +08:00
|
|
|
|
|
|
|
|
/**
|
2016-06-23 20:42:50 +08:00
|
|
|
* Report success for a get operation
|
|
|
|
|
*
|
|
|
|
|
* @param messagecount Number of messages left in the queue
|
|
|
|
|
* @param deliveryTag Delivery tag of the message coming in
|
|
|
|
|
* @param redelivered Was the message redelivered?
|
2014-07-31 18:58:13 +08:00
|
|
|
*/
|
2016-06-23 20:42:50 +08:00
|
|
|
const std::shared_ptr<Deferred> &reportSuccess(uint32_t messagecount, uint64_t deliveryTag, bool redelivered) override;
|
2014-07-31 18:58:13 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Report success when queue was empty
|
|
|
|
|
* @return Deferred
|
|
|
|
|
*/
|
2014-09-02 16:32:55 +08:00
|
|
|
virtual const std::shared_ptr<Deferred> &reportSuccess() const override;
|
2016-06-23 20:42:50 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Emit a message
|
|
|
|
|
*
|
|
|
|
|
* @param message The message to emit
|
|
|
|
|
* @param deliveryTag The delivery tag (for ack()ing)
|
|
|
|
|
* @param redelivered Is this a redelivered message
|
|
|
|
|
*/
|
|
|
|
|
void emit(Message &&message, uint64_t deliveryTag, bool redelivered) const override;
|
|
|
|
|
|
2014-07-31 18:58:13 +08:00
|
|
|
/**
|
|
|
|
|
* The channel implementation may call our
|
|
|
|
|
* private members and construct us
|
|
|
|
|
*/
|
|
|
|
|
friend class ChannelImpl;
|
|
|
|
|
friend class ConsumedMessage;
|
|
|
|
|
|
2014-09-02 16:32:55 +08:00
|
|
|
public:
|
2014-07-31 18:58:13 +08:00
|
|
|
/**
|
|
|
|
|
* Protected constructor that can only be called
|
|
|
|
|
* from within the channel implementation
|
|
|
|
|
*
|
2014-09-02 16:32:55 +08:00
|
|
|
* Note: this constructor _should_ be protected, but because make_shared
|
|
|
|
|
* will then not work, we have decided to make it public after all,
|
|
|
|
|
* because the work-around would result in not-so-easy-to-read code.
|
2016-06-23 20:42:50 +08:00
|
|
|
*
|
2014-07-31 18:58:13 +08:00
|
|
|
* @param channel the channel implementation
|
|
|
|
|
* @param failed are we already failed?
|
|
|
|
|
*/
|
2016-06-23 20:42:50 +08:00
|
|
|
DeferredGet(ChannelImpl *channel, bool failed = false) :
|
|
|
|
|
DeferredConsumerBase(failed, channel) {}
|
2014-07-31 18:58:13 +08:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Register a function to be called when a message arrives
|
|
|
|
|
* This fuction is also available as onReceived() and onMessage() because I always forget which name I gave to it
|
|
|
|
|
* @param callback
|
|
|
|
|
*/
|
|
|
|
|
DeferredGet &onSuccess(const MessageCallback &callback)
|
|
|
|
|
{
|
|
|
|
|
// store the callback
|
|
|
|
|
_messageCallback = callback;
|
|
|
|
|
|
|
|
|
|
// allow chaining
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register a function to be called if no message could be fetched
|
|
|
|
|
* @param callback the callback to execute
|
|
|
|
|
*/
|
|
|
|
|
DeferredGet &onEmpty(const EmptyCallback &callback)
|
|
|
|
|
{
|
|
|
|
|
// store callback
|
|
|
|
|
_emptyCallback = callback;
|
2016-06-23 20:42:50 +08:00
|
|
|
|
2014-07-31 18:58:13 +08:00
|
|
|
// allow chaining
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2016-06-23 20:42:50 +08:00
|
|
|
|
2014-08-28 16:02:01 +08:00
|
|
|
/**
|
|
|
|
|
* Register a function to be called when size information is known
|
|
|
|
|
* @param callback the callback to execute
|
|
|
|
|
*/
|
|
|
|
|
DeferredGet &onSize(const SizeCallback &callback)
|
|
|
|
|
{
|
|
|
|
|
// store callback
|
|
|
|
|
_sizeCallback = callback;
|
2016-06-23 20:42:50 +08:00
|
|
|
|
2014-08-28 16:02:01 +08:00
|
|
|
// allow chaining
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2014-07-31 18:58:13 +08:00
|
|
|
};
|
2016-06-23 20:42:50 +08:00
|
|
|
|
2014-07-31 18:58:13 +08:00
|
|
|
/**
|
|
|
|
|
* End of namespace
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|