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; |
| 21 | |
mlumish | 156e67d | 2015-01-02 14:59:16 -0800 | [diff] [blame] | 22 | /** |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 23 | * Represents an active call that sends a single message and then gets a |
| 24 | * single response. |
mlumish | 156e67d | 2015-01-02 14:59:16 -0800 | [diff] [blame] | 25 | */ |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 26 | class UnaryCall extends AbstractCall |
| 27 | { |
| 28 | /** |
| 29 | * Start the call. |
| 30 | * |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 31 | * @param mixed $data The data to send |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 32 | * @param array $metadata Metadata to send with the call, if applicable |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 33 | * (optional) |
| 34 | * @param array $options An array of options, possible keys: |
| 35 | * 'flags' => a number (optional) |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 36 | */ |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 37 | public function start($data, array $metadata = [], array $options = []) |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 38 | { |
thinkerou | 8772a36 | 2017-01-20 18:20:47 +0800 | [diff] [blame] | 39 | $message_array = ['message' => $this->_serializeMessage($data)]; |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 40 | if (isset($options['flags'])) { |
| 41 | $message_array['flags'] = $options['flags']; |
| 42 | } |
| 43 | $event = $this->call->startBatch([ |
| 44 | OP_SEND_INITIAL_METADATA => $metadata, |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 45 | OP_SEND_MESSAGE => $message_array, |
| 46 | OP_SEND_CLOSE_FROM_CLIENT => true, |
| 47 | ]); |
| 48 | $this->metadata = $event->metadata; |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 49 | } |
mlumish | 156e67d | 2015-01-02 14:59:16 -0800 | [diff] [blame] | 50 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Wait for the server to respond with data and a status. |
| 53 | * |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 54 | * @return array [response data, status] |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 55 | */ |
| 56 | public function wait() |
| 57 | { |
Michael Bausor | 81fd629 | 2017-08-15 10:13:06 -0700 | [diff] [blame^] | 58 | $batch = [ |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 59 | OP_RECV_MESSAGE => true, |
| 60 | OP_RECV_STATUS_ON_CLIENT => true, |
Michael Bausor | 81fd629 | 2017-08-15 10:13:06 -0700 | [diff] [blame^] | 61 | ]; |
| 62 | if ($this->metadata === null) { |
| 63 | $batch[OP_RECV_INITIAL_METADATA] = true; |
| 64 | } |
| 65 | $event = $this->call->startBatch($batch); |
| 66 | if ($this->metadata === null) { |
| 67 | $this->metadata = $event->metadata; |
| 68 | } |
Stanley Cheung | 6668d51 | 2016-05-18 14:05:09 -0700 | [diff] [blame] | 69 | $status = $event->status; |
| 70 | $this->trailing_metadata = $status->metadata; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 71 | |
thinkerou | 8772a36 | 2017-01-20 18:20:47 +0800 | [diff] [blame] | 72 | return [$this->_deserializeResponse($event->message), $status]; |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 73 | } |
Michael Bausor | 81fd629 | 2017-08-15 10:13:06 -0700 | [diff] [blame^] | 74 | |
| 75 | /** |
| 76 | * @return mixed The metadata sent by the server |
| 77 | */ |
| 78 | public function getMetadata() |
| 79 | { |
| 80 | if ($this->metadata === null) { |
| 81 | $event = $this->call->startBatch([OP_RECV_INITIAL_METADATA => true]); |
| 82 | $this->metadata = $event->metadata; |
| 83 | } |
| 84 | return $this->metadata; |
| 85 | } |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 86 | } |