diff --git a/MQTTSNGateway/predefinedTopic.conf b/MQTTSNGateway/predefinedTopic.conf index 95d23ea..4ace6b1 100644 --- a/MQTTSNGateway/predefinedTopic.conf +++ b/MQTTSNGateway/predefinedTopic.conf @@ -16,14 +16,13 @@ # # ClientID, TopicName, TopicID # -# This file is consist from two sections. -# One for QoS-1 PUBLISH Clients, the other for another clients. +# Topics is common to all clients, ClientID should be COMMON. # # pre-defined-topics for Clients # -GatewayTestClient,ty4tw/predefinedTopic1, 1 +COMMON,ty4tw/predefinedTopic1, 1 GatewayTestClient,ty4tw/predefinedTopic2, 2 GatewayTestClient,ty4tw/predefinedTopic3, 3 @@ -31,16 +30,7 @@ GatewayTestClient,ty4tw/predefinedTopic3, 3 # pre-defined-topics for QoS-1 clients. # -QoS-1_Client01,ty4tw/proxy/predefTopic1, 1 -QoS-1_Client01,ty4tw/proxy/predefTopic2, 2 -QoS-1_Client01,ty4tw/proxy/predefTopic3, 3 - -QoS-1_Client02,ty4tw/proxy/predefTopic1, 1 -QoS-1_Client02,ty4tw/proxy/predefTopic3, 2 -QoS-1_Client02,ty4tw/proxy/predefTopic3, 3 - -QoS-1_Client03,ty4tw/proxy/predefTopic1, 1 -QoS-1_Client03,ty4tw/proxy/predefTopic2, 2 -QoS-1_Client03,ty4tw/proxy/predefTopic3, 3 - +QoS-1_Client03,ty4tw/proxy/predefTopic4, 1 +QoS-1_Client03,ty4tw/proxy/predefTopic5, 2 +QoS-1_Client03,ty4tw/proxy/predefTopic6, 3 diff --git a/MQTTSNGateway/src/MQTTSNGWClientList.cpp b/MQTTSNGateway/src/MQTTSNGWClientList.cpp index 488944b..2e4bf92 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientList.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientList.cpp @@ -24,6 +24,8 @@ extern Gateway* theGateway; /*===================================== Class ClientList =====================================*/ +const char* common_topic = "COMMON"; + ClientList::ClientList() { _clientCnt = 0; @@ -414,50 +416,58 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId, Client* ClientList::createPredefinedTopic( MQTTSNString* clientId, string topicName, uint16_t topicId, bool aggregate) { - Client* client = getClient(clientId); + if ( clientId->cstring == common_topic ) + { + _gateway->getTopics()->add((const char*)topicName.c_str(), topicId); + return 0; + } + else + { + Client* client = getClient(clientId); - if ( _authorize && client == nullptr) - { - return 0; - } + if ( _authorize && client == nullptr ) + { + return 0; + } - /* anonimous clients */ - if ( _clientCnt > MAX_CLIENTS ) - { - return nullptr; // full of clients - } + /* anonimous clients */ + if ( _clientCnt > MAX_CLIENTS ) + { + return nullptr; // full of clients + } - if ( client == nullptr ) - { - /* creat a new client */ - client = new Client(); - client->setClientId(*clientId); - if ( aggregate ) - { - client->setAggregated(); - } - _mutex.lock(); + if ( client == nullptr ) + { + /* creat a new client */ + client = new Client(); + client->setClientId(*clientId); + if ( aggregate ) + { + client->setAggregated(); + } + _mutex.lock(); - /* add the list */ - if ( _firstClient == nullptr ) - { - _firstClient = client; - _endClient = client; - } - else - { - _endClient->_nextClient = client; - client->_prevClient = _endClient; - _endClient = client; - } - _clientCnt++; - _mutex.unlock(); - } + /* add the list */ + if ( _firstClient == nullptr ) + { + _firstClient = client; + _endClient = client; + } + else + { + _endClient->_nextClient = client; + client->_prevClient = _endClient; + _endClient = client; + } + _clientCnt++; + _mutex.unlock(); + } - // create Topic & Add it - client->getTopics()->add((const char*)topicName.c_str(), topicId); - client->_hasPredefTopic = true; - return client; + // create Topic & Add it + client->getTopics()->add((const char*)topicName.c_str(), topicId); + client->_hasPredefTopic = true; + return client; + } } uint16_t ClientList::getClientCount() diff --git a/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp b/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp index dd01c50..3162f04 100644 --- a/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp +++ b/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp @@ -79,10 +79,18 @@ MQTTGWPacket* MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket* else { topic = client->getTopics()->getTopicById(&topicid); + if ( !topic ) + { + topic = _gateway->getTopics()->getTopicById(&topicid); + if ( topic ) + { + topic = client->getTopics()->add(topic->getTopicName()->c_str(), topic->getTopicId()); + } + } if( !topic && qos == 3 ) { - WRITELOG("%s Invali TopicId.%s %s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER); + WRITELOG("%s Invalid TopicId.%s %s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER); return nullptr; } diff --git a/MQTTSNGateway/src/MQTTSNGateway.cpp b/MQTTSNGateway/src/MQTTSNGateway.cpp index 46d4281..c5894ce 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.cpp +++ b/MQTTSNGateway/src/MQTTSNGateway.cpp @@ -37,6 +37,7 @@ Gateway::Gateway(void) _packetEventQue.setMaxSize(MAX_INFLIGHTMESSAGES * MAX_CLIENTS); _clientList = new ClientList(); _adapterManager = new AdapterManager(this); + _topics = new Topics(); } Gateway::~Gateway() @@ -103,6 +104,11 @@ Gateway::~Gateway() { delete _clientList; } + + if ( _topics ) + { + delete _topics; + } } int Gateway::getParam(const char* parameter, char* value) @@ -312,6 +318,11 @@ AdapterManager* Gateway::getAdapterManager(void) return _adapterManager; } +Topics* Gateway::getTopics(void) +{ + return _topics; +} + bool Gateway::hasSecureConnection(void) { return ( _params.certKey diff --git a/MQTTSNGateway/src/MQTTSNGateway.h b/MQTTSNGateway/src/MQTTSNGateway.h index b4c0f5c..d9b4857 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.h +++ b/MQTTSNGateway/src/MQTTSNGateway.h @@ -191,6 +191,7 @@ public: AdapterManager* getAdapterManager(void); int getParam(const char* parameter, char* value); bool hasSecureConnection(void); + Topics* getTopics(void); private: GatewayParams _params; @@ -201,6 +202,7 @@ private: LightIndicator _lightIndicator; SensorNetwork _sensorNetwork; AdapterManager* _adapterManager {nullptr}; + Topics* _topics; }; }