math_client.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /*
  3. *
  4. * Copyright 2015 gRPC authors.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * 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, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. # Fix the following line to point to your installation
  20. # This assumes that you are using protoc 3.2.0+ and the generated stubs
  21. # were being autoloaded via composer.
  22. include 'vendor/autoload.php';
  23. function p($line)
  24. {
  25. echo "$line<br/>\n";
  26. }
  27. $host = 'localhost:50051';
  28. p("Connecting to host: $host");
  29. $client = new Math\MathClient($host, [
  30. 'credentials' => Grpc\ChannelCredentials::createInsecure(),
  31. ]);
  32. p('Client class: '.get_class($client));
  33. p('');
  34. p('Running unary call test:');
  35. $dividend = 7;
  36. $divisor = 4;
  37. $div_arg = new Math\DivArgs();
  38. $div_arg->setDividend($dividend);
  39. $div_arg->setDivisor($divisor);
  40. $call = $client->Div($div_arg);
  41. p('Call peer: '.$call->getPeer());
  42. p("Dividing $dividend by $divisor");
  43. list($response, $status) = $call->wait();
  44. p('quotient = '.$response->getQuotient());
  45. p('remainder = '.$response->getRemainder());
  46. p('');
  47. p('Running server streaming test:');
  48. $limit = 7;
  49. $fib_arg = new Math\FibArgs();
  50. $fib_arg->setLimit($limit);
  51. $call = $client->Fib($fib_arg);
  52. $result_array = iterator_to_array($call->responses());
  53. $result = '';
  54. foreach ($result_array as $num) {
  55. $result .= ' '.$num->getNum();
  56. }
  57. p("The first $limit Fibonacci numbers are:".$result);
  58. p('');
  59. p('Running client streaming test:');
  60. $call = $client->Sum();
  61. for ($i = 0; $i <= $limit; ++$i) {
  62. $num = new Math\Num();
  63. $num->setNum($i);
  64. $call->write($num);
  65. }
  66. list($response, $status) = $call->wait();
  67. p(sprintf('The first %d positive integers sum to: %d',
  68. $limit, $response->getNum()));
  69. p('');
  70. p('Running bidi-streaming test:');
  71. $call = $client->DivMany();
  72. for ($i = 0; $i < 7; ++$i) {
  73. $div_arg = new Math\DivArgs();
  74. $dividend = 2 * $i + 1;
  75. $divisor = 3;
  76. $div_arg->setDividend($dividend);
  77. $div_arg->setDivisor($divisor);
  78. $call->write($div_arg);
  79. p("client writing: $dividend / $divisor");
  80. $response = $call->read();
  81. p(sprintf('server writing: quotient = %d, remainder = %d',
  82. $response->getQuotient(), $response->getRemainder()));
  83. }
  84. $call->writesDone();