forked from php-amqp/php-amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amqp_queue.c
1486 lines (1196 loc) · 42.4 KB
/
amqp_queue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2007 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Alexandre Kalendarev akalend@mail.ru Copyright (c) 2009-2010 |
| Lead: |
| - Pieter de Zwart |
| Maintainers: |
| - Brad Rodriguez |
| - Jonathan Tansavatdi |
+----------------------------------------------------------------------+
*/
/* $Id: amqp_queue.c 327551 2012-09-09 03:49:34Z pdezwart $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "zend_exceptions.h"
#ifdef PHP_WIN32
# include "win32/php_stdint.h"
# include "win32/signal.h"
#else
# include <signal.h>
# include <stdint.h>
#endif
#include <amqp.h>
#include <amqp_framing.h>
#ifdef PHP_WIN32
# include "win32/unistd.h"
#else
# include <unistd.h>
#endif
#include "php_amqp.h"
#if PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3
zend_object_handlers amqp_queue_object_handlers;
HashTable *amqp_queue_object_get_debug_info(zval *object, int *is_temp TSRMLS_DC) {
zval *value;
HashTable *debug_info;
/* Get the envelope object from which to read */
amqp_queue_object *queue = (amqp_queue_object *)zend_object_store_get_object(object TSRMLS_CC);
/* Let zend clean up for us: */
*is_temp = 1;
/* Keep the # 7 matching the number of entries in this table*/
ALLOC_HASHTABLE(debug_info);
ZEND_INIT_SYMTABLE_EX(debug_info, 7 + 1, 0);
/* Start adding values */
MAKE_STD_ZVAL(value);
ZVAL_STRINGL(value, queue->name, strlen(queue->name), 1);
zend_hash_add(debug_info, "queue_name", sizeof("queue_name"), &value, sizeof(zval *), NULL);
MAKE_STD_ZVAL(value);
ZVAL_STRINGL(value, queue->consumer_tag, strlen(queue->consumer_tag), 1);
zend_hash_add(debug_info, "consumer_tag", sizeof("consumer_tag"), &value, sizeof(zval *), NULL);
MAKE_STD_ZVAL(value);
ZVAL_BOOL(value, queue->passive);
zend_hash_add(debug_info, "passive", sizeof("passive"), &value, sizeof(zval *), NULL);
MAKE_STD_ZVAL(value);
ZVAL_BOOL(value, queue->durable);
zend_hash_add(debug_info, "durable", sizeof("durable"), &value, sizeof(zval *), NULL);
MAKE_STD_ZVAL(value);
ZVAL_BOOL(value, queue->exclusive);
zend_hash_add(debug_info, "exclusive", sizeof("exclusive"), &value, sizeof(zval *), NULL);
MAKE_STD_ZVAL(value);
ZVAL_BOOL(value, queue->auto_delete);
zend_hash_add(debug_info, "auto_delete", sizeof("auto_delete"), &value, sizeof(zval *), NULL);
zend_hash_add(debug_info, "arguments", sizeof("arguments"), &queue->arguments, sizeof(&queue->arguments), NULL);
return debug_info;
}
#endif
/* Used in ctor, so must be declated first */
void amqp_queue_dtor(void *object TSRMLS_DC)
{
amqp_queue_object *queue = (amqp_queue_object*)object;
/* Destroy the connection object */
if (queue->channel) {
zval_ptr_dtor(&queue->channel);
}
/* Destroy the arguments storage */
if (queue->arguments) {
zval_ptr_dtor(&queue->arguments);
}
zend_object_std_dtor(&queue->zo TSRMLS_CC);
/* Destroy this object */
efree(object);
}
zend_object_value amqp_queue_ctor(zend_class_entry *ce TSRMLS_DC)
{
zend_object_value new_value;
amqp_queue_object* queue = (amqp_queue_object*)emalloc(sizeof(amqp_queue_object));
memset(queue, 0, sizeof(amqp_queue_object));
zend_object_std_init(&queue->zo, ce TSRMLS_CC);
AMQP_OBJECT_PROPERTIES_INIT(queue->zo, ce);
/* Initialize the arguments array: */
MAKE_STD_ZVAL(queue->arguments);
array_init(queue->arguments);
new_value.handle = zend_objects_store_put(
queue,
(zend_objects_store_dtor_t)zend_objects_destroy_object,
(zend_objects_free_object_storage_t)amqp_queue_dtor,
NULL TSRMLS_CC
);
#if PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3
memcpy((void *)&amqp_queue_object_handlers, (void *)zend_get_std_object_handlers(), sizeof(zend_object_handlers));
amqp_queue_object_handlers.get_debug_info = amqp_queue_object_get_debug_info;
new_value.handlers = &amqp_queue_object_handlers;
#else
new_value.handlers = zend_get_std_object_handlers();
#endif
return new_value;
}
void parse_amqp_table(amqp_table_t *table, zval *result)
{
int i;
array_init(result);
for (i = 0; i < table->num_entries; i++) {
zval *value;
amqp_table_entry_t *entry = &(table->entries[i]);
MAKE_STD_ZVAL(value);
switch (entry->value.kind) {
case AMQP_FIELD_KIND_BOOLEAN:
ZVAL_BOOL(value, entry->value.value.boolean);
break;
case AMQP_FIELD_KIND_I8:
ZVAL_LONG(value, entry->value.value.i8);
break;
case AMQP_FIELD_KIND_U8:
ZVAL_LONG(value, entry->value.value.u8);
break;
case AMQP_FIELD_KIND_I16:
ZVAL_LONG(value, entry->value.value.i16);
break;
case AMQP_FIELD_KIND_U16:
ZVAL_LONG(value, entry->value.value.u16);
break;
case AMQP_FIELD_KIND_I32:
ZVAL_LONG(value, entry->value.value.i32);
break;
case AMQP_FIELD_KIND_U32:
ZVAL_LONG(value, entry->value.value.u32);
break;
case AMQP_FIELD_KIND_I64:
ZVAL_LONG(value, entry->value.value.i64);
break;
case AMQP_FIELD_KIND_U64:
ZVAL_LONG(value, entry->value.value.i64);
break;
case AMQP_FIELD_KIND_F32:
ZVAL_DOUBLE(value, entry->value.value.f32);
break;
case AMQP_FIELD_KIND_F64:
ZVAL_DOUBLE(value, entry->value.value.f64);
break;
case AMQP_FIELD_KIND_UTF8:
case AMQP_FIELD_KIND_BYTES:
ZVAL_STRINGL(value, entry->value.value.bytes.bytes, entry->value.value.bytes.len, 1);
break;
case AMQP_FIELD_KIND_ARRAY:
{
int j;
array_init(value);
for (j = 0; j < entry->value.value.array.num_entries; ++j) {
switch (entry->value.value.array.entries[j].kind) {
case AMQP_FIELD_KIND_UTF8:
add_next_index_stringl(
value,
entry->value.value.array.entries[j].value.bytes.bytes,
entry->value.value.array.entries[j].value.bytes.len,
1
);
break;
case AMQP_FIELD_KIND_TABLE:
{
zval *subtable;
MAKE_STD_ZVAL(subtable);
parse_amqp_table(
&(entry->value.value.array.entries[j].value.table),
subtable
);
add_next_index_zval(value, subtable);
}
break;
}
}
}
break;
case AMQP_FIELD_KIND_TABLE:
array_init(value);
parse_amqp_table(&(entry->value.value.table), value);
break;
case AMQP_FIELD_KIND_TIMESTAMP:
ZVAL_DOUBLE(value, entry->value.value.u64);
break;
case AMQP_FIELD_KIND_VOID:
case AMQP_FIELD_KIND_DECIMAL:
default:
ZVAL_NULL(value);
}
if (Z_TYPE_P(value) != IS_NULL) {
char *key = estrndup(entry->key.bytes, entry->key.len);
add_assoc_zval(result, key, value);
efree(key);
} else {
zval_dtor(value);
}
}
return;
}
/*
Read a message that is pending on a channel. A call to basic.get or basic.consume must preceed this call.
*/
int read_message_from_channel(amqp_connection_state_t connection, zval *envelopeZval TSRMLS_DC) {
size_t body_received = 0;
size_t body_target = 0;
char *message_body_buffer = NULL;
amqp_frame_t frame;
int result = 0;
amqp_envelope_object *envelope;
amqp_basic_properties_t * p;
/* Build the envelope */
object_init_ex(envelopeZval, amqp_envelope_class_entry);
envelope = (amqp_envelope_object *)zend_object_store_get_object(envelopeZval TSRMLS_CC);
/* The "standard" for this is to continuously loop over frames in the pipeline until an entire message is read */
while (1) {
/* Release pending buffers for ingestion */
amqp_maybe_release_buffers(connection);
result = amqp_simple_wait_frame(connection, &frame);
/* Check that the basic read from frame did not fail */
if (result < 0) {
zend_throw_exception(amqp_connection_exception_class_entry, amqp_error_string(-result), -result TSRMLS_CC);
return AMQP_READ_ERROR;
}
if (frame.payload.method.id == AMQP_CHANNEL_CLOSE_OK_METHOD) {
amqp_channel_close_t *err = (amqp_channel_close_t *)frame.payload.method.decoded;
char str[256];
char **pstr = (char **)&str;
if (err) {
spprintf(pstr, 0, "Server error: %d", (int)err->reply_code);
} else {
spprintf(pstr, 0, "Unknown server error occurred.");
}
zend_throw_exception(amqp_queue_exception_class_entry, *pstr, 0 TSRMLS_CC);
return AMQP_READ_ERROR;
}
/* Verify that we have a method frame first */
if (frame.frame_type != AMQP_FRAME_METHOD) {
continue;
}
if (frame.payload.method.id == AMQP_BASIC_GET_OK_METHOD) {
/* get message metadata */
amqp_basic_get_ok_t *delivery = (amqp_basic_get_ok_t *) frame.payload.method.decoded;
AMQP_SET_STR_PROPERTY(envelope->routing_key, delivery->routing_key.bytes, delivery->routing_key.len);
AMQP_SET_STR_PROPERTY(envelope->exchange_name, delivery->exchange.bytes, delivery->exchange.len);
AMQP_SET_LONG_PROPERTY(envelope->delivery_tag, delivery->delivery_tag);
AMQP_SET_BOOL_PROPERTY(envelope->is_redelivery, delivery->redelivered);
} else if (frame.payload.method.id == AMQP_BASIC_DELIVER_METHOD) {
/* get message metadata */
amqp_basic_deliver_t *delivery = (amqp_basic_deliver_t *) frame.payload.method.decoded;
AMQP_SET_STR_PROPERTY(envelope->routing_key, delivery->routing_key.bytes, delivery->routing_key.len);
AMQP_SET_STR_PROPERTY(envelope->exchange_name, delivery->exchange.bytes, delivery->exchange.len);
AMQP_SET_LONG_PROPERTY(envelope->delivery_tag, delivery->delivery_tag);
AMQP_SET_BOOL_PROPERTY(envelope->is_redelivery, delivery->redelivered);
} else if (frame.payload.method.id == AMQP_BASIC_GET_EMPTY_METHOD) {
/* We did a get and there were no messages */
return AMQP_READ_NO_MESSAGES;
} else if (frame.payload.method.id == AMQP_CONNECTION_CLOSE_METHOD) {
amqp_channel_close_t *err = NULL;
amqp_send_method(
connection,
frame.channel,
AMQP_CONNECTION_CLOSE_OK_METHOD,
&err
);
return AMQP_READ_ERROR;
}
/* Read in the next frame */
result = amqp_simple_wait_frame(connection, &frame);
if (result < 0) {
zend_throw_exception(amqp_connection_exception_class_entry, amqp_error_string(-result), -result TSRMLS_CC);
return AMQP_READ_ERROR;
}
/* Make sure it is something we expect*/
if (frame.frame_type != AMQP_FRAME_HEADER) {
zend_throw_exception(amqp_queue_exception_class_entry, "Invalid frame type, expecting header.", 0 TSRMLS_CC);
return AMQP_READ_ERROR;
}
body_target = frame.payload.properties.body_size;
/* Only allocate space for the body if there is a body */
if (body_target > 0) {
message_body_buffer = (char *) emalloc(body_target);
memset(message_body_buffer, 0, body_target);
}
p = (amqp_basic_properties_t *) frame.payload.properties.decoded;
if (p->_flags & AMQP_BASIC_CONTENT_TYPE_FLAG) {
AMQP_SET_STR_PROPERTY(envelope->content_type, p->content_type.bytes, p->content_type.len);
}
if (p->_flags & AMQP_BASIC_CONTENT_ENCODING_FLAG) {
AMQP_SET_STR_PROPERTY(envelope->content_encoding, p->content_encoding.bytes, p->content_encoding.len);
}
if (p->_flags & AMQP_BASIC_TYPE_FLAG) {
AMQP_SET_STR_PROPERTY(envelope->type, p->type.bytes, p->type.len);
}
if (p->_flags & AMQP_BASIC_TIMESTAMP_FLAG) {
AMQP_SET_LONG_PROPERTY(envelope->timestamp, p->timestamp);
}
if (p->_flags & AMQP_BASIC_DELIVERY_MODE_FLAG) {
AMQP_SET_LONG_PROPERTY(envelope->delivery_mode, p->delivery_mode);
}
if (p->_flags & AMQP_BASIC_PRIORITY_FLAG) {
AMQP_SET_LONG_PROPERTY(envelope->priority, p->priority);
}
if (p->_flags & AMQP_BASIC_EXPIRATION_FLAG) {
AMQP_SET_STR_PROPERTY(envelope->expiration, p->expiration.bytes, p->expiration.len);
}
if (p->_flags & AMQP_BASIC_USER_ID_FLAG) {
AMQP_SET_STR_PROPERTY(envelope->user_id, p->user_id.bytes, p->user_id.len);
}
if (p->_flags & AMQP_BASIC_APP_ID_FLAG) {
AMQP_SET_STR_PROPERTY(envelope->app_id, p->app_id.bytes, p->app_id.len);
}
if (p->_flags & AMQP_BASIC_MESSAGE_ID_FLAG) {
AMQP_SET_STR_PROPERTY(envelope->message_id, p->message_id.bytes, p->message_id.len);
}
if (p->_flags & AMQP_BASIC_REPLY_TO_FLAG) {
AMQP_SET_STR_PROPERTY(envelope->reply_to, p->reply_to.bytes, p->reply_to.len);
}
if (p->_flags & AMQP_BASIC_CORRELATION_ID_FLAG) {
AMQP_SET_STR_PROPERTY(envelope->correlation_id, p->correlation_id.bytes, p->correlation_id.len);
}
if (p->_flags & AMQP_BASIC_HEADERS_FLAG) {
zval_dtor(envelope->headers);
parse_amqp_table(&(p->headers), envelope->headers);
}
/* Check if we are going to even get a body */
if (frame.payload.properties.body_size == 0) {
/* No point in reading the next frame. Bail! */
break;
}
/* Lets get the body */
while (body_received < body_target) {
/* Read in the next frame */
result = amqp_simple_wait_frame(connection, &frame);
if (result < 0) {
zend_throw_exception(amqp_connection_exception_class_entry, amqp_error_string(-result), -result TSRMLS_CC);
return AMQP_READ_ERROR;
}
if (frame.frame_type != AMQP_FRAME_BODY) {
zend_throw_exception(amqp_queue_exception_class_entry, "Invalid frame type, expecting body.", 0 TSRMLS_CC);
return AMQP_READ_ERROR;
}
memcpy(message_body_buffer + body_received, frame.payload.body_fragment.bytes, frame.payload.body_fragment.len);
body_received += frame.payload.body_fragment.len;
}
/* We are done with this message, we can get out of the loop now */
break;
}
/* Put the final touches into the zval */
envelope->body = estrndup(message_body_buffer, body_target);
envelope->body_len = body_target;
/* Clean up message buffer */
if (message_body_buffer) {
efree(message_body_buffer);
}
return AMQP_READ_SUCCESS;
}
/* {{{ proto AMQPQueue::__construct(AMQPChannel channel)
AMQPQueue constructor
*/
PHP_METHOD(amqp_queue_class, __construct)
{
zval *id;
zval *channelObj = NULL;
amqp_queue_object *queue;
amqp_channel_object *channel;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OO", &id, amqp_queue_class_entry, &channelObj, amqp_channel_class_entry) == FAILURE) {
zend_throw_exception(amqp_queue_exception_class_entry, "Parameter must be an instance of AMQPChannel.", 0 TSRMLS_CC);
RETURN_NULL();
}
/* Store the connection object for later */
queue = (amqp_queue_object *)zend_object_store_get_object(id TSRMLS_CC);
/* Store the channel object */
queue->channel = channelObj;
/* Increment the ref count */
Z_ADDREF_P(channelObj);
/* Pull the channel out */
channel = AMQP_GET_CHANNEL(queue);
/* Check that the given connection has a channel, before trying to pull the connection off the stack */
AMQP_VERIFY_CHANNEL(channel, "Could not construct queue.");
/* We have a valid connection: */
queue->is_connected = '\1';
/* By default, the auto_delete flag should be set */
queue->auto_delete = 1;
}
/* }}} */
/* {{{ proto AMQPQueue::getName()
Get the queue name */
PHP_METHOD(amqp_queue_class, getName)
{
zval *id;
amqp_queue_object *queue;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, amqp_queue_class_entry) == FAILURE) {
return;
}
queue = (amqp_queue_object *)zend_object_store_get_object(id TSRMLS_CC);
/* Check if there is a name to be had: */
if (queue->name_len) {
RETURN_STRING(queue->name, 1);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto AMQPQueue::setName(string name)
Set the queue name */
PHP_METHOD(amqp_queue_class, setName)
{
zval *id;
amqp_queue_object *queue;
char *name = NULL;
int name_len = 0;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, amqp_queue_class_entry, &name, &name_len) == FAILURE) {
return;
}
/* Pull the queue off the object store */
queue = (amqp_queue_object *)zend_object_store_get_object(id TSRMLS_CC);
/* Verify that the name is not null and not an empty string */
if (name_len < 1 || name_len > 255) {
zend_throw_exception(amqp_queue_exception_class_entry, "Invalid queue name given, must be between 1 and 255 characters long.", 0 TSRMLS_CC);
return;
}
/* Set the queue name */
AMQP_SET_NAME(queue, name);
}
/* }}} */
/* {{{ proto AMQPQueue::getFlags()
Get the queue parameters */
PHP_METHOD(amqp_queue_class, getFlags)
{
zval *id;
amqp_queue_object *queue;
long flagBitmask = 0;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, amqp_queue_class_entry) == FAILURE) {
return;
}
queue = (amqp_queue_object *)zend_object_store_get_object(id TSRMLS_CC);
/* Set the bitmask based on what is set in the queue */
flagBitmask |= (queue->passive ? AMQP_PASSIVE : 0);
flagBitmask |= (queue->durable ? AMQP_DURABLE : 0);
flagBitmask |= (queue->exclusive ? AMQP_EXCLUSIVE : 0);
flagBitmask |= (queue->auto_delete ? AMQP_AUTODELETE : 0);
RETURN_LONG(flagBitmask);
}
/* }}} */
/* {{{ proto AMQPQueue::setFlags(long bitmask)
Set the queue parameters */
PHP_METHOD(amqp_queue_class, setFlags)
{
zval *id;
amqp_queue_object *queue;
long flagBitmask;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &id, amqp_queue_class_entry, &flagBitmask) == FAILURE) {
return;
}
/* Pull the queue off the object store */
queue = (amqp_queue_object *)zend_object_store_get_object(id TSRMLS_CC);
/* Set the flags based on the bitmask we were given */
queue->passive = IS_PASSIVE(flagBitmask);
queue->durable = IS_DURABLE(flagBitmask);
queue->exclusive = IS_EXCLUSIVE(flagBitmask);
queue->auto_delete = IS_AUTODELETE(flagBitmask);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto AMQPQueue::getArgument(string key)
Get the queue argument referenced by key */
PHP_METHOD(amqp_queue_class, getArgument)
{
zval *id;
zval **tmp;
amqp_queue_object *queue;
char *key;
int key_len;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, amqp_queue_class_entry, &key, &key_len) == FAILURE) {
return;
}
queue = (amqp_queue_object *)zend_object_store_get_object(id TSRMLS_CC);
if (zend_hash_find(Z_ARRVAL_P(queue->arguments), key, key_len + 1, (void **)&tmp) == FAILURE) {
RETURN_FALSE;
}
*return_value = **tmp;
zval_copy_ctor(return_value);
INIT_PZVAL(return_value);
}
/* }}} */
/* {{{ proto AMQPQueue::getArguments
Get the queue arguments */
PHP_METHOD(amqp_queue_class, getArguments)
{
zval *id;
amqp_queue_object *queue;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, amqp_queue_class_entry) == FAILURE) {
return;
}
queue = (amqp_queue_object *)zend_object_store_get_object(id TSRMLS_CC);
*return_value = *queue->arguments;
zval_copy_ctor(return_value);
/* Increment the ref count */
Z_ADDREF_P(queue->arguments);
}
/* }}} */
/* {{{ proto AMQPQueue::setArguments(array args)
Overwrite all queue arguments with given args */
PHP_METHOD(amqp_queue_class, setArguments)
{
zval *id, *zvalArguments;
amqp_queue_object *queue;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oa", &id, amqp_queue_class_entry, &zvalArguments) == FAILURE) {
return;
}
/* Pull the queue off the object store */
queue = (amqp_queue_object *)zend_object_store_get_object(id TSRMLS_CC);
/* Destroy the arguments storage */
if (queue->arguments) {
zval_ptr_dtor(&queue->arguments);
}
queue->arguments = zvalArguments;
/* Increment the ref count */
Z_ADDREF_P(queue->arguments);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto AMQPQueue::setArgument(key, value)
Get the queue name */
PHP_METHOD(amqp_queue_class, setArgument)
{
zval *id, *value;
amqp_queue_object *queue;
char *key;
int key_len;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osz", &id, amqp_queue_class_entry, &key, &key_len, &value) == FAILURE) {
return;
}
/* Pull the queue off the object store */
queue = (amqp_queue_object *)zend_object_store_get_object(id TSRMLS_CC);
switch (Z_TYPE_P(value)) {
case IS_NULL:
zend_hash_del_key_or_index(Z_ARRVAL_P(queue->arguments), key, key_len + 1, 0, HASH_DEL_KEY);
break;
case IS_BOOL:
case IS_LONG:
case IS_DOUBLE:
case IS_STRING:
add_assoc_zval(queue->arguments, key, value);
Z_ADDREF_P(value);
break;
default:
zend_throw_exception(amqp_queue_exception_class_entry, "The value parameter must be of type NULL, int, double or string.", 0 TSRMLS_CC);
return;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int AMQPQueue::declareQueue();
declare queue
*/
PHP_METHOD(amqp_queue_class, declareQueue)
{
zval *id;
amqp_queue_object *queue;
amqp_channel_object *channel;
amqp_connection_object *connection;
amqp_rpc_reply_t res;
amqp_queue_declare_ok_t *r;
char *name = "";
amqp_table_t *arguments;
long message_count;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, amqp_queue_class_entry) == FAILURE) {
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error parsing parameters." ,0 TSRMLS_CC);
return;
}
queue = (amqp_queue_object *)zend_object_store_get_object(id TSRMLS_CC);
/* Make sure we have a queue name, even if its empty: */
if (queue->name_len < 1) {
AMQP_SET_NAME(queue, name);
}
AMQP_ASSIGN_CHANNEL(channel, queue);
AMQP_VERIFY_CHANNEL(channel, "Could not declare queue.");
connection = AMQP_GET_CONNECTION(channel);
AMQP_VERIFY_CONNECTION(connection, "Could not declare queue.");
arguments = convert_zval_to_arguments(queue->arguments);
r = amqp_queue_declare(
connection->connection_resource->connection_state,
channel->channel_id,
amqp_cstring_bytes(queue->name),
queue->passive,
queue->durable,
queue->exclusive,
queue->auto_delete,
*arguments
);
res = AMQP_RPC_REPLY_T_CAST amqp_get_rpc_reply(connection->connection_resource->connection_state);
AMQP_EFREE_ARGUMENTS(arguments);
/* handle any errors that occured outside of signals */
if (res.reply_type != AMQP_RESPONSE_NORMAL) {
char str[256];
char ** pstr = (char **) &str;
amqp_error(res, pstr);
channel->is_connected = '\0';
zend_throw_exception(amqp_queue_exception_class_entry, *pstr, 0 TSRMLS_CC);
amqp_maybe_release_buffers(connection->connection_resource->connection_state);
return;
}
message_count = r->message_count;
/* Set the queue name, in case it is an autogenerated queue name */
name = stringify_bytes(r->queue);
AMQP_SET_NAME(queue, name);
efree(name);
amqp_maybe_release_buffers(connection->connection_resource->connection_state);
RETURN_LONG(message_count);
}
/* }}} */
/* {{{ proto int AMQPQueue::bind(string exchangeName, [string routingKey]);
bind queue to exchange by routing key
*/
PHP_METHOD(amqp_queue_class, bind)
{
zval *id;
amqp_queue_object *queue;
amqp_channel_object *channel;
amqp_connection_object *connection;
char *exchange_name;
int exchange_name_len;
char *keyname = NULL;
int keyname_len = 0;
amqp_rpc_reply_t res;
amqp_queue_bind_t s;
amqp_method_number_t bind_ok = AMQP_QUEUE_BIND_OK_METHOD;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|s", &id, amqp_queue_class_entry, &exchange_name, &exchange_name_len, &keyname, &keyname_len) == FAILURE) {
return;
}
queue = (amqp_queue_object *)zend_object_store_get_object(id TSRMLS_CC);
/* Check that the given connection has a channel, before trying to pull the connection off the stack */
if (queue->is_connected != '\1') {
zend_throw_exception(amqp_queue_exception_class_entry, "Could not bind queue. No connection available.", 0 TSRMLS_CC);
return;
}
channel = AMQP_GET_CHANNEL(queue);
AMQP_VERIFY_CHANNEL(channel, "Could not bind queue.");
connection = AMQP_GET_CONNECTION(channel);
AMQP_VERIFY_CONNECTION(connection, "Could not bind queue.");
s.ticket = 0;
s.queue.len = queue->name_len;
s.queue.bytes = queue->name;
s.exchange.len = exchange_name_len;
s.exchange.bytes = exchange_name;
s.routing_key.len = keyname_len;
s.routing_key.bytes = keyname;
s.nowait = 0;
s.arguments.num_entries = 0;
s.arguments.entries = NULL;
res = AMQP_RPC_REPLY_T_CAST amqp_simple_rpc(
connection->connection_resource->connection_state,
channel->channel_id,
AMQP_QUEUE_BIND_METHOD,
&bind_ok,
&s
);
if (res.reply_type != AMQP_RESPONSE_NORMAL) {
char str[256];
char **pstr = (char **)&str;
amqp_error(res, pstr);
channel->is_connected = 0;
zend_throw_exception(amqp_queue_exception_class_entry, *pstr, 0 TSRMLS_CC);
amqp_maybe_release_buffers(connection->connection_resource->connection_state);
return;
}
amqp_maybe_release_buffers(connection->connection_resource->connection_state);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int AMQPQueue::get([bit flags=AMQP_NOPARAM]);
read messages from queue
return array (messages)
*/
PHP_METHOD(amqp_queue_class, get)
{
zval *id;
amqp_queue_object *queue;
amqp_channel_object *channel;
amqp_connection_object *connection;
amqp_basic_get_t s;
zval *message;
int read;
long flags = INI_INT("amqp.auto_ack") ? AMQP_AUTOACK : AMQP_NOPARAM;
/* Parse out the method parameters */
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|l", &id, amqp_queue_class_entry, &flags) == FAILURE) {
return;
}
queue = (amqp_queue_object *)zend_object_store_get_object(id TSRMLS_CC);
/* Check that the given connection has a channel, before trying to pull the connection off the stack */
if (queue->is_connected != '\1') {
zend_throw_exception(amqp_queue_exception_class_entry, "Could not get messages from queue. No connection available.", 0 TSRMLS_CC);
return;
}
channel = AMQP_GET_CHANNEL(queue);
AMQP_VERIFY_CHANNEL(channel, "Could not get messages from queue.");
connection = AMQP_GET_CONNECTION(channel);
AMQP_VERIFY_CONNECTION(connection, "Could not get messages from queue.");
/* Set the QOS for this channel to only pull off one message at a time */
amqp_basic_qos(
connection->connection_resource->connection_state,
channel->channel_id,
0, /* prefetch window size */
1, /* prefetch message count */
0 /* global flag */
);
/* Build basic.get request */
s.ticket = 0,
s.queue = amqp_cstring_bytes(queue->name);
s.no_ack = (AMQP_AUTOACK & flags) ? 1 : 0;
/* Get the next message using basic.get */
amqp_send_method(
connection->connection_resource->connection_state,
channel->channel_id,
AMQP_BASIC_GET_METHOD,
&s
);
/* Read the message off of the channel */
MAKE_STD_ZVAL(message);
read = read_message_from_channel(connection->connection_resource->connection_state, message TSRMLS_CC);
/* Set the QOS back to what the user requested at the beginning */
amqp_basic_qos(
connection->connection_resource->connection_state,
channel->channel_id,
channel->prefetch_size, /* prefetch window size */
channel->prefetch_count, /* prefetch message count */
0 /* global flag */
);
if (read == AMQP_READ_SUCCESS) {
COPY_PZVAL_TO_ZVAL(*return_value, message);
} else {
zval_ptr_dtor(&message);
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto array AMQPQueue::consume(callback, [flags = <bitmask>, consumer_tag]);
consume the message
return array messages
*/
PHP_METHOD(amqp_queue_class, consume)
{
zval *id;
amqp_queue_object *queue;
amqp_channel_object *channel;
amqp_connection_object *connection;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
int function_call_succeeded = 1;
int read;
amqp_table_t *arguments;
char *consumer_tag;
int consumer_tag_len = 0;
amqp_bytes_t consumer_tag_bytes;
long flags = INI_INT("amqp.auto_ack") ? AMQP_AUTOACK : AMQP_NOPARAM;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Of|ls", &id, amqp_queue_class_entry, &fci, &fci_cache, &flags, &consumer_tag, &consumer_tag_len) == FAILURE) {
return;
}
/* Pull the queue out */
queue = (amqp_queue_object *)zend_object_store_get_object(id TSRMLS_CC);
channel = AMQP_GET_CHANNEL(queue);
AMQP_VERIFY_CHANNEL(channel, "Could not get queue.");
connection = AMQP_GET_CONNECTION(channel);
AMQP_VERIFY_CONNECTION(connection, "Could not get queue.");
/* Setup the consume */
arguments = convert_zval_to_arguments(queue->arguments);
consumer_tag_bytes.bytes = (void *) consumer_tag;
consumer_tag_bytes.len = consumer_tag_len;
amqp_basic_consume(
connection->connection_resource->connection_state,
channel->channel_id,
amqp_cstring_bytes(queue->name),
consumer_tag_bytes, /* Consumer tag */
(AMQP_NOLOCAL & flags) ? 1 : 0, /* No local */
(AMQP_AUTOACK & flags) ? 1 : 0, /* no_ack, aka AUTOACK */
queue->exclusive,
*arguments
);
AMQP_EFREE_ARGUMENTS(arguments);
do {
/* Initialize the message */
zval *message;
MAKE_STD_ZVAL(message);
/* Read the message */
read = read_message_from_channel(connection->connection_resource->connection_state, message TSRMLS_CC);
/* Make the callback */
if (read == AMQP_READ_SUCCESS) {
zval *params;
zval *retval_ptr = NULL;
/* Initialize the return value pointer */