From 19c4d8208ecf76d61fed17bb694e1cf95e3c37c7 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 25 Oct 2016 21:10:22 +0900 Subject: [PATCH] BugFix of Wildcard of Topic Issue #40 Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWClient.cpp | 118 ++++++----- MQTTSNGateway/src/MQTTSNGWClient.h | 4 - MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp | 16 +- MQTTSNGateway/src/MQTTSNGWPacket.cpp | 5 +- MQTTSNGateway/src/MQTTSNGateway.h | 2 +- MQTTSNGateway/src/linux/udp/SensorNetwork.cpp | 1 - .../src/linux/xbee/SensorNetwork.cpp | 10 +- MQTTSNGateway/src/linux/xbee/SensorNetwork.h | 3 +- ...stProcessFramework.cpp => TestProcess.cpp} | 114 ++++------- .../{TestProcessFramework.h => TestProcess.h} | 25 +-- MQTTSNGateway/src/tests/TestQue.cpp | 101 ++++++++++ MQTTSNGateway/src/tests/TestQue.h | 41 ++++ MQTTSNGateway/src/tests/TestTask.cpp | 13 +- MQTTSNGateway/src/tests/TestTask.h | 6 +- MQTTSNGateway/src/tests/TestTopicIdMap.cpp | 125 ++++++++++++ MQTTSNGateway/src/tests/TestTopicIdMap.h | 32 +++ MQTTSNGateway/src/tests/TestTopics.cpp | 183 ++++++++++++++++++ MQTTSNGateway/src/tests/TestTopics.h | 32 +++ ...ocessFramework.cpp => mainTestProcess.cpp} | 12 +- Makefile | 7 +- 20 files changed, 667 insertions(+), 183 deletions(-) rename MQTTSNGateway/src/tests/{TestProcessFramework.cpp => TestProcess.cpp} (64%) rename MQTTSNGateway/src/tests/{TestProcessFramework.h => TestProcess.h} (71%) create mode 100644 MQTTSNGateway/src/tests/TestQue.cpp create mode 100644 MQTTSNGateway/src/tests/TestQue.h create mode 100644 MQTTSNGateway/src/tests/TestTopicIdMap.cpp create mode 100644 MQTTSNGateway/src/tests/TestTopicIdMap.h create mode 100644 MQTTSNGateway/src/tests/TestTopics.cpp create mode 100644 MQTTSNGateway/src/tests/TestTopics.h rename MQTTSNGateway/src/tests/{mainTestProcessFramework.cpp => mainTestProcess.cpp} (81%) diff --git a/MQTTSNGateway/src/MQTTSNGWClient.cpp b/MQTTSNGateway/src/MQTTSNGWClient.cpp index 74b07ac..55fb3b2 100644 --- a/MQTTSNGateway/src/MQTTSNGWClient.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClient.cpp @@ -703,75 +703,95 @@ uint16_t Topic::getTopicId(void) return _topicId; } -int Topic::hasWildCard(unsigned int* pos) -{ - unsigned int p = _topicName->find("+", 0); - if (p != string::npos) - { - *pos = p; - return 1; - } - else - { - string::iterator it = _topicName->end(); - if (*it == '#') - { - *pos = _topicName->size() - 1; - return 2; - } - } - *pos = 0; - return 0; -} - bool Topic::isMatch(string* topicName) { - unsigned int pos; - - if (topicName->size() < _topicName->size()) + string::size_type tlen = _topicName->size(); + if (topicName->size() < tlen - 2) { return false; } - if (hasWildCard(&pos) == 1) + string::size_type tpos = 0; + string::size_type tloc = 0; + string::size_type pos = 0; + string::size_type loc = 0; + string wildcard = "#"; + string wildcards = "+"; + + while(true) { - if (_topicName->compare(0, pos - 1, *topicName, 0, pos - 1) == 0) + loc = topicName->find('/', pos); + tloc = _topicName->find('/', tpos); + + if ( loc != string::npos && tloc != string::npos ) { - if (_topicName->compare(pos + 1, 1, "/") == 0) + string subtopic = topicName->substr(pos, loc - pos); + string subtopict = _topicName->substr(tpos, tloc - tpos); + if (subtopict == wildcard) { - unsigned int loc = topicName->find('/', pos); - if (loc != 0) + return true; + } + else if (subtopict == wildcards) + { + if ( (tpos = tloc + 1 ) > tlen ) { - if (_topicName->compare(pos + 1, _topicName->size() - pos - 1, *topicName, loc, - topicName->size() - pos - 1) == 0) + pos = loc + 1; + loc = topicName->find('/', pos); + if ( loc == string::npos ) { return true; } - } - } - else - { - unsigned int loc = _topicName->find(pos, '/'); - if (loc != 0) - { - if (topicName->find('/', loc) != 0) + else { return false; } } + pos = loc + 1; + } + else if ( subtopic != subtopict ) + { + return false; + } + else + { + if ( (tpos = tloc + 1) > tlen ) + { + return false; + } + + pos = loc + 1; + } + } + else if ( loc == string::npos && tloc == string::npos ) + { + string subtopic = topicName->substr(pos); + string subtopict = _topicName->substr(tpos); + if ( subtopict == wildcard || subtopict == wildcards) + { + return true; + } + else if ( subtopic == subtopict ) + { + return true; + } + else + { + return false; + } + } + else if ( loc != string::npos && tloc == string::npos ) + { + tloc = _topicName->find('#', --tpos); + if ( tloc == string::npos ) + { + return false; + } + else + { + return true; } - return true; } } - else if (hasWildCard(&pos) == 2 && (_topicName->compare(0, pos, *topicName, 0, pos) == 0)) - { - return true; - } - else if (_topicName->compare(*topicName) == 0) - { - return true; - } - return false; } /*===================================== diff --git a/MQTTSNGateway/src/MQTTSNGWClient.h b/MQTTSNGateway/src/MQTTSNGWClient.h index d210fc3..396ff43 100644 --- a/MQTTSNGateway/src/MQTTSNGWClient.h +++ b/MQTTSNGateway/src/MQTTSNGWClient.h @@ -109,10 +109,8 @@ public: Topic(); Topic(string* topic); ~Topic(); - string* getTopicName(void); uint16_t getTopicId(void); - int hasWildCard(unsigned int* pos); bool isMatch(string* topicName); private: @@ -167,12 +165,10 @@ public: TopicIdMap(); ~TopicIdMap(); uint16_t getTopicId(uint16_t msgId, MQTTSN_topicTypes* type); - Topic* getTopic(MQTTSN_topicTypes type); int add(uint16_t msgId, uint16_t topicId, MQTTSN_topicTypes type); void erase(uint16_t msgId); void clear(void); private: - int find(uint16_t msgId); uint16_t* _msgIds; TopicIdMapelement* _first; TopicIdMapelement* _end; diff --git a/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp index 1ba6398..62010e1 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp @@ -40,6 +40,7 @@ void ClientSendTask::run() { Client* client = 0; MQTTSNPacket* packet = 0; + int rc = 0; while (true) { @@ -55,20 +56,27 @@ void ClientSendTask::run() { client = ev->getClient(); packet = ev->getMQTTSNPacket(); - packet->unicast(_sensorNetwork, client->getSensorNetAddress()); + log(client, packet); + rc = packet->unicast(_sensorNetwork, client->getSensorNetAddress()); } else if (ev->getEventType() == EtBroadcast) { packet = ev->getMQTTSNPacket(); - packet->broadcast(_sensorNetwork); + log(client, packet); + rc = packet->broadcast(_sensorNetwork); } else if (ev->getEventType() == EtSensornetSend) { packet = ev->getMQTTSNPacket(); - packet->unicast(_sensorNetwork, ev->getSensorNetAddress()); + log(client, packet); + rc = packet->unicast(_sensorNetwork, ev->getSensorNetAddress()); } - log(client, packet); + if ( rc < 0 ) + { + WRITELOG("%s ClientSendTask can't send a packet to the client.\n", + ERRMSG_HEADER, (client ? (const char*)client->getClientId() : UNKNOWNCL ), ERRMSG_FOOTER); + } delete ev; } } diff --git a/MQTTSNGateway/src/MQTTSNGWPacket.cpp b/MQTTSNGateway/src/MQTTSNGWPacket.cpp index 23ec9f9..74c00fb 100644 --- a/MQTTSNGateway/src/MQTTSNGWPacket.cpp +++ b/MQTTSNGateway/src/MQTTSNGWPacket.cpp @@ -334,7 +334,10 @@ int MQTTSNPacket::getPINGREQ(void) int MQTTSNPacket::getDISCONNECT(uint16_t* duration) { - return MQTTSNDeserialize_disconnect((int*) duration, _buf, _bufLen); + int dur = 0; + int rc = MQTTSNDeserialize_disconnect(&dur, _buf, _bufLen); + *duration = (uint16_t)dur; + return rc; } int MQTTSNPacket::getWILLTOPICUPD(uint8_t* willQoS, uint8_t* willRetain, MQTTSNString* willTopic) diff --git a/MQTTSNGateway/src/MQTTSNGateway.h b/MQTTSNGateway/src/MQTTSNGateway.h index 210c11a..c5bdc1c 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.h +++ b/MQTTSNGateway/src/MQTTSNGateway.h @@ -25,7 +25,7 @@ namespace MQTTSNGW /*================================= * Starting prompt ==================================*/ -#define GATEWAY_VERSION " * Version: 0.9.2" +#define GATEWAY_VERSION " * Version: 0.9.3" #define PAHO_COPYRIGHT0 " * MQTT-SN Transparent Gateway" #define PAHO_COPYRIGHT1 " * Part of Project Paho in Eclipse" diff --git a/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp b/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp index 2f820fe..c64a0a2 100644 --- a/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp +++ b/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp @@ -301,7 +301,6 @@ int UDPPort::unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* addr dest.sin_family = AF_INET; dest.sin_port = addr->getPortNo(); dest.sin_addr.s_addr = addr->getIpAddress(); - ; int status = ::sendto(_sockfdUnicast, buf, length, 0, (const sockaddr*) &dest, sizeof(dest)); if (status < 0) diff --git a/MQTTSNGateway/src/linux/xbee/SensorNetwork.cpp b/MQTTSNGateway/src/linux/xbee/SensorNetwork.cpp index 9ee5d20..1152890 100644 --- a/MQTTSNGateway/src/linux/xbee/SensorNetwork.cpp +++ b/MQTTSNGateway/src/linux/xbee/SensorNetwork.cpp @@ -207,13 +207,11 @@ int XBee::open(char* device, int baudrate) int XBee::broadcast(const uint8_t* payload, uint16_t payloadLen){ SensorNetAddress addr; addr.setBroadcastAddress(); - send(payload, (uint8_t) payloadLen, &addr); - return 1; + return send(payload, (uint8_t) payloadLen, &addr); } int XBee:: unicast(const uint8_t* payload, uint16_t payloadLen, SensorNetAddress* addr){ - send(payload, (uint8_t) payloadLen, addr); - return 1; + return send(payload, (uint8_t) payloadLen, addr); } int XBee::recv(uint8_t* buf, uint16_t bufLen, SensorNetAddress* clientAddr) @@ -355,14 +353,14 @@ int XBee::send(const uint8_t* payload, uint8_t pLen, SensorNetAddress* addr){ D_NWSTACK("\r\n"); /* wait Txim Status 0x8B */ - _sem.timedwait(5000); // 5sec + _sem.timedwait(XMIT_STATUS_TIME_OVER); if ( _respCd || _frameId != _respId ) { D_NWSTACK(" frameId = %02x Not Acknowleged\r\n", _frameId); return -1; } - return 0; + return (int)pLen; } void XBee::send(uint8_t c) diff --git a/MQTTSNGateway/src/linux/xbee/SensorNetwork.h b/MQTTSNGateway/src/linux/xbee/SensorNetwork.h index 0861d2b..202f08b 100644 --- a/MQTTSNGateway/src/linux/xbee/SensorNetwork.h +++ b/MQTTSNGateway/src/linux/xbee/SensorNetwork.h @@ -38,7 +38,8 @@ namespace MQTTSNGW #define API_MODEMSTATUS 0x8A #define API_XMITSTATUS 0x8B -#define START_BYTE 0x7e +#define XMIT_STATUS_TIME_OVER 5000 + #define ESCAPE 0x7d #define XON 0x11 #define XOFF 0x13 diff --git a/MQTTSNGateway/src/tests/TestProcessFramework.cpp b/MQTTSNGateway/src/tests/TestProcess.cpp similarity index 64% rename from MQTTSNGateway/src/tests/TestProcessFramework.cpp rename to MQTTSNGateway/src/tests/TestProcess.cpp index 0aef1e1..f460335 100644 --- a/MQTTSNGateway/src/tests/TestProcessFramework.cpp +++ b/MQTTSNGateway/src/tests/TestProcess.cpp @@ -16,7 +16,10 @@ #include #include -#include "TestProcessFramework.h" +#include "TestProcess.h" +#include "TestTopics.h" +#include "TestQue.h" +#include "TestTopicIdMap.h" #include "MQTTSNGWProcess.h" #include "MQTTSNGWClient.h" #include "MQTTSNGWPacket.h" @@ -31,18 +34,18 @@ using namespace MQTTSNGW; const char* currentDateTime(void); -TestProcessFramework::TestProcessFramework() +TestProcess::TestProcess() { theMultiTaskProcess = this; theProcess = this; } -TestProcessFramework::~TestProcessFramework() +TestProcess::~TestProcess() { } -void TestProcessFramework::initialize(int argc, char** argv) +void TestProcess::initialize(int argc, char** argv) { MultiTaskProcess::initialize(argc, argv); assert(0 == strcmp(CONFDIR, getConfigDirName()->c_str())); @@ -50,63 +53,28 @@ void TestProcessFramework::initialize(int argc, char** argv) resetRingBuffer(); } -void TestProcessFramework::run(void) +void TestProcess::run(void) { char value[256]; - int* v = 0; int i = 0; Timer tm; - TestQue que; + + /* Test command line parameter */ assert(1 == getArgc() || 3 == getArgc() ); assert(0 == strcmp(ARGV, *getArgv())); getParam("BrokerName", value); assert(0 == strcmp("iot.eclipse.org", value)); + /* Test RingBuffer */ for ( i = 0; i < 1000; i++) { putLog("Test RingBuffer %d ", 1234567890); } - putLog("\n\nRingBuffer Test complieted.\n"); - - for ( i = 0; i < 10; i++ ) - { - v = new int(i); - que.post(v); - } - assert( 10 == que.size()); - - for ( i = 0; i < 10; i++ ) - { - assert(i == *que.front()); - int* p = que.front(); - if ( p ) - { - assert(i == *p); - que.pop(); - delete p; - } - } - assert(0 == que.front()); - assert(0 == que.size()); - - que.setMaxSize(5); - for ( i = 0; i < 10; i++ ) - { - v = new int(i); - que.post(v); - assert( 5 >= que.size()); - } - for ( i = 0; i < 10; i++ ) - { - int* p = que.front(); - if ( p ) - { - que.pop(); - delete p; - } - } + putLog("\n\nRingBuffer Test complieted.\n\n"); + /* Test Timer */ + printf("Timer Test start\n"); printf("%s Timer start\n", currentDateTime()); tm.start(1000); while (!tm.isTimeup()); @@ -115,7 +83,26 @@ void TestProcessFramework::run(void) tm.start(); while (!tm.isTimeup(1000)); printf("%s Timer 1sec\n", currentDateTime()); + printf("Timer Test completed\n\n"); + /* Test Que */ + TestQue* tque = new TestQue(); + tque->test(); + delete tque; + + /* Test TopicTable */ + TestTopics* testTopic = new TestTopics(); + testTopic->test(); + delete testTopic; + + /* Test TopicIdMap */ + TestTopicIdMap* testMap = new TestTopicIdMap(); + testMap->test(); + delete testMap; + + + + /* Test EventQue */ printf("EventQue test start.\n"); Client* client = new Client(); _evQue.setMaxSize(EVENT_CNT); @@ -128,40 +115,7 @@ void TestProcessFramework::run(void) _evQue.post(ev); } - MultiTaskProcess::run(); - printf("ProcessFramework test complited.\n"); -} - -TestQue::TestQue() -{ - -} - -TestQue::~TestQue() -{ - -} - -int* TestQue::front(void) -{ - return _que.front(); -} -void TestQue::pop(void) -{ - _que.pop(); -} -int TestQue::size(void) -{ - return _que.size(); -} -void TestQue::setMaxSize(int maxsize) -{ - _que.setMaxSize(maxsize); -} - -void TestQue::post(int* val) -{ - _que.post(val); + printf("\n\nAll Tests completed.\n"); } diff --git a/MQTTSNGateway/src/tests/TestProcessFramework.h b/MQTTSNGateway/src/tests/TestProcess.h similarity index 71% rename from MQTTSNGateway/src/tests/TestProcessFramework.h rename to MQTTSNGateway/src/tests/TestProcess.h index 8967927..6890b8d 100644 --- a/MQTTSNGateway/src/tests/TestProcessFramework.h +++ b/MQTTSNGateway/src/tests/TestProcess.h @@ -13,18 +13,18 @@ * Contributors: * Tomoaki Yamaguchi - initial API and implementation **************************************************************************************/ -#ifndef TESTPROCESSFRAMEWORK_H_ -#define TESTPROCESSFRAMEWORK_H_ +#ifndef TESTPROCESS_H_ +#define TESTPROCESS_H_ #include "MQTTSNGWProcess.h" #include "MQTTSNGateway.h" #define EVENT_CNT 10 namespace MQTTSNGW { -class TestProcessFramework: public MultiTaskProcess{ +class TestProcess: public MultiTaskProcess{ public: - TestProcessFramework(); - ~TestProcessFramework(); + TestProcess(); + ~TestProcess(); virtual void initialize(int argc, char** argv); void run(void); EventQue* getEventQue(void) { return &_evQue; } @@ -33,19 +33,6 @@ private: EventQue _evQue; }; -class TestQue -{ -public: - TestQue(); - ~TestQue(); - void post(int*); - int* front(void); - void pop(void); - int size(void); - void setMaxSize(int maxsize); -private: - Que _que; -}; } -#endif /* TESTPROCESSFRAMEWORK_H_ */ +#endif /* TESTPROCESS_H_ */ diff --git a/MQTTSNGateway/src/tests/TestQue.cpp b/MQTTSNGateway/src/tests/TestQue.cpp new file mode 100644 index 0000000..984c74f --- /dev/null +++ b/MQTTSNGateway/src/tests/TestQue.cpp @@ -0,0 +1,101 @@ +/************************************************************************************** + * Copyright (c) 2016, Tomoaki Yamaguchi + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Tomoaki Yamaguchi - initial API and implementation + **************************************************************************************/ +#include +#include +#include +#include "TestQue.h" +using namespace std; +using namespace MQTTSNGW; + +TestQue::TestQue() +{ + +} + +TestQue::~TestQue() +{ + +} + +void TestQue::test(void) +{ + int* v = 0; + int i = 0; + + printf("Que Test start.\n"); + for ( i = 0; i < 10; i++ ) + { + v = new int(i); + this->post(v); + } + assert( 10 == this->size()); + + for ( i = 0; i < 10; i++ ) + { + assert(i == *this->front()); + int* p = this->front(); + if ( p ) + { + assert(i == *p); + this->pop(); + delete p; + } + } + assert(0 == this->front()); + assert(0 == this->size()); + + this->setMaxSize(5); + for ( i = 0; i < 10; i++ ) + { + v = new int(i); + this->post(v); + assert( 5 >= this->size()); + } + for ( i = 0; i < 10; i++ ) + { + int* p = this->front(); + if ( p ) + { + this->pop(); + delete p; + } + } + printf("Que test completed.\n\n"); +} + +int* TestQue::front(void) +{ + return _que.front(); +} +void TestQue::pop(void) +{ + _que.pop(); +} +int TestQue::size(void) +{ + return _que.size(); +} +void TestQue::setMaxSize(int maxsize) +{ + _que.setMaxSize(maxsize); +} + +void TestQue::post(int* val) +{ + _que.post(val); +} + + diff --git a/MQTTSNGateway/src/tests/TestQue.h b/MQTTSNGateway/src/tests/TestQue.h new file mode 100644 index 0000000..3511895 --- /dev/null +++ b/MQTTSNGateway/src/tests/TestQue.h @@ -0,0 +1,41 @@ +/************************************************************************************** + * Copyright (c) 2016, Tomoaki Yamaguchi + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Tomoaki Yamaguchi - initial API and implementation + **************************************************************************************/ +#ifndef MQTTSNGATEWAY_SRC_TESTS_TESTQUE_H_ +#define MQTTSNGATEWAY_SRC_TESTS_TESTQUE_H_ + +#include "MQTTSNGWProcess.h" + +namespace MQTTSNGW +{ + +class TestQue +{ +public: + TestQue(); + ~TestQue(); + void post(int*); + int* front(void); + void pop(void); + int size(void); + void setMaxSize(int maxsize); + void test(void); +private: + Que _que; +}; +} + + +#endif /* MQTTSNGATEWAY_SRC_TESTS_TESTQUE_H_ */ diff --git a/MQTTSNGateway/src/tests/TestTask.cpp b/MQTTSNGateway/src/tests/TestTask.cpp index ab451e9..dcbfa4f 100644 --- a/MQTTSNGateway/src/tests/TestTask.cpp +++ b/MQTTSNGateway/src/tests/TestTask.cpp @@ -17,11 +17,11 @@ #include #include "TestTask.h" #include "Threading.h" - +#include "TestProcess.h" using namespace std; using namespace MQTTSNGW; -TestTask::TestTask(TestProcessFramework* proc) +TestTask::TestTask(TestProcess* proc) { proc->attach((Thread*)this); _proc = proc; @@ -39,23 +39,24 @@ void TestTask::initialize(int argc, char** argv) void TestTask::run(void) { + int evcnt = 0; EventQue* evQue = _proc->getEventQue(); uint16_t duration = 0; - int cnt = 0; + while (true) { Event* ev = evQue->timedwait(5000); + evcnt++; if ( ev->getEventType() == EtTimeout ) { - assert(EVENT_CNT == cnt); + assert(EVENT_CNT + 1 == evcnt); delete ev; + printf("EventQue test complete.\n\n"); break; } - cnt++; MQTTSNPacket* packet = ev->getMQTTSNPacket(); packet->getDISCONNECT(&duration); - printf("Event %d\n", duration); delete ev; } diff --git a/MQTTSNGateway/src/tests/TestTask.h b/MQTTSNGateway/src/tests/TestTask.h index a11076a..42ba855 100644 --- a/MQTTSNGateway/src/tests/TestTask.h +++ b/MQTTSNGateway/src/tests/TestTask.h @@ -16,8 +16,8 @@ #ifndef TESTTASK_H_ #define TESTTASK_H_ +#include "TestProcess.h" #include "Threading.h" -#include "TestProcessFramework.h" namespace MQTTSNGW { @@ -27,13 +27,13 @@ class TestTask: public Thread MAGIC_WORD_FOR_THREAD; ; public: - TestTask(TestProcessFramework* proc); + TestTask(TestProcess* proc); ~TestTask(); void initialize(int argc, char** argv); void run(void); private: - TestProcessFramework* _proc; + TestProcess* _proc; }; } diff --git a/MQTTSNGateway/src/tests/TestTopicIdMap.cpp b/MQTTSNGateway/src/tests/TestTopicIdMap.cpp new file mode 100644 index 0000000..3f6510a --- /dev/null +++ b/MQTTSNGateway/src/tests/TestTopicIdMap.cpp @@ -0,0 +1,125 @@ +/************************************************************************************** + * Copyright (c) 2016, Tomoaki Yamaguchi + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Tomoaki Yamaguchi - initial API and implementation + **************************************************************************************/ +#include +#include +#include +#include "TestTopicIdMap.h" + +using namespace std; +using namespace MQTTSNGW; + +TestTopicIdMap::TestTopicIdMap() +{ + _map = new TopicIdMap(); +} + +TestTopicIdMap::~TestTopicIdMap() +{ + delete _map; +} + +#define MAXID 30 + +void TestTopicIdMap::test(void) +{ + uint16_t id[MAXID]; + printf("Test TopicIdMat start.\n"); + + for ( int i = 0; i < MAXID; i++ ) + { + id[i] = i + 1; + } + + for ( int i = 0; i < MAXID; i++ ) + { + _map->add(id[i], id[i], MQTTSN_TOPIC_TYPE_NORMAL); + } + + for ( int i = 0; i < MAXID; i++ ) + { + MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_SHORT; + uint16_t topicId = _map->getTopicId((uint16_t)i, &type); + //printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type); + assert((i <= MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == i) || (i > MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == 0)); + } + + //printf("\n"); + + for ( int i = 0; i < 5; i++ ) + { + _map->erase(i); + } + for ( int i = 0; i < MAXID; i++ ) + { + MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_SHORT; + uint16_t topicId = _map->getTopicId((uint16_t)i, &type); + //printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type); + assert((i < 5 && topicId == 0) || (i >= 5 && topicId != 0) || (i > MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == 0) ); + } + + + _map->clear(); + //printf("\n"); + + for ( int i = 0; i < MAXID; i++ ) + { + MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_SHORT; + uint16_t topicId = _map->getTopicId((uint16_t)i, &type); + //printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type); + assert( topicId == 0 ); + } + + for ( int i = 0; i < MAXID; i++ ) + { + _map->add(id[i], id[i], MQTTSN_TOPIC_TYPE_SHORT); + } + + for ( int i = 0; i < MAXID; i++ ) + { + MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_NORMAL; + uint16_t topicId = _map->getTopicId((uint16_t)i, &type); + //printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type); + assert((i <= MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == i) || (i > MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == 0)); + } + + //printf("\n"); + + for ( int i = 0; i < 5; i++ ) + { + _map->erase(i); + } + for ( int i = 0; i < MAXID; i++ ) + { + MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_NORMAL; + uint16_t topicId = _map->getTopicId((uint16_t)i, &type); + //printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type); + assert((i < 5 && topicId == 0) || (i >= 5 && topicId != 0) || (i > MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == 0) ); + } + + + _map->clear(); + + //printf("\n"); + for ( int i = 0; i < MAXID; i++ ) + { + MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_NORMAL; + uint16_t topicId = _map->getTopicId((uint16_t)i, &type); + //printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type); + assert( topicId == 0 ); + } + printf("Test TopicIdMat completed.\n\n"); +} + diff --git a/MQTTSNGateway/src/tests/TestTopicIdMap.h b/MQTTSNGateway/src/tests/TestTopicIdMap.h new file mode 100644 index 0000000..e70909a --- /dev/null +++ b/MQTTSNGateway/src/tests/TestTopicIdMap.h @@ -0,0 +1,32 @@ +/************************************************************************************** + * Copyright (c) 2016, Tomoaki Yamaguchi + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Tomoaki Yamaguchi - initial API and implementation + **************************************************************************************/ +#ifndef MQTTSNGATEWAY_SRC_TESTS_TESTTOPICIDMAP_H_ +#define MQTTSNGATEWAY_SRC_TESTS_TESTTOPICIDMAP_H_ + +#include "MQTTSNGWClient.h" + +class TestTopicIdMap +{ +public: + TestTopicIdMap(); + ~TestTopicIdMap(); + void test(void); + +private: + TopicIdMap* _map; +}; + +#endif /* MQTTSNGATEWAY_SRC_TESTS_TESTTOPICIDMAP_H_ */ diff --git a/MQTTSNGateway/src/tests/TestTopics.cpp b/MQTTSNGateway/src/tests/TestTopics.cpp new file mode 100644 index 0000000..624f4df --- /dev/null +++ b/MQTTSNGateway/src/tests/TestTopics.cpp @@ -0,0 +1,183 @@ +/************************************************************************************** + * Copyright (c) 2016, Tomoaki Yamaguchi + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Tomoaki Yamaguchi - initial API and implementation + **************************************************************************************/ + +#include +#include +#include +#include "TestTopics.h" + +using namespace std; +using namespace MQTTSNGW; + +TestTopics::TestTopics() +{ + _topics = new Topics(); +} + +TestTopics::~TestTopics() +{ + delete _topics; +} + +void TestTopics::test(void) +{ + printf("Topics Test start.\n"); + + MQTTSN_topicid topic[12]; + char tp[12][10]; + + /* create Topic */ + strcpy(tp[0], "Topic/+"); + tp[0][7] = 0; + topic[0].type = MQTTSN_TOPIC_TYPE_NORMAL; + topic[0].data.long_.len = strlen(tp[0]); + topic[0].data.long_.name = tp[0]; + + for ( int i = 1; i < 10 ; i++ ) + { + strcpy(tp[i], "Topic/+/"); + tp[i][8] = 0x30 + i; + tp[i][9] = 0; + topic[i].type = MQTTSN_TOPIC_TYPE_NORMAL; + topic[i].data.long_.len = strlen(tp[i]); + topic[i].data.long_.name = tp[i]; + } + strcpy(tp[10], "TOPIC/#"); + tp[10][7] = 0; + topic[10].type = MQTTSN_TOPIC_TYPE_NORMAL; + topic[10].data.long_.len = strlen(tp[10]); + topic[10].data.long_.name = tp[10]; + + strcpy(tp[11], "+/0/#"); + tp[11][7] = 0; + topic[11].type = MQTTSN_TOPIC_TYPE_NORMAL; + topic[11].data.long_.len = strlen(tp[11]); + topic[11].data.long_.name = tp[11]; + + /* Add Topic to Topics */ + for ( int i = 0; i < 12; i++ ) + { + MQTTSN_topicid pos = topic[i]; + Topic* t = _topics->add(&pos); + //printf("Topic=%s ID=%d\n", t->getTopicName()->c_str(), t->getTopicId()); + assert(t !=0); + } + + for ( int i = 0; i < 5; i++ ) + { + string str = "Test/"; + str += 0x30 + i; + Topic* t = _topics->add(&str); + //printf("Topic=%s ID=%d\n", t->getTopicName()->c_str(), t->getTopicId()); + assert(t !=0); + } + + /* Get Topic by MQTTSN_topicid */ + for ( int i = 0; i < 12; i++ ) + { + Topic* t = _topics->getTopic(&topic[i]); + //printf("Topic=%s ID=%d ID=%d\n", t->getTopicName()->c_str(), t->getTopicId(),_topics->getTopicId(&topic[i])); + assert(t->getTopicId() == i + 1); + } + + /* Get TopicId by MQTTSN_topicid */ + for ( int i = 0; i < 12; i++ ) + { + uint16_t id = _topics->getTopicId(&topic[i]); + //printf("ID=%d \n", id); + assert(id == i + 1); + } + + /* Test Wilecard */ + for ( int i = 0; i < 10 ; i++ ) + { + MQTTSN_topicid tp1; + char tp0[10]; + strcpy(tp0, "Topic/"); + tp0[6] = 0x30 + i; + tp0[7] = '/'; + tp0[8] = 0x30 + i; + tp0[9] = 0; + tp1.type = MQTTSN_TOPIC_TYPE_NORMAL; + tp1.data.long_.len = strlen(tp0); + tp1.data.long_.name = tp0; + + Topic* t = _topics->match(&tp1); +/* + if (t) + { + printf("Topic=%s match to %s\n", tp0, t->getTopicName()->c_str()); + } + else + { + printf("Topic=%s unmatch\n", tp0); + } +*/ + assert(t != 0); + } + for ( int i = 0; i < 10 ; i++ ) + { + MQTTSN_topicid tp1; + char tp0[10]; + strcpy(tp0, "Topic/"); + tp0[6] = 0x30 + i; + tp0[7] = 0; + tp1.type = MQTTSN_TOPIC_TYPE_NORMAL; + tp1.data.long_.len = strlen(tp0); + tp1.data.long_.name = tp0; + + Topic* t = _topics->match(&tp1); +/* + if (t) + { + printf("Topic=%s match to %s\n", tp0, t->getTopicName()->c_str()); + } + else + { + printf("Topic=%s unmatch\n", tp0); + } +*/ + assert(t != 0); + } + + for ( int i = 0; i < 10 ; i++ ) + { + MQTTSN_topicid tpid1; + char tp0[10]; + strcpy(tp0, "TOPIC/"); + tp0[6] = 0x30 + i; + tp0[7] = '/'; + tp0[8] = 0x30 + i; + tp0[9] = 0; + tpid1.type = MQTTSN_TOPIC_TYPE_NORMAL; + tpid1.data.long_.len = strlen(tp0); + tpid1.data.long_.name = tp0; + + Topic* t = _topics->match(&tpid1); +/* + if (t) + { + printf("Topic=%s match to %s\n", tp0, t->getTopicName()->c_str()); + } + else + { + printf("Topic=%s unmatch\n", tp0); + } +*/ + assert( t != 0); + } + printf("Topics Test complete.\n\n"); +} diff --git a/MQTTSNGateway/src/tests/TestTopics.h b/MQTTSNGateway/src/tests/TestTopics.h new file mode 100644 index 0000000..9665851 --- /dev/null +++ b/MQTTSNGateway/src/tests/TestTopics.h @@ -0,0 +1,32 @@ +/************************************************************************************** + * Copyright (c) 2016, Tomoaki Yamaguchi + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Tomoaki Yamaguchi - initial API and implementation + **************************************************************************************/ +#ifndef MQTTSNGATEWAY_SRC_TESTS_TESTTOPICS_H_ +#define MQTTSNGATEWAY_SRC_TESTS_TESTTOPICS_H_ + +#include "MQTTSNGWClient.h" + +class TestTopics +{ +public: + TestTopics(); + ~TestTopics(); + void test(void); + +private: + Topics* _topics; +}; + +#endif /* MQTTSNGATEWAY_SRC_TESTS_TESTTOPICS_H_ */ diff --git a/MQTTSNGateway/src/tests/mainTestProcessFramework.cpp b/MQTTSNGateway/src/tests/mainTestProcess.cpp similarity index 81% rename from MQTTSNGateway/src/tests/mainTestProcessFramework.cpp rename to MQTTSNGateway/src/tests/mainTestProcess.cpp index 9c23e04..ab99096 100644 --- a/MQTTSNGateway/src/tests/mainTestProcessFramework.cpp +++ b/MQTTSNGateway/src/tests/mainTestProcess.cpp @@ -13,19 +13,19 @@ * Contributors: * Tomoaki Yamaguchi - initial API and implementation **************************************************************************************/ -#include "TestProcessFramework.h" +#include "TestProcess.h" #include "TestTask.h" using namespace MQTTSNGW; -TestProcessFramework* proc = new TestProcessFramework(); -TestTask* task = new TestTask(proc); +TestProcess* test = new TestProcess(); +TestTask* task = new TestTask(test); int main(int argc, char** argv) { - proc->initialize(argc, argv); - proc->run(); - delete proc; + test->initialize(argc, argv); + test->run(); + delete test; return 0; } diff --git a/Makefile b/Makefile index 8fee46a..26df06a 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ LPROGNAME := MQTT-SNLogmonitor LAPPL := mainLogmonitor TESTPROGNAME := testPFW -TESTAPPL := mainTestProcessFramework +TESTAPPL := mainTestProcess CONFIG := MQTTSNGateway/gateway.conf CLIENTS := MQTTSNGateway/clients.conf @@ -39,7 +39,10 @@ $(SRCDIR)/$(OS)/$(SENSORNET)/SensorNetwork.cpp \ $(SRCDIR)/$(OS)/Timer.cpp \ $(SRCDIR)/$(OS)/Network.cpp \ $(SRCDIR)/$(OS)/Threading.cpp \ -$(SRCDIR)/$(TEST)/TestProcessFramework.cpp \ +$(SRCDIR)/$(TEST)/TestProcess.cpp \ +$(SRCDIR)/$(TEST)/TestQue.cpp \ +$(SRCDIR)/$(TEST)/TestTopics.cpp \ +$(SRCDIR)/$(TEST)/TestTopicIdMap.cpp \ $(SRCDIR)/$(TEST)/TestTask.cpp