Skip to content

Commit

Permalink
Add support for handling methods from broker
Browse files Browse the repository at this point in the history
  • Loading branch information
pinepain committed Mar 1, 2016
1 parent fe6920b commit ca28cd7
Show file tree
Hide file tree
Showing 13 changed files with 520 additions and 625 deletions.
258 changes: 144 additions & 114 deletions amqp.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,125 @@ zend_class_entry *amqp_exception_class_entry,
*Every user visible function must have an entry in amqp_functions[].
*/
zend_function_entry amqp_functions[] = {
{NULL, NULL, NULL} /* Must be the last line in amqp_functions[] */
{NULL, NULL, NULL}
};
/* }}} */

PHP_INI_BEGIN()
PHP_INI_ENTRY("amqp.host", DEFAULT_HOST, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.vhost", DEFAULT_VHOST, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.port", DEFAULT_PORT, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.timeout", DEFAULT_TIMEOUT, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.read_timeout", DEFAULT_READ_TIMEOUT, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.write_timeout", DEFAULT_WRITE_TIMEOUT, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.connect_timeout", DEFAULT_CONNECT_TIMEOUT, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.login", DEFAULT_LOGIN, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.password", DEFAULT_PASSWORD, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.auto_ack", DEFAULT_AUTOACK, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.prefetch_count", DEFAULT_PREFETCH_COUNT, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.channel_max", DEFAULT_CHANNEL_MAX, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.frame_max", DEFAULT_FRAME_MAX, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.heartbeat", DEFAULT_HEARTBEAT, PHP_INI_ALL, NULL)
PHP_INI_END()

ZEND_DECLARE_MODULE_GLOBALS(amqp);

static PHP_GINIT_FUNCTION(amqp) /* {{{ */
{
amqp_globals->error_message = NULL;
} /* }}} */

static PHP_MINIT_FUNCTION(amqp) /* {{{ */
{
zend_class_entry ce;

/* Set up the connection resource */
le_amqp_connection_resource = zend_register_list_destructors_ex(amqp_connection_resource_dtor, NULL, PHP_AMQP_CONNECTION_RES_NAME, module_number);
le_amqp_connection_resource_persistent = zend_register_list_destructors_ex(NULL, amqp_connection_resource_dtor_persistent, PHP_AMQP_CONNECTION_RES_NAME, module_number);

PHP_MINIT(amqp_connection)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(amqp_channel)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(amqp_queue)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(amqp_exchange)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(amqp_envelope)(INIT_FUNC_ARGS_PASSTHRU);

/* Class Exceptions */
INIT_CLASS_ENTRY(ce, "AMQPException", NULL);
amqp_exception_class_entry = PHP5to7_zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C));

INIT_CLASS_ENTRY(ce, "AMQPConnectionException", NULL);
amqp_connection_exception_class_entry = PHP5to7_zend_register_internal_class_ex(&ce, amqp_exception_class_entry);

INIT_CLASS_ENTRY(ce, "AMQPChannelException", NULL);
amqp_channel_exception_class_entry = PHP5to7_zend_register_internal_class_ex(&ce, amqp_exception_class_entry);

INIT_CLASS_ENTRY(ce, "AMQPQueueException", NULL);
amqp_queue_exception_class_entry = PHP5to7_zend_register_internal_class_ex(&ce, amqp_exception_class_entry);

INIT_CLASS_ENTRY(ce, "AMQPExchangeException", NULL);
amqp_exchange_exception_class_entry = PHP5to7_zend_register_internal_class_ex(&ce, amqp_exception_class_entry);

REGISTER_INI_ENTRIES();

