| 1 |
<?php |
| 2 |
/* |
| 3 |
* |
| 4 |
* Copyright 2015 gRPC authors. |
| 5 |
* |
| 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 |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 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. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
namespace Grpc; |
| 21 |
|
| 22 |
/** |
| 23 |
* Class AbstractCall. |
| 24 |
* @package Grpc |
| 25 |
*/ |
| 26 |
abstract class AbstractCall |
| 27 |
{ |
| 28 |
/** |
| 29 |
* @var Call |
| 30 |
*/ |
| 31 |
protected $call; |
| 32 |
protected $deserialize; |
| 33 |
protected $metadata; |
| 34 |
protected $trailing_metadata; |
| 35 |
|
| 36 |
/** |
| 37 |
* Create a new Call wrapper object. |
| 38 |
* |
| 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) |
| 45 |
*/ |
| 46 |
public function __construct(Channel $channel, |
| 47 |
$method, |
| 48 |
$deserialize, |
| 49 |
array $options = []) |
| 50 |
{ |
| 51 |
if (array_key_exists('timeout', $options) && |
| 52 |
is_numeric($timeout = $options['timeout']) |
| 53 |
) { |
| 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; |
| 63 |
$this->trailing_metadata = null; |
| 64 |
if (array_key_exists('call_credentials_callback', $options) && |
| 65 |
is_callable($call_credentials_callback = |
| 66 |
$options['call_credentials_callback']) |
| 67 |
) { |
| 68 |
$call_credentials = CallCredentials::createFromPlugin( |
| 69 |
$call_credentials_callback |
| 70 |
); |
| 71 |
$this->call->setCredentials($call_credentials); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @return mixed The metadata sent by the server |
| 77 |
*/ |
| 78 |
public function getMetadata() |
| 79 |
{ |
| 80 |
return $this->metadata; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @return mixed The trailing metadata sent by the server |
| 85 |
*/ |
| 86 |
public function getTrailingMetadata() |
| 87 |
{ |
| 88 |
return $this->trailing_metadata; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @return string The URI of the endpoint |
| 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 |
/** |
| 108 |
* Serialize a message to the protobuf binary format. |
| 109 |
* |
| 110 |
* @param mixed $data The Protobuf message |
| 111 |
* |
| 112 |
* @return string The protobuf binary format |
| 113 |
*/ |
| 114 |
protected function _serializeMessage($data) |
| 115 |
{ |
| 116 |
// Proto3 implementation |
| 117 |
return $data->serializeToString(); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Deserialize a response value to an object. |
| 122 |
* |
| 123 |
* @param string $value The binary value to deserialize |
| 124 |
* |
| 125 |
* @return mixed The deserialized value |
| 126 |
*/ |
| 127 |
protected function _deserializeResponse($value) |
| 128 |
{ |
| 129 |
if ($value === null) { |
| 130 |
return; |
| 131 |
} |
| 132 |
list($className, $deserializeFunc) = $this->deserialize; |
| 133 |
$obj = new $className(); |
| 134 |
$obj->mergeFromString($value); |
| 135 |
return $obj; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Set the CallCredentials for the underlying Call. |
| 140 |
* |
| 141 |
* @param CallCredentials $call_credentials The CallCredentials object |
| 142 |
*/ |
| 143 |
public function setCallCredentials($call_credentials) |
| 144 |
{ |
| 145 |
$this->call->setCredentials($call_credentials); |
| 146 |
} |
| 147 |
} |