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 | } |
| 63 | |
murgatroid99 | f21eb25 | 2015-01-30 13:47:41 -0800 | [diff] [blame] | 64 | $this->channel = new Channel($hostname, $opts); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Close the communication channel associated with this stub |
| 69 | */ |
| 70 | public function close() { |
| 71 | $channel->close(); |
| 72 | } |
| 73 | |
Stanley Cheung | 358b716 | 2015-05-12 22:51:30 -0700 | [diff] [blame] | 74 | /** |
| 75 | * constructs the auth uri for the jwt |
| 76 | */ |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 77 | private function _get_jwt_aud_uri($method) { |
| 78 | $last_slash_idx = strrpos($method, '/'); |
| 79 | if ($last_slash_idx === false) { |
| 80 | return false; |
| 81 | } |
| 82 | $service_name = substr($method, 0, $last_slash_idx); |
| 83 | return "https://" . $this->hostname . $service_name; |
| 84 | } |
| 85 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 86 | /* This class is intended to be subclassed by generated code, so all functions |
| 87 | begin with "_" to avoid name collisions. */ |
| 88 | |
| 89 | /** |
| 90 | * Call a remote method that takes a single argument and has a single output |
| 91 | * |
| 92 | * @param string $method The name of the method to call |
| 93 | * @param $argument The argument to the method |
| 94 | * @param callable $deserialize A function that deserializes the response |
| 95 | * @param array $metadata A metadata map to send to the server |
| 96 | * @return SimpleSurfaceActiveCall The active call object |
| 97 | */ |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 98 | public function _simpleRequest($method, |
| 99 | $argument, |
| 100 | callable $deserialize, |
| 101 | $metadata = array()) { |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 102 | $call = new UnaryCall($this->channel, $method, $deserialize); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 103 | $actual_metadata = $metadata; |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 104 | $jwt_aud_uri = $this->_get_jwt_aud_uri($method); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 105 | if (is_callable($this->update_metadata)) { |
| 106 | $actual_metadata = call_user_func($this->update_metadata, |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 107 | $actual_metadata, |
| 108 | $jwt_aud_uri); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 109 | } |
| 110 | $call->start($argument, $actual_metadata); |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 111 | return $call; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Call a remote method that takes a stream of arguments and has a single |
| 116 | * output |
| 117 | * |
| 118 | * @param string $method The name of the method to call |
| 119 | * @param $arguments An array or Traversable of arguments to stream to the |
| 120 | * server |
| 121 | * @param callable $deserialize A function that deserializes the response |
| 122 | * @param array $metadata A metadata map to send to the server |
| 123 | * @return ClientStreamingSurfaceActiveCall The active call object |
| 124 | */ |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 125 | public function _clientStreamRequest($method, |
| 126 | $arguments, |
| 127 | callable $deserialize, |
| 128 | $metadata = array()) { |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 129 | $call = new ClientStreamingCall($this->channel, $method, $deserialize); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 130 | $actual_metadata = $metadata; |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 131 | $jwt_aud_uri = $this->_get_jwt_aud_uri($method); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 132 | if (is_callable($this->update_metadata)) { |
| 133 | $actual_metadata = call_user_func($this->update_metadata, |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 134 | $actual_metadata, |
| 135 | $jwt_aud_uri); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 136 | } |
| 137 | $call->start($arguments, $actual_metadata); |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 138 | return $call; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Call a remote method that takes a single argument and returns a stream of |
| 143 | * responses |
| 144 | * |
| 145 | * @param string $method The name of the method to call |
| 146 | * @param $argument The argument to the method |
| 147 | * @param callable $deserialize A function that deserializes the responses |
| 148 | * @param array $metadata A metadata map to send to the server |
| 149 | * @return ServerStreamingSurfaceActiveCall The active call object |
| 150 | */ |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 151 | public function _serverStreamRequest($method, |
| 152 | $argument, |
| 153 | callable $deserialize, |
| 154 | $metadata = array()) { |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 155 | $call = new ServerStreamingCall($this->channel, $method, $deserialize); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 156 | $actual_metadata = $metadata; |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 157 | $jwt_aud_uri = $this->_get_jwt_aud_uri($method); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 158 | if (is_callable($this->update_metadata)) { |
| 159 | $actual_metadata = call_user_func($this->update_metadata, |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 160 | $actual_metadata, |
| 161 | $jwt_aud_uri); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 162 | } |
| 163 | $call->start($argument, $actual_metadata); |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 164 | return $call; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Call a remote method with messages streaming in both directions |
| 169 | * |
| 170 | * @param string $method The name of the method to call |
| 171 | * @param callable $deserialize A function that deserializes the responses |
| 172 | * @param array $metadata A metadata map to send to the server |
| 173 | * @return BidiStreamingSurfaceActiveCall The active call object |
| 174 | */ |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 175 | public function _bidiRequest($method, |
| 176 | callable $deserialize, |
| 177 | $metadata = array()) { |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 178 | $call = new BidiStreamingCall($this->channel, $method, $deserialize); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 179 | $actual_metadata = $metadata; |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 180 | $jwt_aud_uri = $this->_get_jwt_aud_uri($method); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 181 | if (is_callable($this->update_metadata)) { |
| 182 | $actual_metadata = call_user_func($this->update_metadata, |
Stanley Cheung | f420687 | 2015-05-12 17:39:30 -0700 | [diff] [blame] | 183 | $actual_metadata, |
| 184 | $jwt_aud_uri); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 185 | } |
| 186 | $call->start($actual_metadata); |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 187 | return $call; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 188 | } |
| 189 | } |