4 * Copyright 2020 gRPC authors.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 require_once realpath(dirname(__FILE__) . '/../../vendor/autoload.php');
21 class TestService extends \Grpc\Testing\TestServiceStub
23 private function maybeEchoMetadata(\Grpc\ServerContext $context)
25 $ECHO_INITIAL_KEY = 'x-grpc-test-echo-initial';
26 $ECHO_TRAILING_KEY = 'x-grpc-test-echo-trailing-bin';
28 $initial_metadata = [];
29 $trailing_metadata = [];
30 $client_metadata = $context->clientMetadata();
31 if (array_key_exists($ECHO_INITIAL_KEY, $client_metadata)) {
34 $client_metadata[$ECHO_INITIAL_KEY],
37 if (array_key_exists($ECHO_TRAILING_KEY, $client_metadata)) {
38 $trailing_metadata = [
40 $client_metadata[$ECHO_TRAILING_KEY],
43 return [$initial_metadata, $trailing_metadata];
46 private function maybeEchoStatusAndMessage(
48 $trailing_metadata = []
50 if (!$request->hasResponseStatus()) {
53 return \Grpc\Status::status(
54 $request->getResponseStatus()->getCode(),
55 $request->getResponseStatus()->getMessage(),
60 public function EmptyCall(
61 \Grpc\Testing\EmptyMessage $request,
62 \Grpc\ServerContext $context
63 ): ?\Grpc\Testing\EmptyMessage {
64 list($initial_metadata, $trailing_metadata) =
65 $this->maybeEchoMetadata($context);
66 $context->setStatus(\Grpc\Status::ok($trailing_metadata));
67 $context->setInitialMetadata($initial_metadata);
68 return new \Grpc\Testing\EmptyMessage();
71 public function UnaryCall(
72 \Grpc\Testing\SimpleRequest $request,
73 \Grpc\ServerContext $context
74 ): ?\Grpc\Testing\SimpleResponse {
75 list($initial_metadata, $trailing_metadata) =
76 $this->maybeEchoMetadata($context);
77 $echo_status = $this->maybeEchoStatusAndMessage(
82 $payload = new \Grpc\Testing\Payload([
83 'type' => $request->getResponseType(),
84 'body' => str_repeat("\0", $request->getResponseSize()),
86 $response = new \Grpc\Testing\SimpleResponse([
87 'payload' => $payload,
90 $context->setInitialMetadata($initial_metadata);
91 $context->setStatus($echo_status ?? \Grpc\Status::ok($trailing_metadata));
95 public function CacheableUnaryCall(
96 \Grpc\Testing\SimpleRequest $request,
97 \Grpc\ServerContext $context
98 ): ?\Grpc\Testing\SimpleResponse {
99 $context->setStatus(\Grpc\Status::unimplemented());
103 public function StreamingOutputCall(
104 \Grpc\Testing\StreamingOutputCallRequest $request,
105 \Grpc\ServerCallWriter $writter,
106 \Grpc\ServerContext $context
108 $echo_status = $this->maybeEchoStatusAndMessage($request);
110 foreach ($request->getResponseParameters() as $parameter) {
111 if ($parameter->getIntervalUs() > 0) {
112 usleep($parameter->getIntervalUs());
114 $payload = new \Grpc\Testing\Payload([
115 'type' => $request->getResponseType(),
116 'body' => str_repeat("\0", $parameter->getSize()),
118 $response = new \Grpc\Testing\StreamingOutputCallResponse([
119 'payload' => $payload,
122 $writter->write($response, $options);
124 $context->setStatus($echo_status ?? \Grpc\Status::ok());
128 public function StreamingInputCall(
129 \Grpc\ServerCallReader $reader,
130 \Grpc\ServerContext $context
131 ): ?\Grpc\Testing\StreamingInputCallResponse {
133 while ($request = $reader->read()) {
134 if ($request->hasPayload()) {
135 $aggregate_size += strlen($request->getPayload()->getBody());
138 $response = new \Grpc\Testing\StreamingInputCallResponse();
139 $response->setAggregatedPayloadSize($aggregate_size);
143 public function FullDuplexCall(
144 \Grpc\ServerCallReader $reader,
145 \Grpc\ServerCallWriter $writter,
146 \Grpc\ServerContext $context
148 list($initial_metadata, $trailing_metadata) =
149 $this->maybeEchoMetadata($context);
150 $context->setInitialMetadata($initial_metadata);
151 while ($request = $reader->read()) {
152 $echo_status = $this->maybeEchoStatusAndMessage(
157 $context->setStatus($echo_status);
162 foreach ($request->getResponseParameters() as $parameter) {
163 if ($parameter->getIntervalUs() > 0) {
164 usleep($parameter->getIntervalUs());
166 $payload = new \Grpc\Testing\Payload([
167 'type' => $request->getResponseType(),
168 'body' => str_repeat("\0", $parameter->getSize()),
170 $response = new \Grpc\Testing\StreamingOutputCallResponse([
171 'payload' => $payload,
174 $writter->write($response, $options);
177 $context->setStatus(\Grpc\Status::ok($trailing_metadata));
181 public function HalfDuplexCall(
182 \Grpc\ServerCallReader $reader,
183 \Grpc\ServerCallWriter $writter,
184 \Grpc\ServerContext $context
186 $context->setStatus(\Grpc\Status::unimplemented());
190 public function UnimplementedCall(
191 \Grpc\Testing\EmptyMessage $request,
192 \Grpc\ServerContext $context
193 ): ?\Grpc\Testing\EmptyMessage {
194 $context->setStatus(\Grpc\Status::unimplemented());
200 $args = getopt('', ['port:', 'use_tls::',]);
202 $server = new \Grpc\RpcServer();
204 $listening_address = '0.0.0.0:' . $args['port'];
205 if ($args['use_tls']) {
206 $server_credentials = \Grpc\ServerCredentials::createSsl(
208 file_get_contents(dirname(__FILE__) . '/../data/server1.key'),
209 file_get_contents(dirname(__FILE__) . '/../data/server1.pem')
211 $server->addSecureHttp2Port($listening_address, $server_credentials);
213 $server->addHttp2Port($listening_address);
215 $server->handle(new TestService());
216 echo 'Server running on ' . $listening_address . PHP_EOL;