Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: third_party/grpc/src/php/lib/Grpc/BaseStub.php

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
35 namespace Grpc;
36
37 /**
38 * Base class for generated client stubs. Stub methods are expected to call
39 * _simpleRequest or _streamRequest and return the result.
40 */
41 class BaseStub
42 {
43 private $hostname;
44 private $channel;
45
46 // a callback function
47 private $update_metadata;
48
49 /**
50 * @param $hostname string
51 * @param $opts array
52 * - 'update_metadata': (optional) a callback function which takes in a
53 * metadata array, and returns an updated metadata array
54 * - 'grpc.primary_user_agent': (optional) a user-agent string
55 */
56 public function __construct($hostname, $opts)
57 {
58 $this->hostname = $hostname;
59 $this->update_metadata = null;
60 if (isset($opts['update_metadata'])) {
61 if (is_callable($opts['update_metadata'])) {
62 $this->update_metadata = $opts['update_metadata'];
63 }
64 unset($opts['update_metadata']);
65 }
66 $package_config = json_decode(
67 file_get_contents(dirname(__FILE__).'/../../composer.json'), true);
68 if (!empty($opts['grpc.primary_user_agent'])) {
69 $opts['grpc.primary_user_agent'] .= ' ';
70 } else {
71 $opts['grpc.primary_user_agent'] = '';
72 }
73 $opts['grpc.primary_user_agent'] .=
74 'grpc-php/'.$package_config['version'];
75 if (!array_key_exists('credentials', $opts)) {
76 throw new \Exception("The opts['credentials'] key is now ".
77 'required. Please see one of the '.
78 'ChannelCredentials::create methods');
79 }
80 $this->channel = new Channel($hostname, $opts);
81 }
82
83 /**
84 * @return string The URI of the endpoint.
85 */
86 public function getTarget()
87 {
88 return $this->channel->getTarget();
89 }
90
91 /**
92 * @param $try_to_connect bool
93 *
94 * @return int The grpc connectivity state
95 */
96 public function getConnectivityState($try_to_connect = false)
97 {
98 return $this->channel->getConnectivityState($try_to_connect);
99 }
100
101 /**
102 * @param $timeout in microseconds
103 *
104 * @return bool true if channel is ready
105 * @throw Exception if channel is in FATAL_ERROR state
106 */
107 public function waitForReady($timeout)
108 {
109 $new_state = $this->getConnectivityState(true);
110 if ($this->_checkConnectivityState($new_state)) {
111 return true;
112 }
113
114 $now = Timeval::now();
115 $delta = new Timeval($timeout);
116 $deadline = $now->add($delta);
117
118 while ($this->channel->watchConnectivityState($new_state, $deadline)) {
119 // state has changed before deadline
120 $new_state = $this->getConnectivityState();
121 if ($this->_checkConnectivityState($new_state)) {
122 return true;
123 }
124 }
125 // deadline has passed
126 $new_state = $this->getConnectivityState();
127
128 return $this->_checkConnectivityState($new_state);
129 }
130
131 private function _checkConnectivityState($new_state)
132 {
133 if ($new_state == \Grpc\CHANNEL_READY) {
134 return true;
135 }
136 if ($new_state == \Grpc\CHANNEL_FATAL_FAILURE) {
137 throw new \Exception('Failed to connect to server');
138 }
139
140 return false;
141 }
142
143 /**
144 * Close the communication channel associated with this stub.
145 */
146 public function close()
147 {
148 $this->channel->close();
149 }
150
151 /**
152 * constructs the auth uri for the jwt.
153 */
154 private function _get_jwt_aud_uri($method)
155 {
156 $last_slash_idx = strrpos($method, '/');
157 if ($last_slash_idx === false) {
158 throw new \InvalidArgumentException(
159 'service name must have a slash');
160 }
161 $service_name = substr($method, 0, $last_slash_idx);
162
163 return 'https://'.$this->hostname.$service_name;
164 }
165
166 /**
167 * validate and normalize the metadata array.
168 *
169 * @param $metadata The metadata map
170 *
171 * @return $metadata Validated and key-normalized metadata map
172 * @throw InvalidArgumentException if key contains invalid characters
173 */
174 private function _validate_and_normalize_metadata($metadata)
175 {
176 $metadata_copy = [];
177 foreach ($metadata as $key => $value) {
178 if (!preg_match('/^[A-Za-z\d_-]+$/', $key)) {
179 throw new \InvalidArgumentException(
180 'Metadata keys must be nonempty strings containing only '.
181 'alphanumeric characters, hyphens and underscores');
182 }
183 $metadata_copy[strtolower($key)] = $value;
184 }
185
186 return $metadata_copy;
187 }
188
189 /* This class is intended to be subclassed by generated code, so
190 * all functions begin with "_" to avoid name collisions. */
191
192 /**
193 * Call a remote method that takes a single argument and has a
194 * single output.
195 *
196 * @param string $method The name of the method to call
197 * @param $argument The argument to the method
198 * @param callable $deserialize A function that deserializes the response
199 * @param array $metadata A metadata map to send to the server
200 *
201 * @return SimpleSurfaceActiveCall The active call object
202 */
203 public function _simpleRequest($method,
204 $argument,
205 $deserialize,
206 $metadata = [],
207 $options = [])
208 {
209 $call = new UnaryCall($this->channel,
210 $method,
211 $deserialize,
212 $options);
213 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
214 if (is_callable($this->update_metadata)) {
215 $metadata = call_user_func($this->update_metadata,
216 $metadata,
217 $jwt_aud_uri);
218 }
219 $metadata = $this->_validate_and_normalize_metadata(
220 $metadata);
221 $call->start($argument, $metadata, $options);
222
223 return $call;
224 }
225
226 /**
227 * Call a remote method that takes a stream of arguments and has a single
228 * output.
229 *
230 * @param string $method The name of the method to call
231 * @param $arguments An array or Traversable of arguments to stream to the
232 * server
233 * @param callable $deserialize A function that deserializes the response
234 * @param array $metadata A metadata map to send to the server
235 *
236 * @return ClientStreamingSurfaceActiveCall The active call object
237 */
238 public function _clientStreamRequest($method,
239 callable $deserialize,
240 $metadata = [],
241 $options = [])
242 {
243 $call = new ClientStreamingCall($this->channel,
244 $method,
245 $deserialize,
246 $options);
247 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
248 if (is_callable($this->update_metadata)) {
249 $metadata = call_user_func($this->update_metadata,
250 $metadata,
251 $jwt_aud_uri);
252 }
253 $metadata = $this->_validate_and_normalize_metadata(
254 $metadata);
255 $call->start($metadata);
256
257 return $call;
258 }
259
260 /**
261 * Call a remote method that takes a single argument and returns a stream of
262 * responses.
263 *
264 * @param string $method The name of the method to call
265 * @param $argument The argument to the method
266 * @param callable $deserialize A function that deserializes the responses
267 * @param array $metadata A metadata map to send to the server
268 *
269 * @return ServerStreamingSurfaceActiveCall The active call object
270 */
271 public function _serverStreamRequest($method,
272 $argument,
273 callable $deserialize,
274 $metadata = [],
275 $options = [])
276 {
277 $call = new ServerStreamingCall($this->channel,
278 $method,
279 $deserialize,
280 $options);
281 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
282 if (is_callable($this->update_metadata)) {
283 $metadata = call_user_func($this->update_metadata,
284 $metadata,
285 $jwt_aud_uri);
286 }
287 $metadata = $this->_validate_and_normalize_metadata(
288 $metadata);
289 $call->start($argument, $metadata, $options);
290
291 return $call;
292 }
293
294 /**
295 * Call a remote method with messages streaming in both directions.
296 *
297 * @param string $method The name of the method to call
298 * @param callable $deserialize A function that deserializes the responses
299 * @param array $metadata A metadata map to send to the server
300 *
301 * @return BidiStreamingSurfaceActiveCall The active call object
302 */
303 public function _bidiRequest($method,
304 callable $deserialize,
305 $metadata = [],
306 $options = [])
307 {
308 $call = new BidiStreamingCall($this->channel,
309 $method,
310 $deserialize,
311 $options);
312 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
313 if (is_callable($this->update_metadata)) {
314 $metadata = call_user_func($this->update_metadata,
315 $metadata,
316 $jwt_aud_uri);
317 }
318 $metadata = $this->_validate_and_normalize_metadata(
319 $metadata);
320 $call->start($metadata);
321
322 return $call;
323 }
324 }
OLDNEW
« no previous file with comments | « third_party/grpc/src/php/lib/Grpc/AbstractCall.php ('k') | third_party/grpc/src/php/lib/Grpc/BidiStreamingCall.php » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698