Add DeferredConfirm class

This commit is contained in:
Marcin Gibula 2018-05-14 21:05:49 +02:00
parent a90c620709
commit bf1caa1eae
3 changed files with 134 additions and 0 deletions

View File

@ -0,0 +1,93 @@
/**
* DeferredDelete.h
*
* Deferred callback for RabbitMQ-specific publisher confirms mechanism.
*
* @author Marcin Gibula <m.gibula@gmail.com>
* @copyright 2018 Copernica BV
*/
/**
* Include guard
*/
#pragma once
/**
* Set up namespace
*/
namespace AMQP {
/**
* We extend from the default deferred and add extra functionality
*/
class DeferredConfirm : public Deferred
{
private:
/**
* Callback to execute when server confirms that message is processed
* @var AckCallback
*/
AckCallback _ackCallback;
/**
* Callback to execute when server sends negative acknowledgement
* @var NackCallback
*/
NackCallback _nackCallback;
/**
* Process an ACK frame
*
* @param frame The frame to process
*/
void process(BasicAckFrame &frame);
/**
* Process an ACK frame
*
* @param frame The frame to process
*/
void process(BasicNackFrame &frame);
/**
* The channel implementation may call our
* private members and construct us
*/
friend class ChannelImpl;
friend class BasicAckFrame;
friend class BasicNackFrame;
public:
/**
* Protected constructor that can only be called
* from within the channel implementation
*
* 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.
*
* @param boolean are we already failed?
*/
DeferredConfirm(bool failed = false) : Deferred(failed) {}
public:
/**
* Register the function that is called when channel is put in publisher
* confirmed mode
* @param callback
*/
DeferredConfirm &onSuccess(const SuccessCallback &callback)
{
// call base
Deferred::onSuccess(callback);
// allow chaining
return *this;
}
};
/**
* End namespace
*/
}

40
src/deferredconfirm.cpp Normal file
View File

@ -0,0 +1,40 @@
/**
* DeferredConfirm.cpp
*
* Implementation file for the DeferredConfirm class
*
* @author Marcin Gibula <m.gibula@gmail.com>
* @copyright 2018 Copernica BV
*/
#include "includes.h"
/**
* Namespace
*/
namespace AMQP {
/**
* Process an ACK frame
*
* @param frame The frame to process
*/
void DeferredConfirm::process(BasicAckFrame &frame)
{
if (_ackCallback) _ackCallback(frame.deliveryTag(), frame.multiple());
}
/**
* Process a NACK frame
*
* @param frame The frame to process
*/
void DeferredConfirm::process(BasicNackFrame &frame)
{
if (_nackCallback) _nackCallback(frame.deliveryTag(), frame.multiple(), frame.requeue());
}
/**
* End namespace
*/
}

View File

@ -72,6 +72,7 @@
#include "amqpcpp/deferredqueue.h"
#include "amqpcpp/deferreddelete.h"
#include "amqpcpp/deferredcancel.h"
#include "amqpcpp/deferredconfirm.h"
#include "amqpcpp/deferredget.h"
#include "amqpcpp/channelimpl.h"
#include "amqpcpp/channel.h"