Skip to content
Browse files

Sixth tutorial in php.

  • Loading branch information...
1 parent 7268ecd commit c8b9e1e88189a6d927d5d1c6059abcbcf43ec410 @majek majek committed Sep 14, 2011
Showing with 95 additions and 2 deletions.
  1. +49 −0 php/rpc_client.php
  2. +44 −0 php/rpc_server.php
  3. +2 −2 test.py
View
49 php/rpc_client.php
@@ -0,0 +1,49 @@
+<?php
+
+require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
+
+class FibonacciRpcClient {
+ private $connection;
+ private $channel;
+ private $callback_queue;
+ private $response;
+ private $corr_id;
+
+ function __construct() {
@videlalvaro
RabbitMQ member
videlalvaro added a note Sep 15, 2011

As good practice from PHP 5 onwards we should declare the visibility off all the methods. If you don't add them then the method will be public of course but it might be better to include the 'public' there.

There's an exception when declaring interfaces since in a Interface all methods are public so you just omit the public keyword there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $this->connection = new AMQPConnection(
+ 'localhost', 5672, 'guest', 'guest');
+ $this->channel = $this->connection->channel();
+ list($this->callback_queue, ,) = $this->channel->queue_declare(
+ "", false, false, true, false);
+ $this->channel->basic_consume(
+ $this->callback_queue, '', false, false, false, false,
+ array($this, 'on_response'));
+ }
+ public function on_response($rep) {
+ if($rep->properties['correlation_id'] == $this->corr_id) {
+ $this->response = $rep->body;
+ }
+ }
+
+ public function call($n) {
+ $this->response = null;
+ $this->corr_id = uniqid();
+
+ $msg = new AMQPMessage(
+ '' + $n,
@videlalvaro
RabbitMQ member
videlalvaro added a note Sep 15, 2011

If you want to cast $n to a string then just do: (string) $n. That should work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ array('correlation_id' => $this->corr_id,
+ 'reply_to' => $this->callback_queue)
+ );
+ $this->channel->basic_publish($msg, '', 'rpc_queue');
+ while(!$this->response) {
+ $this->channel->wait();
+ }
+ return intval($this->response);
+ }
+};
+
+$fibonacci_rpc = new FibonacciRpcClient();
+$response = $fibonacci_rpc->call(30);
+echo " [.] Got ", $response, "\n";
+
+?>
View
44 php/rpc_server.php
@@ -0,0 +1,44 @@
+<?php
+
+require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
+
+$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
+$channel = $connection->channel();
+
+$channel->queue_declare('rpc_queue', false, false, false, false);
+
+function fib($n) {
+ if ($n == 0)
+ return 0;
+ if ($n == 1)
+ return 1;
+ return fib($n-1) + fib($n-2);
+}
+
+echo " [x] Awaiting RPC requests\n";
+$callback = function($req) {
+ $n = intval($req->body);
+ echo " [.] fib(", $n, ")\n";
+
+ $msg = new AMQPMessage(
+ '' + fib($n),
@videlalvaro
RabbitMQ member
videlalvaro added a note Sep 15, 2011

If you want to cast the results of fib($n) to string then just do: (string) fib($n). That should work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ array('correlation_id' => $req->properties['correlation_id'])
+ );
+
+ $req->delivery_info['channel']->basic_publish(
+ $msg, '', $req->properties['reply_to']);
+ $req->delivery_info['channel']->basic_ack(
+ $req->delivery_info['delivery_tag']);
+};
+
+$channel->basic_qos(null, 1, null);
+$channel->basic_consume('rpc_queue', '', false, false, false, false, $callback);
+
+while(count($channel->callbacks)) {
+ $channel->wait();
+}
+
+$channel->close();
+$connection->close();
+
+?>
View
4 test.py
@@ -78,9 +78,9 @@ def skip(cwd_cmd, to_skip):
['php']),
'%(arg2)s'),
'tut6': (skip(gen('rpc_client', java='RPCClient', dotnet='RPCClient'),
- ['erlang', 'php']),
+ ['erlang']),
skip(gen('rpc_server', java='RPCServer', dotnet='RPCServer'),
- ['erlang', 'php']),
+ ['erlang']),
'fib[(]30[)]'),
}

2 comments on commit c8b9e1e

@videlalvaro
RabbitMQ member

I've commented the code, mostly cosmetic stuff.

@majek
majek commented on c8b9e1e Sep 15, 2011

Thanks. Fixed in ffd850e.

Please sign in to comment.
Something went wrong with that request. Please try again.