-
Notifications
You must be signed in to change notification settings - Fork 5
/
amqp.inc
1660 lines (1473 loc) · 47 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/
*
* @category AMQP
* @package AMQP
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @author Vadim Zaliva <lord@crocodile.org>
*/
require_once('amqp_wire.inc');
require_once('hexdump.inc');
function methodSig($a)
{
if(is_string($a))
return $a;
else
return $a[0].','.$a[1];
}
class AMQPException extends Exception
{
public function __construct($reply_code, $reply_text, $method_sig)
{
parent::__construct(NULL,0);
$this->amqp_reply_code = $reply_code;
$this->amqp_reply_text = $reply_text;
$this->amqp_method_sig = $method_sig;
global $METHOD_NAME_MAP;
$ms=methodSig($method_sig);
if(array_key_exists($ms, $METHOD_NAME_MAP))
$mn = $METHOD_NAME_MAP[$ms];
else
$mn = "";
$this->args = array(
$reply_code,
$reply_text,
$method_sig,
$mn
);
}
}
class AMQPConnectionException extends AMQPException
{
}
class AMQPChannelException extends AMQPException
{
}
$METHOD_NAME_MAP = 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",
"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"
);
class AbstractChannel
{
/**
* @var array
*/
private static $CONTENT_METHODS = array(
"60,60", // Basic.deliver
"60,71", // Basic.get_ok
);
/**
* @var array
*/
private static $CLOSE_METHODS = array(
"10,60", // Connection.close
"20,40", // Channel.close
);
/**
* @var boolean
*/
private $debug;
/**
* @var AMQPConnection
*/
var $connection;
/**
* @var int
*/
var $channel_id;
/**
* @var array
*/
var $frame_queue;
/**
* @var array
*/
var $method_queue;
/**
* @var bool
*/
var $auto_decode;
/**
* @var array
*/
protected static $METHOD_MAP = array();
/**
* @param AMQPConnection $connection
* @param int $channel_id
* @return void
*/
public function __construct(AMQPConnection $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;
}
/**
*
* @param string $method_sig Method to call on remote server
* @param array $args
* @param string $content
* @return mixed
*/
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()
{
$this->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) {
$this->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.
*
* @param array $allowed_methods Methods to wait for, null waits for any method
* @return mixed
*/
public function wait($allowed_methods=NULL)
{
if($allowed_methods)
$this->debug_msg("waiting for " . implode(", ", $allowed_methods));
else
$this->debug_msg("waiting for any method");
//Process deferred methods
foreach($this->method_queue as $qk=>$queued_method)
{
$this->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]);
$this->debug_msg("Executing queued method: $method_sig: " .
$METHOD_NAME_MAP[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));
global $METHOD_NAME_MAP;
$this->debug_msg("> $method_sig: " . $METHOD_NAME_MAP[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
global $METHOD_NAME_MAP;
$this->debug_msg("Queueing for later: $method_sig: " . $METHOD_NAME_MAP[methodSig($method_sig)]);
array_push($this->method_queue,array($method_sig, $args, $content));
}
}
public function debug($flag) {
$this->debug = ($flag) ? true:false;
}
public function debug_msg($msg) {
if ($this->debug) {
error_log($msg);
}
}
}
class AMQPConnection extends AbstractChannel
{
/**
* @var string
*/
public static $AMQP_PROTOCOL_HEADER = "AMQP\x01\x01\x09\x01";
/**
* @var array
*/
public static $LIBRARY_PROPERTIES = array(
"library" => array('S', "PHP Simple AMQP lib"),
"library_version" => array('S', "0.1")
);
/**
* @var array
*/
protected static $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"
);
/**
* @var AMQPReader
*/
protected $input;
/**
* @var boolean
*/
protected $debug = false;
/**
* Connect to AMQP server
*
* @param string $host Server host
* @param int $port Server port
* @param string $user
* @param string $password
* @param string $vhost
* @param bool $insist
* @param string $login_method
* @param AMQPWriter $login_response
* @param string $locale
* @param int $connection_timeout
* @param int $read_write_timeout
* @return void
*/
public function __construct($host, $port,
$user, $password,
$vhost="/",$insist=false,
$login_method="AMQPLAIN",
AMQPWriter $login_response=NULL,
$locale="en_US",
$connection_timeout = 10,
$read_write_timeout = 3)
{
$this->method_map = AMQPConnection::$METHOD_MAP;
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;
if (!($this->sock = fsockopen($host,$port,$errno,$errstr,$connection_timeout)))
{
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
$this->debug_msg("closing socket");
@fclose($this->sock); $this->sock=NULL;
}
}
/**
* Disconnect from server
*
* @return void
*/
public function __destruct()
{
if(isset($this->input))
if($this->input)
$this->close();
if($this->sock)
{
$this->debug_msg("closing socket");
@fclose($this->sock);
}
}
/**
* Write data on socket
*
* @param string $data
* @return void
*/
protected function write($data)
{
if ($this->debug) {
$this->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;
}
}
/**
* Disconnect from server
*
* @return void
*/
protected function do_close()
{
if(isset($this->input))
if($this->input)
{
$this->input->close();
$this->input = NULL;
}
if($this->sock)
{
$this->debug_msg("closing socket");
@fclose($this->sock);
$this->sock = NULL;
}
}
/**
* Find a free channel id
*
* @return int Available channel id
*/
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");
}
/**
* Send content on channel
*
* @param int $channel Channel ID
* @param int $class_id
* @param int $weight
* @param int $body_size
* @param string $packed_properties
* @param string $body
* @return void
*/
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(is_a($args, "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);
global $METHOD_NAME_MAP;
$this->debug_msg("< " . methodSig($method_sig) . ": " . $METHOD_NAME_MAP[methodSig($method_sig)]);
}
/**
* Wait for a frame from the server
*
* @return array
* @throws Exception On Framing error
*/
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.
*
* @param int $channel_id
* @return array
*/
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.
*
* @param int $channel_id Channel number
* @return AMQPCHannel
*/
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]) . ')';
case 'T':
$val = date('Y-m-d H:i:s', $value[1]);
break;
default:
$val = $value[1];
}
$tokens[] = $name . '=' . $val;
}
return implode(', ', $tokens);
}
/**
* Server force a close on client
*
* @param AMQPReader $args
* @return void
* @throws AMQPConnectionException
*/
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
*
* @param AMQPReader $args
* @return NULL
*/
protected function open_ok($args)
{
$this->known_hosts = $args->read_shortstr();
$this->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();
$this->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());
$this->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 static $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",
"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"
);
/**
* AMQPChannel constructor
*
* @param AMQPConnection $connection Connection to AMQP server
* @param int $channel_id Channel number
* @param bool $auto_decode
* @return void
*/
public function __construct(AMQPConnection $connection,
$channel_id=NULL,
$auto_decode=true)
{
if($channel_id == NULL)
$channel_id = $connection->get_free_channel_id();
$this->debug_msg("using channel_id: " . $channel_id);
parent::__construct($connection, $channel_id);
$this->method_map = AMQPChannel::$METHOD_MAP;
$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