Ejemplo n.º 1
0
/*-------------------------------------------------------
                Downstream MQTTPublish
 -------------------------------------------------------*/
void GatewayControlTask::handlePublish(Event* ev, ClientNode* clnode, MQTTMessage* msg){

	MQTTPublish* mqMsg = static_cast<MQTTPublish*>(msg);
	MQTTSnPublish* snMsg = new MQTTSnPublish();

	string* tp = mqMsg->getTopic();
	uint16_t tpId;

	if(tp->size() == 2){
		tpId = getUint16((uint8_t*)tp);
		snMsg->setFlags(MQTTSN_TOPIC_TYPE_SHORT);
	}else{
		tpId = clnode->getTopics()->getTopicId(tp);
		snMsg->setFlags(MQTTSN_TOPIC_TYPE_NORMAL);
	}
	if(tpId == 0){
		/* ----- may be a publish message response of subscribed with '#' or '+' -----*/
		tpId = clnode->getTopics()->createTopic(tp);

		if(tpId > 0){
			MQTTSnRegister* regMsg = new MQTTSnRegister();
			regMsg->setTopicId(tpId);
			regMsg->setTopicName(tp);
			printf(FORMAT2, currentDateTime(), "REGISTER", RIGHTARROW, clnode->getNodeId()->c_str(), msgPrint(regMsg));

			clnode->setClientSendMessage(regMsg);
			Event* evrg = new Event();
			evrg->setClientSendEvent(clnode);
			_res->getClientSendQue()->post(evrg);   // Send Register first.
		}else{
			printf("GatewayControlTask Can't create Topic   %s\n", tp->c_str());
			return;
		}
	}

	snMsg->setTopicId(tpId);
	snMsg->setMsgId(mqMsg->getMessageId());
	snMsg->setData(mqMsg->getPayload(),mqMsg->getPayloadLength());
	snMsg->setQos(mqMsg->getQos());

	if(mqMsg->isDup()){
		snMsg->setDup();
	}

	if(mqMsg->isRetain()){
		snMsg->setDup();
	}
	clnode->setClientSendMessage(snMsg);
	printf(GREEN_FORMAT1, currentDateTime(), "PUBLISH", RIGHTARROW, clnode->getNodeId()->c_str(), msgPrint(snMsg));

	Event* ev1 = new Event();
	ev1->setClientSendEvent(clnode);
	_res->getClientSendQue()->post(ev1);

}
Ejemplo n.º 2
0
void BrokerSendTask::run(){
	Event* ev = 0;
	MQTTMessage* srcMsg = 0;
	ClientNode* clnode = 0;
	char param[TOMYFRAME_PARAM_MAX];

		if(_res->getParam("BrokerName",param) == 0){
			_host = strdup(param);
		}
		if(_res->getParam("BrokerPortNo",param) == 0){
			_service =strdup( param);
		}
		_light = _res->getLightIndicator();


	while(true){

		uint16_t length = 0;
		memset(_buffer, 0, SOCKET_MAXBUFFER_LENGTH);

		ev = _res->getBrokerSendQue()->wait();

		clnode = ev->getClientNode();
		srcMsg = clnode->getBrokerSendMessage();

		if(srcMsg->getType() == MQTT_TYPE_PUBLISH){
			MQTTPublish* msg = static_cast<MQTTPublish*>(srcMsg);
			length = msg->serialize(_buffer);
			LOGWRITE(BLUE_FORMAT, currentDateTime(), "PUBLISH", RIGHTARROW, GREEN_BROKER, msgPrint(msg));

			if(send(clnode, length) == 0){
				LOGWRITE(SEND_COMPLETE);
			}
		}else if(srcMsg->getType() == MQTT_TYPE_PUBACK){
			MQTTPubAck* msg = static_cast<MQTTPubAck*>(srcMsg);
			length = msg->serialize(_buffer);
			LOGWRITE(GREEN_FORMAT, currentDateTime(), "PUBACK", RIGHTARROW, GREEN_BROKER, msgPrint(msg));

			if(send(clnode, length) == 0){
				LOGWRITE(SEND_COMPLETE);
			}
		}else if(srcMsg->getType() == MQTT_TYPE_PUBREL){
			MQTTPubRel* msg = static_cast<MQTTPubRel*>(srcMsg);
			length = msg->serialize(_buffer);
			LOGWRITE(GREEN_FORMAT, currentDateTime(), "PUBREL", RIGHTARROW, GREEN_BROKER, msgPrint(msg));

			if(send(clnode, length) == 0){
				LOGWRITE(SEND_COMPLETE);
			}

		}else if(srcMsg->getType() == MQTT_TYPE_PINGREQ){
			MQTTPingReq* msg = static_cast<MQTTPingReq*>(srcMsg);
			length = msg->serialize(_buffer);
			LOGWRITE(FORMAT, currentDateTime(), "PINGREQ", RIGHTARROW, GREEN_BROKER, msgPrint(msg));

			if(send(clnode, length) == 0){
				LOGWRITE(SEND_COMPLETE);
			}
		}else if(srcMsg->getType() == MQTT_TYPE_SUBSCRIBE){
			MQTTSubscribe* msg = static_cast<MQTTSubscribe*>(srcMsg);
			length = msg->serialize(_buffer);
			LOGWRITE(FORMAT, currentDateTime(), "SUBSCRIBE", RIGHTARROW, GREEN_BROKER, msgPrint(msg));

			if(send(clnode, length) == 0){
				LOGWRITE(SEND_COMPLETE);
			}
		}else if(srcMsg->getType() == MQTT_TYPE_UNSUBSCRIBE){
			MQTTUnsubscribe* msg = static_cast<MQTTUnsubscribe*>(srcMsg);
			length = msg->serialize(_buffer);
			LOGWRITE(FORMAT, currentDateTime(), "UNSUBSCRIBE", RIGHTARROW, GREEN_BROKER, msgPrint(msg));

			if(send(clnode, length) == 0){
				LOGWRITE(SEND_COMPLETE);
			}
		}else if(srcMsg->getType() == MQTT_TYPE_CONNECT){
			MQTTConnect* msg = static_cast<MQTTConnect*>(srcMsg);
			length = msg->serialize(_buffer);
			LOGWRITE(FORMAT, currentDateTime(), "CONNECT", RIGHTARROW, GREEN_BROKER, msgPrint(msg));

			clnode->connectSended();
			if(send(clnode, length) == 0){
				LOGWRITE(SEND_COMPLETE);
			}
		}else if(srcMsg->getType() == MQTT_TYPE_DISCONNECT){
			MQTTDisconnect* msg = static_cast<MQTTDisconnect*>(srcMsg);
			length = msg->serialize(_buffer);
			LOGWRITE(FORMAT, currentDateTime(), "DISCONNECT", RIGHTARROW, GREEN_BROKER, msgPrint(msg));

			if(send(clnode, length) == 0){
				LOGWRITE(SEND_COMPLETE);
			}
			clnode->getStack()->disconnect();
		}

		delete ev;
	}
}
Ejemplo n.º 3
0
/*-------------------------------------------------------
 *               Upstream MQTTSnPublish
 -------------------------------------------------------*/
