Skip to content

Commit

Permalink
Added context options
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoppanen committed Nov 28, 2012
1 parent 175a13a commit f519700
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 2 deletions.
25 changes: 25 additions & 0 deletions tests/032-contextopt.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Test context options
--SKIPIF--
<?php
require_once(dirname(__FILE__) . '/skipif.inc');
if(!defined('ZMQ::CTXOPT_MAX_SOCKETS')) die('skip');
?>
--FILE--
<?php

include dirname(__FILE__) . '/zeromq_test_helper.inc';

$context = new ZMQContext();

var_dump($context->getOpt(ZMQ::CTXOPT_MAX_SOCKETS));
var_dump($context->setOpt(ZMQ::CTXOPT_MAX_SOCKETS, 512));
var_dump($context->getOpt(ZMQ::CTXOPT_MAX_SOCKETS));

echo "OK";

--EXPECT--
int(1024)
NULL
int(512)
OK
93 changes: 91 additions & 2 deletions zmq.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ static zend_object_handlers zmq_device_object_handlers;
static const zend_fcall_info empty_fcall_info = { 0, NULL, NULL, NULL, NULL, 0, NULL, NULL, 0 };
#endif

zend_class_entry *php_zmq_context_exception_sc_entry_get ()
{
return php_zmq_context_exception_sc_entry;
}

zend_class_entry *php_zmq_socket_exception_sc_entry_get ()
{
return php_zmq_socket_exception_sc_entry;
Expand Down Expand Up @@ -212,6 +217,75 @@ PHP_METHOD(zmqcontext, __construct)
}
/* }}} */

#if ZMQ_VERSION_MAJOR == 3 && ZMQ_VERSION_MINOR >= 2
/* {{{ proto ZMQContext ZMQContext::setOpt(int option, int value)
Set a context option
*/
PHP_METHOD(zmqcontext, setOpt)
{
php_zmq_context_object *intern;
long option, value;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &option, &value) == FAILURE) {
return;
}
intern = PHP_ZMQ_CONTEXT_OBJECT;

switch (option) {

case ZMQ_MAX_SOCKETS:
{
if (zmq_ctx_set(intern->context->z_ctx, option, value) != 0) {
zend_throw_exception_ex(php_zmq_context_exception_sc_entry_get (), errno TSRMLS_CC, "Failed to set the option ZMQ::CTXOPT_MAX_SOCKETS value: %s", zmq_strerror(errno));
return;
}
}
break;

default:
{
zend_throw_exception(php_zmq_context_exception_sc_entry_get (), "Unknown option key", PHP_ZMQ_INTERNAL_ERROR TSRMLS_CC);
return;
}
}
return;
}
/* }}} */

/* {{{ proto ZMQContext ZMQContext::getOpt(int option)
Set a context option
*/
PHP_METHOD(zmqcontext, getOpt)
{
php_zmq_context_object *intern;
long option;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &option) == FAILURE) {
return;
}
intern = PHP_ZMQ_CONTEXT_OBJECT;

switch (option) {

case ZMQ_MAX_SOCKETS:
{
int value = zmq_ctx_get(intern->context->z_ctx, option);
RETURN_LONG(value);
}
break;

default:
{
zend_throw_exception(php_zmq_context_exception_sc_entry_get (), "Unknown option key", PHP_ZMQ_INTERNAL_ERROR TSRMLS_CC);
return;
}
}
return;
}
/* }}} */
#endif


/* {{{ static php_zmq_socket *php_zmq_socket_new(php_zmq_context *context, int type, zend_bool is_persistent TSRMLS_DC)
Create a new zmq socket
*/
Expand Down Expand Up @@ -1290,11 +1364,22 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(zmq_context_clone_args, 0, 0, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(zmq_context_setopt_args, 0, 0, 2)
ZEND_ARG_INFO(0, option)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(zmq_context_getopt_args, 0, 0, 2)
ZEND_ARG_INFO(0, option)
ZEND_END_ARG_INFO()

static zend_function_entry php_zmq_context_class_methods[] = {
PHP_ME(zmqcontext, __construct, zmq_context_construct_args, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR|ZEND_ACC_FINAL)
PHP_ME(zmqcontext, getsocket, zmq_context_getsocket_args, ZEND_ACC_PUBLIC)
PHP_ME(zmqcontext, __construct, zmq_context_construct_args, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR|ZEND_ACC_FINAL)
PHP_ME(zmqcontext, getsocket, zmq_context_getsocket_args, ZEND_ACC_PUBLIC)
PHP_ME(zmqcontext, ispersistent, zmq_context_ispersistent_args, ZEND_ACC_PUBLIC)
PHP_ME(zmqcontext, __clone, zmq_context_clone_args, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL)
PHP_ME(zmqcontext, setOpt, zmq_context_setopt_args, ZEND_ACC_PUBLIC)
PHP_ME(zmqcontext, getOpt, zmq_context_getopt_args, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};

Expand Down Expand Up @@ -1781,6 +1866,10 @@ PHP_MINIT_FUNCTION(zmq)

php_zmq_register_sockopt_constants (php_zmq_sc_entry TSRMLS_CC);

#if ZMQ_VERSION_MAJOR == 3 && ZMQ_VERSION_MINOR >= 2
PHP_ZMQ_REGISTER_CONST_LONG("CTXOPT_MAX_SOCKETS", ZMQ_MAX_SOCKETS);
#endif

#undef PHP_ZMQ_REGISTER_CONST_LONG
#undef PHP_ZMQ_REGISTER_CONST_STRING

Expand Down

0 comments on commit f519700

Please sign in to comment.