mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 1 | <?php |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 2 | /* |
| 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 | */ |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 34 | namespace Grpc; |
mlumish | 156e67d | 2015-01-02 14:59:16 -0800 | [diff] [blame] | 35 | require_once realpath(dirname(__FILE__) . '/../autoload.php'); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Represents an active call that allows sending and recieving binary data |
| 39 | */ |
| 40 | class ActiveCall { |
| 41 | private $completion_queue; |
| 42 | private $call; |
| 43 | private $flags; |
| 44 | private $metadata; |
| 45 | |
| 46 | /** |
| 47 | * Create a new active call. |
| 48 | * @param Channel $channel The channel to communicate on |
| 49 | * @param string $method The method to call on the remote server |
| 50 | * @param array $metadata Metadata to send with the call, if applicable |
| 51 | * @param long $flags Write flags to use with this call |
| 52 | */ |
| 53 | public function __construct(Channel $channel, |
| 54 | $method, |
| 55 | $metadata = array(), |
| 56 | $flags = 0) { |
| 57 | $this->completion_queue = new CompletionQueue(); |
| 58 | $this->call = new Call($channel, $method, Timeval::inf_future()); |
| 59 | $this->call->add_metadata($metadata, 0); |
| 60 | $this->flags = $flags; |
| 61 | |
| 62 | // Invoke the call. |
murgatroid99 | f21eb25 | 2015-01-30 13:47:41 -0800 | [diff] [blame] | 63 | $this->call->invoke($this->completion_queue, |
| 64 | CLIENT_METADATA_READ, |
| 65 | FINISHED, 0); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 66 | $metadata_event = $this->completion_queue->pluck(CLIENT_METADATA_READ, |
| 67 | Timeval::inf_future()); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 68 | $this->metadata = $metadata_event->data; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @return The metadata sent by the server. |
| 73 | */ |
| 74 | public function getMetadata() { |
| 75 | return $this->metadata; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Cancels the call |
| 80 | */ |
| 81 | public function cancel() { |
| 82 | $this->call->cancel(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Read a single message from the server. |
| 87 | * @return The next message from the server, or null if there is none. |
| 88 | */ |
| 89 | public function read() { |
| 90 | $this->call->start_read(READ); |
| 91 | $read_event = $this->completion_queue->pluck(READ, Timeval::inf_future()); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 92 | return $read_event->data; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Write a single message to the server. This cannot be called after |
| 97 | * writesDone is called. |
| 98 | * @param ByteBuffer $data The data to write |
| 99 | */ |
| 100 | public function write($data) { |
murgatroid99 | 25e5f67 | 2015-02-02 11:05:01 -0800 | [diff] [blame] | 101 | $this->call->start_write($data, WRITE_ACCEPTED, $this->flags); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 102 | $this->completion_queue->pluck(WRITE_ACCEPTED, Timeval::inf_future()); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Indicate that no more writes will be sent. |
| 107 | */ |
| 108 | public function writesDone() { |
| 109 | $this->call->writes_done(FINISH_ACCEPTED); |
| 110 | $this->completion_queue->pluck(FINISH_ACCEPTED, Timeval::inf_future()); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Wait for the server to send the status, and return it. |
mlumish | a7baac5 | 2014-12-16 09:23:51 -0800 | [diff] [blame] | 115 | * @return object The status object, with integer $code, string $details, |
| 116 | * and array $metadata members |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 117 | */ |
| 118 | public function getStatus() { |
| 119 | $status_event = $this->completion_queue->pluck(FINISHED, |
| 120 | Timeval::inf_future()); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 121 | return $status_event->data; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 122 | } |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 123 | } |