• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
20namespace Grpc;
21
22/**
23 * Class AbstractCall.
24 * @package Grpc
25 */
26abstract 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        if (method_exists($data, 'encode')) {
118            return $data->encode();
119        } elseif (method_exists($data, 'serializeToString')) {
120            return $data->serializeToString();
121        }
122
123        // Protobuf-PHP implementation
124        return $data->serialize();
125    }
126
127    /**
128     * Deserialize a response value to an object.
129     *
130     * @param string $value The binary value to deserialize
131     *
132     * @return mixed The deserialized value
133     */
134    protected function _deserializeResponse($value)
135    {
136        if ($value === null) {
137            return;
138        }
139
140        // Proto3 implementation
141        if (is_array($this->deserialize)) {
142            list($className, $deserializeFunc) = $this->deserialize;
143            $obj = new $className();
144            if (method_exists($obj, $deserializeFunc)) {
145                $obj->$deserializeFunc($value);
146            } else {
147                $obj->mergeFromString($value);
148            }
149
150            return $obj;
151        }
152
153        // Protobuf-PHP implementation
154        return call_user_func($this->deserialize, $value);
155    }
156
157    /**
158     * Set the CallCredentials for the underlying Call.
159     *
160     * @param CallCredentials $call_credentials The CallCredentials object
161     */
162    public function setCallCredentials($call_credentials)
163    {
164        $this->call->setCredentials($call_credentials);
165    }
166}
167