REGISTER_LONG_CONSTANT("AMQP_NOPARAM", AMQP_NOPARAM, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_JUST_CONSUME", AMQP_JUST_CONSUME, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_DURABLE", AMQP_DURABLE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_PASSIVE", AMQP_PASSIVE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_EXCLUSIVE", AMQP_EXCLUSIVE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_AUTODELETE", AMQP_AUTODELETE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_INTERNAL", AMQP_INTERNAL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_NOLOCAL", AMQP_NOLOCAL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_AUTOACK", AMQP_AUTOACK, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_IFEMPTY", AMQP_IFEMPTY, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_IFUNUSED", AMQP_IFUNUSED, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_MANDATORY", AMQP_MANDATORY, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_IMMEDIATE", AMQP_IMMEDIATE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_MULTIPLE", AMQP_MULTIPLE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_NOWAIT", AMQP_NOWAIT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_REQUEUE", AMQP_REQUEUE, CONST_CS | CONST_PERSISTENT);

REGISTER_STRING_CONSTANT("AMQP_EX_TYPE_DIRECT", AMQP_EX_TYPE_DIRECT, CONST_CS | CONST_PERSISTENT);
REGISTER_STRING_CONSTANT("AMQP_EX_TYPE_FANOUT", AMQP_EX_TYPE_FANOUT, CONST_CS | CONST_PERSISTENT);
REGISTER_STRING_CONSTANT("AMQP_EX_TYPE_TOPIC", AMQP_EX_TYPE_TOPIC, CONST_CS | CONST_PERSISTENT);
REGISTER_STRING_CONSTANT("AMQP_EX_TYPE_HEADERS",AMQP_EX_TYPE_HEADERS, CONST_CS | CONST_PERSISTENT);

REGISTER_LONG_CONSTANT("AMQP_OS_SOCKET_TIMEOUT_ERRNO", AMQP_OS_SOCKET_TIMEOUT_ERRNO, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PHP_AMQP_MAX_CHANNELS", PHP_AMQP_MAX_CHANNELS, CONST_CS | CONST_PERSISTENT);

return SUCCESS;
} /* }}} */

static PHP_MSHUTDOWN_FUNCTION(amqp) /* {{{ */
{
UNREGISTER_INI_ENTRIES();

return SUCCESS;
} /* }}} */

static PHP_RSHUTDOWN_FUNCTION(amqp) /* {{{ */
{
if (NULL != PHP_AMQP_G(error_message)) {
efree(PHP_AMQP_G(error_message));
PHP_AMQP_G(error_message) = NULL;
}

return SUCCESS;
} /* }}} */

static PHP_MINFO_FUNCTION(amqp) /* {{{ */
{
php_info_print_table_start();
php_info_print_table_header(2, "Version", PHP_AMQP_VERSION);
php_info_print_table_header(2, "Revision", PHP_AMQP_REVISION);
php_info_print_table_header(2, "Compiled", __DATE__ " @ " __TIME__);
php_info_print_table_header(2, "AMQP protocol version", "0-9-1");
php_info_print_table_header(2, "librabbitmq version", amqp_version());
php_info_print_table_header(2, "Default max channels per connection", DEFAULT_CHANNEL_MAX);
php_info_print_table_header(2, "Default max frame size", DEFAULT_FRAME_MAX);
php_info_print_table_header(2, "Default heartbeats interval", DEFAULT_HEARTBEAT);
DISPLAY_INI_ENTRIES();
} /* }}} */

/* {{{ amqp_module_entry
*/
zend_module_entry amqp_module_entry = {
Expand All @@ -91,25 +206,43 @@ zend_module_entry amqp_module_entry = {
PHP_MINIT(amqp),
PHP_MSHUTDOWN(amqp),
NULL,
NULL,
PHP_RSHUTDOWN(amqp),
PHP_MINFO(amqp),
PHP_AMQP_VERSION,
STANDARD_MODULE_PROPERTIES
PHP_MODULE_GLOBALS(amqp),
PHP_GINIT(amqp),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */

#ifdef COMPILE_DL_AMQP
ZEND_GET_MODULE(amqp)
#endif

void php_amqp_error(amqp_rpc_reply_t reply, char **message, amqp_connection_resource *connection_resource, amqp_channel_resource *channel_resource TSRMLS_DC)
int php_amqp_error(amqp_rpc_reply_t reply, char **message, amqp_connection_resource *connection_resource, amqp_channel_resource *channel_resource TSRMLS_DC)
{
return php_amqp_error_advanced(reply, message, connection_resource, channel_resource, 1 TSRMLS_CC);
}

int php_amqp_error_advanced(amqp_rpc_reply_t reply, char **message, amqp_connection_resource *connection_resource, amqp_channel_resource *channel_resource, int fail_on_errors TSRMLS_DC)
{
assert(connection_resource != NULL);

switch (php_amqp_connection_resource_error(reply, message, connection_resource, (amqp_channel_t)(channel_resource ? channel_resource->channel_id : 0) TSRMLS_CC)) {
if (*message != NULL) {
efree(*message);
}

int res = php_amqp_connection_resource_error(reply, message, connection_resource, (amqp_channel_t)(channel_resource ? channel_resource->channel_id : 0) TSRMLS_CC);

switch (res) {
case PHP_AMQP_RESOURCE_RESPONSE_OK:
break;
case PHP_AMQP_RESOURCE_RESPONSE_ERROR:
if (!fail_on_errors) {
break;
}
/* Library or other non-protocol or even protocol related errors may be here. */
/* In most cases it designate some underlying hard errors. Fail fast. */
case PHP_AMQP_RESOURCE_RESPONSE_ERROR_CONNECTION_CLOSED:
Expand All @@ -135,6 +268,12 @@ void php_amqp_error(amqp_rpc_reply_t reply, char **message, amqp_connection_reso
spprintf(message, 0, "Unknown server error, method id 0x%08X (not handled by extension)", reply.reply.id);
break;
}

return res;
}

void php_amqp_zend_throw_exception_short(amqp_rpc_reply_t reply, zend_class_entry *exception_ce TSRMLS_DC) {
php_amqp_zend_throw_exception(reply, exception_ce, PHP_AMQP_G(error_message), 0 TSRMLS_CC);
}

void php_amqp_zend_throw_exception(amqp_rpc_reply_t reply, zend_class_entry *exception_ce, const char *message, PHP5to7_param_long_type_t code TSRMLS_DC)
Expand Down Expand Up @@ -379,115 +518,6 @@ void php_amqp_free_amqp_table(amqp_table_t *object)
internal_php_amqp_free_amqp_table(object, 1);
}


PHP_INI_BEGIN()
PHP_INI_ENTRY("amqp.host", DEFAULT_HOST, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.vhost", DEFAULT_VHOST, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.port", DEFAULT_PORT, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.timeout", DEFAULT_TIMEOUT, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.read_timeout", DEFAULT_READ_TIMEOUT, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.write_timeout", DEFAULT_WRITE_TIMEOUT, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.connect_timeout", DEFAULT_CONNECT_TIMEOUT, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.login", DEFAULT_LOGIN, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.password", DEFAULT_PASSWORD, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.auto_ack", DEFAULT_AUTOACK, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.prefetch_count", DEFAULT_PREFETCH_COUNT, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.channel_max", DEFAULT_CHANNEL_MAX, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.frame_max", DEFAULT_FRAME_MAX, PHP_INI_ALL, NULL)
PHP_INI_ENTRY("amqp.heartbeat", DEFAULT_HEARTBEAT, PHP_INI_ALL, NULL)
PHP_INI_END()

/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(amqp)
{
zend_class_entry ce;

/* Set up the connection resource */
le_amqp_connection_resource = zend_register_list_destructors_ex(amqp_connection_resource_dtor, NULL, PHP_AMQP_CONNECTION_RES_NAME, module_number);
le_amqp_connection_resource_persistent = zend_register_list_destructors_ex(NULL, amqp_connection_resource_dtor_persistent, PHP_AMQP_CONNECTION_RES_NAME, module_number);

PHP_MINIT(amqp_connection)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(amqp_channel)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(amqp_queue)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(amqp_exchange)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(amqp_envelope)(INIT_FUNC_ARGS_PASSTHRU);

/* Class Exceptions */
INIT_CLASS_ENTRY(ce, "AMQPException", NULL);
amqp_exception_class_entry = PHP5to7_zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C));

INIT_CLASS_ENTRY(ce, "AMQPConnectionException", NULL);
amqp_connection_exception_class_entry = PHP5to7_zend_register_internal_class_ex(&ce, amqp_exception_class_entry);

INIT_CLASS_ENTRY(ce, "AMQPChannelException", NULL);
amqp_channel_exception_class_entry = PHP5to7_zend_register_internal_class_ex(&ce, amqp_exception_class_entry);

INIT_CLASS_ENTRY(ce, "AMQPQueueException", NULL);
amqp_queue_exception_class_entry = PHP5to7_zend_register_internal_class_ex(&ce, amqp_exception_class_entry);

INIT_CLASS_ENTRY(ce, "AMQPExchangeException", NULL);
amqp_exchange_exception_class_entry = PHP5to7_zend_register_internal_class_ex(&ce, amqp_exception_class_entry);

REGISTER_INI_ENTRIES();

REGISTER_LONG_CONSTANT("AMQP_NOPARAM", AMQP_NOPARAM, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_JUST_CONSUME", AMQP_JUST_CONSUME, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_DURABLE", AMQP_DURABLE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_PASSIVE", AMQP_PASSIVE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_EXCLUSIVE", AMQP_EXCLUSIVE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_AUTODELETE", AMQP_AUTODELETE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_INTERNAL", AMQP_INTERNAL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_NOLOCAL", AMQP_NOLOCAL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_AUTOACK", AMQP_AUTOACK, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_IFEMPTY", AMQP_IFEMPTY, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_IFUNUSED", AMQP_IFUNUSED, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_MANDATORY", AMQP_MANDATORY, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_IMMEDIATE", AMQP_IMMEDIATE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_MULTIPLE", AMQP_MULTIPLE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_NOWAIT", AMQP_NOWAIT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("AMQP_REQUEUE", AMQP_REQUEUE, CONST_CS | CONST_PERSISTENT);

REGISTER_STRING_CONSTANT("AMQP_EX_TYPE_DIRECT", AMQP_EX_TYPE_DIRECT, CONST_CS | CONST_PERSISTENT);
REGISTER_STRING_CONSTANT("AMQP_EX_TYPE_FANOUT", AMQP_EX_TYPE_FANOUT, CONST_CS | CONST_PERSISTENT);
REGISTER_STRING_CONSTANT("AMQP_EX_TYPE_TOPIC", AMQP_EX_TYPE_TOPIC, CONST_CS | CONST_PERSISTENT);
REGISTER_STRING_CONSTANT("AMQP_EX_TYPE_HEADERS",AMQP_EX_TYPE_HEADERS, CONST_CS | CONST_PERSISTENT);

REGISTER_LONG_CONSTANT("AMQP_OS_SOCKET_TIMEOUT_ERRNO", AMQP_OS_SOCKET_TIMEOUT_ERRNO, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PHP_AMQP_MAX_CHANNELS", PHP_AMQP_MAX_CHANNELS, CONST_CS | CONST_PERSISTENT);

return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(amqp)
{
UNREGISTER_INI_ENTRIES();

return SUCCESS;
}
/* }}} */


/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(amqp)
{
php_info_print_table_start();
php_info_print_table_header(2, "Version", PHP_AMQP_VERSION);
php_info_print_table_header(2, "Revision", PHP_AMQP_REVISION);
php_info_print_table_header(2, "Compiled", __DATE__ " @ " __TIME__);
php_info_print_table_header(2, "AMQP protocol version", "0-9-1");
php_info_print_table_header(2, "librabbitmq version", amqp_version());
php_info_print_table_header(2, "Default max channels per connection", DEFAULT_CHANNEL_MAX);
php_info_print_table_header(2, "Default max frame size", DEFAULT_FRAME_MAX);
php_info_print_table_header(2, "Default heartbeats interval", DEFAULT_HEARTBEAT);
DISPLAY_INI_ENTRIES();
}
/* }}} */

/*
*Local variables:
*tab-width: 4
Expand Down
Loading

0 comments on commit ca28cd7

Please sign in to comment.