void MQTTConnector::handleOnConnect(MQTTAsync_successData* response) { IBMRAS_LOG_2(info, "Connected to broker %s:%s", brokerHost.c_str(), brokerPort.c_str()); char *topic = new char[agentTopic.length() + 2]; #if defined(_ZOS) #pragma convert("ISO8859-1") #endif sprintf(topic, "%s#", agentTopic.c_str()); #if defined(_ZOS) #pragma convert(pop) #endif IBMRAS_DEBUG_1(debug, "MQTTAsync_subscribe to %s", topic); MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; opts.context = this; int rc = MQTTAsync_subscribe(mqttClient, topic, 1, &opts); if (rc != MQTTASYNC_SUCCESS) { IBMRAS_DEBUG_2(fine, "MQTTAsync_subscribe to %s failed. rc=%d", topic, rc); } delete[] topic; char identifyTopic[] = CLIENT_IDENTIFY_TOPIC; IBMRAS_DEBUG_1(debug, "MQTTAsync_subscribe to %s", identifyTopic); rc = MQTTAsync_subscribe(mqttClient, identifyTopic, 1, &opts); if (rc != MQTTASYNC_SUCCESS) { IBMRAS_DEBUG_2(fine, "MQTTAsync_subscribe to %s failed. rc=%d", CLIENT_IDENTIFY_TOPIC, rc); } else { sendIdentityMessage(); } }
int janus_mqtt_client_subscribe(janus_mqtt_context *ctx, gboolean admin) { MQTTAsync_responseOptions options = MQTTAsync_responseOptions_initializer; options.context = ctx; if(admin) { options.onSuccess = janus_mqtt_client_admin_subscribe_success; options.onFailure = janus_mqtt_client_admin_subscribe_failure; return MQTTAsync_subscribe(ctx->client, ctx->admin.subscribe.topic, ctx->admin.subscribe.qos, &options); } else { options.onSuccess = janus_mqtt_client_subscribe_success; options.onFailure = janus_mqtt_client_subscribe_failure; return MQTTAsync_subscribe(ctx->client, ctx->subscribe.topic, ctx->subscribe.qos, &options); } }
bool mqtt_async::MqttClient::Subscribe(string topic, int qos, const ActDoneCB& cb) { MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; Token* tok = nullptr; std::shared_ptr<Token> stok; if (cb) { tok = new Token(this); stok = std::shared_ptr<Token>(tok); stok->act_done_cb = cb; SaveToken(stok); } opts.onSuccess = cb?&Token::OnSuccess : nullptr ; opts.onFailure = cb?&Token::OnFailed : nullptr ; opts.context = tok; int rc; if ((rc = MQTTAsync_subscribe(client, topic.c_str(), qos, &opts)) != MQTTASYNC_SUCCESS) { if (tok) RemoveToken(tok); return false; } return true; }
/** * @brief async subscribe * @param[in] topic The subscription topic, which may include wildcards. * @param[in] qos The requested quality of service for the subscription. * @return MQTTASYNC_SUCCESS if the subscription request is successful. */ int MQTTAsyncSubscribe(char* topic, int qos) { MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; opts.onSuccess = OnSubscribe; opts.onFailure = OnSubscribeFailure; opts.context = mClient; int rc = MQTTAsync_subscribe(mClient, topic, qos, &opts); return rc; }
void AWSIoTClient::subscribe(QString topic) { MQTTAsync_responseOptions options = MQTTAsync_responseOptions_initializer; options.onSuccess = onSubscribed; options.onFailure = onSubscribeFailure; options.context = this; int rc = MQTTAsync_subscribe(client, topic.toUtf8(), 0, &options); if (rc != MQTTASYNC_SUCCESS) { qFatal("Failed so subscribe to topic %d", rc); } }
void test4_onConnect(void* context, MQTTAsync_successData* response) { MQTTAsync c = (MQTTAsync)context; MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; int rc; MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context); opts.onSuccess = test4_onSubscribe; opts.context = c; rc = MQTTAsync_subscribe(c, test_topic, 2, &opts); assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); if (rc != MQTTASYNC_SUCCESS) test_finished = 1; }
void test3_onConnect(void* context, MQTTAsync_successData* response) { client_data* cd = (client_data*)context; MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; int rc; MyLog(LOGA_DEBUG, "In connect onSuccess callback, \"%s\"", cd->clientid); opts.onSuccess = test3_onSubscribe; opts.context = cd; rc = MQTTAsync_subscribe(cd->c, cd->test_topic, 2, &opts); assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); if (rc != MQTTASYNC_SUCCESS) test_finished++; }
void onConnect(void* context, MQTTAsync_successData* response) { MQTTAsync client = (MQTTAsync)context; MQTTAsync_responseOptions ropts = MQTTAsync_responseOptions_initializer; MQTTAsync_message pubmsg = MQTTAsync_message_initializer; int rc; if (opts.showtopics) printf("Subscribing to topic %s with client %s at QoS %d\n", topic, opts.clientid, opts.qos); ropts.onSuccess = onSubscribe; ropts.onFailure = onSubscribeFailure; ropts.context = client; if ((rc = MQTTAsync_subscribe(client, topic, opts.qos, &ropts)) != MQTTASYNC_SUCCESS) { printf("Failed to start subscribe, return code %d\n", rc); finished = 1; } }
/** * @name Subscribes to a topic * @brief Subscribes to a topic with an MQTT broker. * @param[in] topic needs to be subscribed to * @return boolean, which specifies whether successfully subscribed or not */ int subscribe() { MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; int rc = 0; char *topic = "data"; opts.onSuccess = onSubscribe; opts.onFailure = onSubscribeFailure; opts.context = client; if ((rc = MQTTAsync_subscribe(client, topic, QOS, &opts)) != MQTTASYNC_SUCCESS) { printf("Failed to subscribe, return code %d\n", rc); exit(-1); } while (!subscribed) { sleep(1); // waiting for subscribe } return rc; }
void onConnect(void* context, MQTTAsync_successData* response) { MQTTAsync client = (MQTTAsync)context; MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; MQTTAsync_message pubmsg = MQTTAsync_message_initializer; int rc; printf("Successful connection\n"); printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n" "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS); opts.onSuccess = onSubscribe; opts.onFailure = onSubscribeFailure; opts.context = client; deliveredtoken = 0; if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS) { printf("Failed to start subscribe, return code %d\n", rc); exit(EXIT_FAILURE); } }
int subscribe(MQTTAsync* client, char *topic) { MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; int rc = MQTTASYNC_SUCCESS; opts.onSuccess = onSubscription; opts.context = *client; if ((rc = MQTTAsync_subscribe(*client, topic, QOS, &opts)) != MQTTASYNC_SUCCESS) { #ifdef ERROR syslog(LOG_ERR, "Failed to subscribe, return code %d\n", rc); #endif return rc; } #ifdef INFO syslog(LOG_DEBUG, "Waiting for subscription " "on topic %s\n", topic); #endif return rc; }
/** * 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; }