Bugfix of Handling Exception #236

Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
tomoaki
2021-04-27 21:34:08 +09:00
parent 60fcab2504
commit aaad3a0122
11 changed files with 56 additions and 60 deletions

View File

@@ -32,11 +32,11 @@ BrokerRecvTask::BrokerRecvTask(Gateway* gateway)
_gateway = gateway; _gateway = gateway;
_gateway->attach((Thread*) this); _gateway->attach((Thread*) this);
_light = nullptr; _light = nullptr;
setTaskName("BrokerRecvTask");
} }
BrokerRecvTask::~BrokerRecvTask() BrokerRecvTask::~BrokerRecvTask()
{ {
} }
/** /**
@@ -64,7 +64,7 @@ void BrokerRecvTask::run(void)
_light->blueLight(false); _light->blueLight(false);
if (CHK_SIGINT) if (CHK_SIGINT)
{ {
WRITELOG("\n%s BrokerRecvTask stopped.", currentDateTime()); WRITELOG("\n%s %s stopped.", currentDateTime(), getTaskName());
return; return;
} }
timeout.tv_sec = 0; timeout.tv_sec = 0;

View File

@@ -37,11 +37,11 @@ BrokerSendTask::BrokerSendTask(Gateway* gateway)
_gateway->attach((Thread*) this); _gateway->attach((Thread*) this);
_gwparams = nullptr; _gwparams = nullptr;
_light = nullptr; _light = nullptr;
setTaskName("BrokerSendTask");
} }
BrokerSendTask::~BrokerSendTask() BrokerSendTask::~BrokerSendTask()
{ {
// WRITELOG("BrokerSendTask is deleted normally.\r\n");
} }
/** /**
@@ -70,7 +70,7 @@ void BrokerSendTask::run()
if (ev->getEventType() == EtStop) if (ev->getEventType() == EtStop)
{ {
WRITELOG("\n%s BrokerSendTask stopped.", currentDateTime()); WRITELOG("\n%s %s stopped.", currentDateTime(), getTaskName());
delete ev; delete ev;
return; return;
} }

View File

@@ -32,6 +32,7 @@ ClientRecvTask::ClientRecvTask(Gateway* gateway)
_gateway = gateway; _gateway = gateway;
_gateway->attach((Thread*) this); _gateway->attach((Thread*) this);
_sensorNetwork = _gateway->getSensorNetwork(); _sensorNetwork = _gateway->getSensorNetwork();
setTaskName("ClientRecvTask");
} }
ClientRecvTask::~ClientRecvTask() ClientRecvTask::~ClientRecvTask()
@@ -78,7 +79,7 @@ void ClientRecvTask::run()
if (CHK_SIGINT) if (CHK_SIGINT)
{ {
WRITELOG("\n%s ClientRecvTask stopped.", currentDateTime()); WRITELOG("\n%s %s stopped.", currentDateTime(), getTaskName());
delete packet; delete packet;
return; return;
} }

View File

@@ -31,11 +31,11 @@ ClientSendTask::ClientSendTask(Gateway* gateway)
_gateway = gateway; _gateway = gateway;
_gateway->attach((Thread*) this); _gateway->attach((Thread*) this);
_sensorNetwork = _gateway->getSensorNetwork(); _sensorNetwork = _gateway->getSensorNetwork();
setTaskName("ClientSendTask");
} }
ClientSendTask::~ClientSendTask() ClientSendTask::~ClientSendTask()
{ {
// WRITELOG("ClientSendTask is deleted normally.\r\n");
} }
void ClientSendTask::run() void ClientSendTask::run()
@@ -51,7 +51,7 @@ void ClientSendTask::run()
if (ev->getEventType() == EtStop || _gateway->IsStopping()) if (ev->getEventType() == EtStop || _gateway->IsStopping())
{ {
WRITELOG("\n%s ClientSendTask stopped.", currentDateTime()); WRITELOG("\n%s %s stopped.", currentDateTime(), getTaskName());
delete ev; delete ev;
break; break;
} }

View File

@@ -54,6 +54,7 @@ PacketHandleTask::PacketHandleTask(Gateway* gateway)
_mqttsnSubscribe = new MQTTSNSubscribeHandler(_gateway); _mqttsnSubscribe = new MQTTSNSubscribeHandler(_gateway);
_mqttsnAggrConnection = new MQTTSNAggregateConnectionHandler(_gateway); _mqttsnAggrConnection = new MQTTSNAggregateConnectionHandler(_gateway);
setTaskName("PacketHandleTask");
} }
/** /**
@@ -113,7 +114,7 @@ void PacketHandleTask::run()
if (ev->getEventType() == EtStop) if (ev->getEventType() == EtStop)
{ {
WRITELOG("\n%s PacketHandleTask stopped.", currentDateTime()); WRITELOG("\n%s %s stopped.", currentDateTime(), getTaskName());
delete ev; delete ev;
return; return;
} }

View File

@@ -57,6 +57,8 @@ Process::Process()
_configDir = CONFIG_DIRECTORY; _configDir = CONFIG_DIRECTORY;
_configFile = CONFIG_FILE; _configFile = CONFIG_FILE;
_log = 0; _log = 0;
_rbsem = NULL;
_rb = NULL;
} }
Process::~Process() Process::~Process()
@@ -99,7 +101,6 @@ void Process::initialize(int argc, char** argv)
else else
{ {
_configFile = config.substr(pos + 1, config.size() - pos - 1); _configFile = config.substr(pos + 1, config.size() - pos - 1);
;
_configDir = config.substr(0, pos + 1); _configDir = config.substr(0, pos + 1);
} }
} }
@@ -278,22 +279,17 @@ void MultiTaskProcess::run(void)
_threadList[i]->start(); _threadList[i]->start();
} }
try while (true)
{ {
while (true) if (theProcess->checkSignal() == SIGINT )
{ {
if (theProcess->checkSignal() == SIGINT) return;
{
return;
}
sleep(1);
} }
} catch (Exception* ex) else if (_stopCount > 0)
{ {
ex->writeMessage(); throw Exception(0,"Abort",__FILE__, __func__, __LINE__);
} catch (...) }
{ sleep(1);
throw;
} }
} }
@@ -334,10 +330,6 @@ int MultiTaskProcess::getParam(const char* parameter, char* value)
_mutex.lock(); _mutex.lock();
int rc = Process::getParam(parameter, value); int rc = Process::getParam(parameter, value);
_mutex.unlock(); _mutex.unlock();
if (rc == -1)
{
throw Exception("No config file.");
}
return rc; return rc;
} }
@@ -348,8 +340,8 @@ Exception::Exception(const string& message)
{ {
_message = message; _message = message;
_exNo = 0; _exNo = 0;
_fileName = 0; _fileName = nullptr;
_functionName = 0; _functionName = nullptr;
_line = 0; _line = 0;
} }

View File

@@ -17,6 +17,6 @@
#ifndef MQTTSNGWVERSION_H_IN_ #ifndef MQTTSNGWVERSION_H_IN_
#define MQTTSNGWVERSION_H_IN_ #define MQTTSNGWVERSION_H_IN_
#define PAHO_GATEWAY_VERSION "1.5.0" #define PAHO_GATEWAY_VERSION "1.6.0"
#endif /* MQTTSNGWVERSION_H_IN_ */ #endif /* MQTTSNGWVERSION_H_IN_ */

