diff --git a/mosquitto.c b/mosquitto.c index aab10c5..19ef2f0 100644 --- a/mosquitto.c +++ b/mosquitto.c @@ -13,35 +13,56 @@ zend_class_entry *mosquitto_ce_exception; zend_object_handlers mosquitto_std_object_handlers; zend_error_handling mosquitto_original_error_handling; -PHP_FUNCTION(mosquitto_version) +/* {{{ */ +PHP_METHOD(Mosquitto_Client, __construct) { - if (zend_parse_parameters_none() == FAILURE) { + mosquitto_client_object *object; + char *id = NULL; + int id_len = 0; + zend_bool clean_session = 0; + + PHP_MOSQUITTO_ERROR_HANDLING(); + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!|b", &id, &id_len, &clean_session) == FAILURE) { + PHP_MOSQUITTO_RESTORE_ERRORS(); return; } + PHP_MOSQUITTO_RESTORE_ERRORS(); - RETURN_LONG(mosquitto_lib_version(NULL, NULL, NULL)); + object = (mosquitto_client_object *) zend_object_store_get_object(getThis() TSRMLS_CC); + object->client = mosquitto_new(id, clean_session, object); + + if (!object->client) { + char buf[0x100]; + strerror_r(errno, buf, 0x100); + zend_throw_exception(mosquitto_ce_exception, buf, 1 TSRMLS_CC); + } } +/* }}} */ /* {{{ */ -PHP_METHOD(Mosquitto_Client, __construct) +PHP_METHOD(Mosquitto_Client, connect) { mosquitto_client_object *object; - char *id = NULL; - int id_len = 0; - zend_bool clean_session = 0; + char *host = NULL, *interface = NULL; + int host_len, interface_len, retval; + long port = 1883; + long keepalive = 0; PHP_MOSQUITTO_ERROR_HANDLING(); - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sb", &id, &id_len, &clean_session) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lls!", + &host, &host_len, &port, &keepalive, &interface, &interface_len) == FAILURE) { PHP_MOSQUITTO_RESTORE_ERRORS(); return; } PHP_MOSQUITTO_RESTORE_ERRORS(); object = (mosquitto_client_object *) zend_object_store_get_object(getThis() TSRMLS_CC); - object->client = mosquitto_new(id, clean_session, NULL); + retval = mosquitto_connect(object->client, host, port, keepalive); - if (!object->client) { - zend_throw_exception(mosquitto_ce_exception, "Failed to create a Mosquitto client", 0 TSRMLS_CC); + if (retval != MOSQ_ERR_SUCCESS) { + char buf[0x100]; + strerror_r(errno, buf, 0x100); + zend_throw_exception(mosquitto_ce_exception, buf, 1 TSRMLS_CC); } } /* }}} */ @@ -81,6 +102,7 @@ static zend_object_value mosquitto_client_object_new() { /* {{{ mosquitto_client_methods */ const zend_function_entry mosquitto_client_methods[] = { PHP_ME(Mosquitto_Client, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) + PHP_ME(Mosquitto_Client, connect, NULL, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; /* }}} */ diff --git a/php_mosquitto.h b/php_mosquitto.h index 2864496..1403614 100644 --- a/php_mosquitto.h +++ b/php_mosquitto.h @@ -33,8 +33,6 @@ PHP_MINIT_FUNCTION(mosquitto); PHP_MSHUTDOWN_FUNCTION(mosquitto); PHP_MINFO_FUNCTION(mosquitto); -PHP_FUNCTION(mosquitto_version); - #endif /* PHP_MOSQUITTO_H */ /* __footer_here__ */