From 2adc167207a3dbec70313d9b520e62f3a8aac5f2 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 4 May 2021 16:14:39 +0900 Subject: [PATCH] Refactor Exception handle scheme Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWClient.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWClientList.cpp | 8 +- MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp | 4 +- MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp | 2 +- .../src/MQTTSNGWPacketHandleTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWProcess.cpp | 88 ++++++++++--------- MQTTSNGateway/src/MQTTSNGWProcess.h | 16 ++-- MQTTSNGateway/src/MQTTSNGateway.cpp | 4 +- MQTTSNGateway/src/MQTTSNGateway.h | 1 + MQTTSNGateway/src/linux/Threading.cpp | 24 ++--- MQTTSNGateway/src/linux/Threading.h | 8 +- MQTTSNGateway/src/linux/udp/SensorNetwork.cpp | 1 + 14 files changed, 85 insertions(+), 79 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp index b4a6e59..4138db4 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp @@ -30,7 +30,7 @@ char* currentDateTime(void); BrokerRecvTask::BrokerRecvTask(Gateway* gateway) { _gateway = gateway; - Runnable::threadNo =_gateway->attach((Thread*) this); + _gateway->attach((Thread*) this); _light = nullptr; setTaskName("BrokerRecvTask"); } diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp index c3b33d4..4f27645 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp @@ -34,7 +34,7 @@ char* currentDateTime(); BrokerSendTask::BrokerSendTask(Gateway* gateway) { _gateway = gateway; - Runnable::threadNo =_gateway->attach((Thread*) this); + _gateway->attach((Thread*) this); _gwparams = nullptr; _light = nullptr; setTaskName("BrokerSendTask"); diff --git a/MQTTSNGateway/src/MQTTSNGWClient.cpp b/MQTTSNGateway/src/MQTTSNGWClient.cpp index 03244bc..bafb99b 100644 --- a/MQTTSNGateway/src/MQTTSNGWClient.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClient.cpp @@ -544,7 +544,7 @@ void Client::setAdapterType(AdapterType type) _clientType = Ctype_Aggregater; break; default: - throw Exception("Client::setAdapterType(): Invalid Type."); + throw EXCEPTION("Client::setAdapterType(): Invalid Type.", 0); break; } } diff --git a/MQTTSNGateway/src/MQTTSNGWClientList.cpp b/MQTTSNGateway/src/MQTTSNGWClientList.cpp index a1e0dfb..d40c0fc 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientList.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientList.cpp @@ -72,8 +72,8 @@ void ClientList::setClientList(int type) { if (!createList(theGateway->getGWParams()->clientListName, type)) { - throw Exception( - "ClientList::setClientList No client list defined by config file."); + throw EXCEPTION( + "ClientList::setClientList No client list defined by config file.", 0); } } @@ -82,8 +82,8 @@ void ClientList::setPredefinedTopics(bool aggrecate) if (!readPredefinedList(theGateway->getGWParams()->predefinedTopicFileName, aggrecate)) { - throw Exception( - "ClientList::setPredefinedTopics No predefindTopi list defined by config file."); + throw EXCEPTION( + "ClientList::setPredefinedTopics No predefindTopi list defined by config file.",0); } } diff --git a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp index 1fdf3f3..ccf6192 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp @@ -30,7 +30,7 @@ char* currentDateTime(void); ClientRecvTask::ClientRecvTask(Gateway* gateway) { _gateway = gateway; - Runnable::threadNo =_gateway->attach((Thread*) this); + _gateway->attach((Thread*) this); _sensorNetwork = _gateway->getSensorNetwork(); setTaskName("ClientRecvTask"); } @@ -47,7 +47,7 @@ void ClientRecvTask::initialize(int argc, char** argv) { if (_sensorNetwork->initialize() < 0) { - throw Exception(" Can't open the sensor network.\n"); + throw EXCEPTION(" Can't open the sensor network.\n", 0); } } diff --git a/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp index a247e59..2d2fa97 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp @@ -29,7 +29,7 @@ char* currentDateTime(void); ClientSendTask::ClientSendTask(Gateway* gateway) { _gateway = gateway; - Runnable::threadNo =_gateway->attach((Thread*) this); + _gateway->attach((Thread*) this); _sensorNetwork = _gateway->getSensorNetwork(); setTaskName("ClientSendTask"); } diff --git a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp index 8ff3687..6540a87 100644 --- a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp @@ -45,7 +45,7 @@ char* currentDateTime(void); PacketHandleTask::PacketHandleTask(Gateway* gateway) { _gateway = gateway; - Runnable::threadNo =_gateway->attach((Thread*) this); + _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 96af29c..7cded4b 100644 --- a/MQTTSNGateway/src/MQTTSNGWProcess.cpp +++ b/MQTTSNGateway/src/MQTTSNGWProcess.cpp @@ -18,8 +18,6 @@ #include #include #include -#include -#include #include #include #include @@ -165,7 +163,7 @@ int Process::getParam(const char* parameter, char* value) if ((fp = fopen(configPath.c_str(), "r")) == NULL) { - throw Exception("No config file:[" + configPath + "]\n\nUsage: Command -f path/config_file_name\n"); + throw Exception("No config file:\n\nUsage: Command -f path/config_file_name\n", 0); } while (true) @@ -253,17 +251,13 @@ MultiTaskProcess::MultiTaskProcess() theMultiTaskProcess = this; _threadCount = 0; _stopCount = 0; - _abortThreadNo = -1; } MultiTaskProcess::~MultiTaskProcess() { for (int i = 0; i < _threadCount; i++) { - if ( i != _abortThreadNo) - { - _threadList[i]->stop(); - } + _threadList[i]->stop(); } } @@ -286,7 +280,7 @@ void MultiTaskProcess::run(void) while (true) { - if (theProcess->checkSignal() == SIGINT || _abortThreadNo > -1) + if (theProcess->checkSignal() == SIGINT) { return; } @@ -296,10 +290,6 @@ void MultiTaskProcess::run(void) void MultiTaskProcess::waitStop(void) { - /* Let threads exit from Select() */ - pid_t pid = getpid(); - kill(pid, SIGINT); - while (_stopCount < _threadCount) { sleep(1); @@ -314,29 +304,25 @@ void MultiTaskProcess::threadStopped(void) } -void MultiTaskProcess::abort(int threadNo) +void MultiTaskProcess::abort(void) { - _abortThreadNo = threadNo; - threadStopped(); + signalHandler(SIGINT); } -int MultiTaskProcess::attach(Thread* thread) +void MultiTaskProcess::attach(Thread* thread) { - int indexNo = 0; _mutex.lock(); if (_threadCount < MQTTSNGW_MAX_TASK) { - indexNo = _threadCount; _threadList[_threadCount] = thread; _threadCount++; } else { _mutex.unlock(); - throw Exception("Full of Threads"); + throw Exception("The maximum number of threads has been exceeded.", -1); } _mutex.unlock(); - return indexNo; } int MultiTaskProcess::getParam(const char* parameter, char* value) @@ -350,30 +336,20 @@ int MultiTaskProcess::getParam(const char* parameter, char* value) /*===================================== Class Exception ======================================*/ -Exception::Exception(const string& message) +Exception::Exception(const char* message, const int exNo) { - _message = message; - _exNo = 0; - _fileName = nullptr; - _functionName = nullptr; - _line = 0; -} - -Exception::Exception(const int exNo, const string& message) -{ - _message = message; + _message = message; _exNo = exNo; _fileName = nullptr; _functionName = nullptr; _line = 0; } - -Exception::Exception(const int exNo, const string& message, const char* file, +Exception::Exception(const char* message, const int exNo, const char* file, const char* function, const int line) { - _message = message; + _message = message; _exNo = exNo; - _fileName = file; + _fileName = getFileName(file);; _functionName = function; _line = line; } @@ -385,7 +361,7 @@ Exception::~Exception() throw () const char* Exception::what() const throw () { - return _message.c_str(); + return _message; } const char* Exception::getFileName() @@ -410,14 +386,42 @@ const int Exception::getExceptionNo() void Exception::writeMessage() { - if (getExceptionNo() == 0) + if (_fileName == nullptr) { - WRITELOG("%s %s\n", currentDateTime(), what()); + if (_exNo == 0) + { + WRITELOG("%s %s\n", currentDateTime(), _message); + } + else + { + + WRITELOG("%s %s ExNo: %d\n", currentDateTime(), _message, _exNo); + } } else { - WRITELOG("%s:%-6d %s line %-4d %s() : %s\n", currentDateTime(), - getExceptionNo(), getFileName(), getLineNo(), getFunctionName(), - what()); + if (_exNo == 0) + { + WRITELOG("%s %s line %-4d %s() : %s\n", + currentDateTime(), _fileName, _line, _functionName, _message); + } + else + { + WRITELOG("%s %s line %-4d %s() : %s ExNo: %d\n", + currentDateTime(), _fileName, _line, _functionName, _message, _exNo); + } } } + +const char* Exception::getFileName(const char* file) +{ + for ( int len = strlen(file); len > 0; len-- ) + { + if (*(file + len) == '/') + { + return file + len + 1; + } + } + return file; +} + diff --git a/MQTTSNGateway/src/MQTTSNGWProcess.h b/MQTTSNGateway/src/MQTTSNGWProcess.h index cad637f..be69da8 100644 --- a/MQTTSNGateway/src/MQTTSNGWProcess.h +++ b/MQTTSNGateway/src/MQTTSNGWProcess.h @@ -41,6 +41,8 @@ namespace MQTTSNGW #define WRITELOG theProcess->putLog #define CHK_SIGINT (theProcess->checkSignal() == SIGINT) #define UNUSED(x) ((void)(x)) +#define EXCEPTION(...) Exception(__VA_ARGS__, __FILE__, __func__, __LINE__) + /*================================= Class Process ==================================*/ @@ -85,15 +87,14 @@ public: void run(void); void waitStop(void); void threadStopped(void); - int attach(Thread* thread); - void abort(int threadNo); + void attach(Thread* thread); + void abort(void); private: Thread* _threadList[MQTTSNGW_MAX_TASK]; Mutex _mutex; int _threadCount; int _stopCount; - int _abortThreadNo; }; /*===================================== @@ -102,10 +103,8 @@ private: class Exception: public exception { public: - Exception(const string& message); - Exception(const int exNo, const string& message); - Exception(const int exNo, const string& message, const char* file, - const char* func, const int line); + Exception(const char* message, const int exNo); + Exception(const char* message, const int exNo, const char* file, const char* func, int line); virtual ~Exception() throw (); const char* getFileName(); const char* getFunctionName(); @@ -115,8 +114,9 @@ public: void writeMessage(); private: + const char* getFileName(const char* file); int _exNo; - string _message; + const char* _message; const char* _fileName; const char* _functionName; int _line; diff --git a/MQTTSNGateway/src/MQTTSNGateway.cpp b/MQTTSNGateway/src/MQTTSNGateway.cpp index b722a56..81ad403 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.cpp +++ b/MQTTSNGateway/src/MQTTSNGateway.cpp @@ -182,7 +182,7 @@ void Gateway::initialize(int argc, char** argv) if (_params.gatewayId == 0 || _params.gatewayId > 255) { - throw Exception("Gateway::initialize: invalid Gateway Id"); + throw Exception("Gateway::initialize: invalid Gateway Id", 0); } if (getParam("GatewayName", param) == 0) @@ -192,7 +192,7 @@ void Gateway::initialize(int argc, char** argv) if (_params.gatewayName == 0) { - throw Exception("Gateway::initialize: Gateway Name is missing."); + throw Exception("Gateway::initialize: Gateway Name is missing.", 0); } _params.mqttVersion = DEFAULT_MQTT_VERSION; diff --git a/MQTTSNGateway/src/MQTTSNGateway.h b/MQTTSNGateway/src/MQTTSNGateway.h index 10c6c2a..1f6d6de 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.h +++ b/MQTTSNGateway/src/MQTTSNGateway.h @@ -20,6 +20,7 @@ #include "MQTTSNGWProcess.h" #include "MQTTSNPacket.h" #include "MQTTSNGWClient.h" +#include "MQTTSNGWProcess.h" namespace MQTTSNGW { diff --git a/MQTTSNGateway/src/linux/Threading.cpp b/MQTTSNGateway/src/linux/Threading.cpp index d1cb5c8..9fe834e 100644 --- a/MQTTSNGateway/src/linux/Threading.cpp +++ b/MQTTSNGateway/src/linux/Threading.cpp @@ -79,23 +79,23 @@ Mutex::Mutex(const char* fileName) if ((_shmid = shmget(key, sizeof(pthread_mutex_t), IPC_CREAT | 0666)) < 0) { - throw Exception( -1, "Mutex can't create a shared memory."); + throw Exception("Mutex can't create a shared memory.", -1); } _pmutex = (pthread_mutex_t*) shmat(_shmid, NULL, 0); if (_pmutex == (void*) -1) { - throw Exception( -1, "Mutex can't attach shared memory."); + throw Exception("Mutex can't attach shared memory.", -1); } pthread_mutexattr_init(&attr); if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) != 0) { - throw Exception( -1, "Mutex can't set the process-shared flag"); + throw Exception("Mutex can't set the process-shared flag", -1); } if (pthread_mutex_init(_pmutex, &attr) != 0) { - throw Exception( -1, "Mutex can't initialize."); + throw Exception("Mutex can't initialize.", -1); } } @@ -135,7 +135,7 @@ void Mutex::lock(void) } } catch (char* errmsg) { - throw Exception( -1, "The same thread can't acquire a mutex twice."); + throw Exception("The same thread can't acquire a mutex twice.", -1); } } } @@ -157,7 +157,7 @@ void Mutex::unlock(void) } } catch (char* errmsg) { - throw Exception( -1, "Mutex can't unlock."); + throw Exception("Mutex can't unlock.", -1); } } } @@ -225,12 +225,12 @@ NamedSemaphore::NamedSemaphore(const char* name, unsigned int val) _psem = sem_open(name, O_CREAT, 0666, val); if (_psem == SEM_FAILED) { - throw Exception( -1, "Semaphore can't be created."); + throw Exception("Semaphore can't be created.", -1); } _name = strdup(name); if (_name == NULL) { - throw Exception( -1, "Semaphore can't allocate memories."); + throw Exception("Semaphore can't allocate memories.", -1); } } @@ -298,7 +298,7 @@ RingBuffer::RingBuffer(const char* keyDirectory) } else { - throw Exception(-1, "RingBuffer can't attach shared memory."); + throw Exception("RingBuffer can't attach shared memory.", -1); } } else if ((_shmid = shmget(key, PROCESS_LOG_BUFFER_SIZE, IPC_CREAT | 0666)) != -1) @@ -313,12 +313,12 @@ RingBuffer::RingBuffer(const char* keyDirectory) } else { - throw Exception(-1, "RingBuffer can't create a shared memory."); + throw Exception("RingBuffer can't create a shared memory.", -1); } } else { - throw Exception(-1, "RingBuffer can't create a shared memory."); + throw Exception( "RingBuffer can't create a shared memory.", -1); } _pmx = new Mutex(MQTTSNGW_RB_MUTEX_KEY); @@ -490,7 +490,7 @@ void RingBuffer::reset() } else { - throw Exception(-1, "RingBuffer can't reset. need to clear shared memory."); + throw Exception("RingBuffer can't reset. need to clear shared memory.", -1); } _pmx->unlock(); } diff --git a/MQTTSNGateway/src/linux/Threading.h b/MQTTSNGateway/src/linux/Threading.h index 980d025..7969fdd 100644 --- a/MQTTSNGateway/src/linux/Threading.h +++ b/MQTTSNGateway/src/linux/Threading.h @@ -118,7 +118,6 @@ public: Runnable(){} virtual ~Runnable(){} virtual void EXECRUN(){} - int threadNo {0}; }; #define MAGIC_WORD_FOR_THREAD \ @@ -127,14 +126,15 @@ public: void EXECRUN() \ try \ { \ run(); \ - theMultiTaskProcess->threadStopped(); \ } \ catch ( Exception &ex ) \ { \ - WRITELOG("%s catch exception\n", getTaskName()); \ + WRITELOG("\033[0m\033[0;31m"); \ ex.writeMessage(); \ - theMultiTaskProcess->abort(threadNo); \ + WRITELOG("\033[0m\033[0;37m%s caught an exception and stopped.\n", getTaskName()); \ + theMultiTaskProcess->abort(); \ } \ + theMultiTaskProcess->threadStopped(); \ } /*===================================== diff --git a/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp b/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp index 1b57ff5..e96dca6 100644 --- a/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp +++ b/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp @@ -246,6 +246,7 @@ UDPPort::UDPPort() _disconReq = false; _sockfdUnicast = -1; _sockfdMulticast = -1; + _ttl = 0; } UDPPort::~UDPPort()