diff options
Diffstat (limited to 'src/php/lib')
-rw-r--r-- | src/php/lib/Grpc/AbstractCall.php | 79 | ||||
-rwxr-xr-x | src/php/lib/Grpc/AbstractSurfaceActiveCall.php | 98 | ||||
-rwxr-xr-x | src/php/lib/Grpc/ActiveCall.php | 123 | ||||
-rwxr-xr-x | src/php/lib/Grpc/BaseStub.php | 31 | ||||
-rw-r--r--[-rwxr-xr-x] | src/php/lib/Grpc/BidiStreamingCall.php (renamed from src/php/lib/Grpc/BidiStreamingSurfaceActiveCall.php) | 38 | ||||
-rw-r--r--[-rwxr-xr-x] | src/php/lib/Grpc/ClientStreamingCall.php (renamed from src/php/lib/Grpc/ClientStreamingSurfaceActiveCall.php) | 31 | ||||
-rw-r--r--[-rwxr-xr-x] | src/php/lib/Grpc/ServerStreamingCall.php (renamed from src/php/lib/Grpc/ServerStreamingSurfaceActiveCall.php) | 41 | ||||
-rw-r--r--[-rwxr-xr-x] | src/php/lib/Grpc/UnaryCall.php (renamed from src/php/lib/Grpc/SimpleSurfaceActiveCall.php) | 32 |
8 files changed, 168 insertions, 305 deletions
diff --git a/src/php/lib/Grpc/AbstractCall.php b/src/php/lib/Grpc/AbstractCall.php new file mode 100644 index 0000000000..b813d16470 --- /dev/null +++ b/src/php/lib/Grpc/AbstractCall.php @@ -0,0 +1,79 @@ +<?php +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +namespace Grpc; + +require_once realpath(dirname(__FILE__) . '/../autoload.php'); + +abstract class AbstractCall { + + protected $call; + protected $deserialize; + protected $metadata; + + /** + * Create a new Call wrapper object. + * @param Channel $channel The channel to communicate on + * @param string $method The method to call on the remote server + */ + public function __construct(Channel $channel, $method, $deserialize) { + $this->call = new Call($channel, $method, Timeval::inf_future()); + $this->deserialize = $deserialize; + } + + /** + * @return The metadata sent by the server. + */ + public function getMetadata() { + return $this->metadata; + } + + /** + * Cancels the call + */ + public function cancel() { + $this->call->cancel(); + } + + /** + * Deserialize a response value to an object. + * @param string $value The binary value to deserialize + * @return The deserialized value + */ + protected function deserializeResponse($value) { + if ($value === null) { + return null; + } + return call_user_func($this->deserialize, $value); + } +}
\ No newline at end of file diff --git a/src/php/lib/Grpc/AbstractSurfaceActiveCall.php b/src/php/lib/Grpc/AbstractSurfaceActiveCall.php deleted file mode 100755 index 9d0af090ce..0000000000 --- a/src/php/lib/Grpc/AbstractSurfaceActiveCall.php +++ /dev/null @@ -1,98 +0,0 @@ -<?php - -/* - * - * Copyright 2015, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -namespace Grpc; - -require_once realpath(dirname(__FILE__) . '/../autoload.php'); - -/** - * Represents an active call that allows sending and recieving messages. - * Subclasses restrict how data can be sent and recieved. - */ -abstract class AbstractSurfaceActiveCall { - private $active_call; - private $deserialize; - - /** - * Create a new surface active call. - * @param Channel $channel The channel to communicate on - * @param string $method The method to call on the remote server - * @param callable $deserialize The function to deserialize a value - * @param array $metadata Metadata to send with the call, if applicable - * @param long $flags Write flags to use with this call - */ - public function __construct(Channel $channel, - $method, - callable $deserialize, - $metadata = array(), - $flags = 0) { - $this->active_call = new ActiveCall($channel, $method, $metadata, $flags); - $this->deserialize = $deserialize; - } - - /** - * @return The metadata sent by the server - */ - public function getMetadata() { - return $this->metadata(); - } - - /** - * Cancels the call - */ - public function cancel() { - $this->active_call->cancel(); - } - - protected function _read() { - $response = $this->active_call->read(); - if ($response === null) { - return null; - } - return call_user_func($this->deserialize, $response); - } - - protected function _write($value) { - return $this->active_call->write($value->serialize()); - } - - protected function _writesDone() { - $this->active_call->writesDone(); - } - - protected function _getStatus() { - return $this->active_call->getStatus(); - } -} diff --git a/src/php/lib/Grpc/ActiveCall.php b/src/php/lib/Grpc/ActiveCall.php deleted file mode 100755 index f0d0d55582..0000000000 --- a/src/php/lib/Grpc/ActiveCall.php +++ /dev/null @@ -1,123 +0,0 @@ -<?php -/* - * - * Copyright 2015, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ -namespace Grpc; -require_once realpath(dirname(__FILE__) . '/../autoload.php'); - -/** - * Represents an active call that allows sending and recieving binary data - */ -class ActiveCall { - private $completion_queue; - private $call; - private $flags; - private $metadata; - - /** - * Create a new active call. - * @param Channel $channel The channel to communicate on - * @param string $method The method to call on the remote server - * @param array $metadata Metadata to send with the call, if applicable - * @param long $flags Write flags to use with this call - */ - public function __construct(Channel $channel, - $method, - $metadata = array(), - $flags = 0) { - $this->completion_queue = new CompletionQueue(); - $this->call = new Call($channel, $method, Timeval::inf_future()); - $this->call->add_metadata($metadata, 0); - $this->flags = $flags; - - // Invoke the call. - $this->call->invoke($this->completion_queue, - CLIENT_METADATA_READ, - FINISHED, 0); - $metadata_event = $this->completion_queue->pluck(CLIENT_METADATA_READ, - Timeval::inf_future()); - $this->metadata = $metadata_event->data; - } - - /** - * @return The metadata sent by the server. - */ - public function getMetadata() { - return $this->metadata; - } - - /** - * Cancels the call - */ - public function cancel() { - $this->call->cancel(); - } - - /** - * Read a single message from the server. - * @return The next message from the server, or null if there is none. - */ - public function read() { - $this->call->start_read(READ); - $read_event = $this->completion_queue->pluck(READ, Timeval::inf_future()); - return $read_event->data; - } - - /** - * Write a single message to the server. This cannot be called after - * writesDone is called. - * @param ByteBuffer $data The data to write - */ - public function write($data) { - $this->call->start_write($data, WRITE_ACCEPTED, $this->flags); - $this->completion_queue->pluck(WRITE_ACCEPTED, Timeval::inf_future()); - } - - /** - * Indicate that no more writes will be sent. - */ - public function writesDone() { - $this->call->writes_done(FINISH_ACCEPTED); - $this->completion_queue->pluck(FINISH_ACCEPTED, Timeval::inf_future()); - } - - /** - * Wait for the server to send the status, and return it. - * @return object The status object, with integer $code, string $details, - * and array $metadata members - */ - public function getStatus() { - $status_event = $this->completion_queue->pluck(FINISHED, - Timeval::inf_future()); - return $status_event->data; - } -} diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php index fde055a3b3..9bc1711110 100755 --- a/src/php/lib/Grpc/BaseStub.php +++ b/src/php/lib/Grpc/BaseStub.php @@ -69,11 +69,9 @@ class BaseStub { $argument, callable $deserialize, $metadata = array()) { - return new SimpleSurfaceActiveCall($this->channel, - $method, - $deserialize, - $argument, - $metadata); + $call = new UnaryCall($this->channel, $method, $deserialize); + $call->start($argument, $metadata); + return $call; } /** @@ -91,11 +89,9 @@ class BaseStub { $arguments, callable $deserialize, $metadata = array()) { - return new ClientStreamingSurfaceActiveCall($this->channel, - $method, - $deserialize, - $arguments, - $metadata); + $call = new ClientStreamingCall($this->channel, $method, $deserialize); + $call->start($arguments, $metadata); + return $call; } /** @@ -112,11 +108,9 @@ class BaseStub { $argument, callable $deserialize, $metadata = array()) { - return new ServerStreamingSurfaceActiveCall($this->channel, - $method, - $deserialize, - $argument, - $metadata); + $call = new ServerStreamingCall($this->channel, $method, $deserialize); + $call->start($argument, $metadata); + return $call; } /** @@ -130,9 +124,8 @@ class BaseStub { public function _bidiRequest($method, callable $deserialize, $metadata = array()) { - return new BidiStreamingSurfaceActiveCall($this->channel, - $method, - $deserialize, - $metadata); + $call = new BidiStreamingCall($this->channel, $method, $deserialize); + $call->start($metadata); + return $call; } } diff --git a/src/php/lib/Grpc/BidiStreamingSurfaceActiveCall.php b/src/php/lib/Grpc/BidiStreamingCall.php index 0459f21e27..0d3dd629f2 100755..100644 --- a/src/php/lib/Grpc/BidiStreamingSurfaceActiveCall.php +++ b/src/php/lib/Grpc/BidiStreamingCall.php @@ -38,38 +38,52 @@ require_once realpath(dirname(__FILE__) . '/../autoload.php'); * Represents an active call that allows for sending and recieving messages in * streams in any order. */ -class BidiStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall { +class BidiStreamingCall extends AbstractCall { + /** + * Start the call + * @param array $metadata Metadata to send with the call, if applicable + */ + public function start($metadata) { + $event = $this->call->start_batch([ + OP_SEND_INITIAL_METADATA => $metadata, + OP_RECV_INITIAL_METADATA => true]); + $this->metadata = $event->metadata; + } /** * Reads the next value from the server. * @return The next value from the server, or null if there is none */ public function read() { - return $this->_read(); + $read_event = $this->call->start_batch([OP_RECV_MESSAGE => true]); + return $this->deserializeResponse($read_event->message); } /** - * Writes a single message to the server. This cannot be called after + * Write a single message to the server. This cannot be called after * writesDone is called. - * @param $value The message to send + * @param ByteBuffer $data The data to write */ - public function write($value) { - $this->_write($value); + public function write($data) { + $this->call->start_batch([OP_SEND_MESSAGE => $data->serialize()]); } /** - * Indicate that no more writes will be sent + * Indicate that no more writes will be sent. */ public function writesDone() { - $this->_writesDone(); + $this->call->start_batch([OP_SEND_CLOSE_FROM_CLIENT => true]); } /** * Wait for the server to send the status, and return it. - * @return object The status object, with integer $code and string $details - * members + * @return object The status object, with integer $code, string $details, + * and array $metadata members */ public function getStatus() { - return $this->_getStatus(); + $status_event = $this->call->start_batch([ + OP_RECV_STATUS_ON_CLIENT => true + ]); + return $status_event->status; } -} +}
\ No newline at end of file diff --git a/src/php/lib/Grpc/ClientStreamingSurfaceActiveCall.php b/src/php/lib/Grpc/ClientStreamingCall.php index d33f09fbe4..4b3abcbdec 100755..100644 --- a/src/php/lib/Grpc/ClientStreamingSurfaceActiveCall.php +++ b/src/php/lib/Grpc/ClientStreamingCall.php @@ -38,25 +38,21 @@ require_once realpath(dirname(__FILE__) . '/../autoload.php'); * Represents an active call that sends a stream of messages and then gets a * single response. */ -class ClientStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall { +class ClientStreamingCall extends AbstractCall { /** - * Create a new simple (single request/single response) active call. - * @param Channel $channel The channel to communicate on - * @param string $method The method to call on the remote server - * @param callable $deserialize The function to deserialize a value + * Start the call. * @param Traversable $arg_iter The iterator of arguments to send * @param array $metadata Metadata to send with the call, if applicable */ - public function __construct(Channel $channel, - $method, - callable $deserialize, - $arg_iter, - $metadata = array()) { - parent::__construct($channel, $method, $deserialize, $metadata, 0); + public function start($arg_iter, $metadata = array()) { + $event = $this->call->start_batch([ + OP_SEND_INITIAL_METADATA => $metadata, + OP_RECV_INITIAL_METADATA => true]); + $this->metadata = $event->metadata; foreach($arg_iter as $arg) { - $this->_write($arg); + $this->call->start_batch([OP_SEND_MESSAGE => $arg->serialize()]); } - $this->_writesDone(); + $this->call->start_batch([OP_SEND_CLOSE_FROM_CLIENT => true]); } /** @@ -64,8 +60,9 @@ class ClientStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall { * @return [response data, status] */ public function wait() { - $response = $this->_read(); - $status = $this->_getStatus(); - return array($response, $status); + $event = $this->call->start_batch([ + OP_RECV_MESSAGE => true, + OP_RECV_STATUS_ON_CLIENT => true]); + return array($this->deserializeResponse($event->message), $event->status); } -} +}
\ No newline at end of file diff --git a/src/php/lib/Grpc/ServerStreamingSurfaceActiveCall.php b/src/php/lib/Grpc/ServerStreamingCall.php index fd08e86e51..7458f28bcb 100755..100644 --- a/src/php/lib/Grpc/ServerStreamingSurfaceActiveCall.php +++ b/src/php/lib/Grpc/ServerStreamingCall.php @@ -39,36 +39,41 @@ require_once realpath(dirname(__FILE__) . '/../autoload.php'); * Represents an active call that sends a single message and then gets a stream * of reponses */ -class ServerStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall { +class ServerStreamingCall extends AbstractCall { /** - * Create a new simple (single request/single response) active call. - * @param Channel $channel The channel to communicate on - * @param string $method The method to call on the remote server - * @param callable $deserialize The function to deserialize a value + * Start the call * @param $arg The argument to send * @param array $metadata Metadata to send with the call, if applicable */ - public function __construct(Channel $channel, - $method, - callable $deserialize, - $arg, - $metadata = array()) { - parent::__construct($channel, $method, $deserialize, $metadata, - \Grpc\WRITE_BUFFER_HINT); - $this->_write($arg); - $this->_writesDone(); + public function start($arg, $metadata = array()) { + $event = $this->call->start_batch([ + OP_SEND_INITIAL_METADATA => $metadata, + OP_RECV_INITIAL_METADATA => true, + OP_SEND_MESSAGE => $arg->serialize(), + OP_SEND_CLOSE_FROM_CLIENT => true]); + $this->metadata = $event->metadata; } /** * @return An iterator of response values */ public function responses() { - while(($response = $this->_read()) !== null) { - yield $response; + $response = $this->call->start_batch([OP_RECV_MESSAGE => true])->message; + while($response !== null) { + yield $this->deserializeResponse($response); + $response = $this->call->start_batch([OP_RECV_MESSAGE => true])->message; } } + /** + * Wait for the server to send the status, and return it. + * @return object The status object, with integer $code, string $details, + * and array $metadata members + */ public function getStatus() { - return $this->_getStatus(); + $status_event = $this->call->start_batch([ + OP_RECV_STATUS_ON_CLIENT => true + ]); + return $status_event->status; } -} +}
\ No newline at end of file diff --git a/src/php/lib/Grpc/SimpleSurfaceActiveCall.php b/src/php/lib/Grpc/UnaryCall.php index ba82f5704f..bbf9cfb588 100755..100644 --- a/src/php/lib/Grpc/SimpleSurfaceActiveCall.php +++ b/src/php/lib/Grpc/UnaryCall.php @@ -39,24 +39,19 @@ require_once realpath(dirname(__FILE__) . '/../autoload.php'); * Represents an active call that sends a single message and then gets a single * response. */ -class SimpleSurfaceActiveCall extends AbstractSurfaceActiveCall { +class UnaryCall extends AbstractCall { /** - * Create a new simple (single request/single response) active call. - * @param Channel $channel The channel to communicate on - * @param string $method The method to call on the remote server - * @param callable $deserialize The function to deserialize a value + * Start the call * @param $arg The argument to send * @param array $metadata Metadata to send with the call, if applicable */ - public function __construct(Channel $channel, - $method, - callable $deserialize, - $arg, - $metadata = array()) { - parent::__construct($channel, $method, $deserialize, $metadata, - \Grpc\WRITE_BUFFER_HINT); - $this->_write($arg); - $this->_writesDone(); + public function start($arg, $metadata = array()) { + $event = $this->call->start_batch([ + OP_SEND_INITIAL_METADATA => $metadata, + OP_RECV_INITIAL_METADATA => true, + OP_SEND_MESSAGE => $arg->serialize(), + OP_SEND_CLOSE_FROM_CLIENT => true]); + $this->metadata = $event->metadata; } /** @@ -64,8 +59,9 @@ class SimpleSurfaceActiveCall extends AbstractSurfaceActiveCall { * @return [response data, status] */ public function wait() { - $response = $this->_read(); - $status = $this->_getStatus(); - return array($response, $status); + $event = $this->call->start_batch([ + OP_RECV_MESSAGE => true, + OP_RECV_STATUS_ON_CLIENT => true]); + return array($this->deserializeResponse($event->message), $event->status); } -} +}
\ No newline at end of file |