/******************************************************************** Test5: Connack return codes *********************************************************************/ int test5(struct Options options) { int subsqos = 2; MQTTAsync c; MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer; MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer; int rc = 0; char* test_topic = "C client test1"; test_finished = failures = 0; MyLog(LOGA_INFO, "Starting test 5 - connack return codes"); fprintf(xml, "<testcase classname=\"test4\" name=\"connack return codes\""); global_start_time = start_clock(); rc = MQTTAsync_create(&c, options.connection, "a clientid that is too long to be accepted", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc); if (rc != MQTTASYNC_SUCCESS) { MQTTAsync_destroy(&c); goto exit; } rc = MQTTAsync_setCallbacks(c, c, NULL, test1_messageArrived, NULL); assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); opts.onSuccess = test5_onConnect; opts.onFailure = test5_onConnectFailure; opts.context = c; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTAsync_connect(c, &opts); rc = 0; assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); if (rc != MQTTASYNC_SUCCESS) goto exit; while (!test_finished) #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif MQTTAsync_destroy(&c); exit: MyLog(LOGA_INFO, "TEST5: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
int main(int argc, char* argv[]) { int ch; MQTTAsync client = NULL; const char* serverURI = "tcp://m2m.eclipse.org:1883"; const char* clientId = "pub_sub_thingml_client"; const char* topic = "/sintef/vassik/hello"; char* message = "hello world!"; int qos = 1; ThingMLMQTTContext* context = malloc(sizeof(ThingMLMQTTContext)); do { ch = getchar(); switch (ch) { case 'n' : { printf("Initializing new client...\n"); MQTTAsync_destroy(&client); nullset_thingml_mqtt_context(context); create_mqtt_client(&client, serverURI, clientId, context); }; break; case 'c' : { printf("Connecting client...\n"); context->fn_onconn_callback = connection_callback; context->fn_connlost_callback = connection_lost_callback; context->fn_onconnfail_callback = connection_fail_callback; context->fn_onmsgrcv_callback = message_arrived_callback; connect_mqtt_client(context); }; break; case 's' : { printf("Subscribe to the topic '%s'...\n", topic); context->fn_onsub_callback = subscribe_callback; context->fn_onsubfail_callback = subscribe_fail_callback; subscribe_mqtt_client(context, topic, qos); }; break; case 'p' : { printf("Publishing message '%s' on the topic '%s'...\n", message, topic); context->fn_onsend_callback = message_published_callback; publish_mqtt_client(context, topic, message, qos); }; break; } } while (ch != 'q'); printf("Goodbye! %p, %p\n", (void*) client, (void*) context); MQTTAsync_destroy(&client); free(context); return 1; }
int main(int argc, char* argv[]) { MQTTAsync client; MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer; MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer; MQTTAsync_message pubmsg = MQTTAsync_message_initializer; MQTTAsync_token token; int rc; int ch; MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL); MQTTAsync_setCallbacks(client, NULL, connlost, msgarrvd, NULL); conn_opts.keepAliveInterval = 20; conn_opts.cleansession = 1; conn_opts.onSuccess = onConnect; conn_opts.onFailure = onConnectFailure; conn_opts.context = client; if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS) { printf("Failed to start connect, return code %d\n", rc); exit(EXIT_FAILURE); } while (!subscribed) #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif if (finished) goto exit; do { ch = getchar(); } while (ch!='Q' && ch != 'q'); disc_opts.onSuccess = onDisconnect; if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS) { printf("Failed to start disconnect, return code %d\n", rc); exit(EXIT_FAILURE); } while (!disc_finished) #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif exit: MQTTAsync_destroy(&client); return rc; }
int publish_main(void) { MQTTAsync client; MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer; MQTTAsync_message pubmsg = MQTTAsync_message_initializer; MQTTAsync_token token; int rc; MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL); MQTTAsync_setCallbacks(client, NULL, connlost, NULL, NULL); conn_opts.keepAliveInterval = 700; conn_opts.cleansession = 1; conn_opts.onSuccess = onConnect; conn_opts.onFailure = onConnectFailure; conn_opts.context = client; if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS) { printf("Failed to start connect, return code %d\n", rc); exit(-1); } printf("Waiting for publication of %s\n" "on topic %s for client with ClientID: %s\n", PAYLOAD, TOPIC, CLIENTID); /*while (!finished) #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif */ sleep(3); int i=0; while(i<5){ i++; pubmsg.payload = msg;//PAYLOAD; pubmsg.payloadlen = strlen(msg);//PAYLOAD); msg[0]=msg[0]+1; if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS) { printf("Failed to start sendMessage, return code %d\n", rc); exit(-1); } } sleep(3600); MQTTAsync_destroy(&client); return rc; }
/* * After sending the message disconnect from IoT/mqtt server */ int disconnect_mqtt_client(MQTTAsync* client) { MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer; int rc = MQTTASYNC_SUCCESS; opts.context = client; if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS) { #ifdef ERROR syslog(LOG_ERR, "Failed to start sendMessage, return code %d\n", rc); #endif } MQTTAsync_destroy(client); return rc; }
void janus_mqtt_client_destroy_context(janus_mqtt_context **ptr) { janus_mqtt_context *ctx = (janus_mqtt_context *)*ptr; if(ctx) { MQTTAsync_destroy(&ctx->client); g_free(ctx->subscribe.topic); g_free(ctx->publish.topic); g_free(ctx->connect.username); g_free(ctx->connect.password); g_free(ctx->admin.subscribe.topic); g_free(ctx->admin.publish.topic); g_free(ctx); *ptr = NULL; } JANUS_LOG(LOG_INFO, "%s destroyed!\n", JANUS_MQTT_NAME); }
/** * @name Cleanup the MQTT client * @brief Used to close the connections and for cleanup activities. * @return boolean, which specifies whether the connection is disconnected or not */ int done() { MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer; int rc = 0; opts.onSuccess = onDisconnect; opts.context = client; if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS) { printf("Failed to start disconnect, return code %d\n", rc); exit(-1); } finished = 1; toStop = 0; MQTTAsync_destroy(&client); return rc; }
/** * @brief destroy mqtt */ void MQTTAsyncDestroy() { #ifdef SPT_DEBUG_ENABLE SPTekDebugLog(LOG_LEVEL_INFO, "MQTTAsyncDestroy()"); #else SKTDebugPrint(LOG_LEVEL_INFO, "MQTTAsyncDestroy()"); #endif if(gContent) { if(gContent->data) { free(gContent->data); gContent->data = NULL; } free(gContent); gContent = NULL; } if(mClient != NULL) { // disconnect when connected. if(MQTTAsync_isConnected(mClient)) { MQTTAsyncDisconnect(); } MQTTAsync_destroy(&mClient); mClient = NULL; } }
int main(int argc, char* argv[]) { MQTTAsync client; MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer; MQTTAsync_message pubmsg = MQTTAsync_message_initializer; MQTTAsync_token token; int rc; MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL); MQTTAsync_setCallbacks(client, NULL, connlost, NULL, NULL); conn_opts.keepAliveInterval = 20; conn_opts.cleansession = 1; conn_opts.onSuccess = onConnect; conn_opts.onFailure = onConnectFailure; conn_opts.context = client; if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS) { printf("Failed to start connect, return code %d\n", rc); exit(-1); } printf("Waiting for publication of %s\n" "on topic %s for client with ClientID: %s\n", PAYLOAD, TOPIC, CLIENTID); while (!finished) #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif MQTTAsync_destroy(&client); return rc; }
/******************************************************************** Test6: HA connections *********************************************************************/ int test6(struct Options options) { int subsqos = 2; test6_client_info cinfo; MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer; MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer; int rc = 0; char* test_topic = "C client test1"; char* uris[2] = {options.connection, options.connection}; failures = 0; MyLog(LOGA_INFO, "Starting test 7 - HA connections"); fprintf(xml, "<testcase classname=\"test4\" name=\"HA connections\""); global_start_time = start_clock(); test_finished = 0; cinfo.should_fail = 1; /* fail to connect */ rc = MQTTAsync_create(&cinfo.c, "tcp://rubbish:1883", "async ha connection", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc); if (rc != MQTTASYNC_SUCCESS) { MQTTAsync_destroy(&cinfo.c); goto exit; } rc = MQTTAsync_setCallbacks(cinfo.c, cinfo.c, NULL, test1_messageArrived, NULL); assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); opts.onSuccess = test6_onConnect; opts.onFailure = test6_onConnectFailure; opts.context = &cinfo; opts.MQTTVersion = options.MQTTVersion; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTAsync_connect(cinfo.c, &opts); rc = 0; assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); if (rc != MQTTASYNC_SUCCESS) goto exit; while (!test_finished) #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif test_finished = 0; cinfo.should_fail = 0; /* should connect */ rc = MQTTAsync_create(&cinfo.c, "tcp://rubbish:1883", "async ha connection", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc); if (rc != MQTTASYNC_SUCCESS) { MQTTAsync_destroy(&cinfo.c); goto exit; } rc = MQTTAsync_setCallbacks(cinfo.c, cinfo.c, NULL, test1_messageArrived, NULL); assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); opts.onSuccess = test6_onConnect; opts.onFailure = test6_onConnectFailure; opts.context = &cinfo; opts.serverURIs = uris; opts.serverURIcount = 2; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTAsync_connect(cinfo.c, &opts); rc = 0; assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); if (rc != MQTTASYNC_SUCCESS) goto exit; while (!test_finished) #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif MQTTAsync_destroy(&cinfo.c); exit: MyLog(LOGA_INFO, "TEST6: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
/********************************************************************* Test4: Send and receive big messages *********************************************************************/ int test4(struct Options options) { int subsqos = 2; MQTTAsync c; MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer; MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer; int rc = 0; char* test_topic = "C client test4"; test_finished = failures = 0; MyLog(LOGA_INFO, "Starting test 4 - big messages"); fprintf(xml, "<testcase classname=\"test4\" name=\"big messages\""); global_start_time = start_clock(); rc = MQTTAsync_create(&c, options.connection, "async_test_4", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc); if (rc != MQTTASYNC_SUCCESS) { MQTTAsync_destroy(&c); goto exit; } rc = MQTTAsync_setCallbacks(c, c, NULL, test4_messageArrived, NULL); assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); opts.keepAliveInterval = 20; opts.cleansession = 1; opts.username = "******"; opts.password = "******"; opts.MQTTVersion = options.MQTTVersion; opts.will = &wopts; opts.will->message = "will message"; opts.will->qos = 1; opts.will->retained = 0; opts.will->topicName = "will topic"; opts.will = NULL; opts.onSuccess = test4_onConnect; opts.onFailure = NULL; opts.context = c; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTAsync_connect(c, &opts); rc = 0; assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); if (rc != MQTTASYNC_SUCCESS) goto exit; while (!test_finished) #if defined(WIN32) Sleep(100); #else usleep(1000L); #endif MQTTAsync_destroy(&c); exit: MyLog(LOGA_INFO, "TEST4: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }
int main(int argc, char **argv) { int opt, len; int i2c_bus = DEFAULT_I2C_BUS; int sample_rate = DEFAULT_SAMPLE_RATE_HZ; int yaw_mix_factor = DEFAULT_YAW_MIX_FACTOR; int verbose = 0; char *mag_cal_file = NULL; char *accel_cal_file = NULL; MQTT_init(); while ((opt = getopt(argc, argv, "b:s:y:a:m:vh")) != -1) { switch (opt) { case 'b': i2c_bus = strtoul(optarg, NULL, 0); if (errno == EINVAL) usage(argv[0]); if (i2c_bus < MIN_I2C_BUS || i2c_bus > MAX_I2C_BUS) usage(argv[0]); break; case 's': sample_rate = strtoul(optarg, NULL, 0); if (errno == EINVAL) usage(argv[0]); if (sample_rate < MIN_SAMPLE_RATE || sample_rate > MAX_SAMPLE_RATE) usage(argv[0]); break; case 'y': yaw_mix_factor = strtoul(optarg, NULL, 0); if (errno == EINVAL) usage(argv[0]); if (yaw_mix_factor < 0 || yaw_mix_factor > 100) usage(argv[0]); break; case 'a': len = 1 + strlen(optarg); accel_cal_file = (char *)malloc(len); if (!accel_cal_file) { perror("malloc"); exit(1); } strcpy(accel_cal_file, optarg); break; case 'm': len = 1 + strlen(optarg); mag_cal_file = (char *)malloc(len); if (!mag_cal_file) { perror("malloc"); exit(1); } strcpy(mag_cal_file, optarg); break; case 'v': verbose = 1; break; case 'h': default: usage(argv[0]); break; } } register_sig_handler(); mpu9150_set_debug(verbose); if (mpu9150_init(i2c_bus, sample_rate, yaw_mix_factor)) exit(1); set_cal(0, accel_cal_file); set_cal(1, mag_cal_file); if (accel_cal_file) free(accel_cal_file); if (mag_cal_file) free(mag_cal_file); read_loop(sample_rate); mpu9150_exit(); MQTTAsync_destroy(&client); return 0; }
MQTTConnector::~MQTTConnector() { if (mqttClient != NULL) { MQTTAsync_disconnect(mqttClient, NULL); MQTTAsync_destroy(&mqttClient); } }
int main(int argc, char** argv) { MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer; MQTTAsync_responseOptions pub_opts = MQTTAsync_responseOptions_initializer; MQTTAsync_createOptions create_opts = MQTTAsync_createOptions_initializer; MQTTAsync client; char* topic = NULL; char* buffer = NULL; int rc = 0; char url[100]; if (argc < 2) usage(); getopts(argc, argv); sprintf(url, "%s:%s", opts.host, opts.port); if (opts.verbose) printf("URL is %s\n", url); topic = argv[1]; printf("Using topic %s\n", topic); create_opts.sendWhileDisconnected = 1; rc = MQTTAsync_createWithOptions(&client, url, opts.clientid, MQTTCLIENT_PERSISTENCE_NONE, NULL, &create_opts); signal(SIGINT, cfinish); signal(SIGTERM, cfinish); rc = MQTTAsync_setCallbacks(client, client, connectionLost, messageArrived, NULL); myconnect(&client); buffer = malloc(opts.maxdatalen); while (!toStop) { int data_len = 0; int delim_len = 0; delim_len = (int)strlen(opts.delimiter); do { buffer[data_len++] = getchar(); if (data_len > delim_len) { /* printf("comparing %s %s\n", opts.delimiter, &buffer[data_len - delim_len]); */ if (strncmp(opts.delimiter, &buffer[data_len - delim_len], delim_len) == 0) break; } } while (data_len < opts.maxdatalen); if (opts.verbose) printf("Publishing data of length %d\n", data_len); pub_opts.onSuccess = onPublish; pub_opts.onFailure = onPublishFailure; do { rc = MQTTAsync_send(client, topic, data_len, buffer, opts.qos, opts.retained, &pub_opts); } while (rc != MQTTASYNC_SUCCESS); } printf("Stopping\n"); free(buffer); disc_opts.onSuccess = onDisconnect; if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS) { printf("Failed to start disconnect, return code %d\n", rc); exit(EXIT_FAILURE); } while (!disconnected) #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif MQTTAsync_destroy(&client); return EXIT_SUCCESS; }
int main(int argc, char** argv) { MQTTAsync client; MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer; int rc = 0; char url[100]; if (argc < 2) usage(); topic = argv[1]; if (strchr(topic, '#') || strchr(topic, '+')) opts.showtopics = 1; if (opts.showtopics) printf("topic is %s\n", topic); getopts(argc, argv); sprintf(url, "%s:%s", opts.host, opts.port); rc = MQTTAsync_create(&client, url, opts.clientid, MQTTCLIENT_PERSISTENCE_NONE, NULL); MQTTAsync_setCallbacks(client, client, connectionLost, messageArrived, NULL); signal(SIGINT, cfinish); signal(SIGTERM, cfinish); conn_opts.keepAliveInterval = opts.keepalive; conn_opts.cleansession = 1; conn_opts.username = opts.username; conn_opts.password = opts.password; conn_opts.onSuccess = onConnect; conn_opts.onFailure = onConnectFailure; conn_opts.context = client; if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS) { printf("Failed to start connect, return code %d\n", rc); exit(EXIT_FAILURE); } while (!subscribed) #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif if (finished) goto exit; while (!finished) #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif disc_opts.onSuccess = onDisconnect; if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS) { printf("Failed to start disconnect, return code %d\n", rc); exit(EXIT_FAILURE); } while (!disconnected) #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif exit: MQTTAsync_destroy(&client); return EXIT_SUCCESS; }
/** * The main entry point of the sample. * * This method handles parsing the arguments specified on the * command-line before performing the specified action. */ int main(int argc, char** argv) { int rc = 0; int ch; char url[256]; // Default settings: int i=0; MQTTAsync client; MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer; MQTTAsync_message pubmsg = MQTTAsync_message_initializer; MQTTAsync_token token; signal(SIGINT, handleSignal); signal(SIGTERM, handleSignal); quietMode = 0; // Parse the arguments - for (i=1; i<argc; i++) { // Check this is a valid argument if (strlen(argv[i]) == 2 && argv[i][0] == '-') { char arg = argv[i][1]; // Handle the no-value arguments if (arg == 'h' || arg == '?') { printHelp(); return 255; } else if (arg == 'q') { quietMode = 1; continue; } // Validate there is a value associated with the argument if (i == argc - 1 || argv[i+1][0] == '-') { printf("Missing value for argument: %s\n", argv[i]); printHelp(); return 255; } switch(arg) { case 'a': options.action = argv[++i]; break; case 't': options.topic = argv[++i]; break; case 'm': options.message = argv[++i]; break; case 's': options.qos = atoi(argv[++i]); break; case 'c': options.message_count = atoi(argv[++i]); break; case 'b': options.broker = argv[++i]; break; case 'p': options.port = argv[++i]; break; default: printf("Unrecognised argument: %s\n", argv[i]); printHelp(); return 255; } } else { printf("Unrecognised argument: %s\n", argv[i]); printHelp(); return 255; } } // Validate the provided arguments if (strcmp(options.action, "publish") != 0 && strcmp(options.action, "subscribe") != 0) { printf("Invalid action: %s\n", options.action); printHelp(); return 255; } if (options.qos < 0 || options.qos > 2) { printf("Invalid QoS: %d\n", options.qos); printHelp(); return 255; } if (options.topic == NULL || ( options.topic != NULL && strlen(options.topic) == 0) ) { // Set the default topic according to the specified action if (strcmp(options.action, "publish") == 0) options.topic = "MQTTV3ASample/C/v3"; else options.topic = "MQTTV3ASample/#"; } // Construct the full broker URL and clientId sprintf(url, "tcp://%s:%s", options.broker, options.port); sprintf(clientId, "SampleCV3A_%s", options.action); MQTTAsync_create(&client, url, clientId, MQTTCLIENT_PERSISTENCE_NONE, NULL); MQTTAsync_setTraceCallback(handleTrace); MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR); MQTTAsync_setCallbacks(client, client, connectionLost, messageArrived, deliveryComplete); conn_opts.cleansession = 0; conn_opts.onSuccess = onConnect; conn_opts.onFailure = onConnectFailure; conn_opts.context = client; conn_opts.keepAliveInterval = 0; conn_opts.retryInterval = 0; //conn_opts.maxInflight= 30; if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS) { printf("Failed to start connect, return code %d\n", rc); goto exit; } printf("Waiting for connect\n"); while (connected == 0 && finished == 0 && toStop == 0) { printf("Waiting for connect: %d %d %d\n", connected, finished, toStop); usleep(10000L); } MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; printf("Waiting for connect: %d %d %d\n", connected, finished, toStop); printf("Successful connection\n"); if (connected == 1 && strcmp(options.action, "publish") == 0) { unsigned long i; struct timeval tv; gettimeofday(&tv,NULL); printf("start seconds : %ld\n",tv.tv_sec); for (i = 0; i < options.message_count; i++) { opts.onSuccess = onSend; opts.onFailure = onSendFailure; opts.context = client; pubmsg.payload = options.message; pubmsg.payloadlen = strlen(options.message); pubmsg.qos = options.qos; pubmsg.retained = 0; deliveredtoken = 0; usleep(100); if ((rc = MQTTAsync_sendMessage(client, options.topic, &pubmsg, &opts)) != MQTTASYNC_SUCCESS) { printf("Failed to start sendMessage, return code %d\n", rc); exit(-1); } } gettimeofday(&tv,NULL); printf("end seconds : %ld\n",tv.tv_sec); } else if (strcmp(options.action, "subscribe") == 0) { opts.onSuccess = onSubscribe; opts.onFailure = onSubscribeFailure; opts.context = client; if ((rc = MQTTAsync_subscribe(client, options.topic, options.qos, &opts)) != MQTTASYNC_SUCCESS) { printf("Failed to subscribe, return code %d\n", rc); exit(-1); } } while (!finished) { #if defined(WIN32) Sleep(100); #else usleep(1000L); #endif if (toStop == 1) { MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer; opts.onSuccess = onDisconnect; opts.context = client; printf("Entering disconnection phase\n"); if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS) { printf("Failed to start disconnect, return code %d\n", rc); exit(-1); } toStop = 0; } } exit: printf("calling destroy\n"); MQTTAsync_destroy(&client); return rc; }
int test3(struct Options options) { char* testname = "test3"; int subsqos = 2; MQTTAsync c, d; MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer; MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer; MQTTAsync_createOptions createOptions = MQTTAsync_createOptions_initializer; int rc = 0; int count = 0; char clientidc[50]; char clientidd[50]; int i = 0; MQTTAsync_token *tokens; sprintf(willTopic, "paho-test9-3-%s", unique); sprintf(clientidc, "paho-test9-3-c-%s", unique); sprintf(clientidd, "paho-test9-3-d-%s", unique); sprintf(test_topic, "paho-test9-3-test topic %s", unique); test3Finished = 0; failures = 0; MyLog(LOGA_INFO, "Starting Offline buffering 3 - messages while disconnected"); fprintf(xml, "<testcase classname=\"test3\" name=\"%s\"", testname); global_start_time = start_clock(); createOptions.sendWhileDisconnected = 1; rc = MQTTAsync_createWithOptions(&c, options.proxy_connection, clientidc, MQTTCLIENT_PERSISTENCE_DEFAULT, NULL, &createOptions); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d \n", rc); if (rc != MQTTASYNC_SUCCESS) { MQTTAsync_destroy(&c); goto exit; } rc = MQTTAsync_create(&d, options.connection, clientidd, MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d \n", rc); if (rc != MQTTASYNC_SUCCESS) { MQTTAsync_destroy(&c); goto exit; } opts.keepAliveInterval = 20; opts.cleansession = 1; //opts.username = "******"; //opts.password = "******"; rc = MQTTAsync_setCallbacks(d, d, NULL, test3_messageArrived, NULL); assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); opts.will = NULL; /* don't need will for this client, as it's going to be connected all the time */ opts.context = d; opts.onSuccess = test3dOnConnect; opts.onFailure = test3dOnFailure; MyLog(LOGA_DEBUG, "Connecting client d"); rc = MQTTAsync_connect(d, &opts); assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc); if (rc != MQTTASYNC_SUCCESS) { failures++; goto exit; } /* wait until d is ready: connected and subscribed */ count = 0; while (!test3dReady && ++count < 10000) MySleep(100); assert("Count should be less than 10000", count < 10000, "count was %d", count); /* wrong */ rc = MQTTAsync_setConnected(c, c, test3cConnected); assert("Good rc from setConnectedCallback", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); /* let client c go: connect, and send disconnect command to proxy */ opts.will = &wopts; opts.will->message = "will message"; opts.will->qos = 1; opts.will->retained = 0; opts.will->topicName = willTopic; opts.onSuccess = test3cOnConnect; opts.onFailure = test3cOnFailure; opts.context = c; opts.cleansession = 0; opts.automaticReconnect = 1; MyLog(LOGA_DEBUG, "Connecting client c"); rc = MQTTAsync_connect(c, &opts); assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc); if (rc != MQTTASYNC_SUCCESS) { failures++; goto exit; } /* wait for will message */ while (!test3_will_message_received && ++count < 10000) MySleep(100); MyLog(LOGA_DEBUG, "Now we can send some messages to be buffered"); test3c_connected = 0; /* send some messages. Then reconnect (check connected callback), and check that those messages are received */ for (i = 0; i < 3; ++i) { char buf[50]; MQTTAsync_message pubmsg = MQTTAsync_message_initializer; MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; sprintf(buf, "QoS %d message", i); pubmsg.payload = buf; pubmsg.payloadlen = strlen(pubmsg.payload) + 1; pubmsg.qos = i; pubmsg.retained = 0; rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &opts); assert("Good rc from sendMessage", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc); } rc = MQTTAsync_getPendingTokens(c, &tokens); assert("Good rc from getPendingTokens", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc); i = 0; if (tokens) { while (tokens[i] != -1) ++i; MQTTAsync_free(tokens); } assert("Number of getPendingTokens should be 3", i == 3, "i was %d ", i); /* wait for client to be reconnected */ while (!test3c_connected == 0 && ++count < 10000) MySleep(100); /* wait for success or failure callback */ while (test3_messages_received < 3 && ++count < 10000) MySleep(100); rc = MQTTAsync_getPendingTokens(c, &tokens); assert("Good rc from getPendingTokens", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc); i = 0; if (tokens) { while (tokens[i] != -1) ++i; MQTTAsync_free(tokens); } assert("Number of getPendingTokens should be 0", i == 0, "i was %d ", i); rc = MQTTAsync_disconnect(c, NULL); assert("Good rc from disconnect", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc); rc = MQTTAsync_disconnect(d, NULL); assert("Good rc from disconnect", rc == MQTTASYNC_SUCCESS, "rc was %d ", rc); exit: MQTTAsync_destroy(&c); MQTTAsync_destroy(&d); MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", testname, tests, failures); write_test_result(); return failures; }
/********************************************************************* Test3: More than one client object - simultaneous working. *********************************************************************/ int test3(struct Options options) { #define num_clients 10 int subsqos = 2; MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer; MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer; int rc = 0; int i; client_data clientdata[num_clients]; test_finished = 0; MyLog(LOGA_INFO, "Starting test 3 - multiple connections"); fprintf(xml, "<testcase classname=\"test4\" name=\"multiple connections\""); global_start_time = start_clock(); for (i = 0; i < num_clients; ++i) { sprintf(clientdata[i].clientid, "async_test3_num_%d", i); sprintf(clientdata[i].test_topic, "async test3 topic num %d", i); clientdata[i].index = i; clientdata[i].message_count = 0; rc = MQTTAsync_create(&(clientdata[i].c), options.connection, clientdata[i].clientid, MQTTCLIENT_PERSISTENCE_NONE, NULL); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc); rc = MQTTAsync_setCallbacks(clientdata[i].c, &clientdata[i], NULL, test3_messageArrived, NULL); assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); opts.keepAliveInterval = 20; opts.cleansession = 1; opts.username = "******"; opts.password = "******"; opts.MQTTVersion = options.MQTTVersion; opts.will = &wopts; opts.will->message = "will message"; opts.will->qos = 1; opts.will->retained = 0; opts.will->topicName = "will topic"; opts.onSuccess = test3_onConnect; opts.onFailure = test3_onFailure; opts.context = &clientdata[i]; MyLog(LOGA_DEBUG, "Connecting"); rc = MQTTAsync_connect(clientdata[i].c, &opts); assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); } while (test_finished < num_clients) { MyLog(LOGA_DEBUG, "num_clients %d test_finished %d\n", num_clients, test_finished); #if defined(WIN32) Sleep(100); #else usleep(10000L); #endif } MyLog(LOGA_DEBUG, "TEST3: destroying clients"); for (i = 0; i < num_clients; ++i) MQTTAsync_destroy(&clientdata[i].c); //exit: MyLog(LOGA_INFO, "TEST3: test %s. %d tests run, %d failures.", (failures == 0) ? "passed" : "failed", tests, failures); write_test_result(); return failures; }