blob: e0ea43ab0854018c246c8ba009f85ce2bc84b3a6 [file] [log] [blame]
mlumishb892a272014-12-09 16:28:23 -08001<?php
2namespace Grpc;
mlumish156e67d2015-01-02 14:59:16 -08003require_once realpath(dirname(__FILE__) . '/../autoload.php');
mlumishb892a272014-12-09 16:28:23 -08004
5/**
6 * Represents an active call that allows sending and recieving binary data
7 */
8class 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.
murgatroid99f21eb252015-01-30 13:47:41 -080031 $this->call->invoke($this->completion_queue,
32 CLIENT_METADATA_READ,
33 FINISHED, 0);
mlumishb892a272014-12-09 16:28:23 -080034 $metadata_event = $this->completion_queue->pluck(CLIENT_METADATA_READ,
35 Timeval::inf_future());
mlumish34cd1f02015-01-02 13:32:41 -080036 $this->metadata = $metadata_event->data;
mlumishb892a272014-12-09 16:28:23 -080037 }
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());
mlumish34cd1f02015-01-02 13:32:41 -080060 return $read_event->data;
mlumishb892a272014-12-09 16:28:23 -080061 }
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.
mlumisha7baac52014-12-16 09:23:51 -080088 * @return object The status object, with integer $code, string $details,
89 * and array $metadata members
mlumishb892a272014-12-09 16:28:23 -080090 */
91 public function getStatus() {
92 $status_event = $this->completion_queue->pluck(FINISHED,
93 Timeval::inf_future());
mlumish34cd1f02015-01-02 13:32:41 -080094 return $status_event->data;
mlumishb892a272014-12-09 16:28:23 -080095 }
96}