RabbitMQ.xs

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