Replaced existing PHP files with more batch-based implementation

pull/1137/head
murgatroid99 10 years ago
parent 66990377f8
commit 9140a068b5
  1. 50
      src/php/lib/Grpc/AbstractCall.php
  2. 98
      src/php/lib/Grpc/AbstractSurfaceActiveCall.php
  3. 31
      src/php/lib/Grpc/BaseStub.php
  4. 47
      src/php/lib/Grpc/BidiStreamingCall.php
  5. 31
      src/php/lib/Grpc/ClientStreamingCall.php
  6. 41
      src/php/lib/Grpc/ServerStreamingCall.php
  7. 32
      src/php/lib/Grpc/UnaryCall.php

@ -32,44 +32,48 @@
* *
*/ */
namespace Grpc; namespace Grpc;
require_once realpath(dirname(__FILE__) . '/../autoload.php'); require_once realpath(dirname(__FILE__) . '/../autoload.php');
/** abstract class AbstractCall {
* Represents an active call that allows for sending and recieving messages in
* streams in any order. protected $call;
*/ protected $deserialize;
class BidiStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall { protected $metadata;
/** /**
* Reads the next value from the server. * Create a new Call wrapper object.
* @return The next value from the server, or null if there is none * @param Channel $channel The channel to communicate on
* @param string $method The method to call on the remote server
*/ */
public function read() { public function __construct(Channel $channel, $method, $deserialize) {
return $this->_read(); $this->call = new Call($channel, $method, Timeval::inf_future());
$this->deserialize = $deserialize;
} }
/** /**
* Writes a single message to the server. This cannot be called after * @return The metadata sent by the server.
* writesDone is called.
* @param $value The message to send
*/ */
public function write($value) { public function getMetadata() {
$this->_write($value); return $this->metadata;
} }
/** /**
* Indicate that no more writes will be sent * Cancels the call
*/ */
public function writesDone() { public function cancel() {
$this->_writesDone(); $this->call->cancel();
} }
/** /**
* Wait for the server to send the status, and return it. * Deserialize a response value to an object.
* @return object The status object, with integer $code and string $details * @param string $value The binary value to deserialize
* members * @return The deserialized value
*/ */
public function getStatus() { protected function deserializeResponse($value) {
return $this->_getStatus(); if ($value === null) {
return null;
}
return call_user_func($this->deserialize, $value);
} }
} }

@ -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();
}
}

