1 | /* |
2 | * Copyright (c) 2018, 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 | |
30 | #include "event.h" |
31 | |
32 | using std::allocator; |
33 | using std::string; |
34 | using std::unique_ptr; |
35 | using std::vector; |
36 | |
37 | namespace cppkafka { |
38 | |
39 | Event::Event(rd_kafka_event_t* handle) |
40 | : handle_(handle, &rd_kafka_event_destroy) { |
41 | |
42 | } |
43 | |
44 | string Event::get_name() const { |
45 | return rd_kafka_event_name(handle_.get()); |
46 | } |
47 | |
48 | rd_kafka_event_type_t Event::get_type() const { |
49 | return rd_kafka_event_type(handle_.get()); |
50 | } |
51 | |
52 | Message Event::get_next_message() const { |
53 | // Note: the constness in rd_kafka_event_message_next's return value is not needed and it |
54 | // breaks Message's interface. This is dirty but it looks like it should have no side effects. |
55 | const auto message = |
56 | const_cast<rd_kafka_message_t*>(rd_kafka_event_message_next(handle_.get())); |
57 | return Message::make_non_owning(message); |
58 | } |
59 | |
60 | vector<Message> Event::get_messages() { |
61 | return get_messages(allocator<Message>()); |
62 | } |
63 | |
64 | size_t Event::get_message_count() const { |
65 | return rd_kafka_event_message_count(handle_.get()); |
66 | } |
67 | |
68 | Error Event::get_error() const { |
69 | return rd_kafka_event_error(handle_.get()); |
70 | } |
71 | |
72 | void* Event::get_opaque() const { |
73 | return rd_kafka_event_opaque(handle_.get()); |
74 | } |
75 | |
76 | TopicPartition Event::get_topic_partition() const { |
77 | using TopparHandle = unique_ptr<rd_kafka_topic_partition_t, |
78 | decltype(&rd_kafka_topic_partition_destroy)>; |
79 | TopparHandle toppar_handle{rd_kafka_event_topic_partition(handle_.get()), |
80 | &rd_kafka_topic_partition_destroy}; |
81 | return TopicPartition(toppar_handle->topic, toppar_handle->partition, toppar_handle->offset); |
82 | } |
83 | |
84 | TopicPartitionList Event::get_topic_partition_list() const { |
85 | auto toppars_handle = rd_kafka_event_topic_partition_list(handle_.get()); |
86 | return convert(toppars_handle); |
87 | } |
88 | |
89 | Event::operator bool() const { |
90 | return !!handle_; |
91 | } |
92 | |
93 | } // cppkafka |
94 | |