blob: d1c75cb2029aa94b2670883eb244bdca96d7e3f5 [file] [log] [blame]
mlumish156e67d2015-01-02 14:59:16 -08001<?php
Craig Tiller2e498aa2015-02-16 12:09:31 -08002/*
3 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004 * Copyright 2015 gRPC authors.
Craig Tiller2e498aa2015-02-16 12:09:31 -08005 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02006 * 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 Tiller2e498aa2015-02-16 12:09:31 -08009 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller2e498aa2015-02-16 12:09:31 -080011 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020012 * 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 Tiller2e498aa2015-02-16 12:09:31 -080017 *
18 */
Stanley Cheungd5b20562015-10-27 13:27:05 -070019
mlumish156e67d2015-01-02 14:59:16 -080020namespace Grpc;
murgatroid999140a062015-03-26 11:27:58 -070021
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070022/**
thinkerou9a669b62016-11-01 21:31:43 +080023 * Class AbstractCall.
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070024 * @package Grpc
25 */
Stanley Cheungd5b20562015-10-27 13:27:05 -070026abstract class AbstractCall
27{
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070028 /**
29 * @var Call
30 */
Stanley Cheungd5b20562015-10-27 13:27:05 -070031 protected $call;
32 protected $deserialize;
33 protected $metadata;
Stanley Cheung6668d512016-05-18 14:05:09 -070034 protected $trailing_metadata;
murgatroid999140a062015-03-26 11:27:58 -070035
Stanley Cheungd5b20562015-10-27 13:27:05 -070036 /**
37 * Create a new Call wrapper object.
38 *
Stanley Cheung35805802015-12-10 11:42:55 -080039 * @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 Cheungd5b20562015-10-27 13:27:05 -070045 */
thinkerou16713db2017-01-20 14:43:19 +080046 public function __construct(Channel $channel,
47 $method,
48 $deserialize,
49 array $options = [])
50 {
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070051 if (array_key_exists('timeout', $options) &&
52 is_numeric($timeout = $options['timeout'])
53 ) {
Stanley Cheungd5b20562015-10-27 13:27:05 -070054 $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 Cheung6668d512016-05-18 14:05:09 -070063 $this->trailing_metadata = null;
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070064 if (array_key_exists('call_credentials_callback', $options) &&
Stanley Cheung35805802015-12-10 11:42:55 -080065 is_callable($call_credentials_callback =
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070066 $options['call_credentials_callback'])
67 ) {
Stanley Cheung35805802015-12-10 11:42:55 -080068 $call_credentials = CallCredentials::createFromPlugin(
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070069 $call_credentials_callback
70 );
Stanley Cheung35805802015-12-10 11:42:55 -080071 $this->call->setCredentials($call_credentials);
72 }
Stanley Cheungcc019af2015-06-15 11:45:00 -070073 }
mlumish156e67d2015-01-02 14:59:16 -080074
Stanley Cheungd5b20562015-10-27 13:27:05 -070075 /**
thinkerou0c3e8db2016-12-15 00:27:03 +080076 * @return mixed The metadata sent by the server
Stanley Cheungd5b20562015-10-27 13:27:05 -070077 */
78 public function getMetadata()
79 {
80 return $this->metadata;
murgatroid999140a062015-03-26 11:27:58 -070081 }
Stanley Cheungd5b20562015-10-27 13:27:05 -070082
83 /**
thinkerou0c3e8db2016-12-15 00:27:03 +080084 * @return mixed The trailing metadata sent by the server
Stanley Cheung6668d512016-05-18 14:05:09 -070085 */
86 public function getTrailingMetadata()
87 {
88 return $this->trailing_metadata;
89 }
90
91 /**
thinkerou0c3e8db2016-12-15 00:27:03 +080092 * @return string The URI of the endpoint
Stanley Cheungd5b20562015-10-27 13:27:05 -070093 */
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 /**
thinkerou9a669b62016-11-01 21:31:43 +0800108 * Serialize a message to the protobuf binary format.
Stanley Cheung881f4ff2016-09-22 16:15:58 -0700109 *
110 * @param mixed $data The Protobuf message
111 *
112 * @return string The protobuf binary format
113 */
thinkerou8772a362017-01-20 18:20:47 +0800114 protected function _serializeMessage($data)
thinkerou9a669b62016-11-01 21:31:43 +0800115 {
Stanley Cheung881f4ff2016-09-22 16:15:58 -0700116 // Proto3 implementation
117 if (method_exists($data, 'encode')) {
118 return $data->encode();
thinkerou34d21ce2017-03-26 01:07:27 +0800119 } elseif (method_exists($data, 'serializeToString')) {
Stanley Cheung55bff482017-03-13 13:09:43 -0700120 return $data->serializeToString();
Stanley Cheung881f4ff2016-09-22 16:15:58 -0700121 }
122
123 // Protobuf-PHP implementation
124 return $data->serialize();
125 }
126
127 /**
Stanley Cheungd5b20562015-10-27 13:27:05 -0700128 * Deserialize a response value to an object.
129 *
130 * @param string $value The binary value to deserialize
131 *
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +0700132 * @return mixed The deserialized value
Stanley Cheungd5b20562015-10-27 13:27:05 -0700133 */
thinkerou8772a362017-01-20 18:20:47 +0800134 protected function _deserializeResponse($value)
Stanley Cheungd5b20562015-10-27 13:27:05 -0700135 {
136 if ($value === null) {
137 return;
138 }
139
Stanley Cheung881f4ff2016-09-22 16:15:58 -0700140 // Proto3 implementation
141 if (is_array($this->deserialize)) {
142 list($className, $deserializeFunc) = $this->deserialize;
143 $obj = new $className();
Stanley Cheung55bff482017-03-13 13:09:43 -0700144 if (method_exists($obj, $deserializeFunc)) {
145 $obj->$deserializeFunc($value);
146 } else {
147 $obj->mergeFromString($value);
148 }
thinkerou9a669b62016-11-01 21:31:43 +0800149
Stanley Cheung881f4ff2016-09-22 16:15:58 -0700150 return $obj;
151 }
152
153 // Protobuf-PHP implementation
Stanley Cheungd5b20562015-10-27 13:27:05 -0700154 return call_user_func($this->deserialize, $value);
155 }
Stanley Cheung6bd31802015-12-16 12:58:19 -0800156
157 /**
158 * Set the CallCredentials for the underlying Call.
159 *
thinkerou0c3e8db2016-12-15 00:27:03 +0800160 * @param CallCredentials $call_credentials The CallCredentials object
Stanley Cheung6bd31802015-12-16 12:58:19 -0800161 */
162 public function setCallCredentials($call_credentials)
163 {
164 $this->call->setCredentials($call_credentials);
165 }
Stanley Cheung3ab8e792015-08-24 16:58:42 -0700166}