From 37fb3a9cba22b467f3f52e0b7d3645737812d1c4 Mon Sep 17 00:00:00 2001 From: cmccarthy1 <38653604+cmccarthy1@users.noreply.github.com> Date: Wed, 1 Jul 2020 15:47:47 +0100 Subject: [PATCH] Addition of graceful method to disconnect (#34) * addition of graceful method to disconnect * Fix to error grammar * Change to disconnect output to return null on successful execution --- q/mqtt.q | 5 ++++- src/mqtt.c | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/q/mqtt.q b/q/mqtt.q index 00b996d..5c8e005 100644 --- a/q/mqtt.q +++ b/q/mqtt.q @@ -1,9 +1,12 @@ \d .mqtt -connX :`mqtt 2:(`connX;3); +connX:`mqtt 2:(`connX;3); init :`mqtt 2:(`init;1); pubx :`mqtt 2:(`pub ;4); sub :`mqtt 2:(`sub ;1); unsub:`mqtt 2:(`unsub;1); +isConnected:`mqtt 2:(`isConnected;1); +disconnect :`mqtt 2:(`disconnect;1); + pub:.mqtt.pubx[;;1;0] conn:{[tcpconn;pname;opt]connX[tcpconn;pname](enlist[`]!enlist(::)),opt} diff --git a/src/mqtt.c b/src/mqtt.c index b1ad85d..5b901a3 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -143,6 +143,28 @@ EXP K connX(K tcpconn,K pname, K opt){ return (K)0; } +/* Disconnect from an mqtt client + * timeout = length of time in ms to allow for the client to clean up prior to disconnection +*/ + +EXP K disconnect(K timeout){ + if(!MQTTClient_isConnected(client)) + return krr((S)"No client is currently connected"); + else{ + MQTTClient_disconnect(client,(time_t)timeout->i); + MQTTClient_destroy(&client); + } + return (K)0; +} + +EXP K isConnected(K UNUSED(x)){ + if(!MQTTClient_isConnected(client)) + return kb(0); + else + return kb(1); +} + + /* Publish a message to a specified topic * topic = topic name as a symbol * msg = message content as a string