@ -69,11 +69,9 @@ class BaseStub {
$argument, $argument,
callable $deserialize, callable $deserialize,
$metadata = array()) { $metadata = array()) {
return new SimpleSurfaceActiveCall($this->channel, $call = new UnaryCall($this->channel, $method, $deserialize);
$method, $call->start($argument, $metadata);
$deserialize, return $call;
$argument,
$metadata);
} }
/** /**
@ -91,11 +89,9 @@ class BaseStub {
$arguments, $arguments,
callable $deserialize, callable $deserialize,
$metadata = array()) { $metadata = array()) {
return new ClientStreamingSurfaceActiveCall($this->channel, $call = new ClientStreamingCall($this->channel, $method, $deserialize);
$method, $call->start($arguments, $metadata);
$deserialize, return $call;
$arguments,
$metadata);
} }
/** /**
@ -112,11 +108,9 @@ class BaseStub {
$argument, $argument,
callable $deserialize, callable $deserialize,
$metadata = array()) { $metadata = array()) {
return new ServerStreamingSurfaceActiveCall($this->channel, $call = new ServerStreamingCall($this->channel, $method, $deserialize);
$method, $call->start($argument, $metadata);
$deserialize, return $call;
$argument,
$metadata);
} }
/** /**
@ -130,9 +124,8 @@ class BaseStub {
public function _bidiRequest($method, public function _bidiRequest($method,
callable $deserialize, callable $deserialize,
$metadata = array()) { $metadata = array()) {
return new BidiStreamingSurfaceActiveCall($this->channel, $call = new BidiStreamingCall($this->channel, $method, $deserialize);
$method, $call->start($metadata);
$deserialize, return $call;
$metadata);
} }
} }

@ -35,49 +35,28 @@ namespace Grpc;
require_once realpath(dirname(__FILE__) . '/../autoload.php'); require_once realpath(dirname(__FILE__) . '/../autoload.php');
/** /**
* Represents an active call that allows sending and recieving binary data * Represents an active call that allows for sending and recieving messages in
* streams in any order.
*/ */
class ActiveCall { class BidiStreamingCall extends AbstractCall {
private $call;
private $metadata;
/** /**
* Create a new active call. * Start the 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 array $metadata Metadata to send with the call, if applicable
*/ */
public function __construct(Channel $channel, public function start($metadata) {
$method, $event = $this->call->start_batch([
$metadata = array()) { OP_SEND_INITIAL_METADATA => $metadata,
$this->call = new Call($channel, $method, Timeval::inf_future()); OP_RECV_INITIAL_METADATA => true]);
$event = $this->call->start_batch([OP_SEND_INITIAL_METADATA => $metadata]);
$this->metadata = $event->metadata; $this->metadata = $event->metadata;
} }
/** /**
* @return The metadata sent by the server. * Reads the next value from the server.
*/ * @return The next value from the server, or null if there is none
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() { public function read() {
$read_event = $this->call->start_batch([OP_RECV_MESSAGE => true]); $read_event = $this->call->start_batch([OP_RECV_MESSAGE => true]);
return $read_event->message; return $this->deserializeResponse($read_event->message);
} }
/** /**
@ -86,7 +65,7 @@ class ActiveCall {
* @param ByteBuffer $data The data to write * @param ByteBuffer $data The data to write
*/ */
public function write($data) { public function write($data) {
$this->call->start_batch([OP_SEND_MESSAGE => $data]); $this->call->start_batch([OP_SEND_MESSAGE => $data->serialize()]);
} }
/** /**
@ -107,4 +86,4 @@ class ActiveCall {
]); ]);
return $status_event->status; return $status_event->status;
} }
} }

@ -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 * Represents an active call that sends a stream of messages and then gets a
* single response. * single response.
*/ */
class ClientStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall { class ClientStreamingCall extends AbstractCall {
/** /**
* Create a new simple (single request/single response) active call. * Start the 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 Traversable $arg_iter The iterator of arguments to send * @param Traversable $arg_iter The iterator of arguments to send
* @param array $metadata Metadata to send with the call, if applicable * @param array $metadata Metadata to send with the call, if applicable
*/ */
public function __construct(Channel $channel, public function start($arg_iter, $metadata = array()) {
$method, $event = $this->call->start_batch([
callable $deserialize, OP_SEND_INITIAL_METADATA => $metadata,
$arg_iter, OP_RECV_INITIAL_METADATA => true]);
$metadata = array()) { $this->metadata = $event->metadata;
parent::__construct($channel, $method, $deserialize, $metadata, 0);
foreach($arg_iter as $arg) { 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] * @return [response data, status]
*/ */
public function wait() { public function wait() {
$response = $this->_read(); $event = $this->call->start_batch([
$status = $this->_getStatus(); OP_RECV_MESSAGE => true,
return array($response, $status); OP_RECV_STATUS_ON_CLIENT => true]);
return array($this->deserializeResponse($event->message), $event->status);
} }
} }

@ -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 * Represents an active call that sends a single message and then gets a stream
* of reponses * of reponses
*/ */
class ServerStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall { class ServerStreamingCall extends AbstractCall {
/** /**
* Create a new simple (single request/single response) active call. * Start the 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 $arg The argument to send * @param $arg The argument to send
* @param array $metadata Metadata to send with the call, if applicable * @param array $metadata Metadata to send with the call, if applicable
*/ */
public function __construct(Channel $channel, public function start($arg, $metadata = array()) {
$method, $event = $this->call->start_batch([
callable $deserialize, OP_SEND_INITIAL_METADATA => $metadata,
$arg, OP_RECV_INITIAL_METADATA => true,
$metadata = array()) { OP_SEND_MESSAGE => $arg->serialize(),
parent::__construct($channel, $method, $deserialize, $metadata, OP_SEND_CLOSE_FROM_CLIENT => true]);
\Grpc\WRITE_BUFFER_HINT); $this->metadata = $event->metadata;
$this->_write($arg);
$this->_writesDone();
} }
/** /**
* @return An iterator of response values * @return An iterator of response values
*/ */
public function responses() { public function responses() {
while(($response = $this->_read()) !== null) { $response = $this->call->start_batch([OP_RECV_MESSAGE => true])->message;
yield $response; 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() { public function getStatus() {
return $this->_getStatus(); $status_event = $this->call->start_batch([
OP_RECV_STATUS_ON_CLIENT => true
]);
return $status_event->status;
} }
} }

@ -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 * Represents an active call that sends a single message and then gets a single
* response. * response.
*/ */
class SimpleSurfaceActiveCall extends AbstractSurfaceActiveCall { class UnaryCall extends AbstractCall {
/** /**
* Create a new simple (single request/single response) active call. * Start the 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 $arg The argument to send * @param $arg The argument to send
* @param array $metadata Metadata to send with the call, if applicable * @param array $metadata Metadata to send with the call, if applicable
*/ */
public function __construct(Channel $channel, public function start($arg, $metadata = array()) {
$method, $event = $this->call->start_batch([
callable $deserialize, OP_SEND_INITIAL_METADATA => $metadata,
$arg, OP_RECV_INITIAL_METADATA => true,
$metadata = array()) { OP_SEND_MESSAGE => $arg->serialize(),
parent::__construct($channel, $method, $deserialize, $metadata, OP_SEND_CLOSE_FROM_CLIENT => true]);
\Grpc\WRITE_BUFFER_HINT); $this->metadata = $event->metadata;
$this->_write($arg);
$this->_writesDone();
} }
/** /**
@ -64,8 +59,9 @@ class SimpleSurfaceActiveCall extends AbstractSurfaceActiveCall {
* @return [response data, status] * @return [response data, status]
*/ */
public function wait() { public function wait() {
$response = $this->_read(); $event = $this->call->start_batch([
$status = $this->_getStatus(); OP_RECV_MESSAGE => true,
return array($response, $status); OP_RECV_STATUS_ON_CLIENT => true]);
return array($this->deserializeResponse($event->message), $event->status);
} }
} }
Loading…
Cancel
Save