Debian Codegraph @

ruby/cproton.i

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 %module cproton
20
21 %{
22 #define PN_USE_DEPRECATED_API 1
23
24 #include <proton/connection_driver.h>
25 #include <proton/engine.h>
26 #include <proton/handlers.h>
27 #include <proton/message.h>
28 #include <proton/messenger.h>
29 #include <proton/reactor.h>
30 #include <proton/sasl.h>
31 #include <proton/ssl.h>
32 #include <proton/types.h>
33 #include <proton/url.h>
34 %}
35
36 /*
37 NOTE: According to ccache-swig man page: "Known problems are using
38 preprocessor directives within %inline blocks and the use of ’#pragma SWIG’."
39 This includes using macros in an %inline section.
40
41 Keep preprocessor directives and macro expansions in the normal header section.
42 */
43
44 %include <cstring.i>
45
46 %cstring_output_withsize(char *OUTPUT, size_t *OUTPUT_SIZE)
47 %cstring_output_allocate_size(char **ALLOC_OUTPUT, size_t *ALLOC_SIZE, free(*$1));
48 %cstring_output_maxsize(char *OUTPUT, size_t MAX_OUTPUT_SIZE)
49
50 %{
51 #if !defined(RSTRING_LEN)
52 # define RSTRING_LEN(x) (RSTRING(x)->len)
53 # define RSTRING_PTR(x) (RSTRING(x)->ptr)
54 #endif
55 %}
56
57 /* Treat pn_handle_t like uintptr_t - syntactically it is a C void* but really it's just an int */
58 %apply uintptr_t { pn_handle_t };
59
60 %typemap(in) pn_bytes_t {
61 if ($input == Qnil) {
62 $1.start = NULL;
63 $1.size = 0;
64 } else {
65 $1.start = RSTRING_PTR($input);
66 if (!$1.start) {
67 $1.size = 0;
68 }
69 $1.size = RSTRING_LEN($input);
70 }
71 }
72
73 %typemap(out) pn_bytes_t {
74 $result = rb_str_new($1.start, $1.size);
75 }
76
77 %typemap(in) pn_atom_t
78 {
79 if ($input == Qnil)
80 {
81 $1.type = PN_NULL;
82 }
83 else
84 {
85 switch(TYPE($input))
86 {
87 case T_TRUE:
88 $1.type = PN_BOOL;
89 $1.u.as_bool = true;
90 break;
91
92 case T_FALSE:
93 $1.type = PN_BOOL;
94 $1.u.as_bool = false;
95 break;
96
97 case T_FLOAT:
98 $1.type = PN_FLOAT;
99 $1.u.as_float = NUM2DBL($input);
100 break;
101
102 case T_STRING:
103 $1.type = PN_STRING;
104 $1.u.as_bytes.start = RSTRING_PTR($input);
105 if ($1.u.as_bytes.start)
106 {
107 $1.u.as_bytes.size = RSTRING_LEN($input);
108 }
109 else
110 {
111 $1.u.as_bytes.size = 0;
112 }
113 break;
114
115 case T_FIXNUM:
116 $1.type = PN_INT;
117 $1.u.as_int = FIX2LONG($input);
118 break;
119
120 case T_BIGNUM:
121 $1.type = PN_LONG;
122 $1.u.as_long = NUM2LL($input);
123 break;
124
125 }
126 }
127 }
128
129 %typemap(out) pn_atom_t
130 {
131 switch($1.type)
132 {
133 case PN_NULL:
134 $result = Qnil;
135 break;
136
137 case PN_BOOL:
138 $result = $1.u.as_bool ? Qtrue : Qfalse;
139 break;
140
141 case PN_BYTE:
142 $result = INT2NUM($1.u.as_byte);
143 break;
144
145 case PN_UBYTE:
146 $result = UINT2NUM($1.u.as_ubyte);
147 break;
148
149 case PN_SHORT:
150 $result = INT2NUM($1.u.as_short);
151 break;
152
153 case PN_USHORT:
154 $result = UINT2NUM($1.u.as_ushort);
155 break;
156
157 case PN_INT:
158 $result = INT2NUM($1.u.as_int);
159 break;
160
161 case PN_UINT:
162 $result = UINT2NUM($1.u.as_uint);
163 break;
164
165 case PN_LONG:
166 $result = LL2NUM($1.u.as_long);
167 break;
168
169 case PN_ULONG:
170 $result = ULL2NUM($1.u.as_ulong);
171 break;
172
173 case PN_FLOAT:
174 $result = rb_float_new($1.u.as_float);
175 break;
176
177 case PN_DOUBLE:
178 $result = rb_float_new($1.u.as_double);
179 break;
180
181 case PN_STRING:
182 $result = rb_str_new($1.u.as_bytes.start, $1.u.as_bytes.size);
183 break;
184
185 default:
186 break;
187 }
188 }
189
190 %typemap (in) pn_decimal32_t
191 {
192 $1 = NUM2UINT($input);
193 }
194
195 %typemap (out) pn_decimal32_t
196 {
197 $result = UINT2NUM($1);
198 }
199
200 %typemap (in) pn_decimal64_t
201 {
202 $1 = NUM2ULL($input);
203 }
204
205 %typemap (out) pn_decimal64_t
206 {
207 $result = ULL2NUM($1);
208 }
209
210 %typemap (in) pn_decimal128_t
211 {
212 int index;
213
214 for(index = 0; index < 16; index++)
215 {
216 VALUE element = rb_ary_entry($input, index);
217 $1.bytes[16 - (index + 1)] = FIX2INT(element);
218 }
219 }
220
221 %typemap (out) pn_decimal128_t
222 {
223 int index;
224
225 $result = rb_ary_new2(16);
226 for(index = 0; index < 16; index++)
227 {
228 rb_ary_store($result, 16 - (index + 1), CHR2FIX($1.bytes[index]));
229 }
230 }
231
232 %typemap (in) pn_uuid_t
233 {
234 int index;
235
236 for(index = 0; index < 16; index++)
237 {
238 VALUE element = rb_ary_entry($input, index);
239 $1.bytes[16 - (index + 1)] = FIX2INT(element);
240 }
241 }
242
243 %typemap (out) pn_uuid_t
244 {
245 int index;
246
247 $result = rb_ary_new2(16);
248 for(index = 0; index < 16; index++)
249 {
250 rb_ary_store($result, 16 - (index + 1), CHR2FIX($1.bytes[index]));
251 }
252 }
253
254 int pn_message_encode(pn_message_t *msg, char *OUTPUT, size_t *OUTPUT_SIZE);
255 %ignore pn_message_encode;
256
257 ssize_t pn_link_send(pn_link_t *transport, char *STRING, size_t LENGTH);
258 %ignore pn_link_send;
259
260 %rename(pn_link_recv) wrap_pn_link_recv;
261 %inline %{
262 int wrap_pn_link_recv(pn_link_t *link, char *OUTPUT, size_t *OUTPUT_SIZE) {
263 ssize_t sz = pn_link_recv(link, OUTPUT, *OUTPUT_SIZE);
264 if (sz >= 0) {
265 *OUTPUT_SIZE = sz;
266 } else {
267 *OUTPUT_SIZE = 0;
268 }
269 return sz;
270 }
271 %}
272 %ignore pn_link_recv;
273
274 ssize_t pn_transport_input(pn_transport_t *transport, char *STRING, size_t LENGTH);
275 %ignore pn_transport_input;
276
277 %rename(pn_transport_output) wrap_pn_transport_output;
278 %inline %{
279 int wrap_pn_transport_output(pn_transport_t *transport, char *OUTPUT, size_t *OUTPUT_SIZE) {
280 ssize_t sz = pn_transport_output(transport, OUTPUT, *OUTPUT_SIZE);
281 if (sz >= 0) {
282 *OUTPUT_SIZE = sz;
283 } else {
284 *OUTPUT_SIZE = 0;
285 }
286 return sz;
287 }
288 %}
289 %ignore pn_transport_output;
290
291 %rename(pn_transport_peek) wrap_pn_transport_peek;
292 %inline %{
293 int wrap_pn_transport_peek(pn_transport_t *transport, char *OUTPUT, size_t *OUTPUT_SIZE) {
294 ssize_t sz = pn_transport_peek(transport, OUTPUT, *OUTPUT_SIZE);
295 if(sz >= 0) {
296 *OUTPUT_SIZE = sz;
297 } else {
298 *OUTPUT_SIZE = 0;
299 }
300 return sz;
301 }
302 %}
303 %ignore pn_transport_peek;
304
305 %rename(pn_delivery) wrap_pn_delivery;
306 %inline %{
307 pn_delivery_t *wrap_pn_delivery(pn_link_t *link, char *STRING, size_t LENGTH) {
308 return pn_delivery(link, pn_dtag(STRING, LENGTH));
309 }
310 %}
311 %ignore pn_delivery;
312
313 // Suppress "Warning(451): Setting a const char * variable may leak memory." on pn_delivery_tag_t
314 %warnfilter(451) pn_delivery_tag_t;
315 %rename(pn_delivery_tag) wrap_pn_delivery_tag;
316 %inline %{
317 void wrap_pn_delivery_tag(pn_delivery_t *delivery, char **ALLOC_OUTPUT, size_t *ALLOC_SIZE) {
318 pn_delivery_tag_t tag = pn_delivery_tag(delivery);
319 *ALLOC_OUTPUT = malloc(tag.size);
320 *ALLOC_SIZE = tag.size;
321 memcpy(*ALLOC_OUTPUT, tag.start, tag.size);
322 }
323 %}
324 %ignore pn_delivery_tag;
325
326 bool pn_ssl_get_cipher_name(pn_ssl_t *ssl, char *OUTPUT, size_t MAX_OUTPUT_SIZE);
327 %ignore pn_ssl_get_cipher_name;
328
329 bool pn_ssl_get_protocol_name(pn_ssl_t *ssl, char *OUTPUT, size_t MAX_OUTPUT_SIZE);
330 %ignore pn_ssl_get_protocol_name;
331
332 /* TODO aconway 2018-02-14: Remove RB_BLOCKING_CALL once messenger is deprecated */
333
334 /* Don't use %inline sections for #define */
335 %{
336 #if defined(RUBY_USE_rb_thread_call_without_gvl)
337
338 #include <ruby/thread.h>
339 typedef void *non_blocking_return_t;
340 #define RB_BLOCKING_CALL (VALUE)rb_thread_call_without_gvl
341
342 #elif defined(RUBY_USE_rb_thread_blocking_region)
343
344 typedef VALUE non_blocking_return_t;
345 #define RB_BLOCKING_CALL rb_thread_blocking_region
346
347 #endif
348 %}
349
350 %rename(pn_messenger_send) wrap_pn_messenger_send;
351 %rename(pn_messenger_recv) wrap_pn_messenger_recv;
352 %rename(pn_messenger_work) wrap_pn_messenger_work;
353
354 %inline %{
355
356 #if defined(RB_BLOCKING_CALL)
357
358 static non_blocking_return_t pn_messenger_send_no_gvl(void *args) {
359 VALUE result = Qnil;
360 pn_messenger_t *messenger = (pn_messenger_t *)((void **)args)[0];
361 int *limit = (int *)((void **)args)[1];
362
363 int rc = pn_messenger_send(messenger, *limit);
364
365 result = INT2NUM(rc);
366 return (non_blocking_return_t )result;
367 }
368
369 static non_blocking_return_t pn_messenger_recv_no_gvl(void *args) {
370 VALUE result = Qnil;
371 pn_messenger_t *messenger = (pn_messenger_t *)((void **)args)[0];
372 int *limit = (int *)((void **)args)[1];
373
374 int rc = pn_messenger_recv(messenger, *limit);
375
376 result = INT2NUM(rc);
377 return (non_blocking_return_t )result;
378 }
379
380 static non_blocking_return_t pn_messenger_work_no_gvl(void *args) {
381 VALUE result = Qnil;
382 pn_messenger_t *messenger = (pn_messenger_t *)((void **)args)[0];
383 int *timeout = (int *)((void **)args)[1];
384
385 int rc = pn_messenger_work(messenger, *timeout);
386
387 result = INT2NUM(rc);
388 return (non_blocking_return_t )result;
389 }
390
391 #endif
392
393 int wrap_pn_messenger_send(pn_messenger_t *messenger, int limit) {
394 int result = 0;
395
396 #if defined(RB_BLOCKING_CALL)
397
398 // only release the gil if we're blocking
399 if(pn_messenger_is_blocking(messenger)) {
400 VALUE rc;
401 void* args[2];
402
403 args[0] = messenger;
404 args[1] = &limit;
405
406 rc = RB_BLOCKING_CALL(pn_messenger_send_no_gvl,
407 &args, RUBY_UBF_PROCESS, NULL);
408
409 if(RTEST(rc))
410 {
411 result = FIX2INT(rc);
412 }
413 }
414
415 #else // !defined(RB_BLOCKING_CALL)
416 result = pn_messenger_send(messenger, limit);
417 #endif // defined(RB_BLOCKING_CALL)
418
419 return result;
420 }
421
422 int wrap_pn_messenger_recv(pn_messenger_t *messenger, int limit) {
423 int result = 0;
424
425 #if defined(RB_BLOCKING_CALL)
426 // only release the gil if we're blocking
427 if(pn_messenger_is_blocking(messenger)) {
428 VALUE rc;
429 void* args[2];
430
431 args[0] = messenger;
432 args[1] = &limit;
433
434 rc = RB_BLOCKING_CALL(pn_messenger_recv_no_gvl,
435 &args, RUBY_UBF_PROCESS, NULL);
436
437 if(RTEST(rc))
438 {
439 result = FIX2INT(rc);
440 }
441
442 } else {
443 result = pn_messenger_recv(messenger, limit);
444 }
445 #else // !defined(RB_BLOCKING_CALL)
446 result = pn_messenger_recv(messenger, limit);
447 #endif // defined(RB_BLOCKING_CALL)
448
449 return result;
450 }
451
452 int wrap_pn_messenger_work(pn_messenger_t *messenger, int timeout) {
453 int result = 0;
454
455 #if defined(RB_BLOCKING_CALL)
456 // only release the gil if we're blocking
457 if(timeout) {
458 VALUE rc;
459 void* args[2];
460
461 args[0] = messenger;
462 args[1] = &timeout;
463
464 rc = RB_BLOCKING_CALL(pn_messenger_work_no_gvl,
465 &args, RUBY_UBF_PROCESS, NULL);
466
467 if(RTEST(rc))
468 {
469 result = FIX2INT(rc);
470 }
471 } else {
472 result = pn_messenger_work(messenger, timeout);
473 }
474 #else
475 result = pn_messenger_work(messenger, timeout);
476 #endif
477
478 return result;
479 }
480
481 %}
482
483 %ignore pn_messenger_send;
484 %ignore pn_messenger_recv;
485 %ignore pn_messenger_work;
486
487 %{
488 typedef struct Pn_rbkey_t {
489 void *registry;
490 char *method;
491 char *key_value;
492 } Pn_rbkey_t;
493
494 void Pn_rbkey_initialize(void *vp_rbkey) {
495 Pn_rbkey_t *rbkey = (Pn_rbkey_t*)vp_rbkey;
496 assert(rbkey);
497 rbkey->registry = NULL;
498 rbkey->method = NULL;
499 rbkey->key_value = NULL;
500 }
501
502 void Pn_rbkey_finalize(void *vp_rbkey) {
503 Pn_rbkey_t *rbkey = (Pn_rbkey_t*)vp_rbkey;
504 if(rbkey && rbkey->registry && rbkey->method && rbkey->key_value) {
505 rb_funcall((VALUE )rbkey->registry, rb_intern(rbkey->method), 1, rb_str_new2(rbkey->key_value));
506 }
507 if(rbkey->key_value) {
508 free(rbkey->key_value);
509 rbkey->key_value = NULL;
510 }
511 }
512
513 /* NOTE: no macro or preprocessor definitions in %inline sections */
514 #define CID_Pn_rbkey CID_pn_void
515 #define Pn_rbkey_inspect NULL
516 #define Pn_rbkey_compare NULL
517 #define Pn_rbkey_hashcode NULL
518
519 pn_class_t* Pn_rbkey__class(void) {
520 static pn_class_t clazz = PN_CLASS(Pn_rbkey);
521 return &clazz;
522 }
523
524 Pn_rbkey_t *Pn_rbkey_new(void) {
525 return (Pn_rbkey_t *) pn_class_new(Pn_rbkey__class(), sizeof(Pn_rbkey_t));
526 }
527 %}
528
529 pn_class_t* Pn_rbkey__class(void);
530 Pn_rbkey_t *Pn_rbkey_new(void);
531
532 %inline %{
533
534 Pn_rbkey_t *Pn_rbkey_new(void);
535
536 void Pn_rbkey_set_registry(Pn_rbkey_t *rbkey, void *registry) {
537 assert(rbkey);
538 rbkey->registry = registry;
539 }
540
541 void *Pn_rbkey_get_registry(Pn_rbkey_t *rbkey) {
542 assert(rbkey);
543 return rbkey->registry;
544 }
545
546 void Pn_rbkey_set_method(Pn_rbkey_t *rbkey, char *method) {
547 assert(rbkey);
548 rbkey->method = method;
549 }
550
551 char *Pn_rbkey_get_method(Pn_rbkey_t *rbkey) {
552 assert(rbkey);
553 return rbkey->method;
554 }
555
556 void Pn_rbkey_set_key_value(Pn_rbkey_t *rbkey, char *key_value) {
557 assert(rbkey);
558 rbkey->key_value = malloc(strlen(key_value) + 1);
559 strncpy(rbkey->key_value, key_value, strlen(key_value) + 1);
560 }
561
562 char *Pn_rbkey_get_key_value(Pn_rbkey_t *rbkey) {
563 assert(rbkey);
564 return rbkey->key_value;
565 }
566
567 Pn_rbkey_t *pni_void2rbkey(void *object) {
568 return (Pn_rbkey_t *)object;
569 }
570
571 VALUE pn_void2rb(void *object) {
572 return (VALUE )object;
573 }
574
575 void *pn_rb2void(VALUE object) {
576 return (void *)object;
577 }
578
579 VALUE pni_address_of(void *object) {
580 return ULL2NUM((unsigned long )object);
581 }
582
583 %}
584
585 int pn_ssl_get_peer_hostname(pn_ssl_t *ssl, char *OUTPUT, size_t *OUTPUT_SIZE);
586 %ignore pn_ssl_get_peer_hostname;
587
588 %inline %{
589
590 VALUE pni_ruby_get_proton_module() {
591 VALUE mQpid = rb_define_module("Qpid");
592 return rb_define_module_under(mQpid, "Proton");
593 }
594
595 void pni_ruby_add_to_registry(VALUE key, VALUE value) {
596 VALUE result = rb_funcall(pni_ruby_get_proton_module(), rb_intern("add_to_registry"), 2, key, value);
597 }
598
599 VALUE pni_ruby_get_from_registry(VALUE key) {
600 return rb_funcall(pni_ruby_get_proton_module(), rb_intern("get_from_registry"), 1, key);
601 }
602
603 void pni_ruby_delete_from_registry(VALUE stored_key) {
604 rb_funcall(pni_ruby_get_proton_module(), rb_intern("delete_from_registry"), 1, stored_key);
605 }
606
607 typedef struct {
608 VALUE handler_key;
609 } Pni_rbhandler_t;
610
611 static Pni_rbhandler_t *pni_rbhandler(pn_handler_t *handler) {
612 return (Pni_rbhandler_t *) pn_handler_mem(handler);
613 }
614
615 static void pni_rbdispatch(pn_handler_t *handler, pn_event_t *event, pn_event_type_t type) {
616 Pni_rbhandler_t *rbh = pni_rbhandler(handler);
617 VALUE rbhandler = pni_ruby_get_from_registry(rbh->handler_key);
618
619 rb_funcall(rbhandler, rb_intern("dispatch"), 2, SWIG_NewPointerObj(event, SWIGTYPE_p_pn_event_t, 0), INT2FIX(type));
620 }
621
622 static void pni_rbhandler_finalize(pn_handler_t *handler) {
623 Pni_rbhandler_t *rbh = pni_rbhandler(handler);
624 pni_ruby_delete_from_registry(rbh->handler_key);
625 }
626
627 pn_handler_t *pn_rbhandler(VALUE handler) {
628 pn_handler_t *chandler = pn_handler_new(pni_rbdispatch, sizeof(Pni_rbhandler_t), pni_rbhandler_finalize);
629 Pni_rbhandler_t *rhy = pni_rbhandler(chandler);
630
631 VALUE ruby_key = rb_class_new_instance(0, NULL, rb_cObject);
632 pni_ruby_add_to_registry(ruby_key, handler);
633
634 rhy->handler_key = ruby_key;
635
636 return chandler;
637 }
638
639
640 /* Helpers for working with pn_connection_driver_t */
641
642 size_t pni_connection_driver_read_size(pn_connection_driver_t* d) {
643 return pn_connection_driver_read_buffer(d).size;
644 }
645
646 size_t pni_connection_driver_write_size(pn_connection_driver_t* d) {
647 return pn_connection_driver_write_buffer(d).size;
648 }
649
650 pn_connection_t *pni_connection_driver_connection(pn_connection_driver_t* d) {
651 return d->connection;
652 }
653
654 pn_transport_t *pni_connection_driver_transport(pn_connection_driver_t* d) {
655 return d->transport;
656 }
657
658 size_t pni_connection_driver_read_copy(pn_connection_driver_t* d, char *STRING, size_t LENGTH ) {
659 pn_rwbytes_t rbuf = pn_connection_driver_read_buffer(d);
660 size_t n = LENGTH < rbuf.size ? LENGTH : rbuf.size;
661 memcpy(rbuf.start, STRING, n);
662 pn_connection_driver_read_done(d, n);
663 return n;
664 }
665
666 pn_connection_driver_t *pni_connection_driver() {
667 pn_connection_driver_t *d = (pn_connection_driver_t*)malloc(sizeof(*d));
668 if (pn_connection_driver_init(d, NULL, NULL) != 0) {
669 free(d);
670 return NULL;
671 }
672 return d;
673 }
674
675 %}
676
677 %include "proton/cproton.i"
678
679 %include "proton/url.h"