-
Notifications
You must be signed in to change notification settings - Fork 32
/
amqp.inc
1607 lines (1394 loc) · 45.8 KB
/
amqp.inc
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
/**
* Simple AMQP client library for AMQP for protocol version 0.8
*
* http://code.google.com/p/php-amqplib/
* Vadim Zaliva <lord@crocodile.org>
*
*/
require_once('amqp_wire.inc');
require_once('hexdump.inc');
function debug_msg($s)
{
echo $s, "\n";
}
function methodSig($a)
{
if(is_string($a))
return $a;
else
return sprintf("%d,%d",$a[0] ,$a[1]);
}
class AMQPException extends Exception
{
public function __construct($reply_code, $reply_text, $method_sig)
{
parent::__construct($reply_text,$reply_code);
$this->amqp_reply_code = $reply_code; // redundant, but kept for BC
$this->amqp_reply_text = $reply_text; // redundant, but kept for BC
$this->amqp_method_sig = $method_sig;
$ms=methodSig($method_sig);
if(array_key_exists($ms, AbstractChannel::$GLOBAL_METHOD_NAMES))
$mn = AbstractChannel::$GLOBAL_METHOD_NAMES[$ms];
else
$mn = "";
$this->args = array(
$reply_code,
$reply_text,
$method_sig,
$mn
);
}
}
class AMQPConnectionException extends AMQPException
{
public function __construct($reply_code, $reply_text, $method_sig)
{
parent::__construct($reply_code, $reply_text, $method_sig);
}
}
class AMQPChannelException extends AMQPException
{
public function __construct($reply_code, $reply_text, $method_sig)
{
parent::__construct($reply_code, $reply_text, $method_sig);
}
}
class AbstractChannel
{
private static $CONTENT_METHODS = array(
"60,60", // Basic.deliver
"60,71", // Basic.get_ok
);
private static $CLOSE_METHODS = array(
"10,60", // Connection.close
"20,40", // Channel.close
);
// All the method names
public static $GLOBAL_METHOD_NAMES = array(
"10,10" => "Connection.start",
"10,11" => "Connection.start_ok",
"10,20" => "Connection.secure",
"10,21" => "Connection.secure_ok",
"10,30" => "Connection.tune",
"10,31" => "Connection.tune_ok",
"10,40" => "Connection.open",
"10,41" => "Connection.open_ok",
"10,50" => "Connection.redirect",
"10,60" => "Connection.close",
"10,61" => "Connection.close_ok",
"20,10" => "Channel.open",
"20,11" => "Channel.open_ok",
"20,20" => "Channel.flow",
"20,21" => "Channel.flow_ok",
"20,30" => "Channel.alert",
"20,40" => "Channel.close",
"20,41" => "Channel.close_ok",
"30,10" => "Channel.access_request",
"30,11" => "Channel.access_request_ok",
"40,10" => "Channel.exchange_declare",
"40,11" => "Channel.exchange_declare_ok",
"40,20" => "Channel.exchange_delete",
"40,21" => "Channel.exchange_delete_ok",
"50,10" => "Channel.queue_declare",
"50,11" => "Channel.queue_declare_ok",
"50,20" => "Channel.queue_bind",
"50,21" => "Channel.queue_bind_ok",
"50,30" => "Channel.queue_purge",
"50,31" => "Channel.queue_purge_ok",
"50,40" => "Channel.queue_delete",
"50,41" => "Channel.queue_delete_ok",
"50,50" => "Channel.queue_unbind",
"50,51" => "Channel.queue_unbind_ok",
"60,10" => "Channel.basic_qos",
"60,11" => "Channel.basic_qos_ok",
"60,20" => "Channel.basic_consume",
"60,21" => "Channel.basic_consume_ok",
"60,30" => "Channel.basic_cancel",
"60,31" => "Channel.basic_cancel_ok",
"60,40" => "Channel.basic_publish",
"60,50" => "Channel.basic_return",
"60,60" => "Channel.basic_deliver",
"60,70" => "Channel.basic_get",
"60,71" => "Channel.basic_get_ok",
"60,72" => "Channel.basic_get_empty",
"60,80" => "Channel.basic_ack",
"60,90" => "Channel.basic_reject",
"60,100" => "Channel.basic_recover",
"90,10" => "Channel.tx_select",
"90,11" => "Channel.tx_select_ok",
"90,20" => "Channel.tx_commit",
"90,21" => "Channel.tx_commit_ok",
"90,30" => "Channel.tx_rollback",
"90,31" => "Channel.tx_rollback_ok"
);
protected $debug;
public function __construct($connection, $channel_id)
{
$this->connection = $connection;
$this->channel_id = $channel_id;
$connection->channels[$channel_id] = $this;
$this->frame_queue = array(); // Lower level queue for frames
$this->method_queue = array(); // Higher level queue for methods
$this->auto_decode = false;
$this->debug = defined('AMQP_DEBUG') ? AMQP_DEBUG : false;
}
public function getChannelId()
{
return $this->channel_id;
}
function dispatch($method_sig, $args, $content)
{
if(!array_key_exists($method_sig, $this->method_map))
throw new Exception("Unknown AMQP method $method_sig");
$amqp_method = $this->method_map[$method_sig];
if($content == NULL)
return call_user_func(array($this,$amqp_method), $args);
else
return call_user_func(array($this,$amqp_method), $args, $content);
}
function next_frame()
{
if($this->debug)
{
debug_msg("waiting for a new frame");
}
if($this->frame_queue != NULL)
return array_pop($this->frame_queue);
return $this->connection->wait_channel($this->channel_id);
}
protected function send_method_frame($method_sig, $args="")
{
$this->connection->send_channel_method_frame($this->channel_id, $method_sig, $args);
}
function wait_content()
{
$frm = $this->next_frame();
$frame_type = $frm[0];
$payload = $frm[1];
if($frame_type != 2)
throw new Exception("Expecting Content header");
$payload_reader = new AMQPReader(substr($payload,0,12));
$class_id = $payload_reader->read_short();
$weight = $payload_reader->read_short();
$body_size = $payload_reader->read_longlong();
$msg = new AMQPMessage();
$msg->load_properties(substr($payload,12));
$body_parts = array();
$body_received = 0;
while(bccomp($body_size,$body_received)==1)
{
$frm = $this->next_frame();
$frame_type = $frm[0];
$payload = $frm[1];
if($frame_type != 3)
throw new Exception("Expecting Content body, received frame type $frame_type");
$body_parts[] = $payload;
$body_received = bcadd($body_received, strlen($payload));
}
$msg->body = implode("",$body_parts);
if($this->auto_decode and isset($msg->content_encoding))
{
try
{
$msg->body = $msg->body->decode($msg->content_encoding);
} catch (Exception $e) {
if($this->debug)
{
debug_msg("Ignoring body decoding exception: " . $e->getMessage());
}
}
}
return $msg;
}
/**
* Wait for some expected AMQP methods and dispatch to them.
* Unexpected methods are queued up for later calls to this Python
* method.
*/
public function wait($allowed_methods=NULL)
{
if($allowed_methods)
{
if($this->debug)
{
debug_msg("waiting for " . implode(", ", $allowed_methods));
}
}
else
{
if($this->debug)
{
debug_msg("waiting for any method");
}
}
//Process deferred methods
foreach($this->method_queue as $qk=>$queued_method)
{
if($this->debug)
{
debug_msg("checking queue method " . $qk);
}
$method_sig = $queued_method[0];
if($allowed_methods==NULL || in_array($method_sig, $allowed_methods))
{
unset($this->method_queue[$qk]);
if($this->debug)
{
debug_msg("Executing queued method: $method_sig: " . AbstractChannel::$GLOBAL_METHOD_NAMES[methodSig($method_sig)]);
}
return $this->dispatch($queued_method[0],
$queued_method[1],
$queued_method[2]);
}
}
// No deferred methods? wait for new ones
while(true)
{
$frm = $this->next_frame();
$frame_type = $frm[0];
$payload = $frm[1];
if($frame_type != 1)
throw new Exception("Expecting AMQP method, received frame type: $frame_type");
if(strlen($payload) < 4)
throw new Exception("Method frame too short");
$method_sig_array = unpack("n2", substr($payload,0,4));
$method_sig = "" . $method_sig_array[1] . "," . $method_sig_array[2];
$args = new AMQPReader(substr($payload,4));
if($this->debug)
{
debug_msg("> $method_sig: " . AbstractChannel::$GLOBAL_METHOD_NAMES[methodSig($method_sig)]);
}
if(in_array($method_sig, AbstractChannel::$CONTENT_METHODS))
$content = $this->wait_content();
else
$content = NULL;
if($allowed_methods==NULL ||
in_array($method_sig,$allowed_methods) ||
in_array($method_sig,AbstractChannel::$CLOSE_METHODS))
{
return $this->dispatch($method_sig, $args, $content);
}
// Wasn't what we were looking for? save it for later
if($this->debug)
{
debug_msg("Queueing for later: $method_sig: " . AbstractChannel::$GLOBAL_METHOD_NAMES[methodSig($method_sig)]);
}
array_push($this->method_queue,array($method_sig, $args, $content));
}
}
}
class AMQPSSLConnection extends AMQPConnection
{
public function __construct($host, $port,
$user, $password,
$vhost="/", $ssl_options = array(), $options = array())
{
$ssl_context = empty($ssl_options) ? null : $this->create_ssl_context($ssl_options);
parent::__construct($host, $port, $user, $password, $vhost="/",
isset($options['insist']) ? $options['insist'] : false,
isset($options['login_method']) ? $options['login_method'] : "AMQPLAIN",
isset($options['login_response']) ? $options['login_response'] : null,
isset($options['locale']) ? $options['locale'] : "en_US",
isset($options['connection_timeout']) ? $options['connection_timeout'] : 3,
isset($options['read_write_timeout']) ? $options['read_write_timeout'] : 3,
$ssl_context);
}
private function create_ssl_context($options)
{
$ssl_context = stream_context_create();
foreach ($options as $k => $v) {
stream_context_set_option($ssl_context, 'ssl', $k, $v);
}
return $ssl_context;
}
}
class AMQPConnection extends AbstractChannel
{
public static $AMQP_PROTOCOL_HEADER = "AMQP\x01\x01\x09\x01";
public static $LIBRARY_PROPERTIES = array(
"library" => array('S', "PHP Simple AMQP lib"),
"library_version" => array('S', "0.1")
);
protected $method_map = array(
"10,10" => "start",
"10,20" => "secure",
"10,30" => "tune",
"10,41" => "open_ok",
"10,50" => "redirect",
"10,60" => "_close",
"10,61" => "close_ok"
);
public function __construct($host, $port,
$user, $password,
$vhost="/",$insist=false,
$login_method="AMQPLAIN",
$login_response=NULL,
$locale="en_US",
$connection_timeout = 3,
$read_write_timeout = 3,
$context = null)
{
if($user && $password)
{
$login_response = new AMQPWriter();
$login_response->write_table(array("LOGIN" => array('S',$user),
"PASSWORD" => array('S',$password)));
$login_response = substr($login_response->getvalue(),4); //Skip the length
} else
$login_response = NULL;
$d = AMQPConnection::$LIBRARY_PROPERTIES;
while(true)
{
$this->channels = array();
// The connection object itself is treated as channel 0
parent::__construct($this, 0);
$this->channel_max = 65535;
$this->frame_max = 131072;
$errstr = $errno = NULL;
$this->sock = NULL;
//TODO clean up
if($context)
{
$remote = sprintf('ssl://%s:%s', $host, $port);
$this->sock = stream_socket_client($remote, $errno, $errstr, 60, STREAM_CLIENT_CONNECT, $context);
}
else
{
$remote = sprintf('tcp://%s:%s', $host, $port);
$this->sock = stream_socket_client($remote, $errno, $errstr, 60, STREAM_CLIENT_CONNECT);
}
if (!$this->sock)
{
throw new Exception ("Error Connecting to server($errno): $errstr ");
}
stream_set_timeout($this->sock, $read_write_timeout);
stream_set_blocking($this->sock, 1);
$this->input = new AMQPReader(null, $this->sock);
$this->write(AMQPConnection::$AMQP_PROTOCOL_HEADER);
$this->wait(array("10,10"));
$this->x_start_ok($d, $login_method, $login_response, $locale);
$this->wait_tune_ok = true;
while($this->wait_tune_ok)
{
$this->wait(array(
"10,20", // secure
"10,30", // tune
));
}
$host = $this->x_open($vhost,"", $insist);
if(!$host)
return; // we weren't redirected
// we were redirected, close the socket, loop and try again
if($this->debug)
{
debug_msg("closing socket");
}
@fclose($this->sock); $this->sock=NULL;
}
}
public function __destruct()
{
if(isset($this->input))
if($this->input)
$this->close();
if(is_resource($this->sock))
{
if($this->debug)
{
debug_msg("closing socket");
}
@fclose($this->sock);
}
}
protected function write($data)
{
if($this->debug)
{
debug_msg("< [hex]:\n" . hexdump($data, $htmloutput = false, $uppercase = true, $return = true));
}
$len = strlen($data);
while(true)
{
if(false === ($written = fwrite($this->sock, $data)))
{
throw new Exception ("Error sending data");
}
$len = $len - $written;
if($len>0)
$data=substr($data,0-$len);
else
break;
}
}
protected function do_close()
{
if(isset($this->input))
if($this->input)
{
$this->input->close();
$this->input = NULL;
}
if(is_resource($this->sock))
{
if($this->debug)
{
debug_msg("closing socket");
}
@fclose($this->sock);
$this->sock = NULL;
}
}
public function get_free_channel_id()
{
for($i=1;$i<=$this->channel_max;$i++)
if(!array_key_exists($i,$this->channels))
return $i;
throw new Exception("No free channel ids");
}
public function send_content($channel, $class_id, $weight, $body_size,
$packed_properties, $body)
{
$pkt = new AMQPWriter();
$pkt->write_octet(2);
$pkt->write_short($channel);
$pkt->write_long(strlen($packed_properties)+12);
$pkt->write_short($class_id);
$pkt->write_short($weight);
$pkt->write_longlong($body_size);
$pkt->write($packed_properties);
$pkt->write_octet(0xCE);
$pkt = $pkt->getvalue();
$this->write($pkt);
while($body)
{
$payload = substr($body,0, $this->frame_max-8);
$body = substr($body,$this->frame_max-8);
$pkt = new AMQPWriter();
$pkt->write_octet(3);
$pkt->write_short($channel);
$pkt->write_long(strlen($payload));
$pkt->write($payload);
$pkt->write_octet(0xCE);
$pkt = $pkt->getvalue();
$this->write($pkt);
}
}
protected function send_channel_method_frame($channel, $method_sig, $args="")
{
if($args instanceof AMQPWriter)
$args = $args->getvalue();
$pkt = new AMQPWriter();
$pkt->write_octet(1);
$pkt->write_short($channel);
$pkt->write_long(strlen($args)+4); // 4 = length of class_id and method_id
// in payload
$pkt->write_short($method_sig[0]); // class_id
$pkt->write_short($method_sig[1]); // method_id
$pkt->write($args);
$pkt->write_octet(0xCE);
$pkt = $pkt->getvalue();
$this->write($pkt);
if($this->debug)
{
debug_msg("< " . methodSig($method_sig) . ": " . AbstractChannel::$GLOBAL_METHOD_NAMES[methodSig($method_sig)]);
}
}
/**
* Wait for a frame from the server
*/
protected function wait_frame()
{
$frame_type = $this->input->read_octet();
$channel = $this->input->read_short();
$size = $this->input->read_long();
$payload = $this->input->read($size);
$ch = $this->input->read_octet();
if($ch != 0xCE)
throw new Exception(sprintf("Framing error, unexpected byte: %x", $ch));
return array($frame_type, $channel, $payload);
}
/**
* Wait for a frame from the server destined for
* a particular channel.
*/
protected function wait_channel($channel_id)
{
while(true)
{
list($frame_type, $frame_channel, $payload) = $this->wait_frame();
if($frame_channel == $channel_id)
return array($frame_type, $payload);
// Not the channel we were looking for. Queue this frame
//for later, when the other channel is looking for frames.
array_push($this->channels[$frame_channel]->frame_queue,
array($frame_type, $payload));
// If we just queued up a method for channel 0 (the Connection
// itself) it's probably a close method in reaction to some
// error, so deal with it right away.
if(($frame_type == 1) && ($frame_channel == 0))
$this->wait();
}
}
/**
* Fetch a Channel object identified by the numeric channel_id, or
* create that object if it doesn't already exist.
*/
public function channel($channel_id=NULL)
{
if(array_key_exists($channel_id,$this->channels))
return $this->channels[$channel_id];
return new AMQPChannel($this->connection, $channel_id);
}
/**
* request a connection close
*/
public function close($reply_code=0, $reply_text="", $method_sig=array(0, 0))
{
$args = new AMQPWriter();
$args->write_short($reply_code);
$args->write_shortstr($reply_text);
$args->write_short($method_sig[0]); // class_id
$args->write_short($method_sig[1]); // method_id
$this->send_method_frame(array(10, 60), $args);
return $this->wait(array(
"10,61", // Connection.close_ok
));
}
public static function dump_table($table)
{
$tokens = array();
foreach ($table as $name => $value)
{
switch ($value[0])
{
case 'D':
$val = $value[1]->n . 'E' . $value[1]->e;
break;
case 'F':
$val = '(' . self::dump_table($value[1]) . ')';
break;
case 'T':
$val = date('Y-m-d H:i:s', $value[1]);
break;
default:
$val = $value[1];
}
$tokens[] = $name . '=' . $val;
}
return implode(', ', $tokens);
}
protected function _close($args)
{
$reply_code = $args->read_short();
$reply_text = $args->read_shortstr();
$class_id = $args->read_short();
$method_id = $args->read_short();
$this->x_close_ok();
throw new AMQPConnectionException($reply_code, $reply_text, array($class_id, $method_id));
}
/**
* confirm a connection close
*/
protected function x_close_ok()
{
$this->send_method_frame(array(10, 61));
$this->do_close();
}
/**
* confirm a connection close
*/
protected function close_ok($args)
{
$this->do_close();
}
protected function x_open($virtual_host, $capabilities="", $insist=false)
{
$args = new AMQPWriter();
$args->write_shortstr($virtual_host);
$args->write_shortstr($capabilities);
$args->write_bit($insist);
$this->send_method_frame(array(10, 40), $args);
return $this->wait(array(
"10,41", // Connection.open_ok
"10,50" // Connection.redirect
));
}
/**
* signal that the connection is ready
*/
protected function open_ok($args)
{
$this->known_hosts = $args->read_shortstr();
if($this->debug)
{
debug_msg("Open OK! known_hosts: " . $this->known_hosts);
}
return NULL;
}
/**
* asks the client to use a different server
*/
protected function redirect($args)
{
$host = $args->read_shortstr();
$this->known_hosts = $args->read_shortstr();
if($this->debug)
{
debug_msg("Redirected to [". $host . "], known_hosts [" . $this->known_hosts . "]" );
}
return $host;
}
/**
* security mechanism challenge
*/
protected function secure($args)
{
$challenge = $args->read_longstr();
}
/**
* security mechanism response
*/
protected function x_secure_ok($response)
{
$args = new AMQPWriter();
$args->write_longstr($response);
$this->send_method_frame(array(10, 21), $args);
}
/**
* start connection negotiation
*/
protected function start($args)
{
$this->version_major = $args->read_octet();
$this->version_minor = $args->read_octet();
$this->server_properties = $args->read_table();
$this->mechanisms = explode(" ", $args->read_longstr());
$this->locales = explode(" ", $args->read_longstr());
if($this->debug)
{
debug_msg(sprintf("Start from server, version: %d.%d, properties: %s, mechanisms: %s, locales: %s",
$this->version_major,
$this->version_minor,
self::dump_table($this->server_properties),
implode(', ', $this->mechanisms),
implode(', ', $this->locales)));
}
}
protected function x_start_ok($client_properties, $mechanism, $response, $locale)
{
$args = new AMQPWriter();
$args->write_table($client_properties);
$args->write_shortstr($mechanism);
$args->write_longstr($response);
$args->write_shortstr($locale);
$this->send_method_frame(array(10, 11), $args);
}
/**
* propose connection tuning parameters
*/
protected function tune($args)
{
$v=$args->read_short();
if($v)
$this->channel_max = $v;
$v=$args->read_long();
if($v)
$this->frame_max = $v;
$this->heartbeat = $args->read_short();
$this->x_tune_ok($this->channel_max, $this->frame_max, 0);
}
/**
* negotiate connection tuning parameters
*/
protected function x_tune_ok($channel_max, $frame_max, $heartbeat)
{
$args = new AMQPWriter();
$args->write_short($channel_max);
$args->write_long($frame_max);
$args->write_short($heartbeat);
$this->send_method_frame(array(10, 31), $args);
$this->wait_tune_ok = False;
}
}
class AMQPChannel extends AbstractChannel
{
protected $method_map = array(
"20,11" => "open_ok",
"20,20" => "flow",
"20,21" => "flow_ok",
"20,30" => "alert",
"20,40" => "_close",
"20,41" => "close_ok",
"30,11" => "access_request_ok",
"40,11" => "exchange_declare_ok",
"40,21" => "exchange_delete_ok",
"50,11" => "queue_declare_ok",
"50,21" => "queue_bind_ok",
"50,31" => "queue_purge_ok",
"50,41" => "queue_delete_ok",
"50,51" => "queue_unbind_ok",
"60,11" => "basic_qos_ok",
"60,21" => "basic_consume_ok",
"60,31" => "basic_cancel_ok",
"60,50" => "basic_return",
"60,60" => "basic_deliver",
"60,71" => "basic_get_ok",
"60,72" => "basic_get_empty",
"90,11" => "tx_select_ok",
"90,21" => "tx_commit_ok",
"90,31" => "tx_rollback_ok"
);
public function __construct($connection,
$channel_id=NULL,
$auto_decode=true)
{
if($channel_id == NULL)
$channel_id = $connection->get_free_channel_id();
parent::__construct($connection, $channel_id);
if($this->debug)
{
debug_msg("using channel_id: " . $channel_id);
}
$this->default_ticket = 0;
$this->is_open = false;
$this->active = true; // Flow control
$this->alerts = array();
$this->callbacks = array();
$this->auto_decode = $auto_decode;
$this->x_open();
}
public function __destruct()
{
//TODO:???if($this->connection)
// $this->close("destroying channel");
}
/**
* Tear down this object, after we've agreed to close with the server.
*/
protected function do_close()
{
$this->is_open = false;
unset($this->connection->channels[$this->channel_id]);
$this->channel_id = $this->connection = NULL;
}
/**
* This method allows the server to send a non-fatal warning to
* the client. This is used for methods that are normally
* asynchronous and thus do not have confirmations, and for which
* the server may detect errors that need to be reported. Fatal
* errors are handled as channel or connection exceptions; non-
* fatal errors are sent through this method.
*/
protected function alert($args)
{
$reply_code = $args->read_short();
$reply_text = $args->read_shortstr();
$details = $args->read_table();
array_push($this->alerts,array($reply_code, $reply_text, $details));
}
/**
* request a channel close
*/
public function close($reply_code=0,
$reply_text="",
$method_sig=array(0, 0))
{
$args = new AMQPWriter();
$args->write_short($reply_code);
$args->write_shortstr($reply_text);
$args->write_short($method_sig[0]); // class_id
$args->write_short($method_sig[1]); // method_id
$this->send_method_frame(array(20, 40), $args);
return $this->wait(array(
"20,41" // Channel.close_ok
));
}
protected function _close($args)
{
$reply_code = $args->read_short();
$reply_text = $args->read_shortstr();
$class_id = $args->read_short();
$method_id = $args->read_short();
$this->send_method_frame(array(20, 41));
$this->do_close();
throw new AMQPChannelException($reply_code, $reply_text,
array($class_id, $method_id));
}
/**
* confirm a channel close
*/
protected function close_ok($args)
{
$this->do_close();
}
/**
* enable/disable flow from peer
*/
public function flow($active)
{
$args = new AMQPWriter();
$args->write_bit($active);
$this->send_method_frame(array(20, 20), $args);
return $this->wait(array(
"20,21" //Channel.flow_ok
));
}
protected function _flow($args)
{
$this->active = $args->read_bit();
$this->x_flow_ok($this->active);
}
protected function x_flow_ok($active)
{
$args = new AMQPWriter();
$args->write_bit($active);
$this->send_method_frame(array(20, 21), $args);
}
protected function flow_ok($args)
{
return $args->read_bit();
}
protected function x_open($out_of_band="")
{
if($this->is_open)
return;
$args = new AMQPWriter();