blob: 381b1143993301cf34dba9e1fb85f21c57d9ec49 [file] [log] [blame]
mlumishb892a272014-12-09 16:28:23 -08001<?php
Craig Tiller2e498aa2015-02-16 12:09:31 -08002/*
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 */
mlumishb892a272014-12-09 16:28:23 -080034namespace Grpc;
mlumishb892a272014-12-09 16:28:23 -080035
36/**
37 * Base class for generated client stubs. Stub methods are expected to call
38 * _simpleRequest or _streamRequest and return the result.
39 */
40class BaseStub {
41
Stanley Cheungf4206872015-05-12 17:39:30 -070042 private $hostname;
mlumishb892a272014-12-09 16:28:23 -080043 private $channel;
44
Stanley Cheung2c9c7632015-04-20 14:13:54 -070045 // 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 */
murgatroid99f21eb252015-01-30 13:47:41 -080054 public function __construct($hostname, $opts) {
Stanley Cheungf4206872015-05-12 17:39:30 -070055 $this->hostname = $hostname;
Stanley Cheung2c9c7632015-04-20 14:13:54 -070056 $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 Cheunga75098d2015-07-24 09:03:39 -070063 $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'];
murgatroid99f21eb252015-01-30 13:47:41 -080067 $this->channel = new Channel($hostname, $opts);
mlumishb892a272014-12-09 16:28:23 -080068 }
69
70 /**
Stanley Cheungdb98e082015-07-27 10:19:45 -070071 * @return string The URI of the endpoint.
72 */
73 public function getTarget() {
74 return $this->channel->getTarget();
75 }
76
77 /**
Stanley Cheunge63354a2015-08-10 15:46:42 -070078 * @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 Cheung04b7a412015-08-13 09:39:04 -070086 * @param $timeout in microseconds
87 * @return bool true if channel is ready
Stanley Cheung1567c0c2015-08-13 11:12:54 -070088 * @throw Exception if channel is in FATAL_ERROR state
Stanley Cheung04b7a412015-08-13 09:39:04 -070089 */
Stanley Cheung1567c0c2015-08-13 11:12:54 -070090 public function waitForReady($timeout) {
Stanley Cheung04b7a412015-08-13 09:39:04 -070091 $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 Cheung1567c0c2015-08-13 11:12:54 -0700100 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 Cheung04b7a412015-08-13 09:39:04 -0700106 }
Stanley Cheung1567c0c2015-08-13 11:12:54 -0700107 // deadline has passed
Stanley Cheung04b7a412015-08-13 09:39:04 -0700108 $new_state = $this->getConnectivityState();
Stanley Cheung1567c0c2015-08-13 11:12:54 -0700109 return $this->_checkConnectivityState($new_state);
Stanley Cheung04b7a412015-08-13 09:39:04 -0700110 }
111
112 private function _checkConnectivityState($new_state) {
Stanley Cheungfea1f682015-08-17 14:20:22 -0700113 if ($new_state == \Grpc\CHANNEL_READY) {
Stanley Cheung04b7a412015-08-13 09:39:04 -0700114 return true;
115 }
Stanley Cheungfea1f682015-08-17 14:20:22 -0700116 if ($new_state == \Grpc\CHANNEL_FATAL_FAILURE) {
Stanley Cheung04b7a412015-08-13 09:39:04 -0700117 throw new Exception('Failed to connect to server');
118 }
119 return false;
120 }
121
122 /**
mlumishb892a272014-12-09 16:28:23 -0800123 * Close the communication channel associated with this stub
124 */
125 public function close() {
126 $channel->close();
127 }
128
Stanley Cheung358b7162015-05-12 22:51:30 -0700129 /**
130 * constructs the auth uri for the jwt
131 */
Stanley Cheungf4206872015-05-12 17:39:30 -0700132 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 Cheungcc019af2015-06-15 11:45:00 -0700141 /**
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
mlumishb892a272014-12-09 16:28:23 -0800156 /* This class is intended to be subclassed by generated code, so all functions
157 begin with "_" to avoid name collisions. */
158
159 /**
160 * Call a remote method that takes a single argument and has a single output
161 *
162 * @param string $method The name of the method to call
163 * @param $argument The argument to the method
164 * @param callable $deserialize A function that deserializes the response
165 * @param array $metadata A metadata map to send to the server
166 * @return SimpleSurfaceActiveCall The active call object
167 */
murgatroid9914d2ce22015-01-30 15:36:23 -0800168 public function _simpleRequest($method,
169 $argument,
170 callable $deserialize,
Stanley Cheung3ab8e792015-08-24 16:58:42 -0700171 $metadata = array(),
172 $options = array()) {
Stanley Cheungcc019af2015-06-15 11:45:00 -0700173 list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
174 $call = new UnaryCall($this->channel, $method, $deserialize, $timeout);
Stanley Cheungf4206872015-05-12 17:39:30 -0700175 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700176 if (is_callable($this->update_metadata)) {
177 $actual_metadata = call_user_func($this->update_metadata,
Stanley Cheungf4206872015-05-12 17:39:30 -0700178 $actual_metadata,
179 $jwt_aud_uri);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700180 }
Stanley Cheung3ab8e792015-08-24 16:58:42 -0700181 $call->start($argument, $actual_metadata, $options);
murgatroid999140a062015-03-26 11:27:58 -0700182 return $call;
mlumishb892a272014-12-09 16:28:23 -0800183 }
184
185 /**
186 * Call a remote method that takes a stream of arguments and has a single
187 * output
188 *
189 * @param string $method The name of the method to call
190 * @param $arguments An array or Traversable of arguments to stream to the
191 * server
192 * @param callable $deserialize A function that deserializes the response
193 * @param array $metadata A metadata map to send to the server
194 * @return ClientStreamingSurfaceActiveCall The active call object
195 */
murgatroid9914d2ce22015-01-30 15:36:23 -0800196 public function _clientStreamRequest($method,
murgatroid9914d2ce22015-01-30 15:36:23 -0800197 callable $deserialize,
198 $metadata = array()) {
Stanley Cheungcc019af2015-06-15 11:45:00 -0700199 list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
200 $call = new ClientStreamingCall($this->channel, $method, $deserialize, $timeout);
Stanley Cheungf4206872015-05-12 17:39:30 -0700201 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700202 if (is_callable($this->update_metadata)) {
203 $actual_metadata = call_user_func($this->update_metadata,
Stanley Cheungf4206872015-05-12 17:39:30 -0700204 $actual_metadata,
205 $jwt_aud_uri);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700206 }
Stanley Cheung3ab8e792015-08-24 16:58:42 -0700207 $call->start($actual_metadata);
murgatroid999140a062015-03-26 11:27:58 -0700208 return $call;
mlumishb892a272014-12-09 16:28:23 -0800209 }
210
211 /**
212 * Call a remote method that takes a single argument and returns a stream of
213 * responses
214 *
215 * @param string $method The name of the method to call
216 * @param $argument The argument to the method
217 * @param callable $deserialize A function that deserializes the responses
218 * @param array $metadata A metadata map to send to the server
219 * @return ServerStreamingSurfaceActiveCall The active call object
220 */
murgatroid9914d2ce22015-01-30 15:36:23 -0800221 public function _serverStreamRequest($method,
222 $argument,
223 callable $deserialize,
Stanley Cheung3ab8e792015-08-24 16:58:42 -0700224 $metadata = array(),
225 $options = array()) {
Stanley Cheungcc019af2015-06-15 11:45:00 -0700226 list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
227 $call = new ServerStreamingCall($this->channel, $method, $deserialize, $timeout);
Stanley Cheungf4206872015-05-12 17:39:30 -0700228 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700229 if (is_callable($this->update_metadata)) {
230 $actual_metadata = call_user_func($this->update_metadata,
Stanley Cheungf4206872015-05-12 17:39:30 -0700231 $actual_metadata,
232 $jwt_aud_uri);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700233 }
Stanley Cheung3ab8e792015-08-24 16:58:42 -0700234 $call->start($argument, $actual_metadata, $options);
murgatroid999140a062015-03-26 11:27:58 -0700235 return $call;
mlumishb892a272014-12-09 16:28:23 -0800236 }
237
238 /**
239 * Call a remote method with messages streaming in both directions
240 *
241 * @param string $method The name of the method to call
242 * @param callable $deserialize A function that deserializes the responses
243 * @param array $metadata A metadata map to send to the server
244 * @return BidiStreamingSurfaceActiveCall The active call object
245 */
murgatroid9914d2ce22015-01-30 15:36:23 -0800246 public function _bidiRequest($method,
247 callable $deserialize,
248 $metadata = array()) {
Stanley Cheungcc019af2015-06-15 11:45:00 -0700249 list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
250 $call = new BidiStreamingCall($this->channel, $method, $deserialize, $timeout);
Stanley Cheungf4206872015-05-12 17:39:30 -0700251 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700252 if (is_callable($this->update_metadata)) {
253 $actual_metadata = call_user_func($this->update_metadata,
Stanley Cheungf4206872015-05-12 17:39:30 -0700254 $actual_metadata,
255 $jwt_aud_uri);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700256 }
257 $call->start($actual_metadata);
murgatroid999140a062015-03-26 11:27:58 -0700258 return $call;
mlumishb892a272014-12-09 16:28:23 -0800259 }
260}