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. |
| 31 | $this->call->start_invoke($this->completion_queue, |
| 32 | INVOKE_ACCEPTED, |
| 33 | CLIENT_METADATA_READ, |
| 34 | FINISHED, 0); |
| 35 | $this->completion_queue->pluck(INVOKE_ACCEPTED, |
| 36 | Timeval::inf_future()); |
| 37 | $metadata_event = $this->completion_queue->pluck(CLIENT_METADATA_READ, |
| 38 | Timeval::inf_future()); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 39 | $this->metadata = $metadata_event->data; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @return The metadata sent by the server. |
| 44 | */ |
| 45 | public function getMetadata() { |
| 46 | return $this->metadata; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Cancels the call |
| 51 | */ |
| 52 | public function cancel() { |
| 53 | $this->call->cancel(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Read a single message from the server. |
| 58 | * @return The next message from the server, or null if there is none. |
| 59 | */ |
| 60 | public function read() { |
| 61 | $this->call->start_read(READ); |
| 62 | $read_event = $this->completion_queue->pluck(READ, Timeval::inf_future()); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 63 | return $read_event->data; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Write a single message to the server. This cannot be called after |
| 68 | * writesDone is called. |
| 69 | * @param ByteBuffer $data The data to write |
| 70 | */ |
| 71 | public function write($data) { |
| 72 | if($this->call->start_write($data, |
| 73 | WRITE_ACCEPTED, |
| 74 | $this->flags) != OP_OK) { |
| 75 | // TODO(mlumish): more useful error |
| 76 | throw new \Exception("Cannot call write after writesDone"); |
| 77 | } |
| 78 | $this->completion_queue->pluck(WRITE_ACCEPTED, Timeval::inf_future()); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Indicate that no more writes will be sent. |
| 83 | */ |
| 84 | public function writesDone() { |
| 85 | $this->call->writes_done(FINISH_ACCEPTED); |
| 86 | $this->completion_queue->pluck(FINISH_ACCEPTED, Timeval::inf_future()); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Wait for the server to send the status, and return it. |
mlumish | a7baac5 | 2014-12-16 09:23:51 -0800 | [diff] [blame] | 91 | * @return object The status object, with integer $code, string $details, |
| 92 | * and array $metadata members |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 93 | */ |
| 94 | public function getStatus() { |
| 95 | $status_event = $this->completion_queue->pluck(FINISHED, |
| 96 | Timeval::inf_future()); |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 97 | return $status_event->data; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 98 | } |
| 99 | } |