blob: 48c00977eb8ec9290ef0928132b9beca522e6c81 [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 }
63
murgatroid99f21eb252015-01-30 13:47:41 -080064 $this->channel = new Channel($hostname, $opts);
mlumishb892a272014-12-09 16:28:23 -080065 }
66
67 /**
68 * Close the communication channel associated with this stub
69 */
70 public function close() {
71 $channel->close();
72 }
73
Stanley Cheung358b7162015-05-12 22:51:30 -070074 /**
75 * constructs the auth uri for the jwt
76 */
Stanley Cheungf4206872015-05-12 17:39:30 -070077 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
Stanley Cheungcc019af2015-06-15 11:45:00 -070086 /**
87 * extract $timeout from $metadata
88 * @param $metadata The metadata map
89 * @return list($metadata_copy, $timeout)
90 */
91 private function _extract_timeout_from_metadata($metadata) {
92 $timeout = false;
93 $metadata_copy = $metadata;
94 if (isset($metadata['timeout'])) {
95 $timeout = $metadata['timeout'];
96 unset($metadata_copy['timeout']);
97 }
98 return array($metadata_copy, $timeout);
99 }
100
mlumishb892a272014-12-09 16:28:23 -0800101 /* This class is intended to be subclassed by generated code, so all functions
102 begin with "_" to avoid name collisions. */
103
104 /**
105 * Call a remote method that takes a single argument and has a single output
106 *
107 * @param string $method The name of the method to call
108 * @param $argument The argument to the method
109 * @param callable $deserialize A function that deserializes the response
110 * @param array $metadata A metadata map to send to the server
111 * @return SimpleSurfaceActiveCall The active call object
112 */
murgatroid9914d2ce22015-01-30 15:36:23 -0800113 public function _simpleRequest($method,
114 $argument,
115 callable $deserialize,
116 $metadata = array()) {
Stanley Cheungcc019af2015-06-15 11:45:00 -0700117 list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
118 $call = new UnaryCall($this->channel, $method, $deserialize, $timeout);
Stanley Cheungf4206872015-05-12 17:39:30 -0700119 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700120 if (is_callable($this->update_metadata)) {
121 $actual_metadata = call_user_func($this->update_metadata,
Stanley Cheungf4206872015-05-12 17:39:30 -0700122 $actual_metadata,
123 $jwt_aud_uri);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700124 }
125 $call->start($argument, $actual_metadata);
murgatroid999140a062015-03-26 11:27:58 -0700126 return $call;
mlumishb892a272014-12-09 16:28:23 -0800127 }
128
129 /**
130 * Call a remote method that takes a stream of arguments and has a single
131 * output
132 *
133 * @param string $method The name of the method to call
134 * @param $arguments An array or Traversable of arguments to stream to the
135 * server
136 * @param callable $deserialize A function that deserializes the response
137 * @param array $metadata A metadata map to send to the server
138 * @return ClientStreamingSurfaceActiveCall The active call object
139 */
murgatroid9914d2ce22015-01-30 15:36:23 -0800140 public function _clientStreamRequest($method,
141 $arguments,
142 callable $deserialize,
143 $metadata = array()) {
Stanley Cheungcc019af2015-06-15 11:45:00 -0700144 list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
145 $call = new ClientStreamingCall($this->channel, $method, $deserialize, $timeout);
Stanley Cheungf4206872015-05-12 17:39:30 -0700146 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700147 if (is_callable($this->update_metadata)) {
148 $actual_metadata = call_user_func($this->update_metadata,
Stanley Cheungf4206872015-05-12 17:39:30 -0700149 $actual_metadata,
150 $jwt_aud_uri);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700151 }
152 $call->start($arguments, $actual_metadata);
murgatroid999140a062015-03-26 11:27:58 -0700153 return $call;
mlumishb892a272014-12-09 16:28:23 -0800154 }
155
156 /**
157 * Call a remote method that takes a single argument and returns a stream of
158 * responses
159 *
160 * @param string $method The name of the method to call
161 * @param $argument The argument to the method
162 * @param callable $deserialize A function that deserializes the responses
163 * @param array $metadata A metadata map to send to the server
164 * @return ServerStreamingSurfaceActiveCall The active call object
165 */
murgatroid9914d2ce22015-01-30 15:36:23 -0800166 public function _serverStreamRequest($method,
167 $argument,
168 callable $deserialize,
169 $metadata = array()) {
Stanley Cheungcc019af2015-06-15 11:45:00 -0700170 list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
171 $call = new ServerStreamingCall($this->channel, $method, $deserialize, $timeout);
Stanley Cheungf4206872015-05-12 17:39:30 -0700172 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700173 if (is_callable($this->update_metadata)) {
174 $actual_metadata = call_user_func($this->update_metadata,
Stanley Cheungf4206872015-05-12 17:39:30 -0700175 $actual_metadata,
176 $jwt_aud_uri);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700177 }
178 $call->start($argument, $actual_metadata);
murgatroid999140a062015-03-26 11:27:58 -0700179 return $call;
mlumishb892a272014-12-09 16:28:23 -0800180 }
181
182 /**
183 * Call a remote method with messages streaming in both directions
184 *
185 * @param string $method The name of the method to call
186 * @param callable $deserialize A function that deserializes the responses
187 * @param array $metadata A metadata map to send to the server
188 * @return BidiStreamingSurfaceActiveCall The active call object
189 */
murgatroid9914d2ce22015-01-30 15:36:23 -0800190 public function _bidiRequest($method,
191 callable $deserialize,
192 $metadata = array()) {
Stanley Cheungcc019af2015-06-15 11:45:00 -0700193 list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
194 $call = new BidiStreamingCall($this->channel, $method, $deserialize, $timeout);
Stanley Cheungf4206872015-05-12 17:39:30 -0700195 $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700196 if (is_callable($this->update_metadata)) {
197 $actual_metadata = call_user_func($this->update_metadata,
Stanley Cheungf4206872015-05-12 17:39:30 -0700198 $actual_metadata,
199 $jwt_aud_uri);
Stanley Cheung2c9c7632015-04-20 14:13:54 -0700200 }
201 $call->start($actual_metadata);
murgatroid999140a062015-03-26 11:27:58 -0700202 return $call;
mlumishb892a272014-12-09 16:28:23 -0800203 }
204}