Skip to content

Commit

Permalink
Improve closing connection on interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
non-det-alle committed Dec 11, 2024
1 parent dc9cde3 commit 1e34b00
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
6 changes: 5 additions & 1 deletion examples/elora-example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,11 @@ main(int argc, char* argv[])
***************************/

///////////////////// Signal handling
OnInterrupt([](int signal) { csHelper.CloseConnection(signal); });
OnInterrupt([](int signal) {
csHelper.CloseConnection(signal);
OnInterrupt(SIG_DFL); // avoid multiple executions
exit(0);
});
///////////////////// Register tenant, gateways, and devices on the real server
csHelper.SetTenant(tenant);
csHelper.InitConnection(apiAddr, apiPort, token);
Expand Down
22 changes: 16 additions & 6 deletions examples/utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,28 @@ PrintConfigSetup(int nDevs, double range, int rings, std::vector<int>& devPerSF)
std::cout << ss.str();
}

static const int signals[] = {
SIGHUP,
SIGINT,
SIGQUIT,
SIGILL,
SIGABRT,
SIGFPE,
SIGSEGV,
SIGPIPE,
SIGTERM,
};

/**
* Setup action on interrupt
*/
void
OnInterrupt(sighandler_t action)
{
std::signal(SIGINT, action);
std::signal(SIGSEGV, action);
std::signal(SIGILL, action);
std::signal(SIGFPE, action);
std::signal(SIGABRT, action);
std::signal(SIGTERM, action);
for (int s : signals)
{
std::signal(s, action);
}
}

/**
Expand Down
23 changes: 11 additions & 12 deletions helper/chirpstack-helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ ChirpstackHelper::ChirpstackHelper()
m_session.appKey = "00000000000000000000000000000000";
}

ChirpstackHelper::~ChirpstackHelper()
{
CloseConnection(EXIT_SUCCESS);
}

int
ChirpstackHelper::InitConnection(const str address, uint16_t port, const str token)
{
Expand Down Expand Up @@ -70,7 +65,11 @@ ChirpstackHelper::InitConnection(const str address, uint16_t port, const str tok
void
ChirpstackHelper::CloseConnection(int signal)
{
NS_ASSERT_MSG(!m_session.tenantId.empty(), "Connection was not initialized before closing it");
/* Return immediatly if connection was not initialized to begin with */
if (m_session.tenantId.empty())
{
return;
}

str reply;

Expand Down Expand Up @@ -118,8 +117,8 @@ ChirpstackHelper::Register(NodeContainer c) const
int
ChirpstackHelper::CreateHttpIntegration(const str& encoding, const str& endpoint) const
{
NS_ASSERT_MSG(!m_session.tenantId.empty(),
"Connection was not initialized before creating integration");
NS_ABORT_MSG_IF(m_session.tenantId.empty(),
"Connection was not initialized before registering device");

str payload = "{"
" \"integration\": {"
Expand Down Expand Up @@ -149,8 +148,8 @@ ChirpstackHelper::CreateInfluxDb2Integration(const str& endpoint,
const str& bucket,
const str& token) const
{
NS_ASSERT_MSG(!m_session.tenantId.empty(),
"Connection was not initialized before creating integration");
NS_ABORT_MSG_IF(m_session.tenantId.empty(),
"Connection was not initialized before registering device");

str payload = "{"
" \"integration\": {"
Expand Down Expand Up @@ -376,8 +375,8 @@ int
ChirpstackHelper::RegisterPriv(Ptr<Node> node) const
{
NS_LOG_FUNCTION(this << node);
NS_ASSERT_MSG(!m_session.tenantId.empty(),
"Connection was not initialized before registering device");
NS_ABORT_MSG_IF(m_session.tenantId.empty(),
"Connection was not initialized before registering device");

Ptr<LoraNetDevice> netdev;
// We assume nodes can have at max 1 LoraNetDevice
Expand Down
2 changes: 0 additions & 2 deletions helper/chirpstack-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ class ChirpstackHelper
public:
ChirpstackHelper();

~ChirpstackHelper();

int InitConnection(const str address, uint16_t port, const str token);

void CloseConnection(int signal);
Expand Down

0 comments on commit 1e34b00

Please sign in to comment.