From 7ac0d89fb11b345d107b49f80060b7448c1e436c Mon Sep 17 00:00:00 2001 From: tomoaki Date: Wed, 18 Jan 2017 09:54:07 +0900 Subject: [PATCH] BugFix of Issue #45 Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTGWPublishHandler.cpp | 121 +++++++++++---------- 1 file changed, 61 insertions(+), 60 deletions(-) diff --git a/MQTTSNGateway/src/MQTTGWPublishHandler.cpp b/MQTTSNGateway/src/MQTTGWPublishHandler.cpp index 9497414..67d3df7 100644 --- a/MQTTSNGateway/src/MQTTGWPublishHandler.cpp +++ b/MQTTSNGateway/src/MQTTGWPublishHandler.cpp @@ -63,75 +63,76 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet) topicId.data.long_.len = pub.topiclen; topicId.data.long_.name = pub.topic; id = client->getTopics()->getTopicId(&topicId); - } - if (id > 0) - { - topicId.data.id = id; - } - else - { - /* This message might be subscribed with wild card. */ - Topic* topic = client->getTopics()->match(&topicId); - if (topic == 0) + if ( id > 0 ) { - WRITELOG(" Invalid Topic. PUBLISH message is canceled.\n"); - if (pub.header.bits.qos == 1) - { - replyACK(client, &pub, PUBACK); - } - else if ( pub.header.bits.qos == 2 ) - { - replyACK(client, &pub, PUBREC); - } - return; - } - - /* add the Topic and get a TopicId */ - topic = client->getTopics()->add(&topicId); - id = topic->getTopicId(); - - if (id > 0) - { - /* create REGACK */ - MQTTSNPacket* regPacket = new MQTTSNPacket(); - - MQTTSNString topicName; - topicName.lenstring.len = topicId.data.long_.len; - topicName.lenstring.data = topicId.data.long_.name; - - uint16_t regackMsgId = client->getNextSnMsgId(); - regPacket->setREGISTER(id, regackMsgId, &topicName); - - if (client->isSleep()) - { - client->setClientSleepPacket(regPacket); - WRITELOG(FORMAT_BL_NL, currentDateTime(), regPacket->getName(), - RIGHTARROW, client->getClientId(), "is sleeping. REGISTER was saved."); - } - else if (client->isActive()) - { - /* send REGISTER */ - Event* evrg = new Event(); - evrg->setClientSendEvent(client, regPacket); - _gateway->getClientSendQue()->post(evrg); - } - - /* send PUBLISH */ topicId.data.id = id; - snPacket->setPUBLISH((uint8_t) pub.header.bits.dup, (int) pub.header.bits.qos, - (uint8_t) pub.header.bits.retain, (uint16_t) pub.msgId, topicId, (uint8_t*) pub.payload, - pub.payloadlen); - client->getWaitREGACKPacketList()->setPacket(snPacket, regackMsgId); } else { - WRITELOG("\x1b[0m\x1b[31mMQTTGWPublishHandler Can't create a Topic.\n"); - return; + /* This message might be subscribed with wild card. */ + Topic* topic = client->getTopics()->match(&topicId); + if (topic == 0) + { + WRITELOG(" Invalid Topic. PUBLISH message is canceled.\n"); + if (pub.header.bits.qos == 1) + { + replyACK(client, &pub, PUBACK); + } + else if ( pub.header.bits.qos == 2 ) + { + replyACK(client, &pub, PUBREC); + } + return; + } + + /* add the Topic and get a TopicId */ + topic = client->getTopics()->add(&topicId); + id = topic->getTopicId(); + + if (id > 0) + { + /* create REGACK */ + MQTTSNPacket* regPacket = new MQTTSNPacket(); + + MQTTSNString topicName; + topicName.lenstring.len = topicId.data.long_.len; + topicName.lenstring.data = topicId.data.long_.name; + + uint16_t regackMsgId = client->getNextSnMsgId(); + regPacket->setREGISTER(id, regackMsgId, &topicName); + + if (client->isSleep()) + { + client->setClientSleepPacket(regPacket); + WRITELOG(FORMAT_BL_NL, currentDateTime(), regPacket->getName(), + RIGHTARROW, client->getClientId(), "is sleeping. REGISTER was saved."); + } + else if (client->isActive()) + { + /* send REGISTER */ + Event* evrg = new Event(); + evrg->setClientSendEvent(client, regPacket); + _gateway->getClientSendQue()->post(evrg); + } + + /* send PUBLISH */ + topicId.data.id = id; + snPacket->setPUBLISH((uint8_t) pub.header.bits.dup, (int) pub.header.bits.qos, + (uint8_t) pub.header.bits.retain, (uint16_t) pub.msgId, topicId, (uint8_t*) pub.payload, + pub.payloadlen); + client->getWaitREGACKPacketList()->setPacket(snPacket, regackMsgId); + } + else + { + WRITELOG("\x1b[0m\x1b[31mMQTTGWPublishHandler Can't create a Topic.\n"); + delete snPacket; + return; + } } } - /* TopicId was aquired. */ + /* TopicId was acquired. */ if (client->isSleep()) { /* client is sleeping. save PUBLISH */