Skip to content

Commit

Permalink
Implement loopForever
Browse files Browse the repository at this point in the history
  • Loading branch information
mgdm committed Oct 4, 2013
1 parent 6257e32 commit 4ee6bbb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
14 changes: 5 additions & 9 deletions examples/test.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
<?php

$client = new Mosquitto\Client();
$client->onConnect('callback');
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect("localhost", 1883, 5);
$client->subscribe('/#', 1);

for ($i = 0; $i < 10; $i++) {
$client->loop();
}
$client->disconnect();
unset($client);
$client->loopForever();

function callback($r) {
function connect($r) {
echo "I got code {$r}\n";
}

function subscribe() {
var_dump(func_get_args());
echo "Subscribed to a topic\n";
}

function message($message) {
var_dump($message->topic, $message->payload);
printf("Got a message on topic %s with payload:\n%s\n", $message->topic, $message->payload);
}

function disconnect() {
Expand Down
22 changes: 22 additions & 0 deletions mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,27 @@ PHP_METHOD(Mosquitto_Client, loop)
}
/* }}} */

/* {{{ */
PHP_METHOD(Mosquitto_Client, loopForever)
{
mosquitto_client_object *object;
long timeout = 1000, max_packets = 1, retval = 0;
char *message = NULL;

PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l!l!",
&timeout, &max_packets) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();

object = (mosquitto_client_object *) zend_object_store_get_object(getThis() TSRMLS_CC);
retval = mosquitto_loop_forever(object->client, timeout, max_packets);
php_mosquitto_handle_errno(retval, errno);
}
/* }}} */

/* Internal functions */

PHP_MOSQUITTO_API char *php_mosquitto_strerror_wrapper(int err)
Expand Down Expand Up @@ -582,6 +603,7 @@ const zend_function_entry mosquitto_client_methods[] = {
PHP_ME(Mosquitto_Client, publish, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Mosquitto_Client, subscribe, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Mosquitto_Client, loop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Mosquitto_Client, loopForever, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/* }}} */
Expand Down

0 comments on commit 4ee6bbb

Please sign in to comment.