-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtopic.c
279 lines (236 loc) · 9.64 KB
/
topic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* BSD 3-Clause License
*
* Copyright (c) 2016, Arnaud Le Blanc (Author)
* Copyright (c) 2020, Nick Chiu
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* 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 HOLDER 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_simple_kafka_client_int.h"
#include "ext/spl/spl_iterators.h"
#include "Zend/zend_interfaces.h"
#include "Zend/zend_exceptions.h"
#include "ext/spl/spl_exceptions.h"
#include "topic_arginfo.h"
static zend_object_handlers object_handlers;
zend_class_entry * ce_kafka_consumer_topic;
zend_class_entry * ce_kafka_producer_topic;
zend_class_entry * ce_kafka_topic;
typedef struct _php_callback {
zend_fcall_info fci;
zend_fcall_info_cache fcc;
} php_callback;
static void kafka_topic_free(zend_object *object) /* {{{ */
{
kafka_topic_object *intern = php_kafka_from_obj(kafka_topic_object, object);
if (Z_TYPE(intern->zrk) != IS_UNDEF && intern->rkt) {
kafka_object *kafka_intern = get_kafka_object(&intern->zrk);
if (kafka_intern) {
zend_hash_index_del(&kafka_intern->topics, (zend_ulong)intern);
}
}
zend_object_std_dtor(&intern->std);
}
/* }}} */
static zend_object *kafka_topic_new(zend_class_entry *class_type) /* {{{ */
{
zend_object* retval;
kafka_topic_object *intern;
intern = ecalloc(1, sizeof(kafka_topic_object)+ zend_object_properties_size(class_type));
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
retval = &intern->std;
retval->handlers = &object_handlers;
return retval;
}
/* }}} */
kafka_topic_object * get_kafka_topic_object(zval *zrkt)
{
kafka_topic_object *orkt = Z_KAFKA_P(kafka_topic_object, zrkt);
if (!orkt->rkt) {
zend_throw_exception_ex(NULL, 0, "SimpleKafkaClient\\Topic::__construct() has not been called");
return NULL;
}
return orkt;
}
/* {{{ private constructor */
ZEND_METHOD(SimpleKafkaClient_ProducerTopic, __construct) {}
/* }}} */
/* {{{ proto void SimpleKafkaClient\ProducerTopic::produce(int $partition, int $msgflags[, string $payload, string $key])
Produce and send a single message to broker. */
ZEND_METHOD(SimpleKafkaClient_ProducerTopic, produce)
{
zend_long partition;
zend_long msgflags;
char *payload = NULL;
size_t payload_len = 0;
char *key = NULL;
size_t key_len = 0;
int ret;
rd_kafka_resp_err_t err;
kafka_topic_object *intern;
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 2, 4)
Z_PARAM_LONG(partition)
Z_PARAM_LONG(msgflags)
Z_PARAM_OPTIONAL
Z_PARAM_STRING_OR_NULL(payload, payload_len)
Z_PARAM_STRING_OR_NULL(key, key_len)
ZEND_PARSE_PARAMETERS_END();
if (partition != RD_KAFKA_PARTITION_UA && (partition < 0 || partition > 0x7FFFFFFF)) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Out of range value '%ld' for $partition", partition);
return;
}
if (msgflags != 0 && msgflags != RD_KAFKA_MSG_F_BLOCK) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Invalid value '%ld' for $msgflags", msgflags);
return;
}
intern = get_kafka_topic_object(getThis());
ret = rd_kafka_produce(intern->rkt, partition, msgflags | RD_KAFKA_MSG_F_COPY, payload, payload_len, key, key_len, NULL);
if (ret == -1) {
err = rd_kafka_last_error();
zend_throw_exception(ce_kafka_exception, rd_kafka_err2str(err), err);
return;
}
}
/* }}} */
/* {{{ proto void SimpleKafkaClient\ProducerTopic::producev(int $partition, int $msgflags[, string $payload, string $key, array $headers, int $timestamp_ms])
Produce and send a single message to broker (with headers possibility and timestamp). */
ZEND_METHOD(SimpleKafkaClient_ProducerTopic, producev)
{
zend_long partition;
zend_long msgflags;
char *payload = NULL;
size_t payload_len = 0;
char *key = NULL;
size_t key_len = 0;
rd_kafka_resp_err_t err;
kafka_topic_object *intern;
kafka_object *kafka_intern;
HashTable *headersParam = NULL;
HashPosition headersParamPos;
char *header_key;
zval *header_value;
rd_kafka_headers_t *headers;
zend_long timestamp_ms = 0;
zend_bool timestamp_ms_is_null = 0;
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 2, 6)
Z_PARAM_LONG(partition)
Z_PARAM_LONG(msgflags)
Z_PARAM_OPTIONAL
Z_PARAM_STRING_OR_NULL(payload, payload_len)
Z_PARAM_STRING_OR_NULL(key, key_len)
Z_PARAM_ARRAY_HT_OR_NULL(headersParam)
Z_PARAM_LONG_OR_NULL(timestamp_ms, timestamp_ms_is_null)
ZEND_PARSE_PARAMETERS_END();
if (partition != RD_KAFKA_PARTITION_UA && (partition < 0 || partition > 0x7FFFFFFF)) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Out of range value '%ld' for $partition", partition);
return;
}
if (msgflags != 0 && msgflags != RD_KAFKA_MSG_F_BLOCK) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Invalid value '%ld' for $msgflags", msgflags);
return;
}
if (timestamp_ms_is_null == 1) {
timestamp_ms = 0;
}
intern = get_kafka_topic_object(getThis());
if (headersParam != NULL && zend_hash_num_elements(headersParam) > 0) {
headers = rd_kafka_headers_new(zend_hash_num_elements(headersParam));
for (zend_hash_internal_pointer_reset_ex(headersParam, &headersParamPos);
(header_value = zend_hash_get_current_data_ex(headersParam, &headersParamPos)) != NULL &&
(header_key = kafka_hash_get_current_key_ex(headersParam, &headersParamPos)) != NULL;
zend_hash_move_forward_ex(headersParam, &headersParamPos)) {
convert_to_string_ex(header_value);
rd_kafka_header_add(
headers,
header_key,
-1, // Auto detect header title length
Z_STRVAL_P(header_value),
Z_STRLEN_P(header_value)
);
}
} else {
headers = rd_kafka_headers_new(0);
}
kafka_intern = get_kafka_object(&intern->zrk);
if (!kafka_intern) {
return;
}
err = rd_kafka_producev(
kafka_intern->rk,
RD_KAFKA_V_RKT(intern->rkt),
RD_KAFKA_V_PARTITION(partition),
RD_KAFKA_V_MSGFLAGS(msgflags | RD_KAFKA_MSG_F_COPY),
RD_KAFKA_V_VALUE(payload, payload_len),
RD_KAFKA_V_KEY(key, key_len),
RD_KAFKA_V_TIMESTAMP(timestamp_ms),
RD_KAFKA_V_HEADERS(headers),
RD_KAFKA_V_END
);
if (err != RD_KAFKA_RESP_ERR_NO_ERROR) {
rd_kafka_headers_destroy(headers);
zend_throw_exception(ce_kafka_exception, rd_kafka_err2str(err), err);
return;
}
}
/* }}} */
/* {{{ private constructor */
ZEND_METHOD(SimpleKafkaClient_ConsumerTopic, __construct) {}
/* }}} */
/* {{{ proto string SimpleKafkaClient\Topic::getName() */
ZEND_METHOD(SimpleKafkaClient_Topic, getName)
{
kafka_topic_object *intern;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
intern = get_kafka_topic_object(getThis());
if (!intern) {
return;
}
RETURN_STRING(rd_kafka_topic_name(intern->rkt));
}
/* }}} */
void kafka_topic_init(INIT_FUNC_ARGS) { /* {{{ */
zend_class_entry ce;
memcpy(&object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
object_handlers.clone_obj = NULL;
object_handlers.free_obj = kafka_topic_free;
object_handlers.offset = XtOffsetOf(kafka_topic_object, std);
INIT_NS_CLASS_ENTRY(ce, "SimpleKafkaClient", "Topic", class_SimpleKafkaClient_Topic_methods);
ce_kafka_topic = zend_register_internal_class(&ce);
ce_kafka_topic->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
ce_kafka_topic->create_object = kafka_topic_new;
INIT_NS_CLASS_ENTRY(ce, "SimpleKafkaClient", "ConsumerTopic", class_SimpleKafkaClient_ConsumerTopic_methods);
ce_kafka_consumer_topic = zend_register_internal_class_ex(&ce, ce_kafka_topic);
INIT_NS_CLASS_ENTRY(ce, "SimpleKafkaClient", "ProducerTopic", class_SimpleKafkaClient_ProducerTopic_methods);
ce_kafka_producer_topic = zend_register_internal_class_ex(&ce, ce_kafka_topic);
} /* }}} */