Added PHP to the global gRPC moe config
Change on 2014/12/09 by mlumish <mlumish@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=81727766
diff --git a/src/php/lib/Grpc/ActiveCall.php b/src/php/lib/Grpc/ActiveCall.php
new file mode 100755
index 0000000..10a37cd
--- /dev/null
+++ b/src/php/lib/Grpc/ActiveCall.php
@@ -0,0 +1,98 @@
+<?php
+namespace Grpc;
+
+/**
+ * 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->start_invoke($this->completion_queue,
+ INVOKE_ACCEPTED,
+ CLIENT_METADATA_READ,
+ FINISHED, 0);
+ $this->completion_queue->pluck(INVOKE_ACCEPTED,
+ Timeval::inf_future());
+ $metadata_event = $this->completion_queue->pluck(CLIENT_METADATA_READ,
+ Timeval::inf_future());
+ $this->metadata = $metadata_event->get_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->get_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) {
+ if($this->call->start_write($data,
+ WRITE_ACCEPTED,
+ $this->flags) != OP_OK) {
+ // TODO(mlumish): more useful error
+ throw new \Exception("Cannot call write after writesDone");
+ }
+ $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 and string $details
+ * members
+ */
+ public function getStatus() {
+ $status_event = $this->completion_queue->pluck(FINISHED,
+ Timeval::inf_future());
+ return $status_event->get_data();
+ }
+}
\ No newline at end of file
diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php
new file mode 100755
index 0000000..7aa0c4a
--- /dev/null
+++ b/src/php/lib/Grpc/BaseStub.php
@@ -0,0 +1,106 @@
+<?php
+
+namespace Grpc;
+
+/**
+ * Base class for generated client stubs. Stub methods are expected to call
+ * _simpleRequest or _streamRequest and return the result.
+ */
+class BaseStub {
+
+ private $channel;
+
+ public function __construct($hostname) {
+ $this->channel = new Channel($hostname, []);
+ }
+
+ /**
+ * Close the communication channel associated with this stub
+ */
+ public function close() {
+ $channel->close();
+ }
+
+ /* This class is intended to be subclassed by generated code, so all functions
+ begin with "_" to avoid name collisions. */
+
+ /**
+ * Call a remote method that takes a single argument and has a single output
+ *
+ * @param string $method The name of the method to call
+ * @param $argument The argument to the method
+ * @param callable $deserialize A function that deserializes the response
+ * @param array $metadata A metadata map to send to the server
+ * @return SimpleSurfaceActiveCall The active call object
+ */
+ protected function _simpleRequest($method,
+ $argument,
+ callable $deserialize,
+ $metadata = array()) {
+ return new SimpleSurfaceActiveCall($this->channel,
+ $method,
+ $deserialize,
+ $argument,
+ $metadata);
+ }
+
+ /**
+ * Call a remote method that takes a stream of arguments and has a single
+ * output
+ *
+ * @param string $method The name of the method to call
+ * @param $arguments An array or Traversable of arguments to stream to the
+ * server
+ * @param callable $deserialize A function that deserializes the response
+ * @param array $metadata A metadata map to send to the server
+ * @return ClientStreamingSurfaceActiveCall The active call object
+ */
+ protected function _clientStreamRequest($method,
+ $arguments,
+ callable $deserialize,
+ $metadata = array()) {
+ return new ClientStreamingSurfaceActiveCall($this->channel,
+ $method,
+ $deserialize,
+ $arguments,
+ $metadata);
+ }
+
+ /**
+ * Call a remote method that takes a single argument and returns a stream of
+ * responses
+ *
+ * @param string $method The name of the method to call
+ * @param $argument The argument to the method
+ * @param callable $deserialize A function that deserializes the responses
+ * @param array $metadata A metadata map to send to the server
+ * @return ServerStreamingSurfaceActiveCall The active call object
+ */
+ protected function _serverStreamRequest($method,
+ $argument,
+ callable $deserialize,
+ $metadata = array()) {
+ return new ServerStreamingSurfaceActiveCall($this->channel,
+ $method,
+ $deserialize,
+ $argument,
+ $metadata);
+ }
+
+ /**
+ * Call a remote method with messages streaming in both directions
+ *
+ * @param string $method The name of the method to call
+ * @param callable $deserialize A function that deserializes the responses
+ * @param array $metadata A metadata map to send to the server
+ * @return BidiStreamingSurfaceActiveCall The active call object
+ */
+ protected function _bidiRequest($method,
+ callable $deserialize,
+ $metadata = array()) {
+ return new BidiStreamingSurfaceActiveCall($this->channel,
+ $method,
+ $deserialize,
+ $metadata);
+ }
+}
diff --git a/src/php/lib/Grpc/SurfaceActiveCall.php b/src/php/lib/Grpc/SurfaceActiveCall.php
new file mode 100755
index 0000000..5f94303
--- /dev/null
+++ b/src/php/lib/Grpc/SurfaceActiveCall.php
@@ -0,0 +1,211 @@
+<?php
+namespace Grpc;
+
+/**
+ * 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();
+ }
+}
+
+/**
+ * Represents an active call that sends a single message and then gets a single
+ * response.
+ */
+class SimpleSurfaceActiveCall extends AbstractSurfaceActiveCall {
+ /**
+ * 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
+ * @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();
+ }
+
+ /**
+ * Wait for the server to respond with data and a status
+ * @return [response data, status]
+ */
+ public function wait() {
+ $response = $this->_read();
+ $status = $this->_getStatus();
+ return array($response, $status);
+ }
+}
+
+/**
+ * Represents an active call that sends a stream of messages and then gets a
+ * single response.
+ */
+class ClientStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall {
+ /**
+ * 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
+ * @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);
+ foreach($arg_iter as $arg) {
+ $this->_write($arg);
+ }
+ $this->_writesDone();
+ }
+
+ /**
+ * Wait for the server to respond with data and a status
+ * @return [response data, status]
+ */
+ public function wait() {
+ $response = $this->_read();
+ $status = $this->_getStatus();
+ return array($response, $status);
+ }
+}
+
+/**
+ * Represents an active call that sends a single message and then gets a stream
+ * of reponses
+ */
+class ServerStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall {
+ /**
+ * 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
+ * @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();
+ }
+
+ /**
+ * @return An iterator of response values
+ */
+ public function responses() {
+ while(($response = $this->_read()) != null) {
+ yield $response;
+ }
+ }
+
+ public function getStatus() {
+ return $this->_getStatus();
+ }
+}
+
+/**
+ * Represents an active call that allows for sending and recieving messages in
+ * streams in any order.
+ */
+class BidiStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall {
+
+ /**
+ * 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();
+ }
+
+ /**
+ * Writes a single message to the server. This cannot be called after
+ * writesDone is called.
+ * @param $value The message to send
+ */
+ public function write($value) {
+ $this->_write($value);
+ }
+
+ /**
+ * Indicate that no more writes will be sent
+ */
+ public function writesDone() {
+ $this->_writesDone();
+ }
+
+ /**
+ * Wait for the server to send the status, and return it.
+ * @return object The status object, with integer $code and string $details
+ * members
+ */
+ public function getStatus() {
+ return $this->_getStatus();
+ }
+}
\ No newline at end of file