blob: 9f0b02b8bbcfb75044028aedeef3d709e5282a10 [file] [log] [blame]
mlumish156e67d2015-01-02 14:59:16 -08001<?php
Craig Tiller2e498aa2015-02-16 12:09:31 -08002/*
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 Cheungd5b20562015-10-27 13:27:05 -070034
mlumish156e67d2015-01-02 14:59:16 -080035namespace Grpc;
murgatroid999140a062015-03-26 11:27:58 -070036
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070037/**
thinkerou9a669b62016-11-01 21:31:43 +080038 * Class AbstractCall.
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070039 * @package Grpc
40 */
Stanley Cheungd5b20562015-10-27 13:27:05 -070041abstract class AbstractCall
42{
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070043 /**
44 * @var Call
45 */
Stanley Cheungd5b20562015-10-27 13:27:05 -070046 protected $call;
47 protected $deserialize;
48 protected $metadata;
Stanley Cheung6668d512016-05-18 14:05:09 -070049 protected $trailing_metadata;
murgatroid999140a062015-03-26 11:27:58 -070050
Stanley Cheungd5b20562015-10-27 13:27:05 -070051 /**
52 * Create a new Call wrapper object.
53 *
Stanley Cheung35805802015-12-10 11:42:55 -080054 * @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 Cheungd5b20562015-10-27 13:27:05 -070060 */
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070061 public function __construct(
62 Channel $channel,
63 $method,
64 $deserialize,
thinkerou0c3e8db2016-12-15 00:27:03 +080065 array $options = []
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070066 ) {
67 if (array_key_exists('timeout', $options) &&
68 is_numeric($timeout = $options['timeout'])
69 ) {
Stanley Cheungd5b20562015-10-27 13:27:05 -070070 $now = Timeval::now();
71 $delta = new Timeval($timeout);
72 $deadline = $now->add($delta);
73 } else {
74 $deadline = Timeval::infFuture();
75 }
76 $this->call = new Call($channel, $method, $deadline);
77 $this->deserialize = $deserialize;
78 $this->metadata = null;
Stanley Cheung6668d512016-05-18 14:05:09 -070079 $this->trailing_metadata = null;
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070080 if (array_key_exists('call_credentials_callback', $options) &&
Stanley Cheung35805802015-12-10 11:42:55 -080081 is_callable($call_credentials_callback =
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070082 $options['call_credentials_callback'])
83 ) {
Stanley Cheung35805802015-12-10 11:42:55 -080084 $call_credentials = CallCredentials::createFromPlugin(
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070085 $call_credentials_callback
86 );
Stanley Cheung35805802015-12-10 11:42:55 -080087 $this->call->setCredentials($call_credentials);
88 }
Stanley Cheungcc019af2015-06-15 11:45:00 -070089 }
mlumish156e67d2015-01-02 14:59:16 -080090
Stanley Cheungd5b20562015-10-27 13:27:05 -070091 /**
thinkerou0c3e8db2016-12-15 00:27:03 +080092 * @return mixed The metadata sent by the server
Stanley Cheungd5b20562015-10-27 13:27:05 -070093 */
94 public function getMetadata()
95 {
96 return $this->metadata;
murgatroid999140a062015-03-26 11:27:58 -070097 }
Stanley Cheungd5b20562015-10-27 13:27:05 -070098
99 /**
thinkerou0c3e8db2016-12-15 00:27:03 +0800100 * @return mixed The trailing metadata sent by the server
Stanley Cheung6668d512016-05-18 14:05:09 -0700101 */
102 public function getTrailingMetadata()
103 {
104 return $this->trailing_metadata;
105 }
106
107 /**
thinkerou0c3e8db2016-12-15 00:27:03 +0800108 * @return string The URI of the endpoint
Stanley Cheungd5b20562015-10-27 13:27:05 -0700109 */
110 public function getPeer()
111 {
112 return $this->call->getPeer();
113 }
114
115 /**
116 * Cancels the call.
117 */
118 public function cancel()
119 {
120 $this->call->cancel();
121 }
122
123 /**
thinkerou9a669b62016-11-01 21:31:43 +0800124 * Serialize a message to the protobuf binary format.
Stanley Cheung881f4ff2016-09-22 16:15:58 -0700125 *
126 * @param mixed $data The Protobuf message
127 *
128 * @return string The protobuf binary format
129 */
thinkerou9a669b62016-11-01 21:31:43 +0800130 protected function serializeMessage($data)
131 {
Stanley Cheung881f4ff2016-09-22 16:15:58 -0700132 // Proto3 implementation
133 if (method_exists($data, 'encode')) {
134 return $data->encode();
135 }
136
137 // Protobuf-PHP implementation
138 return $data->serialize();
139 }
140
141 /**
Stanley Cheungd5b20562015-10-27 13:27:05 -0700142 * Deserialize a response value to an object.
143 *
144 * @param string $value The binary value to deserialize
145 *
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +0700146 * @return mixed The deserialized value
Stanley Cheungd5b20562015-10-27 13:27:05 -0700147 */
148 protected function deserializeResponse($value)
149 {
150 if ($value === null) {
151 return;
152 }
153
Stanley Cheung881f4ff2016-09-22 16:15:58 -0700154 // Proto3 implementation
155 if (is_array($this->deserialize)) {
156 list($className, $deserializeFunc) = $this->deserialize;
157 $obj = new $className();
158 $obj->$deserializeFunc($value);
thinkerou9a669b62016-11-01 21:31:43 +0800159
Stanley Cheung881f4ff2016-09-22 16:15:58 -0700160 return $obj;
161 }
162
163 // Protobuf-PHP implementation
Stanley Cheungd5b20562015-10-27 13:27:05 -0700164 return call_user_func($this->deserialize, $value);
165 }
Stanley Cheung6bd31802015-12-16 12:58:19 -0800166
167 /**
168 * Set the CallCredentials for the underlying Call.
169 *
thinkerou0c3e8db2016-12-15 00:27:03 +0800170 * @param CallCredentials $call_credentials The CallCredentials object
Stanley Cheung6bd31802015-12-16 12:58:19 -0800171 */
172 public function setCallCredentials($call_credentials)
173 {
174 $this->call->setCredentials($call_credentials);
175 }
Stanley Cheung3ab8e792015-08-24 16:58:42 -0700176}