示例#1
0
bool mqtt_async::MqttClient::Publish(const std::string& topic, const void* payload,  INT32 len, int qos,  int retained/*default=0*/, const ActDoneCB& cb)    
{
    MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
    Token* tok = nullptr;
    std::shared_ptr<Token> stok;
    TRACE("mqtt_async::MqttClient::Publish");
    if (cb)
    {
        TRACE("mqtt_async::MqttClient::Publish  with  callback");
        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;
    MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
    pubmsg.payload = const_cast<void*>(payload);
    pubmsg.payloadlen =  len;
    pubmsg.qos = qos; 
    pubmsg.retained = retained;

    int rc = MQTTAsync_sendMessage(client, (char*)topic.c_str(), &pubmsg, &opts) ;
     if (rc != MQTTASYNC_SUCCESS)  
     {
         if (tok)
            RemoveToken(tok);

        TRACE("mqtt_async::MqttClient::Publish   return false");    
        return false;
     }  
     return true;    
}   
/* 
 * Function is used to publish events to the IoT MQTT reciver. 
 * This reuses the "client"  
 */
int publishMQTTMessage(MQTTAsync* client, char *topic, char *payload) {

	MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
	MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
	int rc = MQTTASYNC_SUCCESS;

	opts.onSuccess = onSend;
	opts.context = *client;

	pubmsg.payload = payload;
	pubmsg.payloadlen = strlen(payload);
	pubmsg.qos = QOS;
	pubmsg.retained = 0;
	deliveredtoken = 0;

	if ((rc = MQTTAsync_sendMessage(*client, topic, &pubmsg, &opts))
			!= MQTTASYNC_SUCCESS) {
#ifdef ERROR
		syslog(LOG_ERR, "Failed to start sendMessage, return code %d\n", rc);
#endif
		return rc;
	}

#ifdef INFO
	syslog(LOG_DEBUG, "Waiting for publication of %s on topic %s\n", payload,
			topic);
#endif

	return rc;
}
示例#3
0
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;
}
示例#4
0
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;
}
示例#5
0
int janus_mqtt_client_publish_message(janus_mqtt_context *ctx, char *payload, gboolean admin) {
	MQTTAsync_message msg = MQTTAsync_message_initializer;
	msg.payload = payload;
	msg.payloadlen = strlen(payload);
	msg.qos = ctx->publish.qos;
	msg.retained = 0;

	MQTTAsync_responseOptions options;
	options.context = ctx;
	if(admin) {
		options.onSuccess = janus_mqtt_client_publish_admin_success;
		options.onFailure = janus_mqtt_client_publish_admin_failure;
		return MQTTAsync_sendMessage(ctx->client, ctx->admin.publish.topic, &msg, &options);
	} else {
		options.onSuccess = janus_mqtt_client_publish_janus_success;
		options.onFailure = janus_mqtt_client_publish_janus_failure;
		return MQTTAsync_sendMessage(ctx->client, ctx->publish.topic, &msg, &options);
	}
}
示例#6
0
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;
}
示例#7
0
/**
 * @brief publish message
 * @param[in] payload A pointer to the payload of the MQTT message.
 * @return MQTTASYNC_SUCCESS if the message is accepted for publication.
 */
