Пример #1
0
/*-------------------------------------------------------
                Upstream MQTTSnUnsubscribe
 -------------------------------------------------------*/
void GatewayControlTask::handleSnUnsubscribe(Event* ev, ClientNode* clnode, MQTTSnMessage* msg){

	printf(FORMAT2, currentDateTime(), "UNSUBSCRIBE", LEFTARROW, clnode->getNodeId()->c_str(), msgPrint(msg));

	MQTTSnUnsubscribe* sUnsubscribe = new MQTTSnUnsubscribe();
	MQTTUnsubscribe* unsubscribe = new MQTTUnsubscribe();
	sUnsubscribe->absorb(msg);

	uint8_t topicIdType = sUnsubscribe->getFlags() & 0x03;

	unsubscribe->setMessageId(sUnsubscribe->getMsgId());

	if(topicIdType != MQTTSN_FLAG_TOPICID_TYPE_RESV){

		if(topicIdType == MQTTSN_FLAG_TOPICID_TYPE_SHORT){
			unsubscribe->setTopicName(sUnsubscribe->getTopicName()); // TopicName

		}else if(clnode->getTopics()->getTopic(sUnsubscribe->getTopicId())){

			if(topicIdType == MQTTSN_FLAG_TOPICID_TYPE_PREDEFINED) goto uslbl1;

			unsubscribe->setTopicName(sUnsubscribe->getTopicName());
		}

		clnode->setBrokerSendMessage(unsubscribe);

		Event* ev1 = new Event();
		ev1->setBrokerSendEvent(clnode);
		_res->getBrokerSendQue()->post(ev1);    //  UNSUBSCRIBE to Broker
		delete sUnsubscribe;
		return;
	}

	/*-- Irregular TopicIdType or MQTTSN_FLAG_TOPICID_TYPE_PREDEFINED --*/
uslbl1:
	if(sUnsubscribe->getMsgId()){
		MQTTSnUnsubAck* sUnsuback = new MQTTSnUnsubAck();
		sUnsuback->setMsgId(sUnsubscribe->getMsgId());

		clnode->setClientSendMessage(sUnsuback);

		Event* evun = new Event();
		evun->setClientSendEvent(clnode);
		printf(FORMAT1, currentDateTime(), "UNSUBACK", RIGHTARROW, clnode->getNodeId()->c_str(), msgPrint(sUnsuback));

		_res->getClientSendQue()->post(evun);  // Send UNSUBACK to Client
	}

	delete sUnsubscribe;
}
Пример #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;
	}
}
Пример #3
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;
	}
}