2014-04-14 20:10:57 +08:00
|
|
|
/**
|
|
|
|
|
* DeferredConsumer.h
|
|
|
|
|
*
|
|
|
|
|
* Deferred callback for consumers
|
|
|
|
|
*
|
|
|
|
|
* @copyright 2014 Copernica BV
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set up namespace
|
|
|
|
|
*/
|
|
|
|
|
namespace AMQP {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* We extend from the default deferred and add extra functionality
|
|
|
|
|
*/
|
2014-04-15 16:43:33 +08:00
|
|
|
class DeferredConsumer : public Deferred
|
2014-04-14 20:10:57 +08:00
|
|
|
{
|
|
|
|
|
private:
|
2014-04-15 17:39:52 +08:00
|
|
|
/**
|
|
|
|
|
* The channel to which the consumer is linked
|
|
|
|
|
* @var ChannelImpl
|
|
|
|
|
*/
|
|
|
|
|
ChannelImpl *_channel;
|
|
|
|
|
|
2014-04-14 20:10:57 +08:00
|
|
|
/**
|
|
|
|
|
* Callback to execute when a message arrives
|
2014-04-15 17:39:52 +08:00
|
|
|
* @var ConsumeCallback
|
2014-04-14 20:10:57 +08:00
|
|
|
*/
|
2014-04-15 16:43:33 +08:00
|
|
|
ConsumeCallback _consumeCallback;
|
2014-04-14 20:10:57 +08:00
|
|
|
|
|
|
|
|
/**
|
2014-04-15 17:39:52 +08:00
|
|
|
* Callback for incoming messages
|
|
|
|
|
* @var MessageCallback
|
2014-04-14 20:10:57 +08:00
|
|
|
*/
|
2014-04-15 17:39:52 +08:00
|
|
|
MessageCallback _messageCallback;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Report success for frames that report start consumer operations
|
|
|
|
|
* @param name Consumer tag that is started
|
|
|
|
|
* @return Deferred
|
|
|
|
|
*/
|
|
|
|
|
virtual Deferred *reportSuccess(const std::string &name) const override;
|
2014-04-14 20:10:57 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The channel implementation may call our
|
|
|
|
|
* private members and construct us
|
|
|
|
|
*/
|
|
|
|
|
friend class ChannelImpl;
|
|
|
|
|
friend class ConsumedMessage;
|
2014-04-15 14:52:49 +08:00
|
|
|
|
2014-04-14 20:10:57 +08:00
|
|
|
protected:
|
|
|
|
|
/**
|
|
|
|
|
* Protected constructor that can only be called
|
|
|
|
|
* from within the channel implementation
|
|
|
|
|
*
|
2014-04-15 17:39:52 +08:00
|
|
|
* @param channel the channel implementation
|
|
|
|
|
* @param failed are we already failed?
|
2014-04-14 20:10:57 +08:00
|
|
|
*/
|
2014-04-15 17:39:52 +08:00
|
|
|
DeferredConsumer(ChannelImpl *channel, bool failed = false) :
|
|
|
|
|
Deferred(failed), _channel(channel) {}
|
2014-04-15 14:52:49 +08:00
|
|
|
|
2014-04-14 20:10:57 +08:00
|
|
|
public:
|
2014-04-15 17:39:52 +08:00
|
|
|
/**
|
|
|
|
|
* Register the function that is called when the consumer starts
|
|
|
|
|
* @param callback
|
|
|
|
|
*/
|
|
|
|
|
DeferredConsumer &onSuccess(const ConsumeCallback &callback)
|
|
|
|
|
{
|
|
|
|
|
// store the callback
|
|
|
|
|
_consumeCallback = callback;
|
|
|
|
|
|
|
|
|
|
// allow chaining
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-14 20:10:57 +08:00
|
|
|
/**
|
|
|
|
|
* Register a function to be called when a message arrives
|
2014-04-15 20:49:03 +08:00
|
|
|
* This fuction is also available as onMessage() because I always forget which name I gave to it
|
2014-04-14 20:10:57 +08:00
|
|
|
* @param callback the callback to execute
|
|
|
|
|
*/
|
2014-04-15 20:49:03 +08:00
|
|
|
DeferredConsumer &onReceived(const MessageCallback &callback)
|
|
|
|
|
{
|
|
|
|
|
// store callback
|
|
|
|
|
_messageCallback = callback;
|
|
|
|
|
|
|
|
|
|
// allow chaining
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register a function to be called when a message arrives
|
|
|
|
|
* This fuction is also available as onMessage() because I always forget which name I gave to it
|
|
|
|
|
* @param callback the callback to execute
|
|
|
|
|
*/
|
|
|
|
|
DeferredConsumer &onMessage(const MessageCallback &callback)
|
2014-04-14 20:10:57 +08:00
|
|
|
{
|
|
|
|
|
// store callback
|
2014-04-15 17:39:52 +08:00
|
|
|
_messageCallback = callback;
|
|
|
|
|
|
|
|
|
|
// allow chaining
|
2014-04-14 20:10:57 +08:00
|
|
|
return *this;
|
|
|
|
|
}
|
2014-04-15 16:43:33 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* All the onSuccess() functions defined in the base class are accessible too
|
|
|
|
|
*/
|
|
|
|
|
using Deferred::onSuccess;
|
2014-04-14 20:10:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End namespace
|
|
|
|
|
*/
|
|
|
|
|
}
|