blob: f0d0d55582502aa6f06d67d41300d79791d54dc7 [file] [log] [blame]
mlumishb892a272014-12-09 16:28:23 -08001<?php
Craig Tiller2e498aa2015-02-16 12:09:31 -08002/*
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 */
mlumishb892a272014-12-09 16:28:23 -080034namespace Grpc;
mlumish156e67d2015-01-02 14:59:16 -080035require_once realpath(dirname(__FILE__) . '/../autoload.php');
mlumishb892a272014-12-09 16:28:23 -080036
37/**
38 * Represents an active call that allows sending and recieving binary data
39 */
40class 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.
murgatroid99f21eb252015-01-30 13:47:41 -080063 $this->call->invoke($this->completion_queue,
64 CLIENT_METADATA_READ,
65 FINISHED, 0);
mlumishb892a272014-12-09 16:28:23 -080066 $metadata_event = $this->completion_queue->pluck(CLIENT_METADATA_READ,
67 Timeval::inf_future());
mlumish34cd1f02015-01-02 13:32:41 -080068 $this->metadata = $metadata_event->data;
mlumishb892a272014-12-09 16:28:23 -080069 }
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());
mlumish34cd1f02015-01-02 13:32:41 -080092 return $read_event->data;
mlumishb892a272014-12-09 16:28:23 -080093 }
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) {
murgatroid9925e5f672015-02-02 11:05:01 -0800101 $this->call->start_write($data, WRITE_ACCEPTED, $this->flags);
mlumishb892a272014-12-09 16:28:23 -0800102 $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.
mlumisha7baac52014-12-16 09:23:51 -0800115 * @return object The status object, with integer $code, string $details,
116 * and array $metadata members
mlumishb892a272014-12-09 16:28:23 -0800117 */
118 public function getStatus() {
119 $status_event = $this->completion_queue->pluck(FINISHED,
120 Timeval::inf_future());
mlumish34cd1f02015-01-02 13:32:41 -0800121 return $status_event->data;
mlumishb892a272014-12-09 16:28:23 -0800122 }
Craig Tiller2e498aa2015-02-16 12:09:31 -0800123}