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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -17,6 +17,6 @@
#ifndef 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_ */

View File

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

View File

@@ -135,7 +135,7 @@ void Mutex::lock(void)
}
} 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
=========================================*/
RingBuffer::RingBuffer()
{
RingBuffer(MQTTSNGW_KEY_DIRECTORY);
}
RingBuffer::RingBuffer(const char* keyDirectory)
{
int fp = 0;
@@ -506,6 +501,7 @@ void RingBuffer::reset()
Thread::Thread()
{
_threadID = 0;
_taskName = nullptr;
}
Thread::~Thread()
@@ -539,11 +535,6 @@ int Thread::start(void)
return pthread_create(&_threadID, 0, _run, runnable);
}
void Thread::stopProcess(void)
{
theMultiTaskProcess->threadStopped();
}
void Thread::stop(void)
{
if ( _threadID )
@@ -552,3 +543,13 @@ void Thread::stop(void)
_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
{
public:
RingBuffer();
RingBuffer(const char* keyDirctory);
RingBuffer(const char* keyDirctory = MQTTSNGW_KEY_DIRECTORY);
~RingBuffer();
void put(char* buffer);
int get(char* buffer, int bufferLength);
@@ -124,15 +123,17 @@ public:
#define MAGIC_WORD_FOR_THREAD \
public: void EXECRUN() \
{ \
try \
{ \
run(); \
stopProcess(); \
} \
catch(...) \
{ \
throw; \
} \
try \
{ \
run(); \
theMultiTaskProcess->threadStopped(); \
} \
catch ( Exception &ex ) \
{ \
theMultiTaskProcess->threadStopped(); \
WRITELOG("%s catch exception\n", getTaskName()); \
ex.writeMessage(); \
} \
}
/*=====================================
@@ -146,12 +147,14 @@ public:
static pthread_t getID();
static bool equals(pthread_t*, pthread_t*);
virtual void initialize(int argc, char** argv);
void stopProcess(void);
void waitStop(void);
void stop(void);
const char* getTaskName(void);
void setTaskName(const char* name);
private:
static void* _run(void*);
pthread_t _threadID;
const char* _taskName;
};
}

View File

@@ -34,16 +34,14 @@ BrokerSendTask task5(&gateway);
int main(int argc, char** argv)
{
gateway.initialize(argc, argv);
gateway.run();
try
{
gateway.initialize(argc, argv);
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();
}
}