コード例 #1
0
ファイル: test3.c プロジェクト: Adamwuming/paho.mqtt.c
/*********************************************************************

Test: single-threaded client

*********************************************************************/
void singleThread_sendAndReceive(MQTTClient* c, int qos, char* test_topic)
{
	MQTTClient_deliveryToken dt;
	MQTTClient_message pubmsg = MQTTClient_message_initializer;
	MQTTClient_message* m = NULL;
	char* topicName = NULL;
	int topicLen;
	int i = 0;
	int iterations = 50;
	int rc;

	MyLog(LOGA_DEBUG, "%d messages at QoS %d", iterations, qos);
	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 = qos;
	pubmsg.retained = 0;

	for (i = 0; i< iterations; ++i)
	{
		if (i % 10 == 0)
			rc = MQTTClient_publish(c, test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained, NULL);
		else
			rc = MQTTClient_publishMessage(c, test_topic, &pubmsg, &dt);
		assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);

		if (qos > 0)
		{
			rc = MQTTClient_waitForCompletion(c, dt, 20000L);
			assert("Good rc from waitforCompletion", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
		}

		rc = MQTTClient_receive(c, &topicName, &topicLen, &m, 10000);
		assert("Good rc from receive", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
		if (topicName)
		{
			MyLog(LOGA_DEBUG, "Message received on topic %s is %.*s", topicName, m->payloadlen, (char*)(m->payload));
			if (pubmsg.payloadlen != m->payloadlen ||
					memcmp(m->payload, pubmsg.payload, m->payloadlen) != 0)
			{
				failures++;
				MyLog(LOGA_INFO, "Error: wrong data - received lengths %d %d", pubmsg.payloadlen, m->payloadlen);
				break;
			}
			MQTTClient_free(topicName);
			MQTTClient_freeMessage(&m);
		}
		else
			printf("No message received within timeout period\n");
	}

	/* receive any outstanding messages */
	MQTTClient_receive(c, &topicName, &topicLen, &m, 1000);
	while (topicName)
	{
		printf("Message received on topic %s is %.*s.\n", topicName, m->payloadlen, (char*)(m->payload));
		MQTTClient_free(topicName);
		MQTTClient_freeMessage(&m);
		MQTTClient_receive(c, &topicName, &topicLen, &m, 1000);
	}
}
コード例 #2
0
ファイル: mqttsend.c プロジェクト: lawrie/lightwaverf-pi
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    int i;
    char* payloadptr;

    printf("Message arrived\n");
    printf("     topic: %s\n", topicName);
    printf("   message: ");

    payloadptr = (char *) message->payload;
    char* payload = (char*)  malloc(message->payloadlen+1);
    for(i=0;i<message->payloadlen;i++) payload[i] = payloadptr[i];
    payload[message->payloadlen] = 0;
    for(i=0; i<message->payloadlen; i++)
    {
        putchar(*payloadptr++);
    }
    putchar('\n');
    char* token = strtok(payload, " ");
    int channel = atoi(token);
    token = strtok(NULL, " ");
    int cmd = 0;
    int level = 0;
    if (token != NULL) {
        cmd = atoi(token);
    }
    printf("Sending command %d on channel %d, level %d\n", cmd, channel, level);

    lw_cmd(level, channel, cmd, id);

    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    free(payload);
    return 1;
}
コード例 #3
0
ファイル: stdoutsub.c プロジェクト: yunba/yunba-c-sdk
int messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
{
	char action[30];
	char alias[60];
	int ret = -1;
	int i;
	char* payloadptr;

	my_present.action = action;
	my_present.alias = alias;
	ret = get_present_info(topicName, m, &my_present);
	if (ret == 0)
		printf("action:%s alias:%s\n", my_present.action, my_present.alias);
	time_t t;
	time(&t);
	printf("Message arrived, date:%s", ctime(&t));
	printf("     qos: %i\n", m->qos);
	printf("     messageid: %"PRIu64"\n", m->msgid);
	printf("     topic: %s\n", topicName);
	printf("   message: ");

	payloadptr = m->payload;
	for(i = 0; i < m->payloadlen; i++)
	{
		putchar(*payloadptr++);
	}
	putchar('\n');
	MQTTClient_freeMessage(&m);
	MQTTClient_free(topicName);

	/* not expecting any messages */
	return 1;
}
コード例 #4
0
ファイル: ConMQTT.cpp プロジェクト: tuttitan/2016-camp
// メッセージ到着時
int CConMQTT::iMsgArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
	// 局所変数宣言
	int i;                 // ループカウンタ
	char* payloadptr;      // ペイロードの先頭アドレス


	// トピックとメッセージの表示
	printf("Message arrived\n");
	printf("     topic: %s\n", topicName);
	printf("   message: ");

	payloadptr = (char*)message->payload;
	for (i = 0; i<message->payloadlen; i++)
	{
		putchar(*payloadptr++);
	}
	putchar('\n');


	// 解放処理
	MQTTClient_freeMessage(&message);
	MQTTClient_free(topicName);

	return true;
}
コード例 #5
0
ファイル: mqttclient_module.c プロジェクト: CJxD/paho.mqtt.c
int messageArrived(void* context, char* topicName, int topicLen,
		MQTTClient_message* message)
{
	PyObject *result = NULL;
	CallbackEntry* e = context;
	int rc = -99;
	PyGILState_STATE gstate;

	//printf("messageArrived %s %s %d %.*s\n", PyString_AsString(e->context), topicName, topicLen,
	//	message->payloadlen, (char*)message->payload);

	gstate = PyGILState_Ensure();
	if (topicLen == 0)
		result = PyEval_CallFunction(e->ma, "Os{ss#sisisisi}", e->context,
				topicName, "payload", message->payload, message->payloadlen,
				"qos", message->qos, "retained", message->retained, "dup",
				message->dup, "msgid", message->msgid);
	else
		result = PyEval_CallFunction(e->ma, "Os#{ss#sisisisi}", e->context,
				topicName, topicLen, "payload", message->payload,
				message->payloadlen, "qos", message->qos, "retained",
				message->retained, "dup", message->dup, "msgid",
				message->msgid);
	if (result)
	{
		if (PyInt_Check(result))
			rc = (int) PyInt_AsLong(result);
		Py_DECREF(result);
	}
	PyGILState_Release(gstate);
	MQTTClient_free(topicName);
	MQTTClient_freeMessage(&message);
	return rc;
}
コード例 #6
0
ファイル: MQTT.cpp プロジェクト: MartinHaimberger/Homegear
int MQTTMessageArrived(void* context, char* topicName, int topicLength, MQTTClient_message* message)
{
    std::vector<char> payload((char*)message->payload, (char*)message->payload + message->payloadlen);
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    GD::mqtt->messageReceived(payload);
    return 1;
}
コード例 #7
0
int test2_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
{
	++test2_arrivedcount;
	MyLog(LOGA_DEBUG, "Callback: %d message received on topic %s is %.*s.",
					test2_arrivedcount, topicName, m->payloadlen, (char*)(m->payload));
	MQTTClient_free(topicName);
	MQTTClient_freeMessage(&m);
	return 1;
}
コード例 #8
0
ファイル: test3.c プロジェクト: Adamwuming/paho.mqtt.c
int multiThread_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
{
	++multiThread_arrivedcount;
	MyLog(LOGA_DEBUG, "Callback: %d message received on topic %s is %.*s.",
					multiThread_arrivedcount, topicName, m->payloadlen, (char*)(m->payload));
	if (multiThread_pubmsg.payloadlen != m->payloadlen ||
					memcmp(m->payload, multiThread_pubmsg.payload, m->payloadlen) != 0)
	{
		failures++;
		MyLog(LOGA_INFO, "Error: wrong data received lengths %d %d\n", multiThread_pubmsg.payloadlen, m->payloadlen);
	}
	MQTTClient_free(topicName);
	MQTTClient_freeMessage(&m);
	return 1;
}
コード例 #9
0
ファイル: ConMQTT.cpp プロジェクト: tuttitan/2016-camp
// メッセージ到着時
int CConMQTT::msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
	// 局所変数宣言
	int i;                 // ループカウンタ
	char* payloadptr;      // ペイロードの先頭アドレス
	int iLenTopic;         // トピック長
	int iMsg;              // メッセージ [0, 1]

	// トピックとメッセージの表示
	printf("Message arrived\n");
	printf("     topic: %s\n", topicName);
	printf("   message: ");

	payloadptr = (char*)message->payload;
	for (i = 0; i<message->payloadlen; i++)
	{
		putchar(*payloadptr++);
	}
	putchar('\n');

	// トピックの最後の文字を抽出
	iLenTopic = strlen(topicName);
	printf("--- %c --- (%d)\n", topicName[iLenTopic -1], iLenTopic);


	// メッセージを比較(メッセージは[0, 1]しか到着しない想定)
	// 1: enterの場合のみ、処理する
	payloadptr = (char*)message->payload;
	iMsg = atoi(payloadptr);

	if (1 == iMsg) {
		printf("[Message Arrived] Enter Sensor: %c\n", topicName[iLenTopic - 1]);
		//reinterpret_cast<CConMQTT*>(context)->vTestMethod();
		::PostMessage(
			reinterpret_cast<CConMQTT*>(context)->m_hWnd, 
			WM_USER_PROC, PROC_RECV_MQTT, 
			(LPARAM)&topicName[iLenTopic - 1]);
	}
	else {
		printf("[Message Arrived] Leave Sensor: %c\n", topicName[iLenTopic - 1]);
	}

	// 解放処理
	MQTTClient_freeMessage(&message);
	MQTTClient_free(topicName);

	return 1;
}
コード例 #10
0
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    int i;
    char* payloadptr;
    printf("Message arrived\n");
    printf("     topic: %s\n", topicName);
    printf("   message: ");
    payloadptr = static_cast<char*>(message->payload);
    for(i=0; i<message->payloadlen; i++)
    {
        putchar(*payloadptr++);
    }
    putchar('\n');
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}
コード例 #11
0
// -------------------------------------------------------------------
// Fonction Call-Back reception d'un message MQTT
// -------------------------------------------------------------------
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    unsigned int i;
    char* payloadptr;
    char mqtt_rcv_message[500]; // 3x byte TL + 3x100 bytes V

    payloadptr = message->payload;
    for(i=0; i<message->payloadlen; i++)
    {
        mqtt_rcv_message[i]=payloadptr[i];
    }

    // Reconstruction du message ALGOID
    processMqttMsg(mqtt_rcv_message, message->payloadlen, topicName, algoidMsgRXStack);

    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}