View File

@@ -315,7 +315,7 @@ void Gateway::run(void)
_stopFlg = false; _stopFlg = false;
/* Run Tasks until CTRL+C entred */ /* Run Tasks until CTRL+C entered or Exception occurred */
MultiTaskProcess::run(); MultiTaskProcess::run();
_stopFlg = true; _stopFlg = true;

View File

@@ -135,7 +135,7 @@ void Mutex::lock(void)
} }
} catch (char* errmsg) } catch (char* errmsg)
{ {
throw Exception( -1, "The same thread can't aquire a mutex twice."); throw Exception( -1, "The same thread can't acquire a mutex twice.");
} }
} }
} }
@@ -263,11 +263,6 @@ void NamedSemaphore::timedwait(uint16_t millsec)
/*========================================= /*=========================================
Class RingBuffer Class RingBuffer
=========================================*/ =========================================*/
RingBuffer::RingBuffer()
{
RingBuffer(MQTTSNGW_KEY_DIRECTORY);
}
RingBuffer::RingBuffer(const char* keyDirectory) RingBuffer::RingBuffer(const char* keyDirectory)
{ {
int fp = 0; int fp = 0;
@@ -506,6 +501,7 @@ void RingBuffer::reset()
Thread::Thread() Thread::Thread()
{ {
_threadID = 0; _threadID = 0;
_taskName = nullptr;
} }
Thread::~Thread() Thread::~Thread()
@@ -539,11 +535,6 @@ int Thread::start(void)
return pthread_create(&_threadID, 0, _run, runnable); return pthread_create(&_threadID, 0, _run, runnable);
} }
void Thread::stopProcess(void)
{
theMultiTaskProcess->threadStopped();
}
void Thread::stop(void) void Thread::stop(void)
{ {
if ( _threadID ) if ( _threadID )
@@ -552,3 +543,13 @@ void Thread::stop(void)
_threadID = 0; _threadID = 0;
} }
} }
void Thread::setTaskName(const char* name)
{
_taskName = name;
}
const char* Thread::getTaskName(void)
{
return _taskName;
}

