mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 1 | <?php |
| 2 | namespace Grpc; |
mlumish | 156e67d | 2015-01-02 14:59:16 -0800 | [diff] [blame] | 3 | require_once realpath(dirname(__FILE__) . '/../autoload.php'); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 4 | |
| 5 | /** |
| 6 | * Represents an active call that allows sending and recieving binary data |
| 7 | */ |
| 8 | class ActiveCall { |
| 9 | private $completion_queue; |
| 10 | private $call; |
| 11 | private $flags; |
| 12 | private $metadata; |
| 13 | |
| 14 | /** |
| 15 | * Create a new active call. |
| 16 | * @param Channel $channel The channel to communicate on |
| 17 | * @param string $method The method to call on the remote server |
| 18 | * @param array $metadata Metadata to send with the call, if applicable |
| 19 | * @param long $flags Write flags to use with this call |
| 20 | */ |
| 21 | public function __construct(Channel $channel, |
| 22 | $method, |
| 23 | $metadata = array(), |
| 24 | $flags = 0) { |
| 25 | $this->completion_queue = new CompletionQueue(); |
| 26 | $this->call = new Call($channel, $method, Timeval::inf_future()); |
| 27 | $this->call->add_metadata($metadata, 0); |
| 28 | $this->flags = $flags; |
| 29 | |
| 30 | // Invoke the call. |
murgatroid99 | f21eb25 | 2015-01-30 13:47:41 -0800 | [diff] [blame] | 31 | $this->call->invoke($this->completion_queue, |
| 32 | CLIENT_METADATA_READ, |
| 33 | FINISHED, 0); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 34 | $metadata_event = $this->completion_queue->pluck(CLIENT_METADATA_READ, |
| 35 | Timeval::inf_future()); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 36 | $this->metadata = $metadata_event->data; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @return The metadata sent by the server. |
| 41 | */ |
| 42 | public function getMetadata() { |
| 43 | return $this->metadata; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Cancels the call |
| 48 | */ |
| 49 | public function cancel() { |
| 50 | $this->call->cancel(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Read a single message from the server. |
| 55 | * @return The next message from the server, or null if there is none. |
| 56 | */ |
| 57 | public function read() { |
| 58 | $this->call->start_read(READ); |
| 59 | $read_event = $this->completion_queue->pluck(READ, Timeval::inf_future()); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 60 | return $read_event->data; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Write a single message to the server. This cannot be called after |
| 65 | * writesDone is called. |
| 66 | * @param ByteBuffer $data The data to write |
| 67 | */ |
| 68 | public function write($data) { |
| 69 | if($this->call->start_write($data, |
| 70 | WRITE_ACCEPTED, |
| 71 | $this->flags) != OP_OK) { |
| 72 | // TODO(mlumish): more useful error |
| 73 | throw new \Exception("Cannot call write after writesDone"); |
| 74 | } |
| 75 | $this->completion_queue->pluck(WRITE_ACCEPTED, Timeval::inf_future()); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Indicate that no more writes will be sent. |
| 80 | */ |
| 81 | public function writesDone() { |
| 82 | $this->call->writes_done(FINISH_ACCEPTED); |
| 83 | $this->completion_queue->pluck(FINISH_ACCEPTED, Timeval::inf_future()); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Wait for the server to send the status, and return it. |
mlumish | a7baac5 | 2014-12-16 09:23:51 -0800 | [diff] [blame] | 88 | * @return object The status object, with integer $code, string $details, |
| 89 | * and array $metadata members |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 90 | */ |
| 91 | public function getStatus() { |
| 92 | $status_event = $this->completion_queue->pluck(FINISHED, |
| 93 | Timeval::inf_future()); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 94 | return $status_event->data; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 95 | } |
| 96 | } |