コード例 #12
0
ファイル: mqttapi_paho.c プロジェクト: neo7206/azure-iot-sdks
static void connectionLost(void *context, char *cause)
{
	PMQTTTAPI_HANDLE_DATA transportState = (PMQTTTAPI_HANDLE_DATA)context;
	LogError("Lost Connection, reseting Connection State. Cause: %s \r\n", cause);

	if (transportState != NULL)
	{
		transportState->connected = false;
	}

	LogInfo("Trying to connect again\r\n");

	if (!checkAndTryToConnect(transportState))
	{
		LogError("Could not connect again. \r\n");
	}
	else
	{
		LogInfo("Connected.\r\n");
		if (transportState->subscribed)
		{
			int rc;
			LogInfo("Trying to Subscribe after reconnect.\r\n");

			if ((rc = MQTTClient_subscribe(transportState->client, STRING_c_str(transportState->subscribedTopicHandleData->topicName), SUBSCRIBEQOS)) != MQTTCLIENT_SUCCESS)
			{
				LogError("Could not subscribe again. Won't be able to receive messages. Error Code: %d.\r\n", rc);
			}
			else
			{
				LogInfo("Subscribed succesfully after reconnect..\r\n");
			}
		}
	}

	if (cause != NULL)
	{
		MQTTClient_free(cause);
	}
}
コード例 #13
0
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_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');
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    //writePortTest(NULL,NULL);
    char *sendData	=	"nc";
    GW_sendAndReceiveRequest();
    return 1;
}
コード例 #14
0
//Default function in MQTT paho (Runs when msg is received)
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    int i;
    char* payloadptr;
    char messageText[message->payloadlen];

    printf("Message arrived\n");
    printf("     topic: %s\n", topicName);
    printf("   message: ");

    payloadptr = message->payload;
    for(i=0; i < message->payloadlen; i++)
    {
     //   putchar(*payloadptr++);
	  messageText[i] = *payloadptr++;
    }
	  messageText[i] = '\0';
    lightLED(messageText[0]);
    printf("%s",messageText);
    putchar('\n');
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}
コード例 #15
0
ファイル: onpush.c プロジェクト: milk-cocoa/c-examples
int main(int argc, char** argv)
{
	MQTTClient client;
	MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
	char topic[128];
	int rc = 0;
	char url[100];

	create_push_topic(topic, "message");

	printf("topic is %s\n", topic);

  if (strchr(topic, '#') || strchr(topic, '+'))
		opts.showtopics = 1;
  if (opts.showtopics)
		printf("topic is %s\n", topic);

	sprintf(url, "%s:%s", opts.host, opts.port);

	rc = MQTTClient_create(&client, url, opts.clientid, MQTTCLIENT_PERSISTENCE_NONE, NULL);

	signal(SIGINT, cfinish);
	signal(SIGTERM, cfinish);

	conn_opts.keepAliveInterval = 10;
	conn_opts.reliable = 0;
	conn_opts.cleansession = 0;
	conn_opts.username = opts.token;
	conn_opts.password = opts.appid;
	
	myconnect(&client, &conn_opts);
	
	rc = MQTTClient_subscribe(client, topic, opts.qos);

	while (!toStop)
	{
		char* topicName = NULL;
		int topicLen;
		MQTTClient_message* message = NULL;
		
		rc = MQTTClient_receive(client, &topicName, &topicLen, &message, 1000);
		if (message)
		{
			if (opts.showtopics)
				printf("%s\t", topicName);
      if (opts.nodelimiter)
				printf("%.*s", message->payloadlen, (char*)message->payload);
			else
				printf("%.*s%s", message->payloadlen, (char*)message->payload, opts.delimiter);
			fflush(stdout);
			MQTTClient_freeMessage(&message);
			MQTTClient_free(topicName);
		}
		if (rc != 0)
			myconnect(&client, &conn_opts);
	}
	
	printf("Stopping\n");

	MQTTClient_disconnect(client, 0);

 	MQTTClient_destroy(&client);

	return 0;
}
コード例 #16
0
ファイル: TT3Dlg.cpp プロジェクト: 12019/TT3
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
	//char cS[100];
	char * pBuffer = (char *)malloc(message->payloadlen + 1);
	char * pHash = (char *)malloc(20);
	char * pEnoch = (char *)malloc(20);
	
	//strncpy_s(cS, (char *)message->payload, min(message->payloadlen, sizeof(cS)));
	strncpy(pBuffer, (char *)message->payload, message->payloadlen);
	//cS[message->payloadlen] = 0x0;
	*(pBuffer+message->payloadlen) = 0x0;

	CWnd *tA = theApp.GetMainWnd();
	
	// Adjust counter
	CTT3Dlg *tA2 = (CTT3Dlg *)tA;
	//check if alert must play
	if (tA2->iAlert > 0) {
		// find substring match
		int i = 0;
		char * pAnswer;
		while (i < tA2->iAlert) {
			if (!strcmp(tA2->ALERTME[i][0], "1")) {
				if (strstr(pBuffer, tA2->ALERTME[i][1])) {
					Beep(atoi(tA2->ALERTME[i][2]), atoi(tA2->ALERTME[i][3]));
					// run exe
					char * cmdline = (char *)malloc(2000);
					PROCESS_INFORMATION processInformation;
					STARTUPINFO startupInfo;
					memset(&processInformation, 0, sizeof(processInformation));
					memset(&startupInfo, 0, sizeof(startupInfo));
					startupInfo.cb = sizeof(startupInfo);

					strcpy(cmdline, tA2->ALERTME[i][4]);
					if (strlen(cmdline) > 0) {
						strcat(cmdline, " ");
						strcat(cmdline, topicName);
						strcat(cmdline, ",");
						strcat(cmdline, pBuffer);
						if (!CreateProcess(NULL, cmdline, NULL, NULL, false, 0, NULL, NULL, &startupInfo, &processInformation)) {
							char * pErrMsg = (char *)malloc(500);
							strcpy(pErrMsg, "Unknown application ");
							strcat(pErrMsg, tA2->ALERTME[i][4]);
							MessageBox(NULL, pErrMsg, "Alert Error", MB_OK);
							free(pErrMsg);
						}
					}
				}
			}
			i++;
		}
	}
	CListCtrl *pListctrl = (CListCtrl *)tA->GetDlgItem(IDC_LIST3);
	CButton *pButton1 = (CButton*) tA->GetDlgItem(IDC_CHECK4);

	tA->SetDlgItemInt(IDC_EDIT14, ++tA2->iReceived,FALSE);
	tA->SetDlgItemInt(IDC_EDIT18, tA2->iReceived - tA2->iReceivedRemoved, FALSE);

	// Check maximum messages allowed in listbox
	
	if (pButton1->GetCheck() == BST_CHECKED) {
		int iMaxAllowed  = tA->GetDlgItemInt(IDC_EDIT13,NULL,FALSE);
		int iCurrentRows = pListctrl->GetItemCount();
		while (iCurrentRows >= iMaxAllowed) {
			// Delete oldest one
			pListctrl->DeleteItem(iCurrentRows - 1);
			tA2->iReceivedRemoved++;
			iCurrentRows--;
		}
		tA->SetDlgItemInt(IDC_EDIT15, tA2->iReceivedRemoved,FALSE);
		tA->SetDlgItemInt(IDC_EDIT18, tA2->iReceived - tA2->iReceivedRemoved,FALSE);
	}

	LVITEM lvi;
	
	lvi.mask = LVIF_TEXT;
	lvi.iItem = 0;
	
	lvi.iSubItem = 0;
	_itoa(iHash++, pHash, 10);
	lvi.pszText = pHash;
	pListctrl->InsertItem(&lvi);
	
	lvi.iSubItem = 1;
	time_t timer = time(NULL);
	lvi.pszText = ctime(&timer);
	pListctrl->SetItem(&lvi);

	lvi.iSubItem = 2;
	lvi.pszText = topicName;
	pListctrl->SetItem(&lvi);

	lvi.iSubItem = 3;
	lvi.pszText = pBuffer;
	pListctrl->SetItem(&lvi);

	lvi.iSubItem = 4;
	itoa(timer, pEnoch, 10);
	lvi.pszText = pEnoch;
	pListctrl->SetItem(&lvi);

	pListctrl->SetColumnWidth(0, LVSCW_AUTOSIZE);
	pListctrl->SetColumnWidth(1, LVSCW_AUTOSIZE);
	pListctrl->SetColumnWidth(3, LVSCW_AUTOSIZE);

	// Set Column width for topic
	int iTW = pListctrl->GetStringWidth(topicName);
	if (iTW > tA2->iTopicWidth && iTW < 400) {
		tA2->iTopicWidth = iTW;
		pListctrl->SetColumnWidth(2, iTW+15);
	}

	//Beep on receive if checkbox selected
	pButton1 = (CButton*) tA->GetDlgItem(IDC_CHECK2);
	if (pButton1->GetCheck() == BST_CHECKED) {
		Beep(300, 200);
	} 

	// See if topic is in subscriptions list
	CListBox *pListbox = (CListBox *)tA->GetDlgItem(IDC_LIST1);
	if (pListbox->FindStringExact(0, topicName) == LB_ERR) {
		pListbox->InsertString(0, topicName);
		tA->SetDlgItemText(IDC_EDIT11, "Consider saving setup");
	}
	
	free(pHash); free(pEnoch); free(pBuffer);
	MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
	
    return 1;
}