View File

@@ -92,8 +92,7 @@ private:
class RingBuffer class RingBuffer
{ {
public: public:
RingBuffer(); RingBuffer(const char* keyDirctory = MQTTSNGW_KEY_DIRECTORY);
RingBuffer(const char* keyDirctory);
~RingBuffer(); ~RingBuffer();
void put(char* buffer); void put(char* buffer);
int get(char* buffer, int bufferLength); int get(char* buffer, int bufferLength);
@@ -124,15 +123,17 @@ public:
#define MAGIC_WORD_FOR_THREAD \ #define MAGIC_WORD_FOR_THREAD \
public: void EXECRUN() \ public: void EXECRUN() \
{ \ { \
try \ try \
{ \ { \
run(); \ run(); \
stopProcess(); \ theMultiTaskProcess->threadStopped(); \
} \ } \
catch(...) \ catch ( Exception &ex ) \
{ \ { \
throw; \ theMultiTaskProcess->threadStopped(); \
} \ WRITELOG("%s catch exception\n", getTaskName()); \
ex.writeMessage(); \
} \
} }
/*===================================== /*=====================================
@@ -146,12 +147,14 @@ public:
static pthread_t getID(); static pthread_t getID();
static bool equals(pthread_t*, pthread_t*); static bool equals(pthread_t*, pthread_t*);
virtual void initialize(int argc, char** argv); virtual void initialize(int argc, char** argv);
void stopProcess(void);
void waitStop(void); void waitStop(void);
void stop(void); void stop(void);
const char* getTaskName(void);
void setTaskName(const char* name);
private: private:
static void* _run(void*); static void* _run(void*);
pthread_t _threadID; pthread_t _threadID;
const char* _taskName;
}; };
} }

View File

@@ -34,16 +34,14 @@ BrokerSendTask task5(&gateway);
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
gateway.initialize(argc, argv);
gateway.run();
try try
{ {
gateway.initialize(argc, argv); gateway.initialize(argc, argv);
gateway.run(); gateway.run();
} catch (const std::exception &ex)
{
WRITELOG("\nEclipse Paho MQTT-SN Gateway exception: %s\n", ex.what());
WRITELOG("MQTT-SNGateway [-f Config file name]\n");
} }
return 0; catch (Exception &ex)
{
ex.writeMessage();
abort();
}
} }