From e63354a6e71427b273182455b052ffb606e0eadc Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Mon, 10 Aug 2015 15:46:42 -0700 Subject: php: wrap getConnectivityState API --- src/php/ext/grpc/channel.c | 21 +++++++++++++++++++++ src/php/ext/grpc/php_grpc.c | 12 ++++++++++++ src/php/lib/Grpc/BaseStub.php | 8 ++++++++ src/php/tests/unit_tests/EndToEndTest.php | 11 +++++++++++ 4 files changed, 52 insertions(+) (limited to 'src') diff --git a/src/php/ext/grpc/channel.c b/src/php/ext/grpc/channel.c index 7d8a6f87ab..8a3fe11412 100644 --- a/src/php/ext/grpc/channel.c +++ b/src/php/ext/grpc/channel.c @@ -204,6 +204,26 @@ PHP_METHOD(Channel, getTarget) { RETURN_STRING(grpc_channel_get_target(channel->wrapped), 1); } +/** + * Get the connectivity state of the channel + * @param bool (optional) try to connect on the channel + * @return long The grpc connectivity state + */ +PHP_METHOD(Channel, getConnectivityState) { + wrapped_grpc_channel *channel = + (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC); + bool try_to_connect; + /* "|b" == 1 optional bool */ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &try_to_connect) == + FAILURE) { + zend_throw_exception(spl_ce_InvalidArgumentException, + "getConnectivityState expects a bool", 1 TSRMLS_CC); + return; + } + RETURN_LONG(grpc_channel_check_connectivity_state(channel->wrapped, + (int)try_to_connect)); +} + /** * Close the channel */ @@ -219,6 +239,7 @@ PHP_METHOD(Channel, close) { static zend_function_entry channel_methods[] = { PHP_ME(Channel, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) PHP_ME(Channel, getTarget, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Channel, getConnectivityState, NULL, ZEND_ACC_PUBLIC) PHP_ME(Channel, close, NULL, ZEND_ACC_PUBLIC) PHP_FE_END}; diff --git a/src/php/ext/grpc/php_grpc.c b/src/php/ext/grpc/php_grpc.c index fedcf0f539..0f730ea756 100644 --- a/src/php/ext/grpc/php_grpc.c +++ b/src/php/ext/grpc/php_grpc.c @@ -183,6 +183,18 @@ PHP_MINIT_FUNCTION(grpc) { REGISTER_LONG_CONSTANT("Grpc\\OP_RECV_CLOSE_ON_SERVER", GRPC_OP_RECV_CLOSE_ON_SERVER, CONST_CS); + /* Register connectivity state constants */ + REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_IDLE", + GRPC_CHANNEL_IDLE, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_CONNECTING", + GRPC_CHANNEL_CONNECTING, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_READY", + GRPC_CHANNEL_READY, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_TRANSIENT_FAILURE", + GRPC_CHANNEL_TRANSIENT_FAILURE, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_FATAL_FAILURE", + GRPC_CHANNEL_FATAL_FAILURE, CONST_CS); + grpc_init_call(TSRMLS_C); grpc_init_channel(TSRMLS_C); grpc_init_server(TSRMLS_C); diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php index a0c677908c..9f5c07b5f5 100755 --- a/src/php/lib/Grpc/BaseStub.php +++ b/src/php/lib/Grpc/BaseStub.php @@ -74,6 +74,14 @@ class BaseStub { return $this->channel->getTarget(); } + /** + * @param $try_to_connect bool + * @return int The grpc connectivity state + */ + public function getConnectivityState($try_to_connect = false) { + return $this->channel->getConnectivityState($try_to_connect); + } + /** * Close the communication channel associated with this stub */ diff --git a/src/php/tests/unit_tests/EndToEndTest.php b/src/php/tests/unit_tests/EndToEndTest.php index 27e27cdfdf..d49bc9ac3a 100755 --- a/src/php/tests/unit_tests/EndToEndTest.php +++ b/src/php/tests/unit_tests/EndToEndTest.php @@ -153,4 +153,15 @@ class EndToEndTest extends PHPUnit_Framework_TestCase{ public function testGetTarget() { $this->assertTrue(is_string($this->channel->getTarget())); } + + /** + * @medium + */ + public function testGetConnectivityState() { + $old_state = Grpc\CHANNEL_IDLE; + $this->assertTrue($this->channel->getConnectivityState() == $old_state); + $this->assertTrue($this->channel->getConnectivityState(true) == $old_state); + usleep(500000); + $this->assertTrue($this->channel->getConnectivityState() != $old_state); + } } -- cgit v1.2.3