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 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 4 | * Copyright 2015 gRPC authors. |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 5 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 9 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 11 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 17 | * |
| 18 | */ |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 19 | |
mlumish | 156e67d | 2015-01-02 14:59:16 -0800 | [diff] [blame] | 20 | namespace Grpc; |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 21 | |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 22 | /** |
thinkerou | 9a669b6 | 2016-11-01 21:31:43 +0800 | [diff] [blame] | 23 | * Class AbstractCall. |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 24 | * @package Grpc |
| 25 | */ |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 26 | abstract class AbstractCall |
| 27 | { |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 28 | /** |
| 29 | * @var Call |
| 30 | */ |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 31 | protected $call; |
| 32 | protected $deserialize; |
| 33 | protected $metadata; |
Stanley Cheung | 6668d51 | 2016-05-18 14:05:09 -0700 | [diff] [blame] | 34 | protected $trailing_metadata; |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 35 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Create a new Call wrapper object. |
| 38 | * |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 39 | * @param Channel $channel The channel to communicate on |
| 40 | * @param string $method The method to call on the |
| 41 | * remote server |
| 42 | * @param callback $deserialize A callback function to deserialize |
| 43 | * the response |
| 44 | * @param array $options Call options (optional) |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 45 | */ |
thinkerou | 16713db | 2017-01-20 14:43:19 +0800 | [diff] [blame] | 46 | public function __construct(Channel $channel, |
| 47 | $method, |
| 48 | $deserialize, |
| 49 | array $options = []) |
| 50 | { |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 51 | if (array_key_exists('timeout', $options) && |
| 52 | is_numeric($timeout = $options['timeout']) |
| 53 | ) { |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 54 | $now = Timeval::now(); |
| 55 | $delta = new Timeval($timeout); |
| 56 | $deadline = $now->add($delta); |
| 57 | } else { |
| 58 | $deadline = Timeval::infFuture(); |
| 59 | } |
| 60 | $this->call = new Call($channel, $method, $deadline); |
| 61 | $this->deserialize = $deserialize; |
| 62 | $this->metadata = null; |
Stanley Cheung | 6668d51 | 2016-05-18 14:05:09 -0700 | [diff] [blame] | 63 | $this->trailing_metadata = null; |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 64 | if (array_key_exists('call_credentials_callback', $options) && |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 65 | is_callable($call_credentials_callback = |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 66 | $options['call_credentials_callback']) |
| 67 | ) { |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 68 | $call_credentials = CallCredentials::createFromPlugin( |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 69 | $call_credentials_callback |
| 70 | ); |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 71 | $this->call->setCredentials($call_credentials); |
| 72 | } |
Stanley Cheung | cc019af | 2015-06-15 11:45:00 -0700 | [diff] [blame] | 73 | } |
mlumish | 156e67d | 2015-01-02 14:59:16 -0800 | [diff] [blame] | 74 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 75 | /** |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 76 | * @return mixed The metadata sent by the server |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 77 | */ |
| 78 | public function getMetadata() |
| 79 | { |
| 80 | return $this->metadata; |
murgatroid99 | 9140a06 | 2015-03-26 11:27:58 -0700 | [diff] [blame] | 81 | } |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 84 | * @return mixed The trailing metadata sent by the server |
Stanley Cheung | 6668d51 | 2016-05-18 14:05:09 -0700 | [diff] [blame] | 85 | */ |
| 86 | public function getTrailingMetadata() |
| 87 | { |
| 88 | return $this->trailing_metadata; |
| 89 | } |
| 90 | |
| 91 | /** |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 92 | * @return string The URI of the endpoint |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 93 | */ |
| 94 | public function getPeer() |
| 95 | { |
| 96 | return $this->call->getPeer(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Cancels the call. |
| 101 | */ |
| 102 | public function cancel() |
| 103 | { |
| 104 | $this->call->cancel(); |
| 105 | } |
| 106 | |
| 107 | /** |
thinkerou | 9a669b6 | 2016-11-01 21:31:43 +0800 | [diff] [blame] | 108 | * Serialize a message to the protobuf binary format. |
Stanley Cheung | 881f4ff | 2016-09-22 16:15:58 -0700 | [diff] [blame] | 109 | * |
| 110 | * @param mixed $data The Protobuf message |
| 111 | * |
| 112 | * @return string The protobuf binary format |
| 113 | */ |
thinkerou | 8772a36 | 2017-01-20 18:20:47 +0800 | [diff] [blame] | 114 | protected function _serializeMessage($data) |
thinkerou | 9a669b6 | 2016-11-01 21:31:43 +0800 | [diff] [blame] | 115 | { |
Stanley Cheung | 881f4ff | 2016-09-22 16:15:58 -0700 | [diff] [blame] | 116 | // Proto3 implementation |
| 117 | if (method_exists($data, 'encode')) { |
| 118 | return $data->encode(); |
thinkerou | 34d21ce | 2017-03-26 01:07:27 +0800 | [diff] [blame] | 119 | } elseif (method_exists($data, 'serializeToString')) { |
Stanley Cheung | 55bff48 | 2017-03-13 13:09:43 -0700 | [diff] [blame] | 120 | return $data->serializeToString(); |
Stanley Cheung | 881f4ff | 2016-09-22 16:15:58 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Protobuf-PHP implementation |
| 124 | return $data->serialize(); |
| 125 | } |
| 126 | |
| 127 | /** |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 128 | * Deserialize a response value to an object. |
| 129 | * |
| 130 | * @param string $value The binary value to deserialize |
| 131 | * |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 132 | * @return mixed The deserialized value |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 133 | */ |
thinkerou | 8772a36 | 2017-01-20 18:20:47 +0800 | [diff] [blame] | 134 | protected function _deserializeResponse($value) |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 135 | { |
| 136 | if ($value === null) { |
| 137 | return; |
| 138 | } |
| 139 | |
Stanley Cheung | 881f4ff | 2016-09-22 16:15:58 -0700 | [diff] [blame] | 140 | // Proto3 implementation |
| 141 | if (is_array($this->deserialize)) { |
| 142 | list($className, $deserializeFunc) = $this->deserialize; |
| 143 | $obj = new $className(); |
Stanley Cheung | 55bff48 | 2017-03-13 13:09:43 -0700 | [diff] [blame] | 144 | if (method_exists($obj, $deserializeFunc)) { |
| 145 | $obj->$deserializeFunc($value); |
| 146 | } else { |
| 147 | $obj->mergeFromString($value); |
| 148 | } |
thinkerou | 9a669b6 | 2016-11-01 21:31:43 +0800 | [diff] [blame] | 149 | |
Stanley Cheung | 881f4ff | 2016-09-22 16:15:58 -0700 | [diff] [blame] | 150 | return $obj; |
| 151 | } |
| 152 | |
| 153 | // Protobuf-PHP implementation |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 154 | return call_user_func($this->deserialize, $value); |
| 155 | } |
Stanley Cheung | 6bd3180 | 2015-12-16 12:58:19 -0800 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * Set the CallCredentials for the underlying Call. |
| 159 | * |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 160 | * @param CallCredentials $call_credentials The CallCredentials object |
Stanley Cheung | 6bd3180 | 2015-12-16 12:58:19 -0800 | [diff] [blame] | 161 | */ |
| 162 | public function setCallCredentials($call_credentials) |
| 163 | { |
| 164 | $this->call->setCredentials($call_credentials); |
| 165 | } |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 166 | } |