Skip to content

Commit

Permalink
Connecting now works
Browse files Browse the repository at this point in the history
  • Loading branch information
mgdm committed Sep 16, 2013
1 parent 7d64bf3 commit 8edb95c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
44 changes: 33 additions & 11 deletions mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
/* }}} */
Expand Down Expand Up @@ -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}
};
/* }}} */
Expand Down
2 changes: 0 additions & 2 deletions php_mosquitto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */

0 comments on commit 8edb95c

Please sign in to comment.