blob: aa66dbb8488d2454f58c3ed59d923e82b780ceda [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.
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());
mlumish34cd1f02015-01-02 13:32:41 -080039 $this->metadata = $metadata_event->data;
mlumishb892a272014-12-09 16:28:23 -080040 }
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());
mlumish34cd1f02015-01-02 13:32:41 -080063 return $read_event->data;
mlumishb892a272014-12-09 16:28:23 -080064 }
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.
mlumisha7baac52014-12-16 09:23:51 -080091 * @return object The status object, with integer $code, string $details,
92 * and array $metadata members
mlumishb892a272014-12-09 16:28:23 -080093 */
94 public function getStatus() {
95 $status_event = $this->completion_queue->pluck(FINISHED,
96 Timeval::inf_future());
mlumish34cd1f02015-01-02 13:32:41 -080097 return $status_event->data;
mlumishb892a272014-12-09 16:28:23 -080098 }
99}