| 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 |
|
| 20 |
# Fix the following line to point to your installation |
| 21 |
# This assumes that you are using protoc 3.2.0+ and the generated stubs |
| 22 |
# were being autoloaded via composer. |
| 23 |
include 'vendor/autoload.php'; |
| 24 |
|
| 25 |
function p($line) |
| 26 |
{ |
| 27 |
echo "$line<br/>\n"; |
| 28 |
} |
| 29 |
|
| 30 |
$host = 'localhost:50051'; |
| 31 |
p("Connecting to host: $host"); |
| 32 |
$client = new Math\MathClient($host, [ |
| 33 |
'credentials' => Grpc\ChannelCredentials::createInsecure(), |
| 34 |
]); |
| 35 |
p('Client class: '.get_class($client)); |
| 36 |
p(''); |
| 37 |
|
| 38 |
p('Running unary call test:'); |
| 39 |
$dividend = 7; |
| 40 |
$divisor = 4; |
| 41 |
$div_arg = new Math\DivArgs(); |
| 42 |
$div_arg->setDividend($dividend); |
| 43 |
$div_arg->setDivisor($divisor); |
| 44 |
$call = $client->Div($div_arg); |
| 45 |
p('Call peer: '.$call->getPeer()); |
| 46 |
p("Dividing $dividend by $divisor"); |
| 47 |
list($response, $status) = $call->wait(); |
| 48 |
p('quotient = '.$response->getQuotient()); |
| 49 |
p('remainder = '.$response->getRemainder()); |
| 50 |
p(''); |
| 51 |
|
| 52 |
p('Running server streaming test:'); |
| 53 |
$limit = 7; |
| 54 |
$fib_arg = new Math\FibArgs(); |
| 55 |
$fib_arg->setLimit($limit); |
| 56 |
$call = $client->Fib($fib_arg); |
| 57 |
$result_array = iterator_to_array($call->responses()); |
| 58 |
$result = ''; |
| 59 |
foreach ($result_array as $num) { |
| 60 |
$result .= ' '.$num->getNum(); |
| 61 |
} |
| 62 |
p("The first $limit Fibonacci numbers are:".$result); |
| 63 |
p(''); |
| 64 |
|
| 65 |
p('Running client streaming test:'); |
| 66 |
$call = $client->Sum(); |
| 67 |
for ($i = 0; $i <= $limit; ++$i) { |
| 68 |
$num = new Math\Num(); |
| 69 |
$num->setNum($i); |
| 70 |
$call->write($num); |
| 71 |
} |
| 72 |
list($response, $status) = $call->wait(); |
| 73 |
p(sprintf('The first %d positive integers sum to: %d', |
| 74 |
$limit, $response->getNum())); |
| 75 |
p(''); |
| 76 |
|
| 77 |
p('Running bidi-streaming test:'); |
| 78 |
$call = $client->DivMany(); |
| 79 |
for ($i = 0; $i < 7; ++$i) { |
| 80 |
$div_arg = new Math\DivArgs(); |
| 81 |
$dividend = 2 * $i + 1; |
| 82 |
$divisor = 3; |
| 83 |
$div_arg->setDividend($dividend); |
| 84 |
$div_arg->setDivisor($divisor); |
| 85 |
$call->write($div_arg); |
| 86 |
p("client writing: $dividend / $divisor"); |
| 87 |
$response = $call->read(); |
| 88 |
p(sprintf('server writing: quotient = %d, remainder = %d', |
| 89 |
$response->getQuotient(), $response->getRemainder())); |
| 90 |
} |
| 91 |
$call->writesDone(); |