RabbitMQ.xs

filter: Statementstmt 16.0% 133 / 829 Branchbran 17.2% 91 / 528 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 16.5% 224 / 1357 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« amqp_api.c ↑ Summary amqp_openssl.c »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include <float.h>
5 #include <math.h>
6  
7 /* perl -MDevel::PPPort -e'Devel::PPPort::WriteFile();' */
8 /* perl ppport.h --compat-version=5.8.0 --cplusplus RabbitMQ.xs */
9 #define NEED_newSVpvn_flags
10 #include "ppport.h"
11  
12 /* ppport.h knows about MUTABLE_PTR and MUTABLE_SV, but not these?! */
13 #ifndef MUTABLE_AV
14 # define MUTABLE_AV(p) ((AV*)MUTABLE_PTR(p))
15 #endif
16 #ifndef MUTABLE_HV
17 # define MUTABLE_HV(p) ((HV*)MUTABLE_PTR(p))
18 #endif
19  
20 #include "amqp.h"
21 #include "amqp_tcp_socket.h"
22 #include "amqp_ssl_socket.h"
23 /* For struct timeval */
24 #include "amqp_time.h"
25  
26 /* This is for the Math::UInt64 integration */
27 #include "perl_math_int64.h"
28  
29 /* perl Makefile.PL; make CCFLAGS=-DDEBUG */
30 #if DEBUG
31 #define __DEBUG__(X) X
32 #else
33 #define __DEBUG__(X) /* NOOP */
34 #endif
35  
36 typedef amqp_connection_state_t Net__AMQP__RabbitMQ;
37  
38 #define AMQP_STATUS_UNKNOWN_TYPE 0x500
39  
40 #ifdef USE_LONG_DOUBLE
41 /* stolen from Cpanel::JSON::XS
42 * so we don't mess up double => long double for perls with -Duselongdouble */
43 #if defined(_AIX) && (!defined(HAS_LONG_DOUBLE) || AIX_WORKAROUND)
44 #define HAVE_NO_POWL
45 #endif
46  
47 #ifdef HAVE_NO_POWL
48 /* Ulisse Monari: this is a patch for AIX 5.3, perl 5.8.8 without HAS_LONG_DOUBLE
49 There Perl_pow maps to pow(...) - NOT TO powl(...), core dumps at Perl_pow(...)
50 Base code is from http://bytes.com/topic/c/answers/748317-replacement-pow-function
51 This is my change to fs_pow that goes into libc/libm for calling fmod/exp/log.
52 NEED TO MODIFY Makefile, after perl Makefile.PL by adding "-lm" onto the LDDLFLAGS line */
53 static double fs_powEx(double x, double y)
54 {
55 double p = 0;
56  
57 if (0 > x && fmod(y, 1) == 0) {
58 if (fmod(y, 2) == 0) {
59 p = exp(log(-x) * y);
60 } else {
61 p = -exp(log(-x) * y);
62 }
63 } else {
64 if (x != 0 || 0 >= y) {
65 p = exp(log( x) * y);
66 }
67 }
68 return p;
69 }
70  
71 /* powf() unfortunately is not accurate enough */
72 const NV DOUBLE_POW = fs_powEx(10., DBL_DIG );
73 #else
74 const NV DOUBLE_POW = Perl_pow(10., DBL_DIG );
75 #endif
76 #endif
77  
78  
79 /* This is a place to put some stuff that we convert from perl,
80 it's transient and we recycle it as soon as it's finished being used
81 which means we keep memory we've used with the aim of reusing it */
82 /* temp_memory_pool is ugly and suffers from code smell */
83 static amqp_pool_t temp_memory_pool;
84  
85 /* Parallels amqp_maybe_release_buffers */
86 0 static void maybe_release_buffers(amqp_connection_state_t state) {
87 0 ▶ if (amqp_release_buffers_ok(state)) {
Branch: 1 branch
BranchTrueFalseTotal
if (amqp_release_buffers_ok(state)) { 0 0 0
88 0 amqp_release_buffers(state);
89 0 recycle_amqp_pool(&temp_memory_pool);
90 }
91 0 }
92  
93 #define int_from_hv(hv,name) \
94 do { SV **v; if(NULL != (v = hv_fetchs(hv, #name, 0))) name = SvIV(*v); } while(0)
95 #define double_from_hv(hv,name) \
96 do { SV **v; if(NULL != (v = hv_fetchs(hv, #name, 0))) name = SvNV(*v); } while(0)
97 #define str_from_hv(hv,name) \
98 do { SV **v; if(NULL != (v = hv_fetchs(hv, #name, 0))) name = SvPV_nolen(*v); } while(0)
99 #define has_valid_connection(conn) \
100 ( amqp_get_socket( conn ) != NULL && amqp_get_sockfd( conn ) > -1 )
101 #define assert_amqp_connected(conn) \
102 do { \
103 if ( ! has_valid_connection(conn) ) { \
104 Perl_croak(aTHX_ "AMQP socket not connected"); \
105 } \
106 } while(0)
107  
108 void hash_to_amqp_table(HV *hash, amqp_table_t *table, short force_utf8);
109 void array_to_amqp_array(AV *perl_array, amqp_array_t *mq_array, short force_utf8);
110 SV* mq_array_to_arrayref(amqp_array_t *array);
111 SV* mq_table_to_hashref(amqp_table_t *table);
112  
113 30 void die_on_error(pTHX_ int x, amqp_connection_state_t conn, char const *context) {
114 /* Handle socket errors */
115 30 ▶ if ( x == AMQP_STATUS_CONNECTION_CLOSED || x == AMQP_STATUS_SOCKET_ERROR ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( x == AMQP_STATUS_CONNECTION_CLOSED || x == AMQP_STATUS_SOCKET_ERROR ) { 30 0 30
if ( x == AMQP_STATUS_CONNECTION_CLOSED || x == AMQP_STATUS_SOCKET_ERROR ) { 0 30 30
116 0 amqp_socket_close( amqp_get_socket( conn ) );
117 0 Perl_croak(aTHX_ "%s failed because AMQP socket connection was closed.", context);
118 }
119 /* Handle everything else */
120 30 ▶ else if (x < 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (x < 0) { 2 28 30
121 2 Perl_croak(aTHX_ "%s: %s\n", context, amqp_error_string2(x));
122 }
123 28 }
124  
125 28 void die_on_amqp_error(pTHX_ amqp_rpc_reply_t x, amqp_connection_state_t conn, char const *context) {
126 28 switch (x.reply_type) {
127 0 case AMQP_RESPONSE_NORMAL:
128 0 return;
129  
130 0 case AMQP_RESPONSE_NONE:
131 0 Perl_croak(aTHX_ "%s: missing RPC reply type!", context);
132 break;
133  
134 0 case AMQP_RESPONSE_LIBRARY_EXCEPTION:
135 /* If we got a library error saying that there's a socket problem,
136 kill the connection and croak. */
137 0 if (
138 0 ▶ x.library_error == AMQP_STATUS_CONNECTION_CLOSED
Branch: 1 branch
BranchTrueFalseTotal
x.library_error == AMQP_STATUS_CONNECTION_CLOSED 0 0 0
139 0 ||
140 0 ▶ x.library_error == AMQP_STATUS_SOCKET_ERROR
Branch: 1 branch
BranchTrueFalseTotal
x.library_error == AMQP_STATUS_SOCKET_ERROR 0 0 0
141 ) {
142 0 amqp_socket_close( amqp_get_socket( conn ) );
143 0 Perl_croak(aTHX_ "%s: failed since AMQP socket connection closed.\n", context);
144 }
145 /* Otherwise, give a more generic croak. */
146 else {
147 0 Perl_croak(aTHX_ "%s: %s\n", context,
148 0 ▶ (!x.library_error) ? "(end-of-stream)" :
Branch: 1 branch
BranchTrueFalseTotal
(!x.library_error) ? "(end-of-stream)" : 0 0 0
149 0 ▶ (x.library_error == AMQP_STATUS_UNKNOWN_TYPE) ? "unknown AMQP type id" :
Branch: 1 branch
BranchTrueFalseTotal
(x.library_error == AMQP_STATUS_UNKNOWN_TYPE) ? "unknown AMQP type id" : 0 0 0
150 0 amqp_error_string2(x.library_error));
151 }
152 break;
153  
154 28 case AMQP_RESPONSE_SERVER_EXCEPTION:
155 28 switch (x.reply.id) {
156 28 case AMQP_CONNECTION_CLOSE_METHOD:
157 {
158 amqp_connection_close_ok_t req;
159 28 req.dummy = '\0';
160 28 /* res = */ amqp_send_method(conn, 0, AMQP_CONNECTION_CLOSE_OK_METHOD, &req);
161 }
162 28 amqp_set_socket(conn, NULL);
163 {
164 28 amqp_connection_close_t *m = (amqp_connection_close_t *) x.reply.decoded;
165 28 Perl_croak(aTHX_ "%s: server connection error %d, message: %.*s",
166 context,
167 28 m->reply_code,
168 28 (int) m->reply_text.len, (char *) m->reply_text.bytes);
169 }
170 break;
171  
172 0 case AMQP_CHANNEL_CLOSE_METHOD:
173 /* We don't know what channel provoked this error!
174 This information should be in amqp_rpc_reply_t, but it isn't.
175 {
176 amqp_channel_close_ok_t req;
177 req.dummy = '\0';
178 / * res = * / amqp_send_method(conn, channel, AMQP_CHANNEL_CLOSE_OK_METHOD, &req);
179 }
180 */
181 /* Only the channel should be invalidated, but we have no means of doing so! */
182 /* Even if we knew which channel we needed to invalidate! */
183 0 amqp_set_socket(conn, NULL);
184 {
185 0 amqp_channel_close_t *m = (amqp_channel_close_t *) x.reply.decoded;
186 0 Perl_croak(aTHX_ "%s: server channel error %d, message: %.*s",
187 context,
188 0 m->reply_code,
189 0 (int) m->reply_text.len, (char *) m->reply_text.bytes);
190 }
191 break;
192  
193 0 default:
194 0 Perl_croak(aTHX_ "%s: unknown server error, method id 0x%08X", context, x.reply.id);
195 break;
196 }
197 break;
198 }
199 }
200  
201 /*
202 * amqp_kind_for_sv(SV**)
203 * Note: We could handle more types here... but we're trying to take Perl and go to
204 * C. We don't really need to handle much more than this from what I can tell.
205 */
206 0 amqp_field_value_kind_t amqp_kind_for_sv(SV** perl_value, short force_utf8) {
207  
208 0 switch (SvTYPE( *perl_value ))
209 {
210 // Integer types (and references beyond 5.10)
211 0 case SVt_IV:
212 // References
213 0 ▶ if ( SvROK( *perl_value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvROK( *perl_value ) ) { 0 0 0
214 // Array Reference
215 0 ▶ if ( SvTYPE( SvRV( *perl_value ) ) == SVt_PVAV ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvTYPE( SvRV( *perl_value ) ) == SVt_PVAV ) { 0 0 0
216 0 return AMQP_FIELD_KIND_ARRAY;
217 }
218  
219 // Hash Reference
220 0 ▶ if ( SvTYPE( SvRV( *perl_value ) ) == SVt_PVHV ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvTYPE( SvRV( *perl_value ) ) == SVt_PVHV ) { 0 0 0
221 0 return AMQP_FIELD_KIND_TABLE;
222 }
223 0 Perl_croak(
224 aTHX_ "Unsupported Perl Reference Type: %d",
225 0 SvTYPE( SvRV( *perl_value ) )
226 );
227 }
228  
229 // Regular integers
230 // In the event that it could be unsigned
231 0 ▶ if ( SvUOK( *perl_value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvUOK( *perl_value ) ) { 0 0 0
232 0 return AMQP_FIELD_KIND_U64;
233 }
234 0 return AMQP_FIELD_KIND_I64;
235  
236 // Numeric type
237 0 case SVt_NV:
238 0 return AMQP_FIELD_KIND_F64;
239  
240 // String (handle types which are upgraded to handle IV/UV/NV as well as PV)
241 0 case SVt_PVIV:
242 0 ▶ if ( SvI64OK( *perl_value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvI64OK( *perl_value ) ) { 0 0 0
243 0 return AMQP_FIELD_KIND_I64;
244 }
245 0 ▶ if ( SvU64OK( *perl_value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvU64OK( *perl_value ) ) { 0 0 0
246 0 return AMQP_FIELD_KIND_U64;
247 }
248 // It could be a PV or an IV/UV!
249 0 ▶ if ( SvIOK( *perl_value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvIOK( *perl_value ) ) { 0 0 0
250 0 ▶ if ( SvUOK( *perl_value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvUOK( *perl_value ) ) { 0 0 0
251 0 return AMQP_FIELD_KIND_U64;
252 }
253 0 return AMQP_FIELD_KIND_I64;
254 }
255  
256 case SVt_PVNV:
257 // It could be a PV or an NV
258 0 ▶ if ( SvNOK( *perl_value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvNOK( *perl_value ) ) { 0 0 0
259 0 return AMQP_FIELD_KIND_F64;
260 }
261  
262 case SVt_PV:
263 // UTF-8?
264 0 ▶ if ( force_utf8 || SvUTF8( *perl_value ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( force_utf8 || SvUTF8( *perl_value ) ) { 0 0 0
if ( force_utf8 || SvUTF8( *perl_value ) ) { 0 0 0
265 0 return AMQP_FIELD_KIND_UTF8;
266 }
267 0 return AMQP_FIELD_KIND_BYTES;
268  
269 0 case SVt_PVMG:
270 0 ▶ if ( SvPOK( *perl_value ) || SvPOKp( *perl_value ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( SvPOK( *perl_value ) || SvPOKp( *perl_value ) ) { 0 0 0
if ( SvPOK( *perl_value ) || SvPOKp( *perl_value ) ) { 0 0 0
271 0 ▶ if ( force_utf8 || SvUTF8( *perl_value ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( force_utf8 || SvUTF8( *perl_value ) ) { 0 0 0
if ( force_utf8 || SvUTF8( *perl_value ) ) { 0 0 0
272 0 return AMQP_FIELD_KIND_UTF8;
273 }
274 0 return AMQP_FIELD_KIND_BYTES;
275 }
276 0 ▶ if ( SvIOK( *perl_value ) || SvIOKp( *perl_value ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( SvIOK( *perl_value ) || SvIOKp( *perl_value ) ) { 0 0 0
if ( SvIOK( *perl_value ) || SvIOKp( *perl_value ) ) { 0 0 0
277 0 ▶ if ( SvUOK( *perl_value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvUOK( *perl_value ) ) { 0 0 0
278 0 return AMQP_FIELD_KIND_U64;
279 }
280 0 return AMQP_FIELD_KIND_I64;
281 }
282 0 ▶ if ( SvNOK( *perl_value ) || SvNOKp( *perl_value ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( SvNOK( *perl_value ) || SvNOKp( *perl_value ) ) { 0 0 0
if ( SvNOK( *perl_value ) || SvNOKp( *perl_value ) ) { 0 0 0
283 0 return AMQP_FIELD_KIND_F64;
284 }
285  
286 default:
287 0 ▶ if ( SvROK( *perl_value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvROK( *perl_value ) ) { 0 0 0
288 // Array Reference
289 0 ▶ if ( SvTYPE( SvRV( *perl_value ) ) == SVt_PVAV ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvTYPE( SvRV( *perl_value ) ) == SVt_PVAV ) { 0 0 0
290 0 return AMQP_FIELD_KIND_ARRAY;
291 }
292  
293 // Hash Reference
294 0 ▶ if ( SvTYPE( SvRV( *perl_value ) ) == SVt_PVHV ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvTYPE( SvRV( *perl_value ) ) == SVt_PVHV ) { 0 0 0
295 0 return AMQP_FIELD_KIND_TABLE;
296 }
297 0 Perl_croak(
298 aTHX_ "Unsupported Perl Reference Type: %d",
299 0 SvTYPE( SvRV( *perl_value ) )
300 );
301 }
302  
303 0 Perl_croak(
304 aTHX_ "Unsupported scalar type detected >%s<(%d)",
305 SvPV_nolen(*perl_value),
306 0 SvTYPE( *perl_value )
307 );
308 }
309  
310 /* If we're still here... wtf */
311 Perl_croak( aTHX_ "The wheels have fallen off. Please call for help." );
312 }
313  
314 /* Parallels amqp_read_message */
315 0 static amqp_rpc_reply_t read_message(amqp_connection_state_t state, amqp_channel_t channel, SV **props_sv_ptr, SV **body_sv_ptr) {
316 HV *props_hv;
317 SV *body_sv;
318 amqp_rpc_reply_t ret;
319 int res;
320 amqp_frame_t frame;
321 0 int is_utf8_body = 1; /* The body is UTF-8 by default */
322  
323 0 memset(&ret, 0, sizeof(amqp_rpc_reply_t));
324  
325 0 res = amqp_simple_wait_frame_on_channel(state, channel, &frame);
326 0 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 0 0
327 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
328 0 ret.library_error = res;
329 0 goto error_out1;
330 }
331  
332 0 ▶ if (AMQP_FRAME_HEADER != frame.frame_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_FRAME_HEADER != frame.frame_type) { 0 0 0
333 0 ▶ if (AMQP_FRAME_METHOD == frame.frame_type &&
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_FRAME_METHOD == frame.frame_type && 0 0 0
334 0 ▶ (AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id ||
Branch: 1 branch
BranchTrueFalseTotal
(AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id || 0 0 0
335 0 ▶ AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) {
Branch: 1 branch
BranchTrueFalseTotal
AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) { 0 0 0
336  
337 0 ret.reply_type = AMQP_RESPONSE_SERVER_EXCEPTION;
338 0 ret.reply = frame.payload.method;
339  
340 } else {
341 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
342 0 ret.library_error = AMQP_STATUS_UNEXPECTED_STATE;
343  
344 0 amqp_put_back_frame(state, &frame);
345 }
346  
347 0 goto error_out1;
348 }
349  
350 {
351 amqp_basic_properties_t *p;
352  
353 0 props_hv = newHV();
354  
355 0 p = (amqp_basic_properties_t *) frame.payload.properties.decoded;
356 0 ▶ if (p->_flags & AMQP_BASIC_CONTENT_TYPE_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->_flags & AMQP_BASIC_CONTENT_TYPE_FLAG) { 0 0 0
357 0 hv_stores(props_hv, "content_type", newSVpvn(p->content_type.bytes, p->content_type.len));
358 }
359 0 ▶ if (p->_flags & AMQP_BASIC_CONTENT_ENCODING_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->_flags & AMQP_BASIC_CONTENT_ENCODING_FLAG) { 0 0 0
360 0 hv_stores(props_hv, "content_encoding", newSVpvn(p->content_encoding.bytes, p->content_encoding.len));
361  
362 /*
363 * Since we could have UTF-8 in our content-encoding, and most people seem like they
364 * treat this like the default, we're looking for the presence of content-encoding but
365 * the absence of a case-insensitive "UTF-8".
366 */
367 0 if (
368 0 ▶ strnlen(p->content_encoding.bytes, p->content_encoding.len) > 0
Branch: 1 branch
BranchTrueFalseTotal
strnlen(p->content_encoding.bytes, p->content_encoding.len) > 0 0 0 0
369 0 &&
370 0 ▶ (strncasecmp(p->content_encoding.bytes, "UTF-8", p->content_encoding.len) != 0)
Branch: 1 branch
BranchTrueFalseTotal
(strncasecmp(p->content_encoding.bytes, "UTF-8", p->content_encoding.len) != 0) 0 0 0
371 ) {
372 0 is_utf8_body = 0;
373 }
374 }
375 0 ▶ if (p->_flags & AMQP_BASIC_CORRELATION_ID_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->_flags & AMQP_BASIC_CORRELATION_ID_FLAG) { 0 0 0
376 0 hv_stores(props_hv, "correlation_id", newSVpvn(p->correlation_id.bytes, p->correlation_id.len));
377 }
378 0 ▶ if (p->_flags & AMQP_BASIC_REPLY_TO_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->_flags & AMQP_BASIC_REPLY_TO_FLAG) { 0 0 0
379 0 hv_stores(props_hv, "reply_to", newSVpvn(p->reply_to.bytes, p->reply_to.len));
380 }
381 0 ▶ if (p->_flags & AMQP_BASIC_EXPIRATION_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->_flags & AMQP_BASIC_EXPIRATION_FLAG) { 0 0 0
382 0 hv_stores(props_hv, "expiration", newSVpvn(p->expiration.bytes, p->expiration.len));
383 }
384 0 ▶ if (p->_flags & AMQP_BASIC_MESSAGE_ID_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->_flags & AMQP_BASIC_MESSAGE_ID_FLAG) { 0 0 0
385 0 hv_stores(props_hv, "message_id", newSVpvn(p->message_id.bytes, p->message_id.len));
386 }
387 0 ▶ if (p->_flags & AMQP_BASIC_TYPE_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->_flags & AMQP_BASIC_TYPE_FLAG) { 0 0 0
388 0 hv_stores(props_hv, "type", newSVpvn(p->type.bytes, p->type.len));
389 }
390 0 ▶ if (p->_flags & AMQP_BASIC_USER_ID_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->_flags & AMQP_BASIC_USER_ID_FLAG) { 0 0 0
391 0 hv_stores(props_hv, "user_id", newSVpvn(p->user_id.bytes, p->user_id.len));
392 }
393 0 ▶ if (p->_flags & AMQP_BASIC_APP_ID_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->_flags & AMQP_BASIC_APP_ID_FLAG) { 0 0 0
394 0 hv_stores(props_hv, "app_id", newSVpvn(p->app_id.bytes, p->app_id.len));
395 }
396 0 ▶ if (p->_flags & AMQP_BASIC_DELIVERY_MODE_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->_flags & AMQP_BASIC_DELIVERY_MODE_FLAG) { 0 0 0
397 0 hv_stores(props_hv, "delivery_mode", newSViv(p->delivery_mode));
398 }
399 0 ▶ if (p->_flags & AMQP_BASIC_PRIORITY_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->_flags & AMQP_BASIC_PRIORITY_FLAG) { 0 0 0
400 0 hv_stores(props_hv, "priority", newSViv(p->priority));
401 }
402 0 ▶ if (p->_flags & AMQP_BASIC_TIMESTAMP_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->_flags & AMQP_BASIC_TIMESTAMP_FLAG) { 0 0 0
403 0 hv_stores(props_hv, "timestamp", newSViv(p->timestamp));
404 }
405 0 ▶ if (p->_flags & AMQP_BASIC_HEADERS_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->_flags & AMQP_BASIC_HEADERS_FLAG) { 0 0 0
406 int i;
407 0 HV *headers = newHV();
408 0 hv_stores(props_hv, "headers", newRV_noinc(MUTABLE_SV(headers)));
409  
410 __DEBUG__( dump_table( p->headers ) );
411  
412 0 ▶ for( i=0; i < p->headers.num_entries; ++i ) {
Branch: 1 branch
BranchTrueFalseTotal
for( i=0; i < p->headers.num_entries; ++i ) { 0 0 0
413 0 amqp_table_entry_t *header_entry = &(p->headers.entries[i]);
414  
415 __DEBUG__(
416 fprintf(stderr,
417 "~~~ Length: %ld/%d, Key: %.*s, Kind: %c\n",
418 header_entry->key.len,
419 (int)header_entry->key.len,
420 (int)header_entry->key.len,
421 (char*)header_entry->key.bytes,
422 header_entry->value.kind
423 )
424 );
425  
426 0 switch (header_entry->value.kind) {
427 0 case AMQP_FIELD_KIND_BOOLEAN:
428 0 hv_store( headers,
429 header_entry->key.bytes, header_entry->key.len,
430 newSViv(header_entry->value.value.boolean),
431 0
432 );
433 0 break;
434  
435 // Integer types
436 0 case AMQP_FIELD_KIND_I8:
437 0 hv_store( headers,
438 header_entry->key.bytes, header_entry->key.len,
439 newSViv(header_entry->value.value.i8),
440 0
441 );
442 0 break;
443  
444 0 case AMQP_FIELD_KIND_I16:
445 0 hv_store( headers,
446 header_entry->key.bytes, header_entry->key.len,
447 newSViv(header_entry->value.value.i16),
448 0
449 );
450 0 break;
451  
452 0 case AMQP_FIELD_KIND_I32:
453 0 hv_store( headers,
454 header_entry->key.bytes, header_entry->key.len,
455 newSViv(header_entry->value.value.i32),
456 0
457 );
458 0 break;
459  
460 0 case AMQP_FIELD_KIND_I64:
461 0 hv_store( headers,
462 header_entry->key.bytes, header_entry->key.len,
463 newSVi64(header_entry->value.value.i64),
464 0
465 );
466 0 break;
467  
468 0 case AMQP_FIELD_KIND_U8:
469 0 hv_store( headers,
470 header_entry->key.bytes, header_entry->key.len,
471 newSVuv(header_entry->value.value.u8),
472 0
473 );
474 0 break;
475  
476 0 case AMQP_FIELD_KIND_U16:
477 0 hv_store( headers,
478 header_entry->key.bytes, header_entry->key.len,
479 newSVuv(header_entry->value.value.u16),
480 0
481 );
482 0 break;
483  
484 0 case AMQP_FIELD_KIND_U32:
485 0 hv_store( headers,
486 header_entry->key.bytes, header_entry->key.len,
487 newSVuv(header_entry->value.value.u32),
488 0
489 );
490 0 break;
491  
492 0 case AMQP_FIELD_KIND_U64:
493 0 hv_store( headers,
494 header_entry->key.bytes, header_entry->key.len,
495 newSVu64(header_entry->value.value.u64),
496 0
497 );
498 0 break;
499  
500 // Floating point precision
501 0 case AMQP_FIELD_KIND_F32:
502 0 hv_store( headers,
503 header_entry->key.bytes, header_entry->key.len,
504 newSVnv(header_entry->value.value.f32),
505 0
506 );
507 0 break;
508  
509 0 case AMQP_FIELD_KIND_F64:
510 // TODO: I don't think this is a natively supported type on all Perls.
511  
512 0 hv_store( headers,
513 header_entry->key.bytes, header_entry->key.len,
514 #ifdef USE_LONG_DOUBLE
515 /* amqp uses doubles, if perl is -Duselongdouble it messes up the precision
516 * so we always want take the max precision from a double and discard the rest
517 * because it can't be any more precise than a double */
518 newSVnv( ( rint( header_entry->value.value.f64 * DOUBLE_POW ) / DOUBLE_POW ) ),
519 #else
520 /* both of these are doubles so it's ok */
521 newSVnv( header_entry->value.value.f64 ),
522 #endif
523 0
524 );
525 0 break;
526  
527 // Handle kind UTF8 and kind BYTES
528 0 case AMQP_FIELD_KIND_UTF8:
529 case AMQP_FIELD_KIND_BYTES:
530 0 ▶ hv_store( headers,
Branch: 1 branch
BranchTrueFalseTotal
hv_store( headers, 0 0 0
531 header_entry->key.bytes, header_entry->key.len,
532 newSVpvn_utf8(
533 header_entry->value.value.bytes.bytes,
534 header_entry->value.value.bytes.len,
535 AMQP_FIELD_KIND_UTF8 == header_entry->value.kind
536 ),
537 0
538 );
539 0 break;
540  
541 // Handle arrays
542 0 case AMQP_FIELD_KIND_ARRAY:
543 __DEBUG__(
544 fprintf(stderr, "ARRAY KIND FOR KEY:>%.*s< KIND:>%c< AMQP_FIELD_KIND_ARRAY:[%c].\n",
545 (int)header_entry->key.len,
546 (char*)header_entry->key.bytes,
547 header_entry->value.kind,
548 AMQP_FIELD_KIND_ARRAY
549 )
550 );
551 0 hv_store( headers,
552 header_entry->key.bytes, header_entry->key.len,
553 mq_array_to_arrayref( &header_entry->value.value.array ),
554 0
555 );
556 0 break;
557  
558 // Handle tables (hashes when translated to Perl)
559 0 case AMQP_FIELD_KIND_TABLE:
560 0 hv_store( headers,
561 header_entry->key.bytes, header_entry->key.len,
562 mq_table_to_hashref( &header_entry->value.value.table ),
563 0
564 );
565 0 break;
566  
567 0 default:
568 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
569 0 ret.library_error = AMQP_STATUS_UNKNOWN_TYPE;
570 0 goto error_out2;
571 }
572 }
573 }
574 }
575  
576 {
577 char *body;
578 0 size_t body_target = frame.payload.properties.body_size;
579 0 size_t body_remaining = body_target;
580  
581 0 body_sv = newSV(0);
582 0 sv_grow(body_sv, body_target + 1);
583 0 SvCUR_set(body_sv, body_target);
584 0 SvPOK_on(body_sv);
585 0 ▶ if (is_utf8_body)
Branch: 1 branch
BranchTrueFalseTotal
if (is_utf8_body) 0 0 0
586 0 SvUTF8_on(body_sv);
587  
588 0 body = SvPVX(body_sv);
589  
590 0 ▶ while (body_remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (body_remaining > 0) { 0 0 0
591 size_t fragment_len;
592  
593 0 res = amqp_simple_wait_frame_on_channel(state, channel, &frame);
594 0 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 0 0
595 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
596 0 ret.library_error = res;
597 0 goto error_out3;
598 }
599  
600 0 ▶ if (AMQP_FRAME_BODY != frame.frame_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_FRAME_BODY != frame.frame_type) { 0 0 0
601 0 ▶ if (AMQP_FRAME_METHOD == frame.frame_type &&
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_FRAME_METHOD == frame.frame_type && 0 0 0
602 0 ▶ (AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id ||
Branch: 1 branch
BranchTrueFalseTotal
(AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id || 0 0 0
603 0 ▶ AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) {
Branch: 1 branch
BranchTrueFalseTotal
AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) { 0 0 0
604  
605 0 ret.reply_type = AMQP_RESPONSE_SERVER_EXCEPTION;
606 0 ret.reply = frame.payload.method;
607 } else {
608 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
609 0 ret.library_error = AMQP_STATUS_BAD_AMQP_DATA;
610 }
611 0 goto error_out3;
612 }
613  
614 0 fragment_len = frame.payload.body_fragment.len;
615 0 ▶ if (fragment_len > body_remaining) {
Branch: 1 branch
BranchTrueFalseTotal
if (fragment_len > body_remaining) { 0 0 0
616 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
617 0 ret.library_error = AMQP_STATUS_BAD_AMQP_DATA;
618 0 goto error_out3;
619 }
620  
621 0 memcpy(body, frame.payload.body_fragment.bytes, fragment_len);
622 0 body += fragment_len;
623 0 body_remaining -= fragment_len;
624 }
625  
626 0 *body = '\0';
627 }
628  
629 0 *props_sv_ptr = newRV_noinc(MUTABLE_SV(props_hv));
630 0 *body_sv_ptr = body_sv;
631 0 ret.reply_type = AMQP_RESPONSE_NORMAL;
632 0 return ret;
633  
634 0 error_out3:
635 0 SvREFCNT_dec(props_hv);
636 0 error_out2:
637 0 SvREFCNT_dec(body_sv);
638 0 error_out1:
639 0 *props_sv_ptr = &PL_sv_undef;
640 0 *body_sv_ptr = &PL_sv_undef;
641 0 return ret;
642 }
643  
644 /* Parallels amqp_consume_message */
645 0 static amqp_rpc_reply_t consume_message(amqp_connection_state_t state, SV **envelope_sv_ptr, struct timeval *timeout) {
646 amqp_rpc_reply_t ret;
647 HV *envelope_hv;
648 int res;
649 amqp_frame_t frame;
650 amqp_channel_t channel;
651 SV *props;
652 SV *body;
653  
654 0 memset(&ret, 0, sizeof(amqp_rpc_reply_t));
655 0 *envelope_sv_ptr = &PL_sv_undef;
656  
657 0 res = amqp_simple_wait_frame_noblock(state, &frame, timeout);
658 0 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 0 0
659 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
660 0 ret.library_error = res;
661 0 goto error_out1;
662 }
663  
664 0 ▶ if (AMQP_FRAME_METHOD != frame.frame_type ||
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_FRAME_METHOD != frame.frame_type || 0 0 0
665 0 ▶ AMQP_BASIC_DELIVER_METHOD != frame.payload.method.id) {
Branch: 1 branch
BranchTrueFalseTotal
AMQP_BASIC_DELIVER_METHOD != frame.payload.method.id) { 0 0 0
666  
667 0 ▶ if (AMQP_FRAME_METHOD == frame.frame_type &&
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_FRAME_METHOD == frame.frame_type && 0 0 0
668 0 ▶ (AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id ||
Branch: 1 branch
BranchTrueFalseTotal
(AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id || 0 0 0
669 0 ▶ AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) {
Branch: 1 branch
BranchTrueFalseTotal
AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) { 0 0 0
670  
671 0 ret.reply_type = AMQP_RESPONSE_SERVER_EXCEPTION;
672 0 ret.reply = frame.payload.method;
673 } else {
674 0 amqp_put_back_frame(state, &frame);
675 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
676 0 ret.library_error = AMQP_STATUS_UNEXPECTED_STATE;
677 }
678  
679 0 goto error_out1;
680 }
681  
682 0 channel = frame.channel;
683  
684 0 envelope_hv = newHV();
685  
686 {
687 0 amqp_basic_deliver_t *d = (amqp_basic_deliver_t *) frame.payload.method.decoded;
688 0 hv_stores(envelope_hv, "channel", newSViv(channel));
689 0 hv_stores(envelope_hv, "delivery_tag", newSVu64(d->delivery_tag));
690 0 hv_stores(envelope_hv, "redelivered", newSViv(d->redelivered));
691 0 hv_stores(envelope_hv, "exchange", newSVpvn(d->exchange.bytes, d->exchange.len));
692 0 hv_stores(envelope_hv, "consumer_tag", newSVpvn(d->consumer_tag.bytes, d->consumer_tag.len));
693 0 hv_stores(envelope_hv, "routing_key", newSVpvn(d->routing_key.bytes, d->routing_key.len));
694 }
695  
696 0 ret = read_message(state, channel, &props, &body );
697 0 ▶ if (AMQP_RESPONSE_NORMAL != ret.reply_type)
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_RESPONSE_NORMAL != ret.reply_type) 0 0 0
698 0 goto error_out2;
699  
700 0 hv_stores(envelope_hv, "props", props);
701 0 hv_stores(envelope_hv, "body", body);
702  
703 0 *envelope_sv_ptr = newRV_noinc(MUTABLE_SV(envelope_hv));
704 0 ret.reply_type = AMQP_RESPONSE_NORMAL;
705 0 return ret;
706  
707 0 error_out2:
708 0 SvREFCNT_dec(envelope_hv);
709 0 error_out1:
710 0 *envelope_sv_ptr = &PL_sv_undef;
711 0 return ret;
712 }
713  
714 0 void array_to_amqp_array(AV *perl_array, amqp_array_t *mq_array, short force_utf8) {
715 0 int idx = 0;
716 SV **value;
717  
718 0 amqp_field_value_t *new_elements = amqp_pool_alloc(
719 &temp_memory_pool,
720 0 ((av_len(perl_array)+1) * sizeof(amqp_field_value_t))
721 );
722 amqp_field_value_t *element;
723  
724 0 mq_array->entries = new_elements;
725 0 mq_array->num_entries = 0;
726  
727 0 ▶ for ( idx = 0; idx <= av_len(perl_array); idx += 1) {
Branch: 1 branch
BranchTrueFalseTotal
for ( idx = 0; idx <= av_len(perl_array); idx += 1) { 0 0 0
728 0 value = av_fetch( perl_array, idx, 0 );
729  
730 // We really should never see NULL here.
731 assert(value != NULL);
732  
733 // Let's start getting the type...
734 0 element = &mq_array->entries[mq_array->num_entries];
735 0 mq_array->num_entries += 1;
736 0 element->kind = amqp_kind_for_sv(value, force_utf8);
737  
738 __DEBUG__( warn("%d KIND >%c<", __LINE__, (unsigned char)element->kind) );
739  
740 0 switch (element->kind) {
741  
742 0 case AMQP_FIELD_KIND_I64:
743 0 element->value.i64 = (int64_t) SvI64(*value);
744 0 break;
745  
746 0 case AMQP_FIELD_KIND_U64:
747 0 element->value.u64 = (uint64_t) SvU64(*value);
748 0 break;
749  
750 0 case AMQP_FIELD_KIND_F64:
751 // TODO: I don't think this is a native type on all Perls
752 0 element->value.f64 = (double) SvNV(*value);
753 0 break;
754  
755 0 case AMQP_FIELD_KIND_UTF8:
756 case AMQP_FIELD_KIND_BYTES:
757 0 element->value.bytes = amqp_cstring_bytes(SvPV_nolen(*value));
758 0 break;
759  
760 0 case AMQP_FIELD_KIND_ARRAY:
761 0 array_to_amqp_array(MUTABLE_AV(SvRV(*value)), &(element->value.array), force_utf8);
762 0 break;
763  
764 0 case AMQP_FIELD_KIND_TABLE:
765 0 hash_to_amqp_table(MUTABLE_HV(SvRV(*value)), &(element->value.table), force_utf8);
766 0 break;
767  
768 0 default:
769 0 Perl_croak( aTHX_ "Unsupported SvType for array index %d", idx );
770 }
771 }
772 0 }
773  
774 // Iterate over the array entries and decode them to Perl...
775 0 SV* mq_array_to_arrayref(amqp_array_t *mq_array) {
776 0 AV* perl_array = newAV();
777  
778 0 SV* perl_element = &PL_sv_undef;
779 amqp_field_value_t* mq_element;
780  
781 0 int current_entry = 0;
782  
783 0 ▶ for (; current_entry < mq_array->num_entries; current_entry += 1) {
Branch: 1 branch
BranchTrueFalseTotal
for (; current_entry < mq_array->num_entries; current_entry += 1) { 0 0 0
784 0 mq_element = &mq_array->entries[current_entry];
785  
786 __DEBUG__( warn("%d KIND >%c<", __LINE__, mq_element->kind) );
787  
788 0 switch (mq_element->kind) {
789 // Boolean
790 0 case AMQP_FIELD_KIND_BOOLEAN:
791 0 perl_element = newSViv(mq_element->value.boolean);
792 0 break;
793  
794 // Signed values
795 0 case AMQP_FIELD_KIND_I8:
796 0 perl_element = newSViv(mq_element->value.i8);
797 0 break;
798 0 case AMQP_FIELD_KIND_I16:
799 0 perl_element = newSViv(mq_element->value.i16);
800 0 break;
801 0 case AMQP_FIELD_KIND_I32:
802 0 perl_element = newSViv(mq_element->value.i32);
803 0 break;
804 0 case AMQP_FIELD_KIND_I64:
805 0 perl_element = newSVi64(mq_element->value.i64);
806 0 break;
807  
808 // Unsigned values
809 0 case AMQP_FIELD_KIND_U8:
810 0 perl_element = newSViv(mq_element->value.u8);
811 0 break;
812 0 case AMQP_FIELD_KIND_U16:
813 0 perl_element = newSViv(mq_element->value.u16);
814 0 break;
815 0 case AMQP_FIELD_KIND_U32:
816 0 perl_element = newSVuv(mq_element->value.u32);
817 0 break;
818 0 case AMQP_FIELD_KIND_TIMESTAMP: /* Timestamps */
819 case AMQP_FIELD_KIND_U64:
820 0 perl_element = newSVu64(mq_element->value.u64);
821 0 break;
822  
823 // Floats
824 0 case AMQP_FIELD_KIND_F32:
825 0 perl_element = newSVnv(mq_element->value.f32);
826 0 break;
827 0 case AMQP_FIELD_KIND_F64:
828 // TODO: I don't think this is a native type on all Perls
829 0 perl_element = newSVnv(mq_element->value.f64);
830 0 break;
831  
832 // Strings and bytes
833 0 case AMQP_FIELD_KIND_BYTES:
834 0 perl_element = newSVpvn(
835 mq_element->value.bytes.bytes,
836 mq_element->value.bytes.len
837 );
838 0 break;
839  
840 // UTF-8 strings
841 0 case AMQP_FIELD_KIND_UTF8:
842 0 perl_element = newSVpvn(
843 mq_element->value.bytes.bytes,
844 mq_element->value.bytes.len
845 );
846 0 SvUTF8_on(perl_element); // It's UTF-8!
847 0 break;
848  
849 // Arrays
850 0 case AMQP_FIELD_KIND_ARRAY:
851 0 perl_element = mq_array_to_arrayref(&(mq_element->value.array));
852 0 break;
853  
854 // Tables
855 0 case AMQP_FIELD_KIND_TABLE:
856 0 perl_element = mq_table_to_hashref(&(mq_element->value.table));
857 0 break;
858  
859 // WTF
860 0 default:
861 // ACK!
862 0 Perl_croak(
863 aTHX_ "Unsupported Perl type >%c< at index %d",
864 0 (unsigned char)mq_element->kind,
865 current_entry
866 );
867 }
868  
869 0 av_push(perl_array, perl_element);
870 }
871  
872 0 return newRV_noinc(MUTABLE_SV(perl_array));
873 }
874  
875 0 SV* mq_table_to_hashref( amqp_table_t *mq_table ) {
876 // Iterate over the table keys and decode them to Perl...
877 int i;
878 SV *perl_element;
879 0 HV *perl_hash = newHV();
880 0 amqp_table_entry_t *hash_entry = (amqp_table_entry_t*)NULL;
881  
882 0 ▶ for( i=0; i < mq_table->num_entries; i += 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
for( i=0; i < mq_table->num_entries; i += 1 ) { 0 0 0
883 0 hash_entry = &(mq_table->entries[i]);
884 __DEBUG__(
885 fprintf(
886 stderr,
887 "!!! Key: >%.*s< Kind: >%c<\n",
888 (int)hash_entry->key.len,
889 (char*)hash_entry->key.bytes,
890 hash_entry->value.kind
891 );
892 );
893  
894 0 switch (hash_entry->value.kind) {
895 // Boolean
896 0 case AMQP_FIELD_KIND_BOOLEAN:
897 0 perl_element = newSViv(hash_entry->value.value.boolean);
898 0 break;
899  
900 // Integers
901 0 case AMQP_FIELD_KIND_I8:
902 0 perl_element = newSViv(hash_entry->value.value.i8);
903 0 break;
904 0 case AMQP_FIELD_KIND_I16:
905 0 perl_element = newSViv(hash_entry->value.value.i16);
906 0 break;
907 0 case AMQP_FIELD_KIND_I32:
908 0 perl_element = newSViv(hash_entry->value.value.i32);
909 0 break;
910 0 case AMQP_FIELD_KIND_I64:
911 0 perl_element = newSVi64(hash_entry->value.value.i64);
912 0 break;
913 0 case AMQP_FIELD_KIND_U8:
914 0 perl_element = newSViv(hash_entry->value.value.u8);
915 0 break;
916 0 case AMQP_FIELD_KIND_U16:
917 0 perl_element = newSViv(hash_entry->value.value.u16);
918 0 break;
919 0 case AMQP_FIELD_KIND_U32:
920 0 perl_element = newSVuv(hash_entry->value.value.u32);
921 0 break;
922 0 case AMQP_FIELD_KIND_TIMESTAMP: /* Timestamps */
923 case AMQP_FIELD_KIND_U64:
924 0 perl_element = newSVu64(hash_entry->value.value.u64);
925 0 break;
926  
927 // Foats
928 0 case AMQP_FIELD_KIND_F32:
929 0 perl_element = newSVnv(hash_entry->value.value.f32);
930 0 break;
931 0 case AMQP_FIELD_KIND_F64:
932 // TODO: I don't think this is a native type on all Perls.
933 0 perl_element = newSVnv(hash_entry->value.value.f64);
934 0 break;
935  
936 0 case AMQP_FIELD_KIND_BYTES:
937 0 perl_element = newSVpvn(
938 hash_entry->value.value.bytes.bytes,
939 hash_entry->value.value.bytes.len
940 );
941 0 break;
942  
943 0 case AMQP_FIELD_KIND_UTF8:
944 0 perl_element = newSVpvn(
945 hash_entry->value.value.bytes.bytes,
946 hash_entry->value.value.bytes.len
947 );
948 0 SvUTF8_on(perl_element); // It's UTF-8!
949 0 break;
950  
951 0 case AMQP_FIELD_KIND_ARRAY:
952 0 perl_element = mq_array_to_arrayref(&(hash_entry->value.value.array));
953 0 break;
954  
955 0 case AMQP_FIELD_KIND_TABLE:
956 0 perl_element = mq_table_to_hashref(&(hash_entry->value.value.table));
957 0 break;
958  
959 0 default:
960 // ACK!
961 0 Perl_croak(
962 aTHX_ "Unsupported Perl type >%c< at index %d",
963 0 (unsigned char)hash_entry->value.kind,
964 i
965 );
966 }
967  
968 // Stash this in our hash.
969 0 hv_store(
970 perl_hash,
971 hash_entry->key.bytes, hash_entry->key.len,
972 perl_element,
973 0
974 );
975  
976 }
977  
978 0 return newRV_noinc(MUTABLE_SV(perl_hash));
979 }
980  
981 0 void hash_to_amqp_table(HV *hash, amqp_table_t *table, short force_utf8) {
982 HE *he;
983 char *key;
984 SV *value;
985 I32 retlen;
986 amqp_table_entry_t *entry;
987  
988 0 ▶ amqp_table_entry_t *new_entries = amqp_pool_alloc( &temp_memory_pool, HvKEYS(hash) * sizeof(amqp_table_entry_t) );
Branch: 1 branch
BranchTrueFalseTotal
amqp_table_entry_t *new_entries = amqp_pool_alloc( &temp_memory_pool, HvKEYS(hash) * sizeof(amqp_table_entry_t) ); 0 0 0
989 0 table->entries = new_entries;
990  
991 0 hv_iterinit(hash);
992 0 ▶ while (NULL != (he = hv_iternext(hash))) {
Branch: 1 branch
BranchTrueFalseTotal
while (NULL != (he = hv_iternext(hash))) { 0 0 0
993 0 key = hv_iterkey(he, &retlen);
994 __DEBUG__( warn("Key: %s\n", key) );
995 0 value = hv_iterval(hash, he);
996  
997 0 ▶ if (SvGMAGICAL(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(value)) { 0 0 0
998 0 mg_get(value);
999 }
1000  
1001 0 entry = &table->entries[table->num_entries];
1002 0 entry->key = amqp_cstring_bytes( key );
1003  
1004 // Reserved headers, per spec must force UTF-8 for strings.
1005 // Other headers aren't necessarily required to do so.
1006 0 if (
1007 // "x-*" exchanges
1008 (
1009 0 ▶ strlen(key) > 2
Branch: 1 branch
BranchTrueFalseTotal
strlen(key) > 2 0 0 0
1010 0 &&
1011 0 ▶ key[0] == 'x'
Branch: 1 branch
BranchTrueFalseTotal
key[0] == 'x' 0 0 0
1012 0 &&
1013 0 ▶ key[1] == '-'
Branch: 1 branch
BranchTrueFalseTotal
key[1] == '-' 0 0 0
1014 )
1015 ) {
1016 0 entry->value.kind = amqp_kind_for_sv( &value, 1 );
1017 }
1018 else {
1019 0 entry->value.kind = amqp_kind_for_sv( &value, force_utf8 );
1020 }
1021  
1022  
1023 __DEBUG__(
1024 warn("hash_to_amqp_table()");
1025 warn("%s", SvPV_nolen(value) );
1026 fprintf(
1027 stderr,
1028 "Key: >%.*s< Kind: >%c<\n",
1029 (int)entry->key.len,
1030 (char*)entry->key.bytes,
1031 entry->value.kind
1032 );
1033 );
1034  
1035 0 switch ( entry->value.kind ) {
1036 0 case AMQP_FIELD_KIND_I64:
1037 0 entry->value.value.i64 = (int64_t) SvI64( value );
1038 0 break;
1039  
1040 0 case AMQP_FIELD_KIND_U64:
1041 0 entry->value.value.u64 = (uint64_t) SvU64( value );
1042 0 break;
1043  
1044 0 case AMQP_FIELD_KIND_F64:
1045 // TODO: I don't think this is a native type on all Perls.
1046 0 entry->value.value.f64 = (double) SvNV( value );
1047 0 break;
1048  
1049 0 case AMQP_FIELD_KIND_BYTES:
1050 case AMQP_FIELD_KIND_UTF8:
1051 0 entry->value.value.bytes = amqp_cstring_bytes( SvPV_nolen( value )
1052 );
1053 0 break;
1054  
1055 0 case AMQP_FIELD_KIND_ARRAY:
1056 0 array_to_amqp_array(
1057 0 MUTABLE_AV(SvRV(value)),
1058 &(entry->value.value.array),
1059 force_utf8
1060 );
1061 0 break;
1062  
1063 0 case AMQP_FIELD_KIND_TABLE:
1064 0 hash_to_amqp_table(
1065 0 MUTABLE_HV(SvRV(value)),
1066 &(entry->value.value.table),
1067 force_utf8
1068 );
1069 0 break;
1070  
1071 0 default:
1072 0 Perl_croak( aTHX_ "amqp_kind_for_sv() returned a type I don't understand." );
1073 }
1074  
1075 // Successfully (we think) added an entry to the table.
1076 0 table->num_entries++;
1077 }
1078  
1079 0 return;
1080 }
1081  
1082 0 static amqp_rpc_reply_t basic_get(amqp_connection_state_t state, amqp_channel_t channel, amqp_bytes_t queue, SV **envelope_sv_ptr, amqp_boolean_t no_ack) {
1083 amqp_rpc_reply_t ret;
1084 0 HV *envelope_hv = NULL;
1085 SV *props;
1086 SV *body;
1087  
1088 0 ret = amqp_basic_get(state, channel, queue, no_ack);
1089 0 ▶ if (AMQP_RESPONSE_NORMAL != ret.reply_type)
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_RESPONSE_NORMAL != ret.reply_type) 0 0 0
1090 0 goto error_out1;
1091  
1092 0 ▶ if (AMQP_BASIC_GET_OK_METHOD != ret.reply.id)
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_BASIC_GET_OK_METHOD != ret.reply.id) 0 0 0
1093 0 goto success_out;
1094  
1095 0 envelope_hv = newHV();
1096  
1097 {
1098 0 amqp_basic_get_ok_t *ok = (amqp_basic_get_ok_t *) ret.reply.decoded;
1099 0 hv_stores(envelope_hv, "delivery_tag", newSVu64(ok->delivery_tag));
1100 0 hv_stores(envelope_hv, "redelivered", newSViv(ok->redelivered));
1101 0 hv_stores(envelope_hv, "exchange", newSVpvn(ok->exchange.bytes, ok->exchange.len));
1102 0 hv_stores(envelope_hv, "routing_key", newSVpvn(ok->routing_key.bytes, ok->routing_key.len));
1103 0 hv_stores(envelope_hv, "message_count", newSViv(ok->message_count));
1104 }
1105  
1106 0 ret = read_message(state, channel, &props, &body );
1107 0 ▶ if (AMQP_RESPONSE_NORMAL != ret.reply_type)
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_RESPONSE_NORMAL != ret.reply_type) 0 0 0
1108 0 goto error_out2;
1109  
1110 0 hv_stores(envelope_hv, "props", props);
1111 0 hv_stores(envelope_hv, "body", body);
1112
1113 0 success_out:
1114 0 ▶ *envelope_sv_ptr = envelope_hv ? newRV_noinc(MUTABLE_SV(envelope_hv)) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
*envelope_sv_ptr = envelope_hv ? newRV_noinc(MUTABLE_SV(envelope_hv)) : &PL_sv_undef; 0 0 0
1115 0 ret.reply_type = AMQP_RESPONSE_NORMAL;
1116 0 return ret;
1117  
1118 0 error_out2:
1119 0 SvREFCNT_dec(envelope_hv);
1120 0 error_out1:
1121 0 *envelope_sv_ptr = &PL_sv_undef;
1122 0 return ret;
1123 }
1124  
1125  
1126  
1127 MODULE = Net::AMQP::RabbitMQ PACKAGE = Net::AMQP::RabbitMQ PREFIX = net_amqp_rabbitmq_
1128  
1129 REQUIRE: 1.9505
1130 PROTOTYPES: DISABLE
1131  
1132 BOOT:
1133 29 ▶ PERL_MATH_INT64_LOAD_OR_CROAK;
Branch: 1 branch
BranchTrueFalseTotal
PERL_MATH_INT64_LOAD_OR_CROAK; 0 29 29
1134  
1135 int
1136 net_amqp_rabbitmq_connect(conn, hostname, options)
1137 Net::AMQP::RabbitMQ conn
1138 char *hostname
1139 HV *options
1140 PREINIT:
1141 amqp_socket_t *sock;
1142 30 char *user = "guest";
1143 30 char *password = "guest";
1144 30 char *vhost = "/";
1145 30 int port = 5672;
1146 30 int channel_max = 0;
1147 30 int frame_max = 131072;
1148 30 int heartbeat = 0;
1149 30 double timeout = -1;
1150 struct timeval to;
1151  
1152 30 int ssl = 0;
1153 30 char *ssl_cacert = NULL;
1154 30 char *ssl_cert = NULL;
1155 30 char *ssl_key = NULL;
1156 30 int ssl_verify_host = 1;
1157 30 int ssl_init = 1;
1158 30 char *sasl_method = "plain";
1159 30 ▶ amqp_sasl_method_enum sasl_type = AMQP_SASL_METHOD_PLAIN;
Branch: 1 branch
BranchTrueFalseTotal
amqp_sasl_method_enum sasl_type = AMQP_SASL_METHOD_PLAIN; 30 0 30
1160 CODE:
1161 60 ▶ str_from_hv(options, user);
Branch: 1 branch
BranchTrueFalseTotal
str_from_hv(options, user); 30 0 30
1162 60 ▶ str_from_hv(options, password);
Branch: 1 branch
BranchTrueFalseTotal
str_from_hv(options, password); 30 0 30
1163 60 ▶ str_from_hv(options, vhost);
Branch: 1 branch
BranchTrueFalseTotal
str_from_hv(options, vhost); 30 0 30
1164 30 ▶ int_from_hv(options, channel_max);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, channel_max); 0 30 30
1165 30 ▶ int_from_hv(options, frame_max);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, frame_max); 0 30 30
1166 30 ▶ int_from_hv(options, heartbeat);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, heartbeat); 0 30 30
1167 30 ▶ int_from_hv(options, port);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, port); 30 0 30
1168 30 ▶ double_from_hv(options, timeout);
Branch: 1 branch
BranchTrueFalseTotal
double_from_hv(options, timeout); 1 29 30
1169  
1170 30 ▶ int_from_hv(options, ssl);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, ssl); 30 0 30
1171 60 ▶ str_from_hv(options, ssl_cacert);
Branch: 1 branch
BranchTrueFalseTotal
str_from_hv(options, ssl_cacert); 30 0 30
1172 30 ▶ str_from_hv(options, ssl_cert);
Branch: 1 branch
BranchTrueFalseTotal
str_from_hv(options, ssl_cert); 0 30 30
1173 30 ▶ str_from_hv(options, ssl_key);
Branch: 1 branch
BranchTrueFalseTotal
str_from_hv(options, ssl_key); 0 30 30
1174 30 ▶ int_from_hv(options, ssl_verify_host);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, ssl_verify_host); 30 0 30
1175 30 ▶ int_from_hv(options, ssl_init);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, ssl_init); 30 0 30
1176 30 ▶ str_from_hv(options, sasl_method);
Branch: 1 branch
BranchTrueFalseTotal
str_from_hv(options, sasl_method); 0 30 30
1177  
1178 30 ▶ if(timeout >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(timeout >= 0) { 1 29 30
1179 1 to.tv_sec = floor(timeout);
1180 1 to.tv_usec = 1000000.0 * (timeout - floor(timeout));
1181 }
1182  
1183 30 ▶ if ( ssl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ssl ) { 1 29 30
1184 #ifndef NAR_HAVE_OPENSSL
1185 Perl_croak(aTHX_ "no ssl support, please install openssl and reinstall");
1186 #endif
1187 1 amqp_set_initialize_ssl_library( (amqp_boolean_t)ssl_init );
1188 1 sock = amqp_ssl_socket_new(conn);
1189 1 ▶ if ( !sock ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !sock ) { 0 1 1
1190 0 Perl_croak(aTHX_ "error creating SSL socket");
1191 }
1192  
1193 // TODO
1194 // change this to amqp_ssl_socket_set_verify_hostname when next rabbitmq lib
1195 1 amqp_ssl_socket_set_verify( sock, (amqp_boolean_t)ssl_verify_host );
1196  
1197 1 ▶ if ( ( ssl_cacert != NULL ) && strlen(ssl_cacert) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( ( ssl_cacert != NULL ) && strlen(ssl_cacert) ) { 1 0 1
if ( ( ssl_cacert != NULL ) && strlen(ssl_cacert) ) { 1 0 1
1198 1 ▶ if ( amqp_ssl_socket_set_cacert(sock, ssl_cacert) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( amqp_ssl_socket_set_cacert(sock, ssl_cacert) ) { 0 1 1
1199 0 Perl_croak(aTHX_ "error setting CA certificate");
1200 }
1201 }
1202 else {
1203 // TODO
1204 // in librabbitmq > 0.7.1, amqp_ssl_socket_set_verify_peer makes this optional
1205 0 Perl_croak(aTHX_ "required arg ssl_cacert not provided");
1206 }
1207  
1208 1 ▶ if ( ( ssl_key != NULL ) && strlen(ssl_key) && ( ssl_cert != NULL ) && strlen(ssl_cert) ) {
Branch: 4 branches
BranchTrueFalseTotal
if ( ( ssl_key != NULL ) && strlen(ssl_key) && ( ssl_cert != NULL ) && strlen(ssl_cert) ) { 0 1 1
if ( ( ssl_key != NULL ) && strlen(ssl_key) && ( ssl_cert != NULL ) && strlen(ssl_cert) ) { 0 0 0
if ( ( ssl_key != NULL ) && strlen(ssl_key) && ( ssl_cert != NULL ) && strlen(ssl_cert) ) { 0 0 0
if ( ( ssl_key != NULL ) && strlen(ssl_key) && ( ssl_cert != NULL ) && strlen(ssl_cert) ) { 0 0 0
1209 0 ▶ if ( amqp_ssl_socket_set_key( sock, ssl_cert, ssl_key ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( amqp_ssl_socket_set_key( sock, ssl_cert, ssl_key ) ) { 0 0 0
1210 0 Perl_croak(aTHX_ "error setting client cert");
1211 }
1212 }
1213 }
1214 else {
1215 29 sock = amqp_tcp_socket_new(conn);
1216 29 ▶ if (!sock) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sock) { 0 29 29
1217 0 Perl_croak(aTHX_ "error creating TCP socket");
1218 }
1219 }
1220  
1221 //if there's data in the buffer, clear it
1222 30 ▶ while ( amqp_data_in_buffer(conn) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( amqp_data_in_buffer(conn) ) { 0 30 30
1223 amqp_frame_t frame;
1224 0 amqp_simple_wait_frame( conn, &frame );
1225 }
1226  
1227 // should probably be amqp_raw_equal, but this is a minimal hack
1228 30 ▶ if (strcasecmp(sasl_method, "external") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcasecmp(sasl_method, "external") == 0) { 0 30 30
1229 0 sasl_type = AMQP_SASL_METHOD_EXTERNAL;
1230 }
1231  
1232 30 ▶ die_on_error(aTHX_ amqp_socket_open_noblock(sock, hostname, port, (timeout<0)?NULL:&to), conn, "opening socket");
Branch: 1 branch
BranchTrueFalseTotal
die_on_error(aTHX_ amqp_socket_open_noblock(sock, hostname, port, (timeout<0)?NULL:&to), conn, "opening socket"); 29 1 30
1233 28 die_on_amqp_error(aTHX_ amqp_login(conn, vhost, channel_max, frame_max, heartbeat, sasl_type, user, password), conn, "Logging in");
1234  
1235 0 maybe_release_buffers(conn);
1236  
1237 0 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 0 0
1238 OUTPUT:
1239 RETVAL
1240  
1241 void
1242 net_amqp_rabbitmq_channel_open(conn, channel)
1243 Net::AMQP::RabbitMQ conn
1244 int channel
1245 CODE:
1246 29 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 1 28 29
assert_amqp_connected(conn); 1 0 1
1247  
1248 0 amqp_channel_open(conn, channel);
1249 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Opening channel");
1250  
1251 void
1252 net_amqp_rabbitmq_channel_close(conn, channel)
1253 Net::AMQP::RabbitMQ conn
1254 int channel
1255 CODE:
1256 /* If we don't have a socket, just return. */
1257 8 ▶ if ( ! has_valid_connection( conn ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( ! has_valid_connection( conn ) ) { 0 8 8
if ( ! has_valid_connection( conn ) ) { 0 0 0
1258 8 return;
1259 }
1260 0 die_on_amqp_error(aTHX_ amqp_channel_close(conn, channel, AMQP_REPLY_SUCCESS), conn, "Closing channel");
1261  
1262 void
1263 net_amqp_rabbitmq_exchange_declare(conn, channel, exchange, options = NULL, args = NULL)
1264 Net::AMQP::RabbitMQ conn
1265 int channel
1266 char *exchange
1267 HV *options
1268 HV *args
1269 PREINIT:
1270 26 char *exchange_type = "direct";
1271 26 int passive = 0;
1272 26 int durable = 0;
1273 26 int auto_delete = 0;
1274 26 int internal = 0;
1275 26 ▶ amqp_table_t arguments = amqp_empty_table;
Branch: 1 branch
BranchTrueFalseTotal
amqp_table_t arguments = amqp_empty_table; 26 0 26
1276 CODE:
1277 26 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 1 25 26
assert_amqp_connected(conn); 1 0 1
1278  
1279 0 ▶ if(options) {
Branch: 1 branch
BranchTrueFalseTotal
if(options) { 0 0 0
1280 0 ▶ str_from_hv(options, exchange_type);
Branch: 1 branch
BranchTrueFalseTotal
str_from_hv(options, exchange_type); 0 0 0
1281 0 ▶ int_from_hv(options, passive);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, passive); 0 0 0
1282 0 ▶ int_from_hv(options, durable);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, durable); 0 0 0
1283 0 ▶ int_from_hv(options, auto_delete);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, auto_delete); 0 0 0
1284 0 ▶ int_from_hv(options, internal);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, internal); 0 0 0
1285 }
1286 0 ▶ if(args)
Branch: 1 branch
BranchTrueFalseTotal
if(args) 0 0 0
1287 {
1288 0 hash_to_amqp_table(args, &arguments, 1);
1289 }
1290 0 amqp_exchange_declare(
1291 conn,
1292 channel,
1293 amqp_cstring_bytes(exchange),
1294 amqp_cstring_bytes(exchange_type),
1295 passive,
1296 (amqp_boolean_t)durable,
1297 (amqp_boolean_t)auto_delete,
1298 (amqp_boolean_t)internal,
1299 arguments
1300 );
1301 0 maybe_release_buffers(conn);
1302 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Declaring exchange");
1303  
1304 void
1305 net_amqp_rabbitmq_exchange_delete(conn, channel, exchange, options = NULL)
1306 Net::AMQP::RabbitMQ conn
1307 int channel
1308 char *exchange
1309 HV *options
1310 PREINIT:
1311 13 ▶ int if_unused = 1;
Branch: 1 branch
BranchTrueFalseTotal
int if_unused = 1; 13 0 13
1312 CODE:
1313 13 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 0 13 13
assert_amqp_connected(conn); 0 0 0
1314  
1315 0 ▶ if(options) {
Branch: 1 branch
BranchTrueFalseTotal
if(options) { 0 0 0
1316 0 ▶ int_from_hv(options, if_unused);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, if_unused); 0 0 0
1317 }
1318 0 amqp_exchange_delete(conn, channel, amqp_cstring_bytes(exchange), if_unused);
1319 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Deleting exchange");
1320  
1321 void net_amqp_rabbitmq_exchange_bind(conn, channel, destination, source, routing_key, args = NULL)
1322 Net::AMQP::RabbitMQ conn
1323 int channel
1324 char *destination
1325 char *source
1326 char *routing_key
1327 HV *args
1328 PREINIT:
1329 3 amqp_exchange_bind_ok_t *reply = (amqp_exchange_bind_ok_t*)NULL;
1330 3 ▶ amqp_table_t arguments = amqp_empty_table;
Branch: 1 branch
BranchTrueFalseTotal
amqp_table_t arguments = amqp_empty_table; 3 0 3
1331 CODE:
1332 // We must be connected
1333 3 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 0 3 3
assert_amqp_connected(conn); 0 0 0
1334  
1335 // Parameter validation
1336 0 ▶ if( ( source == NULL || 0 == strlen(source) )
Branch: 2 branches
BranchTrueFalseTotal
if( ( source == NULL || 0 == strlen(source) ) 0 0 0
if( ( source == NULL || 0 == strlen(source) ) 0 0 0
1337 0 ▶ ||
Branch: 1 branch
BranchTrueFalseTotal
|| 0 0 0
1338 0 ▶ ( destination == NULL || 0 == strlen(destination) )
Branch: 1 branch
BranchTrueFalseTotal
( destination == NULL || 0 == strlen(destination) ) 0 0 0
1339 )
1340 {
1341 0 Perl_croak(aTHX_ "source and destination must both be specified");
1342 }
1343  
1344 // Pull in arguments if we have any
1345 0 ▶ if(args)
Branch: 1 branch
BranchTrueFalseTotal
if(args) 0 0 0
1346 {
1347 0 hash_to_amqp_table(args, &arguments, 1);
1348 }
1349  
1350 0 reply = amqp_exchange_bind(
1351 conn,
1352 channel,
1353 amqp_cstring_bytes(destination),
1354 amqp_cstring_bytes(source),
1355 amqp_cstring_bytes(routing_key),
1356 arguments
1357 );
1358 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Binding Exchange");
1359  
1360 void net_amqp_rabbitmq_exchange_unbind(conn, channel, destination, source, routing_key, args = NULL)
1361 Net::AMQP::RabbitMQ conn
1362 int channel
1363 char *destination
1364 char *source
1365 char *routing_key
1366 HV *args
1367 PREINIT:
1368 3 amqp_exchange_unbind_ok_t *reply = (amqp_exchange_unbind_ok_t*)NULL;
1369 3 ▶ amqp_table_t arguments = amqp_empty_table;
Branch: 1 branch
BranchTrueFalseTotal
amqp_table_t arguments = amqp_empty_table; 3 0 3
1370 CODE:
1371 // We must be connected
1372 3 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 0 3 3
assert_amqp_connected(conn); 0 0 0
1373  
1374 // Parameter validation
1375 0 ▶ if( ( source == NULL || 0 == strlen(source) )
Branch: 2 branches
BranchTrueFalseTotal
if( ( source == NULL || 0 == strlen(source) ) 0 0 0
if( ( source == NULL || 0 == strlen(source) ) 0 0 0
1376 0 ▶ ||
Branch: 1 branch
BranchTrueFalseTotal
|| 0 0 0
1377 0 ▶ ( destination == NULL || 0 == strlen(destination) )
Branch: 1 branch
BranchTrueFalseTotal
( destination == NULL || 0 == strlen(destination) ) 0 0 0
1378 )
1379 {
1380 0 Perl_croak(aTHX_ "source and destination must both be specified");
1381 }
1382  
1383 // Pull in arguments if we have any
1384 0 ▶ if(args)
Branch: 1 branch
BranchTrueFalseTotal
if(args) 0 0 0
1385 {
1386 0 hash_to_amqp_table(args, &arguments, 1);
1387 }
1388  
1389 0 reply = amqp_exchange_unbind(
1390 conn,
1391 channel,
1392 amqp_cstring_bytes(destination),
1393 amqp_cstring_bytes(source),
1394 amqp_cstring_bytes(routing_key),
1395 arguments
1396 );
1397 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Unbinding Exchange");
1398  
1399 void net_amqp_rabbitmq_queue_delete(conn, channel, queuename, options = NULL)
1400 Net::AMQP::RabbitMQ conn
1401 int channel
1402 char *queuename
1403 HV *options
1404 PREINIT:
1405 7 int if_unused = 1;
1406 7 int if_empty = 1;
1407 7 ▶ amqp_queue_delete_ok_t *reply = (amqp_queue_delete_ok_t*)NULL;
Branch: 1 branch
BranchTrueFalseTotal
amqp_queue_delete_ok_t *reply = (amqp_queue_delete_ok_t*)NULL; 7 0 7
1408 CODE:
1409 7 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 0 7 7
assert_amqp_connected(conn); 0 0 0
1410  
1411 0 ▶ if(options) {
Branch: 1 branch
BranchTrueFalseTotal
if(options) { 0 0 0
1412 0 ▶ int_from_hv(options, if_unused);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, if_unused); 0 0 0
1413 0 ▶ int_from_hv(options, if_empty);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, if_empty); 0 0 0
1414 }
1415 0 reply = amqp_queue_delete(
1416 conn,
1417 channel,
1418 amqp_cstring_bytes(queuename),
1419 if_unused,
1420 if_empty
1421 );
1422 0 ▶ if (reply == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (reply == NULL) { 0 0 0
1423 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Deleting queue");
1424 }
1425 0 ▶ XPUSHs(sv_2mortal(newSVuv(reply->message_count)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv(reply->message_count))); 0 0 0
1426  
1427 void
1428 net_amqp_rabbitmq_queue_declare(conn, channel, queuename, options = NULL, args = NULL)
1429 Net::AMQP::RabbitMQ conn
1430 int channel
1431 char *queuename
1432 HV *options
1433 HV *args
1434 PREINIT:
1435 20 int passive = 0;
1436 20 int durable = 0;
1437 20 int exclusive = 0;
1438 20 int auto_delete = 1;
1439 20 amqp_table_t arguments = amqp_empty_table;
1440 20 amqp_bytes_t queuename_b = amqp_empty_bytes;
1441 20 ▶ amqp_queue_declare_ok_t *r = (amqp_queue_declare_ok_t*)NULL;
Branch: 1 branch
BranchTrueFalseTotal
amqp_queue_declare_ok_t *r = (amqp_queue_declare_ok_t*)NULL; 20 0 20
1442 PPCODE:
1443 20 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 1 19 20
assert_amqp_connected(conn); 1 0 1
1444  
1445 0 ▶ if(queuename && strcmp(queuename, "")) queuename_b = amqp_cstring_bytes(queuename);
Branch: 2 branches
BranchTrueFalseTotal
if(queuename && strcmp(queuename, "")) queuename_b = amqp_cstring_bytes(queuename); 0 0 0
if(queuename && strcmp(queuename, "")) queuename_b = amqp_cstring_bytes(queuename); 0 0 0
1446 0 ▶ if(options) {
Branch: 1 branch
BranchTrueFalseTotal
if(options) { 0 0 0
1447 0 ▶ int_from_hv(options, passive);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, passive); 0 0 0
1448 0 ▶ int_from_hv(options, durable);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, durable); 0 0 0
1449 0 ▶ int_from_hv(options, exclusive);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, exclusive); 0 0 0
1450 0 ▶ int_from_hv(options, auto_delete);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, auto_delete); 0 0 0
1451 }
1452 0 ▶ if(args)
Branch: 1 branch
BranchTrueFalseTotal
if(args) 0 0 0
1453 {
1454 0 hash_to_amqp_table(args, &arguments, 1);
1455 }
1456 0 r = amqp_queue_declare(conn, channel, queuename_b, passive,
1457 durable, exclusive, auto_delete,
1458 arguments);
1459 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Declaring queue");
1460 0 ▶ XPUSHs(sv_2mortal(newSVpvn(r->queue.bytes, r->queue.len)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(r->queue.bytes, r->queue.len))); 0 0 0
1461 0 ▶ if(GIMME_V == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if(GIMME_V == G_ARRAY) { 0 0 0
1462 0 ▶ XPUSHs(sv_2mortal(newSVuv(r->message_count)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv(r->message_count))); 0 0 0
1463 0 ▶ XPUSHs(sv_2mortal(newSVuv(r->consumer_count)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv(r->consumer_count))); 0 0 0
1464 }
1465  
1466 void
1467 net_amqp_rabbitmq_queue_bind(conn, channel, queuename, exchange, bindingkey, args = NULL)
1468 Net::AMQP::RabbitMQ conn
1469 int channel
1470 char *queuename
1471 char *exchange
1472 char *bindingkey
1473 HV *args
1474 PREINIT:
1475 20 ▶ amqp_table_t arguments = amqp_empty_table;
Branch: 1 branch
BranchTrueFalseTotal
amqp_table_t arguments = amqp_empty_table; 20 0 20
1476 CODE:
1477 20 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 1 19 20
assert_amqp_connected(conn); 1 0 1
1478  
1479 0 ▶ if(queuename == NULL
Branch: 1 branch
BranchTrueFalseTotal
if(queuename == NULL 0 0 0
1480 0 ▶ ||
Branch: 1 branch
BranchTrueFalseTotal
|| 0 0 0
1481 exchange == NULL
1482 0 ||
1483 0 ▶ 0 == strlen(queuename)
Branch: 1 branch
BranchTrueFalseTotal
0 == strlen(queuename) 0 0 0
1484 0 ||
1485 0 ▶ 0 == strlen(exchange)
Branch: 1 branch
BranchTrueFalseTotal
0 == strlen(exchange) 0 0 0
1486 )
1487 {
1488 0 Perl_croak(aTHX_ "queuename and exchange must both be specified");
1489 }
1490  
1491 0 ▶ if(args)
Branch: 1 branch
BranchTrueFalseTotal
if(args) 0 0 0
1492 0 hash_to_amqp_table(args, &arguments, 0);
1493 0 amqp_queue_bind(conn, channel, amqp_cstring_bytes(queuename),
1494 amqp_cstring_bytes(exchange),
1495 amqp_cstring_bytes(bindingkey),
1496 arguments);
1497 0 maybe_release_buffers(conn);
1498 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Binding queue");
1499  
1500 void
1501 net_amqp_rabbitmq_queue_unbind(conn, channel, queuename, exchange, bindingkey, args = NULL)
1502 Net::AMQP::RabbitMQ conn
1503 int channel
1504 char *queuename
1505 char *exchange
1506 char *bindingkey
1507 HV *args
1508 PREINIT:
1509 5 ▶ amqp_table_t arguments = amqp_empty_table;
Branch: 1 branch
BranchTrueFalseTotal
amqp_table_t arguments = amqp_empty_table; 5 0 5
1510 CODE:
1511 5 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 0 5 5
assert_amqp_connected(conn); 0 0 0
1512  
1513 0 ▶ if(queuename == NULL || exchange == NULL)
Branch: 2 branches
BranchTrueFalseTotal
if(queuename == NULL || exchange == NULL) 0 0 0
if(queuename == NULL || exchange == NULL) 0 0 0
1514 {
1515 0 Perl_croak(aTHX_ "queuename and exchange must both be specified");
1516 }
1517  
1518 0 ▶ if(args)
Branch: 1 branch
BranchTrueFalseTotal
if(args) 0 0 0
1519 {
1520 0 hash_to_amqp_table(args, &arguments, 0);
1521 }
1522 0 amqp_queue_unbind(conn, channel, amqp_cstring_bytes(queuename),
1523 amqp_cstring_bytes(exchange),
1524 amqp_cstring_bytes(bindingkey),
1525 arguments);
1526 0 maybe_release_buffers(conn);
1527 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Unbinding queue");
1528  
1529 SV *
1530 net_amqp_rabbitmq_consume(conn, channel, queuename, options = NULL)
1531 Net::AMQP::RabbitMQ conn
1532 int channel
1533 char *queuename
1534 HV *options
1535 PREINIT:
1536 amqp_basic_consume_ok_t *r;
1537 17 char *consumer_tag = NULL;
1538 17 int no_local = 0;
1539 17 int no_ack = 1;
1540 17 ▶ int exclusive = 0;
Branch: 1 branch
BranchTrueFalseTotal
int exclusive = 0; 17 0 17
1541 CODE:
1542 17 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 1 16 17
assert_amqp_connected(conn); 1 0 1
1543  
1544 0 ▶ if(options) {
Branch: 1 branch
BranchTrueFalseTotal
if(options) { 0 0 0
1545 0 ▶ str_from_hv(options, consumer_tag);
Branch: 1 branch
BranchTrueFalseTotal
str_from_hv(options, consumer_tag); 0 0 0
1546 0 ▶ int_from_hv(options, no_local);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, no_local); 0 0 0
1547 0 ▶ int_from_hv(options, no_ack);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, no_ack); 0 0 0
1548 0 ▶ int_from_hv(options, exclusive);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, exclusive); 0 0 0
1549 }
1550 0 ▶ r = amqp_basic_consume(conn, channel, amqp_cstring_bytes(queuename),
Branch: 1 branch
BranchTrueFalseTotal
r = amqp_basic_consume(conn, channel, amqp_cstring_bytes(queuename), 0 0 0
1551 0 consumer_tag ? amqp_cstring_bytes(consumer_tag) : amqp_empty_bytes,
1552 no_local, no_ack, exclusive, amqp_empty_table);
1553 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Consume queue");
1554 0 RETVAL = newSVpvn(r->consumer_tag.bytes, r->consumer_tag.len);
1555 OUTPUT:
1556 RETVAL
1557  
1558 int
1559 net_amqp_rabbitmq_cancel(conn, channel, consumer_tag)
1560 Net::AMQP::RabbitMQ conn
1561 int channel
1562 char *consumer_tag
1563 PREINIT:
1564 amqp_basic_cancel_ok_t *r;
1565 CODE:
1566 4 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 0 4 4
assert_amqp_connected(conn); 0 0 0
1567  
1568 0 r = amqp_basic_cancel(conn, channel, amqp_cstring_bytes(consumer_tag));
1569 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "cancel");
1570  
1571 0 ▶ if ( r == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( r == NULL ) { 0 0 0
1572 0 RETVAL = 0;
1573 }
1574 else {
1575 0 ▶ if(strlen(consumer_tag) == r->consumer_tag.len && 0 == strcmp(consumer_tag, (char *)r->consumer_tag.bytes)) {
Branch: 2 branches
BranchTrueFalseTotal
if(strlen(consumer_tag) == r->consumer_tag.len && 0 == strcmp(consumer_tag, (char *)r->consumer_tag.bytes)) { 0 0 0
if(strlen(consumer_tag) == r->consumer_tag.len && 0 == strcmp(consumer_tag, (char *)r->consumer_tag.bytes)) { 0 0 0
1576 0 RETVAL = 1;
1577 } else {
1578 0 RETVAL = 0;
1579 }
1580 }
1581 OUTPUT:
1582 RETVAL
1583  
1584 SV *
1585 net_amqp_rabbitmq_recv(conn, timeout = 0)
1586 Net::AMQP::RabbitMQ conn
1587 int timeout
1588 PREINIT:
1589 SV *envelope;
1590 amqp_rpc_reply_t ret;
1591 struct timeval timeout_tv;
1592 CODE:
1593 23 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 1 22 23
assert_amqp_connected(conn); 1 0 1
1594  
1595 0 ▶ if (timeout > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (timeout > 0) { 0 0 0
1596 0 timeout_tv.tv_sec = timeout / 1000;
1597 0 timeout_tv.tv_usec = (timeout % 1000) * 1000;
1598 }
1599  
1600 // Set the waiting time to 0
1601 0 ▶ if (timeout == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (timeout == -1) { 0 0 0
1602 0 timeout_tv.tv_sec = 0;
1603 0 timeout_tv.tv_usec = 0;
1604 }
1605  
1606 0 maybe_release_buffers(conn);
1607 0 ▶ ret = consume_message(conn, &RETVAL, timeout ? &timeout_tv : NULL);
Branch: 1 branch
BranchTrueFalseTotal
ret = consume_message(conn, &RETVAL, timeout ? &timeout_tv : NULL); 0 0 0
1608 0 ▶ if (AMQP_RESPONSE_LIBRARY_EXCEPTION != ret.reply_type || AMQP_STATUS_TIMEOUT != ret.library_error)
Branch: 2 branches
BranchTrueFalseTotal
if (AMQP_RESPONSE_LIBRARY_EXCEPTION != ret.reply_type || AMQP_STATUS_TIMEOUT != ret.library_error) 0 0 0
if (AMQP_RESPONSE_LIBRARY_EXCEPTION != ret.reply_type || AMQP_STATUS_TIMEOUT != ret.library_error) 0 0 0
1609 0 die_on_amqp_error(aTHX_ ret, conn, "recv");
1610  
1611 OUTPUT:
1612 RETVAL
1613  
1614 void
1615 net_amqp_rabbitmq_ack(conn, channel, delivery_tag, multiple = 0)
1616 Net::AMQP::RabbitMQ conn
1617 int channel
1618 uint64_t delivery_tag
1619 int multiple
1620 CODE:
1621 3 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 0 3 3
assert_amqp_connected(conn); 0 0 0
1622  
1623 0 die_on_error(aTHX_ amqp_basic_ack(conn, channel, delivery_tag, multiple), conn,
1624 "ack");
1625  
1626 void
1627 net_amqp_rabbitmq_nack(conn, channel, delivery_tag, multiple = 0, requeue = 0)
1628 Net::AMQP::RabbitMQ conn
1629 int channel
1630 uint64_t delivery_tag
1631 int multiple
1632 int requeue
1633 CODE:
1634 2 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 0 2 2
assert_amqp_connected(conn); 0 0 0
1635  
1636 0 die_on_error(
1637 aTHX_ amqp_basic_nack(
1638 conn,
1639 channel,
1640 delivery_tag,
1641 (amqp_boolean_t)multiple,
1642 (amqp_boolean_t)requeue
1643 ),
1644 conn,
1645 "nack"
1646 );
1647  
1648 void
1649 net_amqp_rabbitmq_reject(conn, channel, delivery_tag, requeue = 0)
1650 Net::AMQP::RabbitMQ conn
1651 int channel
1652 uint64_t delivery_tag
1653 int requeue
1654 CODE:
1655 2 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 0 2 2
assert_amqp_connected(conn); 0 0 0
1656  
1657 0 die_on_error(
1658 aTHX_ amqp_basic_reject(
1659 conn,
1660 channel,
1661 delivery_tag,
1662 requeue
1663 ),
1664 conn,
1665 "reject"
1666 );
1667  
1668  
1669 void
1670 net_amqp_rabbitmq_purge(conn, channel, queuename)
1671 Net::AMQP::RabbitMQ conn
1672 int channel
1673 char *queuename
1674 CODE:
1675 3 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 0 3 3
assert_amqp_connected(conn); 0 0 0
1676  
1677 0 amqp_queue_purge(conn, channel, amqp_cstring_bytes(queuename));
1678 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Purging queue");
1679  
1680 void
1681 net_amqp_rabbitmq__publish(conn, channel, routing_key, body, options = NULL, props = NULL)
1682 Net::AMQP::RabbitMQ conn
1683 int channel
1684 HV *options;
1685 char *routing_key
1686 SV *body
1687 HV *props
1688 PREINIT:
1689 SV **v;
1690 25 char *exchange = "amq.direct";
1691 25 amqp_boolean_t mandatory = 0;
1692 25 amqp_boolean_t immediate = 0;
1693 int rv;
1694 25 amqp_bytes_t exchange_b = { 0 };
1695 amqp_bytes_t routing_key_b;
1696 amqp_bytes_t body_b;
1697 struct amqp_basic_properties_t_ properties;
1698 STRLEN len;
1699 25 ▶ int force_utf8_in_header_strings = 0;
Branch: 1 branch
BranchTrueFalseTotal
int force_utf8_in_header_strings = 0; 25 0 25
1700 CODE:
1701 25 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 1 24 25
assert_amqp_connected(conn); 1 0 1
1702  
1703 0 routing_key_b = amqp_cstring_bytes(routing_key);
1704 0 body_b.bytes = SvPV(body, len);
1705 0 body_b.len = len;
1706 0 ▶ if(options) {
Branch: 1 branch
BranchTrueFalseTotal
if(options) { 0 0 0
1707 0 ▶ if(NULL != (v = hv_fetchs(options, "mandatory", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if(NULL != (v = hv_fetchs(options, "mandatory", 0))) { 0 0 0
1708 0 mandatory = SvIV(*v) ? 1 : 0;
1709 }
1710 0 ▶ if(NULL != (v = hv_fetchs(options, "immediate", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if(NULL != (v = hv_fetchs(options, "immediate", 0))) { 0 0 0
1711 0 immediate = SvIV(*v) ? 1 : 0;
1712 }
1713 0 ▶ if(NULL != (v = hv_fetchs(options, "exchange", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if(NULL != (v = hv_fetchs(options, "exchange", 0))) { 0 0 0
1714 0 exchange_b = amqp_cstring_bytes(SvPV_nolen(*v));
1715 }
1716  
1717 // This is an internal option, only for determining if we want to force utf8
1718 0 ▶ int_from_hv(options, force_utf8_in_header_strings);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, force_utf8_in_header_strings); 0 0 0
1719 }
1720 0 properties.headers = amqp_empty_table;
1721 0 properties._flags = 0;
1722 0 ▶ if (props) {
Branch: 1 branch
BranchTrueFalseTotal
if (props) { 0 0 0
1723 0 ▶ if (NULL != (v = hv_fetchs(props, "content_type", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != (v = hv_fetchs(props, "content_type", 0))) { 0 0 0
1724 0 properties.content_type = amqp_cstring_bytes(SvPV_nolen(*v));
1725 0 properties._flags |= AMQP_BASIC_CONTENT_TYPE_FLAG;
1726 }
1727 0 ▶ if (NULL != (v = hv_fetchs(props, "content_encoding", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != (v = hv_fetchs(props, "content_encoding", 0))) { 0 0 0
1728 0 properties.content_encoding = amqp_cstring_bytes(SvPV_nolen(*v));
1729 0 properties._flags |= AMQP_BASIC_CONTENT_ENCODING_FLAG;
1730 }
1731 0 ▶ if (NULL != (v = hv_fetchs(props, "correlation_id", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != (v = hv_fetchs(props, "correlation_id", 0))) { 0 0 0
1732 0 properties.correlation_id = amqp_cstring_bytes(SvPV_nolen(*v));
1733 0 properties._flags |= AMQP_BASIC_CORRELATION_ID_FLAG;
1734 }
1735 0 ▶ if (NULL != (v = hv_fetchs(props, "reply_to", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != (v = hv_fetchs(props, "reply_to", 0))) { 0 0 0
1736 0 properties.reply_to = amqp_cstring_bytes(SvPV_nolen(*v));
1737 0 properties._flags |= AMQP_BASIC_REPLY_TO_FLAG;
1738 }
1739 0 ▶ if (NULL != (v = hv_fetchs(props, "expiration", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != (v = hv_fetchs(props, "expiration", 0))) { 0 0 0
1740 0 properties.expiration = amqp_cstring_bytes(SvPV_nolen(*v));
1741 0 properties._flags |= AMQP_BASIC_EXPIRATION_FLAG;
1742 }
1743 0 ▶ if (NULL != (v = hv_fetchs(props, "message_id", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != (v = hv_fetchs(props, "message_id", 0))) { 0 0 0
1744 0 properties.message_id = amqp_cstring_bytes(SvPV_nolen(*v));
1745 0 properties._flags |= AMQP_BASIC_MESSAGE_ID_FLAG;
1746 }
1747 0 ▶ if (NULL != (v = hv_fetchs(props, "type", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != (v = hv_fetchs(props, "type", 0))) { 0 0 0
1748 0 properties.type = amqp_cstring_bytes(SvPV_nolen(*v));
1749 0 properties._flags |= AMQP_BASIC_TYPE_FLAG;
1750 }
1751 0 ▶ if (NULL != (v = hv_fetchs(props, "user_id", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != (v = hv_fetchs(props, "user_id", 0))) { 0 0 0
1752 0 properties.user_id = amqp_cstring_bytes(SvPV_nolen(*v));
1753 0 properties._flags |= AMQP_BASIC_USER_ID_FLAG;
1754 }
1755 0 ▶ if (NULL != (v = hv_fetchs(props, "app_id", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != (v = hv_fetchs(props, "app_id", 0))) { 0 0 0
1756 0 properties.app_id = amqp_cstring_bytes(SvPV_nolen(*v));
1757 0 properties._flags |= AMQP_BASIC_APP_ID_FLAG;
1758 }
1759 0 ▶ if (NULL != (v = hv_fetchs(props, "delivery_mode", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != (v = hv_fetchs(props, "delivery_mode", 0))) { 0 0 0
1760 0 properties.delivery_mode = (uint8_t) SvIV(*v);
1761 0 properties._flags |= AMQP_BASIC_DELIVERY_MODE_FLAG;
1762 }
1763 0 ▶ if (NULL != (v = hv_fetchs(props, "priority", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != (v = hv_fetchs(props, "priority", 0))) { 0 0 0
1764 0 properties.priority = (uint8_t) SvIV(*v);
1765 0 properties._flags |= AMQP_BASIC_PRIORITY_FLAG;
1766 }
1767 0 ▶ if (NULL != (v = hv_fetchs(props, "timestamp", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != (v = hv_fetchs(props, "timestamp", 0))) { 0 0 0
1768 0 properties.timestamp = (uint64_t) SvI64(*v);
1769 0 properties._flags |= AMQP_BASIC_TIMESTAMP_FLAG;
1770 }
1771 0 ▶ if (NULL != (v = hv_fetchs(props, "headers", 0)) && SvOK(*v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != (v = hv_fetchs(props, "headers", 0)) && SvOK(*v)) { 0 0 0
if (NULL != (v = hv_fetchs(props, "headers", 0)) && SvOK(*v)) { 0 0 0
1772 0 hash_to_amqp_table(MUTABLE_HV(SvRV(*v)), &properties.headers, force_utf8_in_header_strings);
1773 0 properties._flags |= AMQP_BASIC_HEADERS_FLAG;
1774 }
1775 }
1776 __DEBUG__( warn("PUBLISHING HEADERS..."); dump_table( properties.headers ) );
1777 0 rv = amqp_basic_publish(conn, channel, exchange_b, routing_key_b, mandatory, immediate, &properties, body_b);
1778 0 maybe_release_buffers(conn);
1779  
1780 /* If the connection failed, blast the file descriptor! */
1781 0 ▶ if ( rv == AMQP_STATUS_CONNECTION_CLOSED || rv == AMQP_STATUS_SOCKET_ERROR ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( rv == AMQP_STATUS_CONNECTION_CLOSED || rv == AMQP_STATUS_SOCKET_ERROR ) { 0 0 0
if ( rv == AMQP_STATUS_CONNECTION_CLOSED || rv == AMQP_STATUS_SOCKET_ERROR ) { 0 0 0
1782 0 amqp_socket_close( amqp_get_socket( conn ) );
1783 0 Perl_croak(aTHX_ "Publish failed because AMQP socket connection was closed.");
1784 }
1785  
1786 /* Otherwise, just croak */
1787 0 ▶ if ( rv != AMQP_STATUS_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( rv != AMQP_STATUS_OK ) { 0 0 0
1788 0 Perl_croak( aTHX_ "Publish failed, %s\n", amqp_error_string2(rv));
1789 }
1790  
1791 SV *
1792 net_amqp_rabbitmq_get(conn, channel, queuename, options = NULL)
1793 Net::AMQP::RabbitMQ conn
1794 int channel
1795 char *queuename
1796 HV *options
1797 PREINIT:
1798 21 ▶ int no_ack = 1;
Branch: 1 branch
BranchTrueFalseTotal
int no_ack = 1; 21 0 21
1799 CODE:
1800 21 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 1 20 21
assert_amqp_connected(conn); 1 0 1
1801  
1802 0 ▶ if (options)
Branch: 1 branch
BranchTrueFalseTotal
if (options) 0 0 0
1803 0 ▶ int_from_hv(options, no_ack);
Branch: 1 branch
BranchTrueFalseTotal
int_from_hv(options, no_ack); 0 0 0
1804  
1805 0 maybe_release_buffers(conn);
1806 0 ▶ die_on_amqp_error(aTHX_ basic_get(conn, channel, queuename ? amqp_cstring_bytes(queuename) : amqp_empty_bytes, &RETVAL, no_ack), conn, "basic_get");
Branch: 1 branch
BranchTrueFalseTotal
die_on_amqp_error(aTHX_ basic_get(conn, channel, queuename ? amqp_cstring_bytes(queuename) : amqp_empty_bytes, &RETVAL, no_ack), conn, "basic_get"); 0 0 0
1807  
1808 OUTPUT:
1809 RETVAL
1810  
1811 int
1812 net_amqp_rabbitmq_get_channel_max(conn)
1813 Net::AMQP::RabbitMQ conn
1814 CODE:
1815 1 RETVAL = amqp_get_channel_max(conn);
1816 OUTPUT:
1817 RETVAL
1818  
1819 SV*
1820 net_amqp_rabbitmq_get_sockfd(conn)
1821 Net::AMQP::RabbitMQ conn
1822 CODE:
1823 /**
1824 * this is the warning from librabbitmq-c. you have been warned.
1825 *
1826 * \warning Use the socket returned from this function carefully, incorrect use
1827 * of the socket outside of the library will lead to undefined behavior.
1828 * Additionally rabbitmq-c may use the socket differently version-to-version,
1829 * what may work in one version, may break in the next version. Be sure to
1830 * throughly test any applications that use the socket returned by this
1831 * function especially when using a newer version of rabbitmq-c
1832 *
1833 */
1834 2 ▶ if ( has_valid_connection( conn ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( has_valid_connection( conn ) ) { 0 2 2
if ( has_valid_connection( conn ) ) { 0 0 0
1835 0 RETVAL = newSViv( amqp_get_sockfd(conn) );
1836 }
1837 else {
1838 // We don't have a connection, we're still here.
1839 2 RETVAL = &PL_sv_undef;
1840 }
1841 OUTPUT:
1842 RETVAL
1843  
1844 SV*
1845 net_amqp_rabbitmq_is_connected(conn)
1846 Net::AMQP::RabbitMQ conn
1847 CODE:
1848 15 ▶ if ( has_valid_connection( conn ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( has_valid_connection( conn ) ) { 1 14 15
if ( has_valid_connection( conn ) ) { 0 1 1
1849 0 RETVAL = newSViv(1);
1850 }
1851 else {
1852 // We don't have a connection, we're still here.
1853 15 RETVAL = &PL_sv_undef;
1854 }
1855 OUTPUT:
1856 RETVAL
1857  
1858 void
1859 net_amqp_rabbitmq_disconnect(conn)
1860 Net::AMQP::RabbitMQ conn
1861 PREINIT:
1862 int sockfd;
1863 CODE:
1864 8 ▶ if ( amqp_get_socket(conn) != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( amqp_get_socket(conn) != NULL ) { 0 8 8
1865 0 amqp_connection_close(conn, AMQP_REPLY_SUCCESS);
1866 0 amqp_socket_close( amqp_get_socket( conn ) );
1867 }
1868  
1869 Net::AMQP::RabbitMQ
1870 net_amqp_rabbitmq__new(clazz)
1871 char *clazz
1872 CODE:
1873 27 RETVAL = amqp_new_connection();
1874 OUTPUT:
1875 RETVAL
1876  
1877 void
1878 net_amqp_rabbitmq__destroy_connection_close(conn)
1879 Net::AMQP::RabbitMQ conn
1880 CODE:
1881 26 ▶ if ( amqp_get_socket(conn) != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( amqp_get_socket(conn) != NULL ) { 2 24 26
1882 2 amqp_connection_close(conn, AMQP_REPLY_SUCCESS);
1883 }
1884  
1885 void
1886 net_amqp_rabbitmq__destroy_cleanup(conn)
1887 Net::AMQP::RabbitMQ conn
1888 CODE:
1889 27 empty_amqp_pool( &temp_memory_pool );
1890 27 amqp_destroy_connection(conn);
1891  
1892 void
1893 net_amqp_rabbitmq_heartbeat(conn)
1894 Net::AMQP::RabbitMQ conn
1895 PREINIT:
1896 amqp_frame_t f;
1897 CODE:
1898 0 f.frame_type = AMQP_FRAME_HEARTBEAT;
1899 0 f.channel = 0;
1900 0 amqp_send_frame(conn, &f);
1901  
1902 void
1903 net_amqp_rabbitmq_tx_select(conn, channel, args = NULL)
1904 Net::AMQP::RabbitMQ conn
1905 int channel
1906 HV *args
1907 CODE:
1908 0 amqp_tx_select(conn, channel);
1909 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Selecting transaction");
1910  
1911 void
1912 net_amqp_rabbitmq_tx_commit(conn, channel, args = NULL)
1913 Net::AMQP::RabbitMQ conn
1914 int channel
1915 HV *args
1916 CODE:
1917 0 amqp_tx_commit(conn, channel);
1918 0 maybe_release_buffers(conn);
1919 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Commiting transaction");
1920  
1921 void
1922 net_amqp_rabbitmq_tx_rollback(conn, channel, args = NULL)
1923 Net::AMQP::RabbitMQ conn
1924 int channel
1925 HV *args
1926 CODE:
1927 0 amqp_tx_rollback(conn, channel);
1928 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Rolling Back transaction");
1929  
1930 void
1931 net_amqp_rabbitmq_basic_qos(conn, channel, args = NULL)
1932 Net::AMQP::RabbitMQ conn
1933 int channel
1934 HV *args
1935 PREINIT:
1936 SV **v;
1937 0 uint32_t prefetch_size = 0;
1938 0 uint16_t prefetch_count = 0;
1939 0 ▶ amqp_boolean_t global = 0;
Branch: 1 branch
BranchTrueFalseTotal
amqp_boolean_t global = 0; 0 0 0
1940 CODE:
1941 0 ▶ if(args) {
Branch: 1 branch
BranchTrueFalseTotal
if(args) { 0 0 0
1942 0 ▶ if(NULL != (v = hv_fetchs(args, "prefetch_size", 0))) prefetch_size = SvIV(*v);
Branch: 1 branch
BranchTrueFalseTotal
if(NULL != (v = hv_fetchs(args, "prefetch_size", 0))) prefetch_size = SvIV(*v); 0 0 0
1943 0 ▶ if(NULL != (v = hv_fetchs(args, "prefetch_count", 0))) prefetch_count = SvIV(*v);
Branch: 1 branch
BranchTrueFalseTotal
if(NULL != (v = hv_fetchs(args, "prefetch_count", 0))) prefetch_count = SvIV(*v); 0 0 0
1944 0 ▶ if(NULL != (v = hv_fetchs(args, "global", 0))) global = SvIV(*v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
if(NULL != (v = hv_fetchs(args, "global", 0))) global = SvIV(*v) ? 1 : 0; 0 0 0
1945 }
1946 0 amqp_basic_qos(conn, channel,
1947 prefetch_size, prefetch_count, global);
1948 0 die_on_amqp_error(aTHX_ amqp_get_rpc_reply(conn), conn, "Basic QoS");
1949  
1950 SV* net_amqp_rabbitmq_get_server_properties(conn)
1951 Net::AMQP::RabbitMQ conn
1952 PREINIT:
1953 amqp_table_t* server_properties;
1954 CODE:
1955 2 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 0 2 2
assert_amqp_connected(conn); 0 0 0
1956 0 server_properties = amqp_get_server_properties(conn);
1957 0 ▶ if ( server_properties )
Branch: 1 branch
BranchTrueFalseTotal
if ( server_properties ) 0 0 0
1958 {
1959 0 RETVAL = mq_table_to_hashref(server_properties);
1960 }
1961 else
1962 {
1963 0 RETVAL = &PL_sv_undef;
1964 }
1965 OUTPUT:
1966 RETVAL
1967  
1968 SV* net_amqp_rabbitmq_get_client_properties(conn)
1969 Net::AMQP::RabbitMQ conn
1970 PREINIT:
1971 amqp_table_t* client_properties;
1972 CODE:
1973 2 ▶ assert_amqp_connected(conn);
Branch: 2 branches
BranchTrueFalseTotal
assert_amqp_connected(conn); 0 2 2
assert_amqp_connected(conn); 0 0 0
1974 0 client_properties = amqp_get_client_properties(conn);
1975 0 ▶ if ( client_properties )
Branch: 1 branch
BranchTrueFalseTotal
if ( client_properties ) 0 0 0
1976 {
1977 0 RETVAL = mq_table_to_hashref(client_properties);
1978 }
1979 else
1980 {
1981 0 RETVAL = &PL_sv_undef;
1982 }
1983 OUTPUT:
1984 RETVAL
1985  
1986 SV* net_amqp_rabbitmq_has_ssl()
1987 CODE:
1988 #ifdef NAR_HAVE_OPENSSL
1989 1 RETVAL = &PL_sv_yes;
1990 #else
1991 RETVAL = &PL_sv_no;
1992 #endif
1993 OUTPUT:
1994 RETVAL
« amqp_api.c ↑ Summary amqp_openssl.c »