diff options
author | Stanley Cheung <stanleycheung@google.com> | 2015-08-10 15:46:42 -0700 |
---|---|---|
committer | Stanley Cheung <stanleycheung@google.com> | 2015-08-13 10:48:32 -0700 |
commit | e63354a6e71427b273182455b052ffb606e0eadc (patch) | |
tree | c9de491f16a00daad2757f4e9630d775171bbc9f /src | |
parent | 35ea361ff5042bc31acd476a9ca6f9d22bf6a1c6 (diff) |
php: wrap getConnectivityState API
Diffstat (limited to 'src')
-rw-r--r-- | src/php/ext/grpc/channel.c | 21 | ||||
-rw-r--r-- | src/php/ext/grpc/php_grpc.c | 12 | ||||
-rwxr-xr-x | src/php/lib/Grpc/BaseStub.php | 8 | ||||
-rwxr-xr-x | src/php/tests/unit_tests/EndToEndTest.php | 11 |
4 files changed, 52 insertions, 0 deletions
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 @@ -205,6 +205,26 @@ PHP_METHOD(Channel, getTarget) { } /** + * 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 */ PHP_METHOD(Channel, close) { @@ -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 @@ -75,6 +75,14 @@ class BaseStub { } /** + * @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 */ public function close() { 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); + } } |