Refactor Exception handle scheme

Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
tomoaki
2021-05-04 16:14:39 +09:00
parent 4478eafc8d
commit 2adc167207
14 changed files with 85 additions and 79 deletions

View File

@@ -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");
}

View File

@@ -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");

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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");
}

View File

@@ -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);

View File

@@ -18,8 +18,6 @@
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <Timer.h>
#include <exception>
#include <getopt.h>
@@ -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;
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -20,6 +20,7 @@
#include "MQTTSNGWProcess.h"
#include "MQTTSNPacket.h"
#include "MQTTSNGWClient.h"
#include "MQTTSNGWProcess.h"
namespace MQTTSNGW
{

View File

@@ -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();
}

View File

@@ -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(); \
}
/*=====================================

View File

@@ -246,6 +246,7 @@ UDPPort::UDPPort()
_disconReq = false;
_sockfdUnicast = -1;
_sockfdMulticast = -1;
_ttl = 0;
}
UDPPort::~UDPPort()