math_client.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /*
  3. *
  4. * Copyright 2015, Google Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following disclaimer
  15. * in the documentation and/or other materials provided with the
  16. * distribution.
  17. * * Neither the name of Google Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived from
  19. * this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. # Fix the following line to point to your installation
  35. # This assumes that you are using protoc 3.2.0+ and the generated stubs
  36. # were being autoloaded via composer.
  37. include 'vendor/autoload.php';
  38. function p($line)
  39. {
  40. echo "$line<br/>\n";
  41. }
  42. $host = 'localhost:50051';
  43. p("Connecting to host: $host");
  44. $client = new Math\MathClient($host, [
  45. 'credentials' => Grpc\ChannelCredentials::createInsecure(),
  46. ]);
  47. p('Client class: '.get_class($client));
  48. p('');
  49. p('Running unary call test:');
  50. $dividend = 7;
  51. $divisor = 4;
  52. $div_arg = new Math\DivArgs();
  53. $div_arg->setDividend($dividend);
  54. $div_arg->setDivisor($divisor);
  55. $call = $client->Div($div_arg);
  56. p('Call peer: '.$call->getPeer());
  57. p("Dividing $dividend by $divisor");
  58. list($response, $status) = $call->wait();
  59. p('quotient = '.$response->getQuotient());
  60. p('remainder = '.$response->getRemainder());
  61. p('');
  62. p('Running server streaming test:');
  63. $limit = 7;
  64. $fib_arg = new Math\FibArgs();
  65. $fib_arg->setLimit($limit);
  66. $call = $client->Fib($fib_arg);
  67. $result_array = iterator_to_array($call->responses());
  68. $result = '';
  69. foreach ($result_array as $num) {
  70. $result .= ' '.$num->getNum();
  71. }
  72. p("The first $limit Fibonacci numbers are:".$result);
  73. p('');
  74. p('Running client streaming test:');
  75. $call = $client->Sum();
  76. for ($i = 0; $i <= $limit; ++$i) {
  77. $num = new Math\Num();
  78. $num->setNum($i);
  79. $call->write($num);
  80. }
  81. list($response, $status) = $call->wait();
  82. p(sprintf('The first %d positive integers sum to: %d',
  83. $limit, $response->getNum()));
  84. p('');
  85. p('Running bidi-streaming test:');
  86. $call = $client->DivMany();
  87. for ($i = 0; $i < 7; ++$i) {
  88. $div_arg = new Math\DivArgs();
  89. $dividend = 2 * $i + 1;
  90. $divisor = 3;
  91. $div_arg->setDividend($dividend);
  92. $div_arg->setDivisor($divisor);
  93. $call->write($div_arg);
  94. p("client writing: $dividend / $divisor");
  95. $response = $call->read();
  96. p(sprintf('server writing: quotient = %d, remainder = %d',
  97. $response->getQuotient(), $response->getRemainder()));
  98. }
  99. $call->writesDone();