From 4478eafc8d1976ac1a3c1c2bdd8e36205f5d0874 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Mon, 3 May 2021 19:20:52 +0900 Subject: [PATCH] Bugfix of Exception Handling Signed-off-by: tomoaki --- MQTTSNGateway/README.md | 22 ++++++-------- MQTTSNGateway/build.sh | 2 +- MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp | 2 +- .../src/MQTTSNGWPacketHandleTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWProcess.cpp | 30 ++++++++++++++----- MQTTSNGateway/src/MQTTSNGWProcess.h | 4 ++- MQTTSNGateway/src/linux/Threading.h | 4 ++- MQTTSNGateway/src/mainGateway.cpp | 1 - 11 files changed, 43 insertions(+), 30 deletions(-) diff --git a/MQTTSNGateway/README.md b/MQTTSNGateway/README.md index e592b4c..81f249a 100644 --- a/MQTTSNGateway/README.md +++ b/MQTTSNGateway/README.md @@ -1,4 +1,4 @@ -# MQTT-SN Transparent / Aggregating Gateway + MQTT-SN Transparent / Aggregating Gateway **MQTT-SN** requires a MQTT-SN Gateway which acts as a protocol converter to convert **MQTT-SN messages to MQTT messages**. MQTT-SN client over SensorNetwork can not communicate directly with MQTT broker(TCP/IP). This Gateway can run as a transparent or aggregating Gateway by specifying the gateway.conf. @@ -7,11 +7,10 @@ This Gateway can run as a transparent or aggregating Gateway by specifying the g ```` $ git clone -b develop https://github.com/eclipse/paho.mqtt-sn.embedded-c $ cd paho.mqtt-sn.embedded-c/MQTTSNGateway -$ ./build.sh {udp|udp6|xbee|loralink}] +$ ./build.sh {udp|udp6|xbee|loralink} -```` -By default, a gateway for UDP is built. -In order to create a gateway for UDP6, XBee or LoRaLink, -DSENSORNET argument is required. +```` +In order to build a gateway, an argument is required. MQTT-SNGateway and MQTT-SNLogmonitor (executable programs) are built in the Build directory. @@ -19,16 +18,16 @@ MQTT-SNGateway and MQTT-SNLogmonitor (executable programs) are built in the Buil ```` -$ ./bin/MQTT-SNGateway -f gateway.conf +$ ./bin/MQTT-SNGateway ```` If you get the error message as follows: ```` -what(): RingBuffer can't create a shared memory. -Aborted (core dumped) +RingBuffer can't create a shared memory. +ABORT Gateway!!! ```` You have to start using sudo command only once for the first time. ```` -$ sudo ./bin/MQTT-SNGateway -f gateway.conf +$ sudo ./bin/MQTT-SNGateway ```` ### **How to Change the configuration of the gateway** @@ -132,7 +131,4 @@ Uncomment the line 62, 63 in MQTTSNDefines.h then you can get more precise logs. ==================================*/ //#define DEBUG // print out log for debug //#define DEBUG_NWSTACK // print out SensorNetwork log -``` - - - +``` \ No newline at end of file diff --git a/MQTTSNGateway/build.sh b/MQTTSNGateway/build.sh index 2895de5..f56afc1 100755 --- a/MQTTSNGateway/build.sh +++ b/MQTTSNGateway/build.sh @@ -11,7 +11,7 @@ else mkdir build.gateway cd build.gateway cmake .. -DSENSORNET=$1 - make MQTTSNPacke + make MQTTSNPacket make MQTT-SNGateway make MQTT-SNLogmonitor fi \ No newline at end of file diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp index 4138db4..b4a6e59 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp @@ -30,7 +30,7 @@ char* currentDateTime(void); BrokerRecvTask::BrokerRecvTask(Gateway* gateway) { _gateway = gateway; - _gateway->attach((Thread*) this); + Runnable::threadNo =_gateway->attach((Thread*) this); _light = nullptr; setTaskName("BrokerRecvTask"); } diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp index 4f27645..c3b33d4 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp @@ -34,7 +34,7 @@ char* currentDateTime(); BrokerSendTask::BrokerSendTask(Gateway* gateway) { _gateway = gateway; - _gateway->attach((Thread*) this); + Runnable::threadNo =_gateway->attach((Thread*) this); _gwparams = nullptr; _light = nullptr; setTaskName("BrokerSendTask"); diff --git a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp index e9720dd..1fdf3f3 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp @@ -30,7 +30,7 @@ char* currentDateTime(void); ClientRecvTask::ClientRecvTask(Gateway* gateway) { _gateway = gateway; - _gateway->attach((Thread*) this); + Runnable::threadNo =_gateway->attach((Thread*) this); _sensorNetwork = _gateway->getSensorNetwork(); setTaskName("ClientRecvTask"); } diff --git a/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp index 2d2fa97..a247e59 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp @@ -29,7 +29,7 @@ char* currentDateTime(void); ClientSendTask::ClientSendTask(Gateway* gateway) { _gateway = gateway; - _gateway->attach((Thread*) this); + Runnable::threadNo =_gateway->attach((Thread*) this); _sensorNetwork = _gateway->getSensorNetwork(); setTaskName("ClientSendTask"); } diff --git a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp index 6540a87..8ff3687 100644 --- a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp @@ -45,7 +45,7 @@ char* currentDateTime(void); PacketHandleTask::PacketHandleTask(Gateway* gateway) { _gateway = gateway; - _gateway->attach((Thread*) this); + Runnable::threadNo =_gateway->attach((Thread*) this); _mqttConnection = new MQTTGWConnectionHandler(_gateway); _mqttPublish = new MQTTGWPublishHandler(_gateway); _mqttSubscribe = new MQTTGWSubscribeHandler(_gateway); diff --git a/MQTTSNGateway/src/MQTTSNGWProcess.cpp b/MQTTSNGateway/src/MQTTSNGWProcess.cpp index 5d16ae4..96af29c 100644 --- a/MQTTSNGateway/src/MQTTSNGWProcess.cpp +++ b/MQTTSNGateway/src/MQTTSNGWProcess.cpp @@ -13,12 +13,13 @@ * Contributors: * Tomoaki Yamaguchi - initial API and implementation and/or initial documentation **************************************************************************************/ - #include #include #include #include #include +#include +#include #include #include #include @@ -252,13 +253,17 @@ MultiTaskProcess::MultiTaskProcess() theMultiTaskProcess = this; _threadCount = 0; _stopCount = 0; + _abortThreadNo = -1; } MultiTaskProcess::~MultiTaskProcess() { for (int i = 0; i < _threadCount; i++) { - _threadList[i]->stop(); + if ( i != _abortThreadNo) + { + _threadList[i]->stop(); + } } } @@ -281,20 +286,20 @@ void MultiTaskProcess::run(void) while (true) { - if (theProcess->checkSignal() == SIGINT ) + if (theProcess->checkSignal() == SIGINT || _abortThreadNo > -1) { return; } - else if (_stopCount > 0) - { - throw Exception("Task stopped !!\n\n"); - } sleep(1); } } void MultiTaskProcess::waitStop(void) { + /* Let threads exit from Select() */ + pid_t pid = getpid(); + kill(pid, SIGINT); + while (_stopCount < _threadCount) { sleep(1); @@ -309,11 +314,19 @@ void MultiTaskProcess::threadStopped(void) } -void MultiTaskProcess::attach(Thread* thread) +void MultiTaskProcess::abort(int threadNo) { + _abortThreadNo = threadNo; + threadStopped(); +} + +int MultiTaskProcess::attach(Thread* thread) +{ + int indexNo = 0; _mutex.lock(); if (_threadCount < MQTTSNGW_MAX_TASK) { + indexNo = _threadCount; _threadList[_threadCount] = thread; _threadCount++; } @@ -323,6 +336,7 @@ void MultiTaskProcess::attach(Thread* thread) throw Exception("Full of Threads"); } _mutex.unlock(); + return indexNo; } int MultiTaskProcess::getParam(const char* parameter, char* value) diff --git a/MQTTSNGateway/src/MQTTSNGWProcess.h b/MQTTSNGateway/src/MQTTSNGWProcess.h index 723bb72..cad637f 100644 --- a/MQTTSNGateway/src/MQTTSNGWProcess.h +++ b/MQTTSNGateway/src/MQTTSNGWProcess.h @@ -85,13 +85,15 @@ public: void run(void); void waitStop(void); void threadStopped(void); - void attach(Thread* thread); + int attach(Thread* thread); + void abort(int threadNo); private: Thread* _threadList[MQTTSNGW_MAX_TASK]; Mutex _mutex; int _threadCount; int _stopCount; + int _abortThreadNo; }; /*===================================== diff --git a/MQTTSNGateway/src/linux/Threading.h b/MQTTSNGateway/src/linux/Threading.h index 4dbd085..980d025 100644 --- a/MQTTSNGateway/src/linux/Threading.h +++ b/MQTTSNGateway/src/linux/Threading.h @@ -118,6 +118,7 @@ public: Runnable(){} virtual ~Runnable(){} virtual void EXECRUN(){} + int threadNo {0}; }; #define MAGIC_WORD_FOR_THREAD \ @@ -130,9 +131,9 @@ public: void EXECRUN() \ } \ catch ( Exception &ex ) \ { \ - theMultiTaskProcess->threadStopped(); \ WRITELOG("%s catch exception\n", getTaskName()); \ ex.writeMessage(); \ + theMultiTaskProcess->abort(threadNo); \ } \ } @@ -151,6 +152,7 @@ public: void stop(void); const char* getTaskName(void); void setTaskName(const char* name); + void abort(int threadNo); private: static void* _run(void*); pthread_t _threadID; diff --git a/MQTTSNGateway/src/mainGateway.cpp b/MQTTSNGateway/src/mainGateway.cpp index b3c59cb..a995fa7 100644 --- a/MQTTSNGateway/src/mainGateway.cpp +++ b/MQTTSNGateway/src/mainGateway.cpp @@ -43,6 +43,5 @@ int main(int argc, char** argv) { ex.writeMessage(); WRITELOG("ABORT Gateway!!!\n\n\n"); - abort(); } }