blob: a0c677908c7c4bc9714c8f5ffdc01401c1cf1d3b [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 /**
mlumishb892a272014-12-09 16:28:23 -080078 * Close the communication channel associated with this stub
79 */
80 public function close() {
81 $channel->close();
82 }
83
Stanley Cheung358b7162015-05-12 22:51:30 -070084 /**
85 * constructs the auth uri for the jwt
86 */
Stanley Cheungf4206872015-05-12 17:39:30 -070087 private function _get_jwt_aud_uri($method) {
88 $last_slash_idx = strrpos($method, '/');
89 if ($last_slash_idx === false) {
90 return false;
91 }
92 $service_name = substr($method, 0, $last_slash_idx);
93 return "https://" . $this->hostname . $service_name;
94 }
95
Stanley Cheungcc019af2015-06-15 11:45:00 -070096 /**
97 * extract $timeout from $metadata
98 * @param $metadata The metadata map
99 * @return list($metadata_copy, $timeout)
100 */
101 private function _extract_timeout_from_metadata($metadata) {
102 $timeout = false;
103 $metadata_copy = $metadata;
104 if (isset($metadata['timeout'])) {
105 $timeout = $metadata['timeout'];
106 unset($metadata_copy['timeout']);
107 }
108 return array($metadata_copy, $timeout);
109 }
110
mlumishb892a272014-12-09 16:28:23 -0800111 /* This class is intended to be subclassed by generated code, so all functions
112 begin with "_" to avoid name collisions. */
113
114 /**
115 * Call a remote method that takes a single argument and has a single output
116 *
117 * @param string $method The name of the method to call
118 * @param $argument The argument to the method
119 * @param callable $deserialize A function that deserializes the response
120 * @param array $metadata A metadata map to send to the server
121 * @return SimpleSurfaceActiveCall The active call object
122 */
murgatroid9914d2ce22015-01-30 15:36:23 -0800123 public function _simpleRequest($method,
124 $argument,
125 callable $deserialize,
126 $metadata = array()) {
Stanley Cheungcc019af2015-06-15 11:45:00 -0700127 list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
128 $call = new UnaryCall($this->channel, $method, $deserialize, $timeout);
Stanley Cheungf4206872015-05-12 17:39:30 -0700129 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700130 if (is_callable($this->update_metadata)) {
131 $actual_metadata = call_user_func($this->update_metadata,
Stanley Cheungf4206872015-05-12 17:39:30 -0700132 $actual_metadata,
133 $jwt_aud_uri);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700134 }
135 $call->start($argument, $actual_metadata);
murgatroid999140a062015-03-26 11:27:58 -0700136 return $call;
mlumishb892a272014-12-09 16:28:23 -0800137 }
138
139 /**
140 * Call a remote method that takes a stream of arguments and has a single
141 * output
142 *
143 * @param string $method The name of the method to call
144 * @param $arguments An array or Traversable of arguments to stream to the
145 * server
146 * @param callable $deserialize A function that deserializes the response
147 * @param array $metadata A metadata map to send to the server
148 * @return ClientStreamingSurfaceActiveCall The active call object
149 */
murgatroid9914d2ce22015-01-30 15:36:23 -0800150 public function _clientStreamRequest($method,
151 $arguments,
152 callable $deserialize,
153 $metadata = array()) {
Stanley Cheungcc019af2015-06-15 11:45:00 -0700154 list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
155 $call = new ClientStreamingCall($this->channel, $method, $deserialize, $timeout);
Stanley Cheungf4206872015-05-12 17:39:30 -0700156 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700157 if (is_callable($this->update_metadata)) {
158 $actual_metadata = call_user_func($this->update_metadata,
Stanley Cheungf4206872015-05-12 17:39:30 -0700159 $actual_metadata,
160 $jwt_aud_uri);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700161 }
162 $call->start($arguments, $actual_metadata);
murgatroid999140a062015-03-26 11:27:58 -0700163 return $call;
mlumishb892a272014-12-09 16:28:23 -0800164 }
165
166 /**
167 * Call a remote method that takes a single argument and returns a stream of
168 * responses
169 *
170 * @param string $method The name of the method to call
171 * @param $argument The argument to the method
172 * @param callable $deserialize A function that deserializes the responses
173 * @param array $metadata A metadata map to send to the server
174 * @return ServerStreamingSurfaceActiveCall The active call object
175 */
murgatroid9914d2ce22015-01-30 15:36:23 -0800176 public function _serverStreamRequest($method,
177 $argument,
178 callable $deserialize,
179 $metadata = array()) {
Stanley Cheungcc019af2015-06-15 11:45:00 -0700180 list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
181 $call = new ServerStreamingCall($this->channel, $method, $deserialize, $timeout);
Stanley Cheungf4206872015-05-12 17:39:30 -0700182 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700183 if (is_callable($this->update_metadata)) {
184 $actual_metadata = call_user_func($this->update_metadata,
Stanley Cheungf4206872015-05-12 17:39:30 -0700185 $actual_metadata,
186 $jwt_aud_uri);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700187 }
188 $call->start($argument, $actual_metadata);
murgatroid999140a062015-03-26 11:27:58 -0700189 return $call;
mlumishb892a272014-12-09 16:28:23 -0800190 }
191
192 /**
193 * Call a remote method with messages streaming in both directions
194 *
195 * @param string $method The name of the method to call
196 * @param callable $deserialize A function that deserializes the responses
197 * @param array $metadata A metadata map to send to the server
198 * @return BidiStreamingSurfaceActiveCall The active call object
199 */
murgatroid9914d2ce22015-01-30 15:36:23 -0800200 public function _bidiRequest($method,
201 callable $deserialize,
202 $metadata = array()) {
Stanley Cheungcc019af2015-06-15 11:45:00 -0700203 list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
204 $call = new BidiStreamingCall($this->channel, $method, $deserialize, $timeout);
Stanley Cheungf4206872015-05-12 17:39:30 -0700205 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700206 if (is_callable($this->update_metadata)) {
207 $actual_metadata = call_user_func($this->update_metadata,
Stanley Cheungf4206872015-05-12 17:39:30 -0700208 $actual_metadata,
209 $jwt_aud_uri);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700210 }
211 $call->start($actual_metadata);
murgatroid999140a062015-03-26 11:27:58 -0700212 return $call;
mlumishb892a272014-12-09 16:28:23 -0800213 }
214}