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 | |
| 42 | private $channel; |
| 43 | |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 44 | // a callback function |
| 45 | private $update_metadata; |
| 46 | |
| 47 | /** |
| 48 | * @param $hostname string |
| 49 | * @param $opts array |
| 50 | * - 'update_metadata': (optional) a callback function which takes in a |
| 51 | * metadata array, and returns an updated metadata array |
| 52 | */ |
murgatroid99 | f21eb25 | 2015-01-30 13:47:41 -0800 | [diff] [blame] | 53 | public function __construct($hostname, $opts) { |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 54 | $this->update_metadata = null; |
| 55 | if (isset($opts['update_metadata'])) { |
| 56 | if (is_callable($opts['update_metadata'])) { |
| 57 | $this->update_metadata = $opts['update_metadata']; |
| 58 | } |
| 59 | unset($opts['update_metadata']); |
| 60 | } |
| 61 | |
murgatroid99 | f21eb25 | 2015-01-30 13:47:41 -0800 | [diff] [blame] | 62 | $this->channel = new Channel($hostname, $opts); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Close the communication channel associated with this stub |
| 67 | */ |
| 68 | public function close() { |
| 69 | $channel->close(); |
| 70 | } |
| 71 | |
| 72 | /* This class is intended to be subclassed by generated code, so all functions |
| 73 | begin with "_" to avoid name collisions. */ |
| 74 | |
| 75 | /** |
| 76 | * Call a remote method that takes a single argument and has a single output |
| 77 | * |
| 78 | * @param string $method The name of the method to call |
| 79 | * @param $argument The argument to the method |
| 80 | * @param callable $deserialize A function that deserializes the response |
| 81 | * @param array $metadata A metadata map to send to the server |
| 82 | * @return SimpleSurfaceActiveCall The active call object |
| 83 | */ |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 84 | public function _simpleRequest($method, |
| 85 | $argument, |
| 86 | callable $deserialize, |
| 87 | $metadata = array()) { |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 88 | $call = new UnaryCall($this->channel, $method, $deserialize); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 89 | $actual_metadata = $metadata; |
| 90 | if (is_callable($this->update_metadata)) { |
| 91 | $actual_metadata = call_user_func($this->update_metadata, |
| 92 | $actual_metadata); |
| 93 | } |
| 94 | $call->start($argument, $actual_metadata); |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 95 | return $call; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Call a remote method that takes a stream of arguments and has a single |
| 100 | * output |
| 101 | * |
| 102 | * @param string $method The name of the method to call |
| 103 | * @param $arguments An array or Traversable of arguments to stream to the |
| 104 | * server |
| 105 | * @param callable $deserialize A function that deserializes the response |
| 106 | * @param array $metadata A metadata map to send to the server |
| 107 | * @return ClientStreamingSurfaceActiveCall The active call object |
| 108 | */ |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 109 | public function _clientStreamRequest($method, |
| 110 | $arguments, |
| 111 | callable $deserialize, |
| 112 | $metadata = array()) { |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 113 | $call = new ClientStreamingCall($this->channel, $method, $deserialize); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 114 | $actual_metadata = $metadata; |
| 115 | if (is_callable($this->update_metadata)) { |
| 116 | $actual_metadata = call_user_func($this->update_metadata, |
| 117 | $actual_metadata); |
| 118 | } |
| 119 | $call->start($arguments, $actual_metadata); |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 120 | return $call; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Call a remote method that takes a single argument and returns a stream of |
| 125 | * responses |
| 126 | * |
| 127 | * @param string $method The name of the method to call |
| 128 | * @param $argument The argument to the method |
| 129 | * @param callable $deserialize A function that deserializes the responses |
| 130 | * @param array $metadata A metadata map to send to the server |
| 131 | * @return ServerStreamingSurfaceActiveCall The active call object |
| 132 | */ |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 133 | public function _serverStreamRequest($method, |
| 134 | $argument, |
| 135 | callable $deserialize, |
| 136 | $metadata = array()) { |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 137 | $call = new ServerStreamingCall($this->channel, $method, $deserialize); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 138 | $actual_metadata = $metadata; |
| 139 | if (is_callable($this->update_metadata)) { |
| 140 | $actual_metadata = call_user_func($this->update_metadata, |
| 141 | $actual_metadata); |
| 142 | } |
| 143 | $call->start($argument, $actual_metadata); |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 144 | return $call; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Call a remote method with messages streaming in both directions |
| 149 | * |
| 150 | * @param string $method The name of the method to call |
| 151 | * @param callable $deserialize A function that deserializes the responses |
| 152 | * @param array $metadata A metadata map to send to the server |
| 153 | * @return BidiStreamingSurfaceActiveCall The active call object |
| 154 | */ |
murgatroid99 | 14d2ce2 | 2015-01-30 15:36:23 -0800 | [diff] [blame] | 155 | public function _bidiRequest($method, |
| 156 | callable $deserialize, |
| 157 | $metadata = array()) { |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 158 | $call = new BidiStreamingCall($this->channel, $method, $deserialize); |
Stanley Cheung | 2c9c763 | 2015-04-20 14:13:54 -0700 | [diff] [blame] | 159 | $actual_metadata = $metadata; |
| 160 | if (is_callable($this->update_metadata)) { |
| 161 | $actual_metadata = call_user_func($this->update_metadata, |
| 162 | $actual_metadata); |
| 163 | } |
| 164 | $call->start($actual_metadata); |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 165 | return $call; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 166 | } |
| 167 | } |