int MQTTAsyncPublishMessage(char* payload) {
    if(mClient == NULL || mPublishTopic == NULL || payload == NULL) {
        return MQTTASYNC_FAILURE;
    }
    MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
    MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;

    pubmsg.payload = payload;
    pubmsg.payloadlen = strlen(payload);
    pubmsg.qos = 2;
    pubmsg.retained = 0;
    
    int rc = MQTTAsync_sendMessage(mClient, mPublishTopic, &pubmsg, &opts);
    return rc;
}
示例#8
0
文件: test9.c 项目: CJxD/paho.mqtt.c
void test3cOnConnect(void* context, MQTTAsync_successData* response)
{
	MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
	MyLog(LOGA_DEBUG, "In connect onSuccess callback for client d, context %p\n", context);
	MQTTAsync c = (MQTTAsync)context;
	int rc;
	
	/* send a message to the proxy to break the connection */
	pubmsg.payload = "TERMINATE";
	pubmsg.payloadlen = strlen(pubmsg.payload);
	pubmsg.qos = 0;
	pubmsg.retained = 0;
	rc = MQTTAsync_sendMessage(c, "MQTTSAS topic", &pubmsg, NULL);
	assert("Good rc from sendMessage", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
}
示例#9
0
/**
 * @name Publish a message
 * @brief Used to send message to a broker.
 * @param[in] message to be published
 * @param[in] context w.r.t topic the message required to be published
 * @return boolean, specifies whether the message is successfully published or not
 */
int send(char *message, Context context) {

    MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
    MQTTAsync_message pubmsg = MQTTAsync_message_initializer;

    int rc = 0;
    char *topic;

    if (context.name != NULL && context.value != NULL && strcmp(context.name, "topic") == 0) {
        topic = context.value;
    }
    else {
        printf("Topic not available in the send command\n");
        return MQTTASYNC_NULL_PARAMETER;
    }

    unsigned long i;
    struct timeval tv;
    gettimeofday(&tv,NULL);

    #if DEBUG
        printf("start seconds : %ld\n",tv.tv_sec);
    #endif

    opts.onSuccess = onSend;
    opts.onFailure = onSendFailure;
    opts.context = client;
    pubmsg.payload = message;
    pubmsg.payloadlen = strlen(message);
    pubmsg.qos = QOS;
    pubmsg.retained = 0;
    usleep(100);

    if ((rc = MQTTAsync_sendMessage(client, topic, &pubmsg, &opts))     \
            != MQTTASYNC_SUCCESS) {
        printf("Failed to start sendMessage, return code %d\n", rc);
        exit(-1);
    }

    gettimeofday(&tv,NULL);

    #if DEBUG
        printf("end seconds : %ld\n",tv.tv_sec);
    #endif

    return rc;
}
示例#10
0
void AWSIoTClient::publish(QString topic, QByteArray msg) {
	MQTTAsync_responseOptions options = MQTTAsync_responseOptions_initializer;
	options.onSuccess = onSent;
	options.onFailure = onSendFailure;
	options.context = this;

	MQTTAsync_message message = MQTTAsync_message_initializer;
	message.payload = msg.data();
	message.payloadlen = msg.size();
	message.qos = 0;
	message.retained = 0;

	int rc = MQTTAsync_sendMessage(client, topic.toUtf8(), &message, &options);
	if (rc != MQTTASYNC_SUCCESS) {
		qFatal("Failed to start sendMessage, return code %d\n", rc);
	}
}
示例#11
0
int publish(void* msg_p)
{
	int rc=0;

		pubmsg.payload = msg_p;//PAYLOAD;
		pubmsg.payloadlen = MPU_MSG_LENGTH;//PAYLOAD;

		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;
}
示例#12
0
void onSend(void* context, MQTTAsync_successData* response)
{
	MQTTAsync client = (MQTTAsync)context;
	MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
	MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
	int rc;

	printf("Message with token value %d delivery confirmed\n", response->token);

	opts.onSuccess = onSendAgain;//onDisconnect;
	opts.context = client;
	
	if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start sendMessage, return code %d\n", rc);
 		exit(-1);	
	}

	/*if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start sendMessage, return code %d\n", rc);
		exit(-1);	
	}*/
}
示例#13
0
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");
	
	opts.onSuccess = onSend;
	opts.context = client;

	pubmsg.payload = PAYLOAD;
	pubmsg.payloadlen = strlen(PAYLOAD);
	pubmsg.qos = QOS;
	pubmsg.retained = 0;
	deliveredtoken = 0;

	if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start sendMessage, return code %d\n", rc);
 		exit(-1);	
	}
}
示例#14
0
/**
 * 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;
 }
示例#15
0
文件: test9.c 项目: CJxD/paho.mqtt.c
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;
}
示例#16
0
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;
}