1/*
2 * Copyright (c) 2017, Matias Fontanini
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29#include "queue.h"
30#include "exceptions.h"
31
32using std::vector;
33using std::exception;
34using std::chrono::milliseconds;
35using std::allocator;
36
37namespace cppkafka {
38
39void dummy_deleter(rd_kafka_queue_t*) {
40
41}
42
43const milliseconds Queue::DEFAULT_TIMEOUT{1000};
44
45Queue Queue::make_non_owning(rd_kafka_queue_t* handle) {
46 return Queue(handle, NonOwningTag{});
47}
48
49Queue Queue::make_queue(rd_kafka_queue_t* handle) {
50 if (rd_kafka_version() <= RD_KAFKA_QUEUE_REFCOUNT_BUG_VERSION) {
51 return Queue::make_non_owning(handle);
52 }
53 else {
54 return Queue(handle);
55 }
56}
57
58Queue::Queue()
59: handle_(nullptr, nullptr),
60 timeout_ms_(DEFAULT_TIMEOUT) {
61
62}
63
64Queue::Queue(rd_kafka_queue_t* handle)
65: handle_(handle, &rd_kafka_queue_destroy),
66 timeout_ms_(DEFAULT_TIMEOUT) {
67
68}
69
70Queue::Queue(rd_kafka_queue_t* handle, NonOwningTag)
71: handle_(handle, &dummy_deleter) {
72
73}
74
75rd_kafka_queue_t* Queue::get_handle() const {
76 return handle_.get();
77}
78
79size_t Queue::get_length() const {
80 return rd_kafka_queue_length(handle_.get());
81}
82
83void Queue::forward_to_queue(const Queue& forward_queue) const {
84 return rd_kafka_queue_forward(handle_.get(), forward_queue.handle_.get());
85}
86
87void Queue::disable_queue_forwarding() const {
88 return rd_kafka_queue_forward(handle_.get(), nullptr);
89}
90
91void Queue::set_timeout(milliseconds timeout) {
92 timeout_ms_ = timeout;
93}
94
95milliseconds Queue::get_timeout() const {
96 return timeout_ms_;
97}
98
99Message Queue::consume() const {
100 return consume(timeout_ms_);
101}
102
103Message Queue::consume(milliseconds timeout) const {
104 return Message(rd_kafka_consume_queue(handle_.get(), static_cast<int>(timeout.count())));
105}
106
107vector<Message> Queue::consume_batch(size_t max_batch_size) const {
108 return consume_batch(max_batch_size, timeout_ms_, allocator<Message>());
109}
110
111vector<Message> Queue::consume_batch(size_t max_batch_size, milliseconds timeout) const {
112 return consume_batch(max_batch_size, timeout, allocator<Message>());
113}
114
115Event Queue::next_event() const {
116 return next_event(timeout_ms_);
117}
118
119Event Queue::next_event(milliseconds timeout) const {
120 return Event(rd_kafka_queue_poll(handle_.get(), timeout.count()));
121}
122
123} //cppkafka
124