int test1_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message) { MQTTAsync c = (MQTTAsync)context; static int message_count = 0; int rc; MyLog(LOGA_DEBUG, "In messageArrived callback %p", c); if (++message_count == 1) { MQTTAsync_message pubmsg = MQTTAsync_message_initializer; MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11"; pubmsg.payloadlen = 11; pubmsg.qos = 2; pubmsg.retained = 0; rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &opts); } else { MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; opts.onSuccess = test1_onUnsubscribe; opts.context = c; rc = MQTTAsync_unsubscribe(c, test_topic, &opts); assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); } MQTTAsync_freeMessage(&message); MQTTAsync_free(topicName); return 1; }
static int messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message) { emit ((AWSIoTClient *) context)->message(topicName, QByteArray((char *) message->payload, message->payloadlen)); MQTTAsync_freeMessage(&message); MQTTAsync_free(topicName); return 1; }
int MessageArrivedCallback(void *context, char *topicName, int topicLen, MQTTAsync_message *message) { if(mMessageArrivedCallback) { mMessageArrivedCallback(topicName, message->payload, message->payloadlen); } MQTTAsync_freeMessage(&message); MQTTAsync_free(topicName); return 1; }
int messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message) { if (opts.showtopics) printf("%s\t", topicName); if (opts.nodelimiter) printf("%.*s", message->payloadlen, (char*)message->payload); else printf("%.*s%c", message->payloadlen, (char*)message->payload, opts.delimiter); fflush(stdout); MQTTAsync_freeMessage(&message); MQTTAsync_free(topicName); return 1; }
int test3_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message) { MQTTAsync c = (MQTTAsync)context; static int message_count = 0; MyLog(LOGA_DEBUG, "Message received on topic %s, \"%.*s\"", topicName, message->payloadlen, message->payload); if (memcmp(message->payload, "will message", message->payloadlen) == 0) test3_will_message_received = 1; else test3_messages_received++; MQTTAsync_freeMessage(&message); MQTTAsync_free(topicName); return 1; }
int test3_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message) { client_data* cd = (client_data*)context; int rc; MyLog(LOGA_DEBUG, "In messageArrived callback \"%s\" message count ", cd->clientid); if (++cd->message_count == 1) { MQTTAsync_message pubmsg = MQTTAsync_message_initializer; MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11"; pubmsg.payloadlen = 25; pubmsg.qos = 1; pubmsg.retained = 0; rc = MQTTAsync_sendMessage(cd->c, cd->test_topic, &pubmsg, &opts); assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); } else if (cd->message_count == 2) { MQTTAsync_message pubmsg = MQTTAsync_message_initializer; MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; pubmsg.payload = "a QoS 0 message that we can shorten to the extent that we need to payload up to 11"; pubmsg.payloadlen = 29; pubmsg.qos = 0; pubmsg.retained = 0; opts.context = cd; opts.onSuccess = test3_onPublish; rc = MQTTAsync_sendMessage(cd->c, cd->test_topic, &pubmsg, &opts); assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); } else { MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; opts.onSuccess = test3_onUnsubscribe; opts.context = cd; rc = MQTTAsync_unsubscribe(cd->c, cd->test_topic, &opts); assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); } MQTTAsync_freeMessage(&message); MQTTAsync_free(topicName); return 1; }
int messageArrived(void *ctx, char *topicName, int topicLen, MQTTAsync_message *message) { int i; char* payloadptr; char *payloadmsg; Context context; #if DEBUG printf("Message arrived\n"); printf("topic: %s\n", topicName); printf("message:"); payloadptr = message->payload; for(i = 0; i<message->payloadlen; i++) { putchar(*payloadptr++); } putchar('\n'); #endif payloadmsg = (char *)malloc(message->payloadlen+1); if (payloadmsg != NULL) { strncpy(payloadmsg, message->payload, message->payloadlen); payloadmsg[message->payloadlen] = '\0'; } context.name = "topic"; context.value = strdup(topicName); if (msgArrhandler != NULL) { msgArrhandler(payloadmsg, context); } else { printf("error: Receive Handler not set\n"); } MQTTAsync_freeMessage(&message); MQTTAsync_free(topicName); if (payloadmsg != NULL) { free(payloadmsg); payloadmsg = NULL; } if (context.value != NULL) { free(context.value); context.value = NULL; } return 1; }
int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message) { int i; char* payloadptr; printf("Message arrived\n"); printf(" topic: %s\n", topicName); printf(" message: "); payloadptr = message->payload; for(i=0; i<message->payloadlen; i++) { putchar(*payloadptr++); } putchar('\n'); MQTTAsync_freeMessage(&message); MQTTAsync_free(topicName); return 1; }
int janus_mqtt_client_message_arrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message) { janus_mqtt_context *ctx = (janus_mqtt_context *)context; gchar *topic = g_strndup(topicName, topicLen); const gboolean janus = janus_mqtt_api_enabled_ && !strcasecmp(topic, ctx->subscribe.topic); const gboolean admin = janus_mqtt_admin_api_enabled_ && !strcasecmp(topic, ctx->admin.subscribe.topic); g_free(topic); if((janus || admin) && message->payloadlen) { JANUS_LOG(LOG_HUGE, "Receiving %s API message over MQTT: %s\n", admin ? "admin" : "Janus", (char *)message->payload); json_error_t error; json_t *root = json_loadb(message->payload, message->payloadlen, 0, &error); ctx->gateway->incoming_request(&janus_mqtt_transport_, mqtt_session, NULL, admin, root, &error); } MQTTAsync_freeMessage(&message); MQTTAsync_free(topicName); return TRUE; }
int messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message) { int i; char* payloadptr; if((sent++ % 1000) == 0) printf("%d messages received\n", sent++); //printf("Message arrived\n"); //printf(" topic: %s\n", topicName); //printf(" message: "); //payloadptr = message->payload; //for(i=0; i<message->payloadlen; i++) //{ // putchar(*payloadptr++); //} //putchar('\n'); MQTTAsync_freeMessage(&message); MQTTAsync_free(topicName); return 1; }
/* * Function to process the subscribed messages */ int subscribeMessage(void *context, char *topicName, int topicLen, MQTTAsync_message *message) { int i; char* payloadptr; char* command; int time_delay = 0; payloadptr = message->payload; time_delay = getDelay(payloadptr); if(time_delay != -1) { sprintf(command,"sudo /sbin/shutdown -r %d", time_delay); syslog(LOG_INFO, "Received command to restart in %d minutes.",time_delay); system(command); } else syslog(LOG_ERR, "Invalid command received."); MQTTAsync_freeMessage(&message); MQTTAsync_free(topicName); return 1; }
int MQTTConnector::handleReceivedmessage(char *topicName, int topicLen, MQTTAsync_message *message) { IBMRAS_DEBUG_1(debug, "MQTT message received for %s", topicName); std::string topic(topicName); if (topic == CLIENT_IDENTIFY_TOPIC) { sendIdentityMessage(); } if (receiver != NULL) { if (topic.find(agentTopic) == 0) { topic = topic.substr(agentTopic.length()); IBMRAS_DEBUG_1(debug, "forwarding message %s", topic.c_str()); } receiver->receiveMessage(topic, message->payloadlen, message->payload); } MQTTAsync_freeMessage(&message); MQTTAsync_free(topicName); return true; }
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; }
int test4_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message) { MQTTAsync c = (MQTTAsync)context; static int message_count = 0; int rc, i; MyLog(LOGA_DEBUG, "In messageArrived callback %p", c); assert("Message size correct", message->payloadlen == test4_payloadlen, "message size was %d", message->payloadlen); for (i = 0; i < options.size; ++i) { if (((char*)test4_payload)[i] != ((char*)message->payload)[i]) { assert("Message contents correct", ((char*)test4_payload)[i] != ((char*)message->payload)[i], "message content was %c", ((char*)message->payload)[i]); break; } } if (++message_count == 1) { MQTTAsync_message pubmsg = MQTTAsync_message_initializer; MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; pubmsg.payload = test4_payload; pubmsg.payloadlen = test4_payloadlen; pubmsg.qos = 1; pubmsg.retained = 0; opts.onSuccess = test4_onPublish; opts.context = c; rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &opts); } else if (message_count == 2) { MQTTAsync_message pubmsg = MQTTAsync_message_initializer; MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; pubmsg.payload = test4_payload; pubmsg.payloadlen = test4_payloadlen; pubmsg.qos = 0; pubmsg.retained = 0; opts.onSuccess = test4_onPublish; opts.context = c; rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &opts); } else { MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; opts.onSuccess = test1_onUnsubscribe; opts.context = c; rc = MQTTAsync_unsubscribe(c, test_topic, &opts); assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); } MQTTAsync_freeMessage(&message); MQTTAsync_free(topicName); return 1; }