void GatewayControlTask::handleSnPublish(Event* ev, ClientNode* clnode, MQTTSnMessage* msg){

	printf(BLUE_FORMAT2, currentDateTime(), "PUBLISH", LEFTARROW, clnode->getNodeId()->c_str(), msgPrint(msg));

	MQTTSnPublish* sPublish = new MQTTSnPublish();
	MQTTPublish* mqMsg = new MQTTPublish();
	sPublish->absorb(msg);

	Topic* tp = clnode->getTopics()->getTopic(sPublish->getTopicId());

	if(tp || ((sPublish->getFlags() && MQTTSN_TOPIC_TYPE) == MQTTSN_TOPIC_TYPE_SHORT)){
		if(tp){
			mqMsg->setTopic(tp->getTopicName());
		}else{
			string str;
			mqMsg->setTopic(sPublish->getTopic(&str));
		}
		if(sPublish->getMsgId()){
			MQTTSnPubAck* sPuback = new MQTTSnPubAck();
			sPuback->setMsgId(sPublish->getMsgId());
			sPuback->setTopicId(sPublish->getTopicId());
			if(clnode->getWaitedPubAck()){
				delete clnode->getWaitedPubAck();
			}
			clnode->setWaitedPubAck(sPuback);

			mqMsg->setMessageId(sPublish->getMsgId());
		}

		mqMsg->setQos(sPublish->getQos());

		if(sPublish->getFlags() && MQTTSN_FLAG_DUP){
			mqMsg->setDup();
		}
		if(sPublish->getFlags() && MQTTSN_FLAG_RETAIN){
			mqMsg->setRetain();
		}

		mqMsg->setPayload(sPublish->getData() , sPublish->getDataLength());

		clnode->setBrokerSendMessage(mqMsg);
		Event* ev1 = new Event();
		ev1->setBrokerSendEvent(clnode);
		_res->getBrokerSendQue()->post(ev1);

	}else{
		if(sPublish->getMsgId()){
			MQTTSnPubAck* sPuback = new MQTTSnPubAck();
			sPuback->setMsgId(sPublish->getMsgId());
			sPuback->setTopicId(sPublish->getTopicId());
			sPuback->setReturnCode(MQTTSN_RC_REJECTED_INVALID_TOPIC_ID);

			clnode->setClientSendMessage(sPuback);

			Event* ev1 = new Event();
			ev1->setClientSendEvent(clnode);
			printf(BLUE_FORMAT1, currentDateTime(), "PUBACK", RIGHTARROW, clnode->getNodeId()->c_str(), msgPrint(sPuback));

			_res->getClientSendQue()->post(ev1);  // Send PubAck INVALID_TOPIC_ID
		}
	}
	delete sPublish;
}
Ejemplo n.º 4
0
void BrokerSendTask::run(){
	Event* ev = NULL;
	MQTTMessage* srcMsg = NULL;
	ClientNode* clnode = NULL;

	uint8_t buffer[SOCKET_MAXBUFFER_LENGTH];

	string host = BROKER_HOST_NAME;

	int port = BROKER_PORT;

	if(_res->getArgc() > ARGV_BROKER_ADDR){
		host = _res->getArgv()[ARGV_BROKER_ADDR];
	}
	if(_res->getArgc() > ARGV_BROKER_PORT){
		port = atoi(_res->getArgv()[ARGV_BROKER_PORT]);
	}

	while(true){

		uint16_t length = 0;
		memset(buffer, 0, SOCKET_MAXBUFFER_LENGTH);

		ev = _res->getBrokerSendQue()->wait();

		clnode = ev->getClientNode();
		srcMsg = clnode->getBrokerSendMessage();

		if(srcMsg->getType() == MQTT_TYPE_PUBLISH){
			MQTTPublish* msg = static_cast<MQTTPublish*>(srcMsg);
			length = msg->serialize(buffer);
			D_MQTT("%s PUBLISH      --->    Broker    %s\n", currentDateTime(), msgPrint(buffer,msg));
		}
		else if(srcMsg->getType() == MQTT_TYPE_PUBACK){
			MQTTPubAck* msg = static_cast<MQTTPubAck*>(srcMsg);
			length = msg->serialize(buffer);
			D_MQTT("%s PUBACK       --->    Broker    %s\n", currentDateTime(), msgPrint(buffer,msg));
		}
		else if(srcMsg->getType() == MQTT_TYPE_PINGREQ){
			MQTTPingReq* msg = static_cast<MQTTPingReq*>(srcMsg);
			length = msg->serialize(buffer);
			D_MQTT("%s PINGREQ      --->    Broker    %s\n", currentDateTime(), msgPrint(buffer,msg));
		}
		else if(srcMsg->getType() == MQTT_TYPE_SUBSCRIBE){
			MQTTSubscribe* msg = static_cast<MQTTSubscribe*>(srcMsg);
			length = msg->serialize(buffer);
			D_MQTT("%s SUBSCRIBE    --->    Broker    %s\n", currentDateTime(), msgPrint(buffer,msg));
		}
		else if(srcMsg->getType() == MQTT_TYPE_UNSUBSCRIBE){
			MQTTUnsubscribe* msg = static_cast<MQTTUnsubscribe*>(srcMsg);
			length = msg->serialize(buffer);
			D_MQTT("%s UNSUBSCRIBE  --->    Broker    %s\n", currentDateTime(), msgPrint(buffer,msg));
		}
		else if(srcMsg->getType() == MQTT_TYPE_CONNECT){
			MQTTConnect* msg = static_cast<MQTTConnect*>(srcMsg);
			length = msg->serialize(buffer);
			D_MQTT("%s CONNECT      --->    Broker    %s\n", currentDateTime(), msgPrint(buffer,msg));
		}
		else if(srcMsg->getType() == MQTT_TYPE_DISCONNECT){
			MQTTDisconnect* msg = static_cast<MQTTDisconnect*>(srcMsg);
			length = msg->serialize(buffer);
			D_MQTT("%s DISCONNECT   --->    Broker    %s\n", currentDateTime(), msgPrint(buffer,msg));
		}

		int rc = 0;

		if(length > 0){
			if( clnode->getSocket()->isValid()){
				rc = clnode->getSocket()->send(buffer, length);
				if(rc == -1){
					clnode->getSocket()->disconnect();
					D_MQTT("       Socket is valid. but can't send Client:%s\n", clnode->getNodeId()->c_str());
				}
			}else{
				if(clnode->getSocket()->create()){
					if(clnode->getSocket()->connect(host, port)){
						rc = clnode->getSocket()->send(buffer, length);
						if(rc == -1){
							clnode->getSocket()->disconnect();
							D_MQTT("       Socket is created. but can't send, Client:%s\n", clnode->getNodeId()->c_str());
						}
					}else{
						D_MQTT("%s Can't connect socket Client:%s\n",
								currentDateTime(), clnode->getNodeId()->c_str());
					}
				}else{
					D_MQTT("%s Can't create socket Client:%s\n",
						currentDateTime(), clnode->getNodeId()->c_str());
				}
			}
		}
		delete ev;
	}
}