| 1 |
<?php |
| 2 |
/* |
| 3 |
* |
| 4 |
* Copyright 2020 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 |
include 'vendor/autoload.php'; |
| 21 |
|
| 22 |
class MathService extends Math\MathStub |
| 23 |
{ |
| 24 |
public function Div( |
| 25 |
\Math\DivArgs $request, |
| 26 |
\Grpc\ServerContext $context |
| 27 |
): ?\Math\DivReply { |
| 28 |
$dividend = $request->getDividend(); |
| 29 |
$divisor = $request->getDivisor(); |
| 30 |
if ($divisor == 0) { |
| 31 |
$context->setStatus( |
| 32 |
\Grpc\Status::status( |
| 33 |
\Grpc\STATUS_INVALID_ARGUMENT, |
| 34 |
'Cannot divide by zero' |
| 35 |
) |
| 36 |
); |
| 37 |
return null; |
| 38 |
} |
| 39 |
// for GeneratedCodeTest::testRetry |
| 40 |
$metadata = $context->clientMetadata(); |
| 41 |
if (array_key_exists('response-unavailable', $metadata)) { |
| 42 |
$trailingMetadata = array_key_exists('grpc-previous-rpc-attempts', $metadata) |
| 43 |
? ['unavailable-retry-attempts' => $metadata['grpc-previous-rpc-attempts']] |
| 44 |
: null; |
| 45 |
$context->setStatus( |
| 46 |
\Grpc\Status::status( |
| 47 |
\Grpc\STATUS_UNAVAILABLE, |
| 48 |
"unavailable", |
| 49 |
$trailingMetadata |
| 50 |
) |
| 51 |
); |
| 52 |
return null; |
| 53 |
} |
| 54 |
usleep(1000); // for GeneratedCodeTest::testTimeout |
| 55 |
$quotient = intdiv($dividend, $divisor); |
| 56 |
$remainder = $dividend % $divisor; |
| 57 |
$reply = new \Math\DivReply([ |
| 58 |
'quotient' => $quotient, |
| 59 |
'remainder' => $remainder, |
| 60 |
]); |
| 61 |
return $reply; |
| 62 |
} |
| 63 |
|
| 64 |
public function DivMany( |
| 65 |
\Grpc\ServerCallReader $reader, |
| 66 |
\Grpc\ServerCallWriter $writter, |
| 67 |
\Grpc\ServerContext $context |
| 68 |
): void { |
| 69 |
while ($divArgs = $reader->read()) { |
| 70 |
$dividend = $divArgs->getDividend(); |
| 71 |
$divisor = $divArgs->getDivisor(); |
| 72 |
if ($divisor == 0) { |
| 73 |
$context->setStatus(\Grpc\Status::status( |
| 74 |
\Grpc\STATUS_INVALID_ARGUMENT, |
| 75 |
'Cannot divide by zero' |
| 76 |
)); |
| 77 |
$writter->finish(); |
| 78 |
return; |
| 79 |
} |
| 80 |
$quotient = intdiv($dividend, $divisor); |
| 81 |
$remainder = $dividend % $divisor; |
| 82 |
$reply = new \Math\DivReply([ |
| 83 |
'quotient' => $quotient, |
| 84 |
'remainder' => $remainder, |
| 85 |
]); |
| 86 |
$writter->write($reply); |
| 87 |
} |
| 88 |
$writter->finish(); |
| 89 |
} |
| 90 |
|
| 91 |
public function Fib( |
| 92 |
\Math\FibArgs $request, |
| 93 |
\Grpc\ServerCallWriter $writter, |
| 94 |
\Grpc\ServerContext $context |
| 95 |
): void { |
| 96 |
$previous = 0; |
| 97 |
$current = 1; |
| 98 |
$limit = $request->getLimit(); |
| 99 |
for ($i = 0; $i < $limit; $i++) { |
| 100 |
$num = new \Math\Num(); |
| 101 |
$num->setNum($current); |
| 102 |
$writter->write($num); |
| 103 |
$next = $previous + $current; |
| 104 |
$previous = $current; |
| 105 |
$current = $next; |
| 106 |
} |
| 107 |
$writter->finish(); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Sum sums a stream of numbers, returning the final result once the stream |
| 112 |
* is closed. |
| 113 |
* @param \Grpc\ServerCallReader $reader read client request data of \Math\Num |
| 114 |
* @param \Grpc\ServerContext $context server request context |
| 115 |
* @return array with [ |
| 116 |
* \Math\Num, // response data |
| 117 |
* optional array = [], // initial metadata |
| 118 |
* optional \Grpc\Status = \Grpc\Status::ok // Grpc status |
| 119 |
* ] |
| 120 |
*/ |
| 121 |
public function Sum( |
| 122 |
\Grpc\ServerCallReader $reader, |
| 123 |
\Grpc\ServerContext $context |
| 124 |
): ?\Math\Num { |
| 125 |
$sum = 0; |
| 126 |
while ($num = $reader->read()) { |
| 127 |
$sum += $num->getNum(); |
| 128 |
} |
| 129 |
$reply = new \Math\Num(); |
| 130 |
$reply->setNum($sum); |
| 131 |
return $reply; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
$server = new \Grpc\RpcServer(); |
| 136 |
$server->addHttp2Port('0.0.0.0:50052'); |
| 137 |
$server_credentials = Grpc\ServerCredentials::createSsl( |
| 138 |
null, |
| 139 |
file_get_contents(dirname(__FILE__) . '/../data/server1.key'), |
| 140 |
file_get_contents(dirname(__FILE__) . '/../data/server1.pem') |
| 141 |
); |
| 142 |
$server->addSecureHttp2Port('0.0.0.0:50051', $server_credentials); |
| 143 |
$server->handle(new MathService()); |
| 144 |
$server->run(); |