Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[mosquitto-dev] Possible Bug in in ping handling

Hi,

I believe the program below demonstrates a bug in the ping handling of a
client.  It appears that receiving a message just
before a ping is expected results in the client sending another
connection request.  The result is

graham@graham-VirtualBox-k14:~/engMQTTClientTest$ ./clientText
Client clientText sending CONNECT
Client clientText received CONNACK
Client clientText sending SUBSCRIBE (Mid: 1, Topic: /test, QoS: 2)
Client clientText received SUBACK


Client clientText received PUBLISH (d0, q0, r0, m0, '/test', ... (3 bytes))
/test:Off
Client clientText sending CONNECT
Client clientText received CONNACK
Client clientText sending SUBSCRIBE (Mid: 2, Topic: /test, QoS: 2)
Client clientText received SUBACK
Client clientText received PUBLISH (d0, q0, r0, m0, '/test', ... (3 bytes))
/test:Off
^C


Of course, I may have misinterpreted something.

Regards

Graham Benton


#include <stdio.h>
#include <string.h>
#include <time.h>
#include <mosquitto.h>

enum fail_codes {
    ERROR_LOG4C_INIT=1,
    ERROR_MOSQ_NEW,
    ERROR_MOSQ_CONNECT,
    ERROR_MOSQ_LOOP_START,
    ERROR_ENER_INIT_FAIL
};

int countdown = 0;

void my_message_callback(struct mosquitto *mosq, void *userdata,
                         const struct mosquitto_message *message)
{
    char tempString[message->payloadlen+1];
    strncpy(tempString, message->payload, message->payloadlen);
    tempString[message->payloadlen] = '\0';

    printf("%s:%s\n", message->topic, tempString);
}


void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
{

    if(!result){
        /* Subscribe to broker information topics on successful connect. */
        mosquitto_subscribe(mosq, NULL,  "/test", 2);

    }else{
    printf("Not Connected");
    }
}

void my_log_callback(struct mosquitto *mosq, void *userdata, int level,
                     const char *str)
{
    printf("%s\n", str);

    countdown = 17;

}

int main(int argc, char **argv){
    struct mosquitto *mosq = NULL;
    int err = 0;
    


    mosquitto_lib_init();
    mosq = mosquitto_new("clientText", true, NULL);
    if(!mosq){
        return ERROR_MOSQ_NEW;
    }

    mosquitto_log_callback_set(mosq, my_log_callback);
    mosquitto_connect_callback_set(mosq, my_connect_callback);
    mosquitto_message_callback_set(mosq, my_message_callback);

    if((err = mosquitto_connect_async(mosq, "localhost", 1883, 20))
       != MOSQ_ERR_SUCCESS){
        return ERROR_MOSQ_CONNECT;
    }

    if ((err = mosquitto_loop_start(mosq)) != MOSQ_ERR_SUCCESS) {
        mosquitto_disconnect(mosq);
        mosquitto_destroy(mosq);
        mosquitto_lib_cleanup();
        return ERROR_MOSQ_LOOP_START;
    }

    while (1) {
    sleep(1);
    /* Send a message just before the ping is due */
    if (--countdown == 0) {
        system("mosquitto_pub -t /test -m Off");
    }
    }

}





Back to the top