예제 #1
0
/*-------------------------------------------------------
                Upstream MQTTSnPubAck
 -------------------------------------------------------*/
void GatewayControlTask::handleSnPubAck(Event* ev, ClientNode* clnode, MQTTSnMessage* msg){

	printf(GREEN_FORMAT1, currentDateTime(), "PUBACK", LEFTARROW, clnode->getNodeId()->c_str(), msgPrint(msg));

	MQTTSnPubAck* sPubAck = new MQTTSnPubAck();
	MQTTPubAck* pubAck = new MQTTPubAck();
	sPubAck->absorb(msg);

	pubAck->setMessageId(sPubAck->getMsgId());

	clnode->setBrokerSendMessage(pubAck);
	Event* ev1 = new Event();
	ev1->setBrokerSendEvent(clnode);
	_res->getBrokerSendQue()->post(ev1);
	delete sPubAck;
}
예제 #2
0
/*-------------------------------------------------------
                Downstream MQTTPubAck
 -------------------------------------------------------*/
void GatewayControlTask::handlePuback(Event* ev, ClientNode* clnode, MQTTMessage* msg){

	MQTTPubAck* mqMsg = static_cast<MQTTPubAck*>(msg);
	MQTTSnPubAck* snMsg = clnode->getWaitedPubAck();

	if(snMsg){
		printf(BLUE_FORMAT1, currentDateTime(), "PUBACK", RIGHTARROW, clnode->getNodeId()->c_str(), msgPrint(snMsg));

		if(snMsg->getMsgId() == mqMsg->getMessageId()){
			clnode->setWaitedPubAck(0);
			clnode->setClientSendMessage(snMsg);
			Event* ev1 = new Event();
			ev1->setClientSendEvent(clnode);
			_res->getClientSendQue()->post(ev1);
		}
	}
}
예제 #3
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;
	}
}
예제 #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;
	}
}