Beispiel #1
0
/*-------------------------------------------------------
                Upstream MQTTSnConnect
 -------------------------------------------------------*/
void GatewayControlTask::handleSnConnect(Event* ev, ClientNode* clnode, MQTTSnMessage* msg){

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

	Topics* topics = clnode->getTopics();
	MQTTSnConnect* sConnect = new MQTTSnConnect();
	MQTTConnect* mqMsg = new MQTTConnect();
	mqMsg->setProtocol(_protocol);
	sConnect->absorb(msg);

	mqMsg->setClientId(clnode->getNodeId());
	mqMsg->setKeepAliveTime(sConnect->getDuration());

	if(_loginId != "" && _password != ""){
		mqMsg->setUserName(&_loginId);
		mqMsg->setPassword(&_password);
	}
	clnode->setConnectMessage(mqMsg);

	if(sConnect->isCleanSession()){
		if(topics){
			delete topics;
		}
		topics = new Topics();
		clnode->setTopics(topics);
		mqMsg->setCleanSessionFlg();
	}

	if(sConnect->isWillRequired()){
		MQTTSnWillTopicReq* reqTopic = new MQTTSnWillTopicReq();

		Event* evwr = new Event();

		clnode->setClientSendMessage(reqTopic);
		evwr->setClientSendEvent(clnode);
		printf(FORMAT1, currentDateTime(), "WILLTOPICREQ", RIGHTARROW, clnode->getNodeId()->c_str(), msgPrint(reqTopic));

		_res->getClientSendQue()->post(evwr);  // Send WILLTOPICREQ to Client
	}else{
		Event* ev1 = new Event();
		clnode->setBrokerSendMessage(clnode->getConnectMessage());
		ev1->setBrokerSendEvent(clnode);
		_res->getBrokerSendQue()->post(ev1);
	}
	delete sConnect;
}
void ClientNode::setKeepAlive(MQTTSnMessage* msg){
	MQTTSnConnect* cm = static_cast<MQTTSnConnect*>(msg);
	_keepAliveMsec = cm->getDuration() * 1000UL;
	_keepAliveTimer.start(_keepAliveMsec * 1.5);
}