mlumish | 156e67d | 2015-01-02 14:59:16 -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 | */ |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 34 | |
mlumish | 156e67d | 2015-01-02 14:59:16 -0800 | [diff] [blame] | 35 | namespace Grpc; |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 36 | |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 37 | /** |
thinkerou | 9a669b6 | 2016-11-01 21:31:43 +0800 | [diff] [blame] | 38 | * Class AbstractCall. |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 39 | * @package Grpc |
| 40 | */ |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 41 | abstract class AbstractCall |
| 42 | { |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 43 | /** |
| 44 | * @var Call |
| 45 | */ |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 46 | protected $call; |
| 47 | protected $deserialize; |
| 48 | protected $metadata; |
Stanley Cheung | 6668d51 | 2016-05-18 14:05:09 -0700 | [diff] [blame] | 49 | protected $trailing_metadata; |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 50 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Create a new Call wrapper object. |
| 53 | * |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 54 | * @param Channel $channel The channel to communicate on |
| 55 | * @param string $method The method to call on the |
| 56 | * remote server |
| 57 | * @param callback $deserialize A callback function to deserialize |
| 58 | * the response |
| 59 | * @param array $options Call options (optional) |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 60 | */ |
thinkerou | 16713db | 2017-01-20 14:43:19 +0800 | [diff] [blame^] | 61 | public function __construct(Channel $channel, |
| 62 | $method, |
| 63 | $deserialize, |
| 64 | array $options = []) |
| 65 | { |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 66 | if (array_key_exists('timeout', $options) && |
| 67 | is_numeric($timeout = $options['timeout']) |
| 68 | ) { |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 69 | $now = Timeval::now(); |
| 70 | $delta = new Timeval($timeout); |
| 71 | $deadline = $now->add($delta); |
| 72 | } else { |
| 73 | $deadline = Timeval::infFuture(); |
| 74 | } |
| 75 | $this->call = new Call($channel, $method, $deadline); |
| 76 | $this->deserialize = $deserialize; |
| 77 | $this->metadata = null; |
Stanley Cheung | 6668d51 | 2016-05-18 14:05:09 -0700 | [diff] [blame] | 78 | $this->trailing_metadata = null; |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 79 | if (array_key_exists('call_credentials_callback', $options) && |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 80 | is_callable($call_credentials_callback = |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 81 | $options['call_credentials_callback']) |
| 82 | ) { |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 83 | $call_credentials = CallCredentials::createFromPlugin( |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 84 | $call_credentials_callback |
| 85 | ); |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 86 | $this->call->setCredentials($call_credentials); |
| 87 | } |
Stanley Cheung | cc019af | 2015-06-15 11:45:00 -0700 | [diff] [blame] | 88 | } |
mlumish | 156e67d | 2015-01-02 14:59:16 -0800 | [diff] [blame] | 89 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 90 | /** |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 91 | * @return mixed The metadata sent by the server |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 92 | */ |
| 93 | public function getMetadata() |
| 94 | { |
| 95 | return $this->metadata; |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 96 | } |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 99 | * @return mixed The trailing metadata sent by the server |
Stanley Cheung | 6668d51 | 2016-05-18 14:05:09 -0700 | [diff] [blame] | 100 | */ |
| 101 | public function getTrailingMetadata() |
| 102 | { |
| 103 | return $this->trailing_metadata; |
| 104 | } |
| 105 | |
| 106 | /** |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 107 | * @return string The URI of the endpoint |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 108 | */ |
| 109 | public function getPeer() |
| 110 | { |
| 111 | return $this->call->getPeer(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Cancels the call. |
| 116 | */ |
| 117 | public function cancel() |
| 118 | { |
| 119 | $this->call->cancel(); |
| 120 | } |
| 121 | |
| 122 | /** |
thinkerou | 9a669b6 | 2016-11-01 21:31:43 +0800 | [diff] [blame] | 123 | * Serialize a message to the protobuf binary format. |
Stanley Cheung | 881f4ff | 2016-09-22 16:15:58 -0700 | [diff] [blame] | 124 | * |
| 125 | * @param mixed $data The Protobuf message |
| 126 | * |
| 127 | * @return string The protobuf binary format |
| 128 | */ |
thinkerou | 9a669b6 | 2016-11-01 21:31:43 +0800 | [diff] [blame] | 129 | protected function serializeMessage($data) |
| 130 | { |
Stanley Cheung | 881f4ff | 2016-09-22 16:15:58 -0700 | [diff] [blame] | 131 | // Proto3 implementation |
| 132 | if (method_exists($data, 'encode')) { |
| 133 | return $data->encode(); |
| 134 | } |
| 135 | |
| 136 | // Protobuf-PHP implementation |
| 137 | return $data->serialize(); |
| 138 | } |
| 139 | |
| 140 | /** |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 141 | * Deserialize a response value to an object. |
| 142 | * |
| 143 | * @param string $value The binary value to deserialize |
| 144 | * |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 145 | * @return mixed The deserialized value |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 146 | */ |
| 147 | protected function deserializeResponse($value) |
| 148 | { |
| 149 | if ($value === null) { |
| 150 | return; |
| 151 | } |
| 152 | |
Stanley Cheung | 881f4ff | 2016-09-22 16:15:58 -0700 | [diff] [blame] | 153 | // Proto3 implementation |
| 154 | if (is_array($this->deserialize)) { |
| 155 | list($className, $deserializeFunc) = $this->deserialize; |
| 156 | $obj = new $className(); |
| 157 | $obj->$deserializeFunc($value); |
thinkerou | 9a669b6 | 2016-11-01 21:31:43 +0800 | [diff] [blame] | 158 | |
Stanley Cheung | 881f4ff | 2016-09-22 16:15:58 -0700 | [diff] [blame] | 159 | return $obj; |
| 160 | } |
| 161 | |
| 162 | // Protobuf-PHP implementation |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 163 | return call_user_func($this->deserialize, $value); |
| 164 | } |
Stanley Cheung | 6bd3180 | 2015-12-16 12:58:19 -0800 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * Set the CallCredentials for the underlying Call. |
| 168 | * |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 169 | * @param CallCredentials $call_credentials The CallCredentials object |
Stanley Cheung | 6bd3180 | 2015-12-16 12:58:19 -0800 | [diff] [blame] | 170 | */ |
| 171 | public function setCallCredentials($call_credentials) |
| 172 | { |
| 173 | $this->call->setCredentials($call_credentials); |
| 174 | } |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 175 | } |