Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / src / php / tests / interop / interop_server.php
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 require_once realpath(dirname(__FILE__) . '/../../vendor/autoload.php');
20
21 class TestService extends \Grpc\Testing\TestServiceStub
22 {
23     private function maybeEchoMetadata(\Grpc\ServerContext $context)
24     {
25         $ECHO_INITIAL_KEY = 'x-grpc-test-echo-initial';
26         $ECHO_TRAILING_KEY = 'x-grpc-test-echo-trailing-bin';
27
28         $initial_metadata = [];
29         $trailing_metadata = [];
30         $client_metadata = $context->clientMetadata();
31         if (array_key_exists($ECHO_INITIAL_KEY, $client_metadata)) {
32             $initial_metadata = [
33                 $ECHO_INITIAL_KEY =>
34                 $client_metadata[$ECHO_INITIAL_KEY],
35             ];
36         }
37         if (array_key_exists($ECHO_TRAILING_KEY, $client_metadata)) {
38             $trailing_metadata = [
39                 $ECHO_TRAILING_KEY =>
40                 $client_metadata[$ECHO_TRAILING_KEY],
41             ];
42         }
43         return [$initial_metadata, $trailing_metadata];
44     }
45
46     private function maybeEchoStatusAndMessage(
47         $request,
48         $trailing_metadata = []
49     ) {
50         if (!$request->hasResponseStatus()) {
51             return null;
52         }
53         return \Grpc\Status::status(
54             $request->getResponseStatus()->getCode(),
55             $request->getResponseStatus()->getMessage(),
56             $trailing_metadata
57         );
58     }
59
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();
69     }
70
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(
78             $request,
79             $trailing_metadata
80         );
81
82         $payload = new \Grpc\Testing\Payload([
83             'type' => $request->getResponseType(),
84             'body' => str_repeat("\0", $request->getResponseSize()),
85         ]);
86         $response = new \Grpc\Testing\SimpleResponse([
87             'payload' => $payload,
88         ]);
89
90         $context->setInitialMetadata($initial_metadata);
91         $context->setStatus($echo_status ?? \Grpc\Status::ok($trailing_metadata));
92         return  $response;
93     }
94
95     public function CacheableUnaryCall(
96         \Grpc\Testing\SimpleRequest $request,
97         \Grpc\ServerContext $context
98     ): ?\Grpc\Testing\SimpleResponse {
99         $context->setStatus(\Grpc\Status::unimplemented());
100         return null;
101     }
102
103     public function StreamingOutputCall(
104         \Grpc\Testing\StreamingOutputCallRequest $request,
105         \Grpc\ServerCallWriter $writter,
106         \Grpc\ServerContext $context
107     ): void {
108         $echo_status = $this->maybeEchoStatusAndMessage($request);
109
110         foreach ($request->getResponseParameters() as $parameter) {
111             if ($parameter->getIntervalUs() > 0) {
112                 usleep($parameter->getIntervalUs());
113             }
114             $payload = new \Grpc\Testing\Payload([
115                 'type' => $request->getResponseType(),
116                 'body' => str_repeat("\0", $parameter->getSize()),
117             ]);
118             $response = new \Grpc\Testing\StreamingOutputCallResponse([
119                 'payload' => $payload,
120             ]);
121             $options = [];
122             $writter->write($response, $options);
123         }
124         $context->setStatus($echo_status ?? \Grpc\Status::ok());
125         $writter->finish();
126     }
127
128     public function StreamingInputCall(
129         \Grpc\ServerCallReader $reader,
130         \Grpc\ServerContext $context
131     ): ?\Grpc\Testing\StreamingInputCallResponse {
132         $aggregate_size = 0;
133         while ($request = $reader->read()) {
134             if ($request->hasPayload()) {
135                 $aggregate_size += strlen($request->getPayload()->getBody());
136             }
137         }
138         $response = new \Grpc\Testing\StreamingInputCallResponse();
139         $response->setAggregatedPayloadSize($aggregate_size);
140         return $response;
141     }
142
143     public function FullDuplexCall(
144         \Grpc\ServerCallReader $reader,
145         \Grpc\ServerCallWriter $writter,
146         \Grpc\ServerContext $context
147     ): void {
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(
153                 $request,
154                 $trailing_metadata
155             );
156             if ($echo_status) {
157                 $context->setStatus($echo_status);
158                 $writter->finish();
159                 return;
160             }
161
162             foreach ($request->getResponseParameters() as $parameter) {
163                 if ($parameter->getIntervalUs() > 0) {
164                     usleep($parameter->getIntervalUs());
165                 }
166                 $payload = new \Grpc\Testing\Payload([
167                     'type' => $request->getResponseType(),
168                     'body' => str_repeat("\0", $parameter->getSize()),
169                 ]);
170                 $response = new \Grpc\Testing\StreamingOutputCallResponse([
171                     'payload' => $payload,
172                 ]);
173                 $options = [];
174                 $writter->write($response, $options);
175             }
176         }
177         $context->setStatus(\Grpc\Status::ok($trailing_metadata));
178         $writter->finish();
179     }
180
181     public function HalfDuplexCall(
182         \Grpc\ServerCallReader $reader,
183         \Grpc\ServerCallWriter $writter,
184         \Grpc\ServerContext $context
185     ): void {
186         $context->setStatus(\Grpc\Status::unimplemented());
187         $writter->finish();
188     }
189
190     public function UnimplementedCall(
191         \Grpc\Testing\EmptyMessage $request,
192         \Grpc\ServerContext $context
193     ): ?\Grpc\Testing\EmptyMessage {
194         $context->setStatus(\Grpc\Status::unimplemented());
195         return null;
196     }
197 };
198
199
200 $args = getopt('', ['port:', 'use_tls::',]);
201
202 $server = new \Grpc\RpcServer();
203
204 $listening_address = '0.0.0.0:' . $args['port'];
205 if ($args['use_tls']) {
206     $server_credentials = \Grpc\ServerCredentials::createSsl(
207         null,
208         file_get_contents(dirname(__FILE__) . '/../data/server1.key'),
209         file_get_contents(dirname(__FILE__) . '/../data/server1.pem')
210     );
211     $server->addSecureHttp2Port($listening_address, $server_credentials);
212 } else {
213     $server->addHttp2Port($listening_address);
214 }
215 $server->handle(new TestService());
216 echo 'Server running on ' . $listening_address . PHP_EOL;
217 $server->run();