mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 1 | <?php |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 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 | */ |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 34 | namespace Grpc; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Base class for generated client stubs. Stub methods are expected to call |
| 38 | * _simpleRequest or _streamRequest and return the result. |
| 39 | */ |
| 40 | class BaseStub { |
| 41 | |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 42 | private $hostname; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 43 | private $channel; |
| 44 | |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 45 | // a callback function |
| 46 | private $update_metadata; |
| 47 | |
| 48 | /** |
| 49 | * @param $hostname string |
| 50 | * @param $opts array |
| 51 | * - 'update_metadata': (optional) a callback function which takes in a |
| 52 | * metadata array, and returns an updated metadata array |
| 53 | */ |
murgatroid99 | f21eb25 | 2015-01-30 13:47:41 -0800 | [diff] [blame] | 54 | public function __construct($hostname, $opts) { |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 55 | $this->hostname = $hostname; |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 56 | $this->update_metadata = null; |
| 57 | if (isset($opts['update_metadata'])) { |
| 58 | if (is_callable($opts['update_metadata'])) { |
| 59 | $this->update_metadata = $opts['update_metadata']; |
| 60 | } |
| 61 | unset($opts['update_metadata']); |
| 62 | } |
Stanley Cheung | a75098d | 2015-07-24 09:03:39 -0700 | [diff] [blame] | 63 | $package_config = json_decode( |
| 64 | file_get_contents(dirname(__FILE__) . '/../../composer.json'), true); |
| 65 | $opts['grpc.primary_user_agent'] = |
| 66 | 'grpc-php/' . $package_config['version']; |
murgatroid99 | f21eb25 | 2015-01-30 13:47:41 -0800 | [diff] [blame] | 67 | $this->channel = new Channel($hostname, $opts); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 71 | * @return string The URI of the endpoint. |
| 72 | */ |
| 73 | public function getTarget() { |
| 74 | return $this->channel->getTarget(); |
| 75 | } |
| 76 | |
| 77 | /** |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 78 | * @param $try_to_connect bool |
| 79 | * @return int The grpc connectivity state |
| 80 | */ |
| 81 | public function getConnectivityState($try_to_connect = false) { |
| 82 | return $this->channel->getConnectivityState($try_to_connect); |
| 83 | } |
| 84 | |
| 85 | /** |
Stanley Cheung | 04b7a41 | 2015-08-13 09:39:04 -0700 | [diff] [blame] | 86 | * @param $timeout in microseconds |
| 87 | * @return bool true if channel is ready |
Stanley Cheung | 1567c0c | 2015-08-13 11:12:54 -0700 | [diff] [blame] | 88 | * @throw Exception if channel is in FATAL_ERROR state |
Stanley Cheung | 04b7a41 | 2015-08-13 09:39:04 -0700 | [diff] [blame] | 89 | */ |
Stanley Cheung | 1567c0c | 2015-08-13 11:12:54 -0700 | [diff] [blame] | 90 | public function waitForReady($timeout) { |
Stanley Cheung | 04b7a41 | 2015-08-13 09:39:04 -0700 | [diff] [blame] | 91 | $new_state = $this->getConnectivityState(true); |
| 92 | if ($this->_checkConnectivityState($new_state)) { |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | $now = Timeval::now(); |
| 97 | $delta = new Timeval($timeout); |
| 98 | $deadline = $now->add($delta); |
| 99 | |
Stanley Cheung | 1567c0c | 2015-08-13 11:12:54 -0700 | [diff] [blame] | 100 | while ($this->channel->watchConnectivityState($new_state, $deadline)) { |
| 101 | // state has changed before deadline |
| 102 | $new_state = $this->getConnectivityState(); |
| 103 | if ($this->_checkConnectivityState($new_state)) { |
| 104 | return true; |
| 105 | } |
Stanley Cheung | 04b7a41 | 2015-08-13 09:39:04 -0700 | [diff] [blame] | 106 | } |
Stanley Cheung | 1567c0c | 2015-08-13 11:12:54 -0700 | [diff] [blame] | 107 | // deadline has passed |
Stanley Cheung | 04b7a41 | 2015-08-13 09:39:04 -0700 | [diff] [blame] | 108 | $new_state = $this->getConnectivityState(); |
Stanley Cheung | 1567c0c | 2015-08-13 11:12:54 -0700 | [diff] [blame] | 109 | return $this->_checkConnectivityState($new_state); |
Stanley Cheung | 04b7a41 | 2015-08-13 09:39:04 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | private function _checkConnectivityState($new_state) { |
Stanley Cheung | fea1f68 | 2015-08-17 14:20:22 -0700 | [diff] [blame] | 113 | if ($new_state == \Grpc\CHANNEL_READY) { |
Stanley Cheung | 04b7a41 | 2015-08-13 09:39:04 -0700 | [diff] [blame] | 114 | return true; |
| 115 | } |
Stanley Cheung | fea1f68 | 2015-08-17 14:20:22 -0700 | [diff] [blame] | 116 | if ($new_state == \Grpc\CHANNEL_FATAL_FAILURE) { |
Stanley Cheung | b0cd08a | 2015-10-09 16:58:01 -0700 | [diff] [blame^] | 117 | throw new \Exception('Failed to connect to server'); |
Stanley Cheung | 04b7a41 | 2015-08-13 09:39:04 -0700 | [diff] [blame] | 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | /** |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 123 | * Close the communication channel associated with this stub |
| 124 | */ |
| 125 | public function close() { |
| 126 | $channel->close(); |
| 127 | } |
| 128 | |
Stanley Cheung | 358b716 | 2015-05-12 22:51:30 -0700 | [diff] [blame] | 129 | /** |
| 130 | * constructs the auth uri for the jwt |
| 131 | */ |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 132 | private function _get_jwt_aud_uri($method) { |
| 133 | $last_slash_idx = strrpos($method, '/'); |
| 134 | if ($last_slash_idx === false) { |
| 135 | return false; |
| 136 | } |
| 137 | $service_name = substr($method, 0, $last_slash_idx); |
| 138 | return "https://" . $this->hostname . $service_name; |
| 139 | } |
| 140 | |
Stanley Cheung | cc019af | 2015-06-15 11:45:00 -0700 | [diff] [blame] | 141 | /** |
| 142 | * extract $timeout from $metadata |
| 143 | * @param $metadata The metadata map |
| 144 | * @return list($metadata_copy, $timeout) |
| 145 | */ |
| 146 | private function _extract_timeout_from_metadata($metadata) { |
| 147 | $timeout = false; |
| 148 | $metadata_copy = $metadata; |
| 149 | if (isset($metadata['timeout'])) { |
| 150 | $timeout = $metadata['timeout']; |
| 151 | unset($metadata_copy['timeout']); |
| 152 | } |
| 153 | return array($metadata_copy, $timeout); |
| 154 | } |
| 155 | |
Stanley Cheung | b0cd08a | 2015-10-09 16:58:01 -0700 | [diff] [blame^] | 156 | /** |
| 157 | * validate and normalize the metadata array |
| 158 | * @param $metadata The metadata map |
| 159 | * @return $metadata Validated and key-normalized metadata map |
| 160 | * @throw InvalidArgumentException if key contains invalid characters |
| 161 | */ |
| 162 | private function _validate_and_normalize_metadata($metadata) { |
| 163 | $metadata_copy = array(); |
| 164 | foreach ($metadata as $key => $value) { |
| 165 | if (!preg_match('/^[A-Za-z\d_-]+$/', $key)) { |
| 166 | throw new \InvalidArgumentException( |
| 167 | 'Metadata keys must be nonempty strings containing only '. |
| 168 | 'alphanumeric characters and hyphens'); |
| 169 | } |
| 170 | $metadata_copy[strtolower($key)] = $value; |
| 171 | } |
| 172 | return $metadata_copy; |
| 173 | } |
| 174 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 175 | /* This class is intended to be subclassed by generated code, so all functions |
| 176 | begin with "_" to avoid name collisions. */ |
| 177 | |
| 178 | /** |
| 179 | * Call a remote method that takes a single argument and has a single output |
| 180 | * |
| 181 | * @param string $method The name of the method to call |
| 182 | * @param $argument The argument to the method |
| 183 | * @param callable $deserialize A function that deserializes the response |
| 184 | * @param array $metadata A metadata map to send to the server |
| 185 | * @return SimpleSurfaceActiveCall The active call object |
| 186 | */ |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 187 | public function _simpleRequest($method, |
| 188 | $argument, |
| 189 | callable $deserialize, |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 190 | $metadata = array(), |
| 191 | $options = array()) { |
Stanley Cheung | cc019af | 2015-06-15 11:45:00 -0700 | [diff] [blame] | 192 | list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata); |
| 193 | $call = new UnaryCall($this->channel, $method, $deserialize, $timeout); |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 194 | $jwt_aud_uri = $this->_get_jwt_aud_uri($method); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 195 | if (is_callable($this->update_metadata)) { |
| 196 | $actual_metadata = call_user_func($this->update_metadata, |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 197 | $actual_metadata, |
| 198 | $jwt_aud_uri); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 199 | } |
Stanley Cheung | b0cd08a | 2015-10-09 16:58:01 -0700 | [diff] [blame^] | 200 | $actual_metadata = $this->_validate_and_normalize_metadata($actual_metadata); |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 201 | $call->start($argument, $actual_metadata, $options); |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 202 | return $call; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Call a remote method that takes a stream of arguments and has a single |
| 207 | * output |
| 208 | * |
| 209 | * @param string $method The name of the method to call |
| 210 | * @param $arguments An array or Traversable of arguments to stream to the |
| 211 | * server |
| 212 | * @param callable $deserialize A function that deserializes the response |
| 213 | * @param array $metadata A metadata map to send to the server |
| 214 | * @return ClientStreamingSurfaceActiveCall The active call object |
| 215 | */ |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 216 | public function _clientStreamRequest($method, |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 217 | callable $deserialize, |
| 218 | $metadata = array()) { |
Stanley Cheung | cc019af | 2015-06-15 11:45:00 -0700 | [diff] [blame] | 219 | list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata); |
| 220 | $call = new ClientStreamingCall($this->channel, $method, $deserialize, $timeout); |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 221 | $jwt_aud_uri = $this->_get_jwt_aud_uri($method); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 222 | if (is_callable($this->update_metadata)) { |
| 223 | $actual_metadata = call_user_func($this->update_metadata, |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 224 | $actual_metadata, |
| 225 | $jwt_aud_uri); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 226 | } |
Stanley Cheung | b0cd08a | 2015-10-09 16:58:01 -0700 | [diff] [blame^] | 227 | $actual_metadata = $this->_validate_and_normalize_metadata($actual_metadata); |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 228 | $call->start($actual_metadata); |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 229 | return $call; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Call a remote method that takes a single argument and returns a stream of |
| 234 | * responses |
| 235 | * |
| 236 | * @param string $method The name of the method to call |
| 237 | * @param $argument The argument to the method |
| 238 | * @param callable $deserialize A function that deserializes the responses |
| 239 | * @param array $metadata A metadata map to send to the server |
| 240 | * @return ServerStreamingSurfaceActiveCall The active call object |
| 241 | */ |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 242 | public function _serverStreamRequest($method, |
| 243 | $argument, |
| 244 | callable $deserialize, |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 245 | $metadata = array(), |
| 246 | $options = array()) { |
Stanley Cheung | cc019af | 2015-06-15 11:45:00 -0700 | [diff] [blame] | 247 | list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata); |
| 248 | $call = new ServerStreamingCall($this->channel, $method, $deserialize, $timeout); |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 249 | $jwt_aud_uri = $this->_get_jwt_aud_uri($method); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 250 | if (is_callable($this->update_metadata)) { |
| 251 | $actual_metadata = call_user_func($this->update_metadata, |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 252 | $actual_metadata, |
| 253 | $jwt_aud_uri); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 254 | } |
Stanley Cheung | b0cd08a | 2015-10-09 16:58:01 -0700 | [diff] [blame^] | 255 | $actual_metadata = $this->_validate_and_normalize_metadata($actual_metadata); |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 256 | $call->start($argument, $actual_metadata, $options); |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 257 | return $call; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Call a remote method with messages streaming in both directions |
| 262 | * |
| 263 | * @param string $method The name of the method to call |
| 264 | * @param callable $deserialize A function that deserializes the responses |
| 265 | * @param array $metadata A metadata map to send to the server |
| 266 | * @return BidiStreamingSurfaceActiveCall The active call object |
| 267 | */ |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 268 | public function _bidiRequest($method, |
| 269 | callable $deserialize, |
| 270 | $metadata = array()) { |
Stanley Cheung | cc019af | 2015-06-15 11:45:00 -0700 | [diff] [blame] | 271 | list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata); |
| 272 | $call = new BidiStreamingCall($this->channel, $method, $deserialize, $timeout); |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 273 | $jwt_aud_uri = $this->_get_jwt_aud_uri($method); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 274 | if (is_callable($this->update_metadata)) { |
| 275 | $actual_metadata = call_user_func($this->update_metadata, |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 276 | $actual_metadata, |
| 277 | $jwt_aud_uri); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 278 | } |
Stanley Cheung | b0cd08a | 2015-10-09 16:58:01 -0700 | [diff] [blame^] | 279 | $actual_metadata = $this->_validate_and_normalize_metadata($actual_metadata); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 280 | $call->start($actual_metadata); |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 281 | return $call; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 282 | } |
| 283 | } |