mirror of
https://github.com/Telecominfraproject/wlan-cloud-lib-cppkafka.git
synced 2026-01-27 10:22:42 +00:00
90 lines
3.4 KiB
C++
90 lines
3.4 KiB
C++
/*
|
|
* Copyright (c) 2016, Matias Fontanini
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are
|
|
* met:
|
|
*
|
|
* * Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* * Redistributions in binary form must reproduce the above
|
|
* copyright notice, this list of conditions and the following disclaimer
|
|
* in the documentation and/or other materials provided with the
|
|
* distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
*/
|
|
|
|
#include <errno.h>
|
|
#include "producer.h"
|
|
#include "exceptions.h"
|
|
|
|
using std::move;
|
|
using std::string;
|
|
|
|
namespace cppkafka {
|
|
|
|
Producer::Producer(Configuration config)
|
|
: KafkaHandleBase(move(config)) {
|
|
char error_buffer[512];
|
|
auto config_handle = get_configuration().get_handle();
|
|
rd_kafka_conf_set_opaque(config_handle, this);
|
|
rd_kafka_t* ptr = rd_kafka_new(RD_KAFKA_PRODUCER,
|
|
rd_kafka_conf_dup(config_handle),
|
|
error_buffer, sizeof(error_buffer));
|
|
if (!ptr) {
|
|
throw Exception("Failed to create producer handle: " + string(error_buffer));
|
|
}
|
|
rd_kafka_set_log_level(ptr, 7);
|
|
set_handle(ptr);
|
|
set_payload_policy(Producer::COPY_PAYLOAD);
|
|
}
|
|
|
|
void Producer::set_payload_policy(PayloadPolicy policy) {
|
|
message_payload_policy_ = policy;
|
|
}
|
|
|
|
Producer::PayloadPolicy Producer::get_payload_policy() const {
|
|
return message_payload_policy_;
|
|
}
|
|
|
|
void Producer::produce(const Topic& topic, const Partition& partition, const Buffer& payload) {
|
|
produce(topic, partition, payload, Buffer{} /*key*/, nullptr /*user_data*/);
|
|
}
|
|
|
|
void Producer::produce(const Topic& topic, const Partition& partition, const Buffer& payload,
|
|
const Buffer& key) {
|
|
produce(topic, partition, payload, key, nullptr /*user_data*/);
|
|
}
|
|
|
|
void Producer::produce(const Topic& topic, const Partition& partition, const Buffer& payload,
|
|
const Buffer& key, void* user_data) {
|
|
void* payload_ptr = (void*)payload.get_data();
|
|
void* key_ptr = (void*)key.get_data();
|
|
const int policy = static_cast<int>(message_payload_policy_);
|
|
int result = rd_kafka_produce(topic.get_handle(), partition.get_partition(),
|
|
policy, payload_ptr, payload.get_size(),
|
|
key_ptr, key.get_size(), user_data);
|
|
if (result == -1) {
|
|
throw HandleException(rd_kafka_errno2err(errno));
|
|
}
|
|
}
|
|
|
|
int Producer::poll() {
|
|
return rd_kafka_poll(get_handle(), get_timeout().count());
|
|
}
|
|
|
|
} // cppkafka
|