mirror of
https://github.com/Telecominfraproject/wlan-cloud-lib-cppkafka.git
synced 2025-11-02 11:37:50 +00:00
Add offset commit and delivery report callbacks
This commit is contained in:
39
include/cppkafka/clonable_ptr.h
Normal file
39
include/cppkafka/clonable_ptr.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef CPPKAFKA_CLONABLE_PTR_H
|
||||
#define CPPKAFKA_CLONABLE_PTR_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace cppkafka {
|
||||
|
||||
template <typename T, typename Deleter, typename Cloner>
|
||||
class ClonablePtr {
|
||||
public:
|
||||
ClonablePtr(T* ptr, const Deleter& deleter, const Cloner& cloner)
|
||||
: handle_(ptr, deleter), cloner_(cloner) {
|
||||
|
||||
}
|
||||
|
||||
ClonablePtr(const ClonablePtr& rhs)
|
||||
: handle_(rhs.cloner_(rhs.handle_.get()), rhs.handle_.get_deleter()), cloner_(rhs.cloner_) {
|
||||
|
||||
}
|
||||
|
||||
ClonablePtr& operator=(const ClonablePtr& rhs) {
|
||||
handle_.reset(cloner_(rhs.handle_.get()));
|
||||
}
|
||||
|
||||
ClonablePtr(ClonablePtr&&) = default;
|
||||
ClonablePtr& operator=(ClonablePtr&&) = default;
|
||||
~ClonablePtr() = default;
|
||||
|
||||
T* get() const {
|
||||
return handle_.get();
|
||||
}
|
||||
private:
|
||||
std::unique_ptr<T, Deleter> handle_;
|
||||
Cloner cloner_;
|
||||
};
|
||||
|
||||
} // cppkafka
|
||||
|
||||
#endif // CPPKAFKA_CLONABLE_PTR_H
|
||||
@@ -3,28 +3,40 @@
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <librdkafka/rdkafka.h>
|
||||
#include "topic_partition_list.h"
|
||||
#include "clonable_ptr.h"
|
||||
|
||||
namespace cppkafka {
|
||||
|
||||
class Message;
|
||||
|
||||
class Configuration {
|
||||
public:
|
||||
using DeliveryReportCallback = std::function<void(const Message&)>;
|
||||
using OffsetCommitCallback = std::function<void(rd_kafka_resp_err_t,
|
||||
const TopicPartitionList& topic_partitions)>;
|
||||
|
||||
Configuration();
|
||||
Configuration(const Configuration& rhs);
|
||||
Configuration(Configuration&& rhs) noexcept = default;
|
||||
Configuration& operator=(const Configuration& rhs);
|
||||
Configuration& operator=(Configuration&& rhs) noexcept = default;
|
||||
|
||||
void set(const std::string& name, const std::string& value);
|
||||
void set_delivery_report_callback(DeliveryReportCallback callback);
|
||||
void set_offset_commit_callback(OffsetCommitCallback callback);
|
||||
|
||||
rd_kafka_conf_t* get_handle() const;
|
||||
const DeliveryReportCallback& get_delivery_report_callback() const;
|
||||
const OffsetCommitCallback& get_offset_commit_callback() const;
|
||||
private:
|
||||
using HandlePtr = std::unique_ptr<rd_kafka_conf_t, decltype(&rd_kafka_conf_destroy)>;
|
||||
using HandlePtr = ClonablePtr<rd_kafka_conf_t, decltype(&rd_kafka_conf_destroy),
|
||||
decltype(&rd_kafka_conf_dup)>;
|
||||
|
||||
Configuration(rd_kafka_conf_t* ptr);
|
||||
static HandlePtr make_handle(rd_kafka_conf_t* ptr);
|
||||
|
||||
HandlePtr handle_;
|
||||
DeliveryReportCallback delivery_report_callback_;
|
||||
OffsetCommitCallback offset_commit_callback_;
|
||||
};
|
||||
|
||||
} // cppkafka
|
||||
|
||||
@@ -7,10 +7,10 @@
|
||||
#include <functional>
|
||||
#include "kafka_handle_base.h"
|
||||
#include "message.h"
|
||||
#include "configuration.h"
|
||||
|
||||
namespace cppkafka {
|
||||
|
||||
class Configuration;
|
||||
class TopicConfiguration;
|
||||
|
||||
class Consumer : public KafkaHandleBase {
|
||||
@@ -46,6 +46,7 @@ public:
|
||||
TopicPartitionList get_offsets_position(const TopicPartitionList& topic_partitions);
|
||||
TopicPartitionList get_subscription();
|
||||
TopicPartitionList get_assignment();
|
||||
const Configuration& get_configuration() const;
|
||||
|
||||
Message poll();
|
||||
private:
|
||||
@@ -59,6 +60,7 @@ private:
|
||||
AssignmentCallback assignment_callback_;
|
||||
RevocationCallback revocation_callback_;
|
||||
RebalanceErrorCallback rebalance_error_callback_;
|
||||
Configuration config_;
|
||||
};
|
||||
|
||||
} // cppkafka
|
||||
|
||||
@@ -11,6 +11,8 @@ namespace cppkafka {
|
||||
|
||||
class Message {
|
||||
public:
|
||||
static Message make_non_owning(rd_kafka_message_t* handle);
|
||||
|
||||
Message();
|
||||
Message(rd_kafka_message_t* handle);
|
||||
Message(const Message&) = delete;
|
||||
@@ -32,6 +34,11 @@ public:
|
||||
private:
|
||||
using HandlePtr = std::unique_ptr<rd_kafka_message_t, decltype(&rd_kafka_message_destroy)>;
|
||||
|
||||
struct NonOwningTag { };
|
||||
|
||||
Message(rd_kafka_message_t* handle, NonOwningTag);
|
||||
Message(HandlePtr handle);
|
||||
|
||||
HandlePtr handle_;
|
||||
Buffer payload_;
|
||||
Buffer key_;
|
||||
|
||||
@@ -27,11 +27,15 @@ public:
|
||||
void set_payload_policy(PayloadPolicy policy);
|
||||
PayloadPolicy get_payload_policy() const;
|
||||
|
||||
const Configuration& get_configuration() const;
|
||||
|
||||
void produce(const Topic& topic, const Partition& partition, const Buffer& payload);
|
||||
void produce(const Topic& topic, const Partition& partition, const Buffer& payload,
|
||||
const Buffer& key);
|
||||
void produce(const Topic& topic, const Partition& partition, const Buffer& payload,
|
||||
const Buffer& key, void* user_data);
|
||||
|
||||
int poll();
|
||||
private:
|
||||
Configuration config_;
|
||||
PayloadPolicy message_payload_policy_;
|
||||
|
||||
@@ -15,6 +15,7 @@ using TopicPartitionList = std::vector<TopicPartition>;
|
||||
|
||||
TopicPartitionsListPtr convert(const std::vector<TopicPartition>& topic_partitions);
|
||||
std::vector<TopicPartition> convert(const TopicPartitionsListPtr& topic_partitions);
|
||||
std::vector<TopicPartition> convert(rd_kafka_topic_partition_list_t* topic_partitions);
|
||||
TopicPartitionsListPtr make_handle(rd_kafka_topic_partition_list_t* handle);
|
||||
|
||||
} // cppkafka
|
||||
|
||||
Reference in New Issue
Block a user