Skip to content

Commit

Permalink
Added "disconnect" operation to the new client.
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed Jul 4, 2024
1 parent 01111db commit 72582d6
Show file tree
Hide file tree
Showing 15 changed files with 810 additions and 65 deletions.
1 change: 1 addition & 0 deletions client/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ function (gen_lib_mqttsn_client config_file)
message (STATUS "Defining library ${lib_name}")
set (src
src/op/ConnectOp.cpp
src/op/DisconnectOp.cpp
src/op/KeepAliveOp.cpp
src/op/Op.cpp
src/op/SearchOp.cpp
Expand Down
17 changes: 17 additions & 0 deletions client/lib/include/cc_mqttsn_client/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ struct CC_MqttsnConnect;
/// @ingroup "connect".
typedef struct CC_MqttsnConnect* CC_MqttsnConnectHandle;

/// @brief Declaration of the hidden structure used to define @ref CC_MqttsnDisconnectHandle
/// @ingroup disconnect
struct CC_MqttsnDisconnect;

/// @brief Handle for "disconnect" operation.
/// @details Returned by @b cc_mqttsn_client_disconnect_prepare() function.
/// @ingroup "disconnect".
typedef struct CC_MqttsnDisconnect* CC_MqttsnDisconnectHandle;


/// @brief Type used to hold Topic ID value.
typedef unsigned short CC_MqttsnTopicId;
Expand Down Expand Up @@ -288,6 +297,14 @@ typedef void (*CC_MqttsnSearchCompleteCb)(void* data, CC_MqttsnAsyncOpStatus sta
/// @ingroup connect
typedef void (*CC_MqttsnConnectCompleteCb)(void* data, CC_MqttsnAsyncOpStatus status, const CC_MqttsnConnectInfo* info);

/// @brief Callback used to report completion of the disconnect operation.
/// @param[in] data Pointer to user data object, passed as the last parameter to
/// the request call.
/// @param[in] status Status of the "disconnect" operation.
/// @post The data members of the reported response can NOT be accessed after the function returns.
/// @ingroup disconnect
typedef void (*CC_MqttsnDisconnectCompleteCb)(void* data, CC_MqttsnAsyncOpStatus status);


#ifdef __cplusplus
}
Expand Down
114 changes: 61 additions & 53 deletions client/lib/src/ClientImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ op::ConnectOp* ClientImpl::connectPrepare(CC_MqttsnErrorCode* ec)
break;
}

if (!m_disconnectOps.empty()) {
// Already allocated
errorLog("Another disconnect operation is in progress.");
updateEc(ec, CC_MqttsnErrorCode_Busy);
break;
}

if (m_sessionState.m_disconnecting) {
errorLog("Session disconnection is in progress, cannot initiate connection.");
updateEc(ec, CC_MqttsnErrorCode_Disconnecting);
Expand Down Expand Up @@ -210,62 +217,63 @@ op::ConnectOp* ClientImpl::connectPrepare(CC_MqttsnErrorCode* ec)
return op;
}

// op::DisconnectOp* ClientImpl::disconnectPrepare(CC_MqttsnErrorCode* ec)
// {
// op::DisconnectOp* disconnectOp = nullptr;
// do {
// if (!m_sessionState.m_connected) {
// errorLog("Client must be connected to allow disconnect.");
// updateEc(ec, CC_MqttsnErrorCode_NotConnected);
// break;
// }
op::DisconnectOp* ClientImpl::disconnectPrepare(CC_MqttsnErrorCode* ec)
{
op::DisconnectOp* op = nullptr;
do {
if (!m_sessionState.m_connected) {
errorLog("Client must be connected to allow disconnect.");
updateEc(ec, CC_MqttsnErrorCode_NotConnected);
break;
}

// if (!m_disconnectOps.empty()) {
// errorLog("Another disconnect operation is in progress.");
// updateEc(ec, CC_MqttsnErrorCode_Busy);
// break;
// }
if (!m_disconnectOps.empty()) {
errorLog("Another disconnect operation is in progress.");
updateEc(ec, CC_MqttsnErrorCode_Busy);
break;
}

// if (m_sessionState.m_disconnecting) {
// errorLog("Session disconnection is in progress, cannot initiate disconnection.");
// updateEc(ec, CC_MqttsnErrorCode_Disconnecting);
// break;
// }
if (!m_connectOps.empty()) {
// Already allocated
errorLog("Another connect operation is in progress.");
updateEc(ec, CC_MqttsnErrorCode_Busy);
break;
}

// if (m_clientState.m_networkDisconnected) {
// errorLog("Network is disconnected.");
// updateEc(ec, CC_MqttsnErrorCode_NetworkDisconnected);
// break;
// }
if (m_sessionState.m_disconnecting) {
errorLog("Session disconnection is in progress, cannot initiate disconnection.");
updateEc(ec, CC_MqttsnErrorCode_Disconnecting);
break;
}

// if (m_ops.max_size() <= m_ops.size()) {
// errorLog("Cannot start disconnect operation, retry in next event loop iteration.");
// updateEc(ec, CC_MqttsnErrorCode_RetryLater);
// break;
// }
if (m_ops.max_size() <= m_ops.size()) {
errorLog("Cannot start disconnect operation, retry in next event loop iteration.");
updateEc(ec, CC_MqttsnErrorCode_RetryLater);
break;
}

// if (m_preparationLocked) {
// errorLog("Another operation is being prepared, cannot prepare \"disconnect\" without \"send\" or \"cancel\" of the previous.");
// updateEc(ec, CC_MqttsnErrorCode_PreparationLocked);
// break;
// }
if (m_preparationLocked) {
errorLog("Another operation is being prepared, cannot prepare \"disconnect\" without \"send\" or \"cancel\" of the previous.");
updateEc(ec, CC_MqttsnErrorCode_PreparationLocked);
break;
}

// auto ptr = m_disconnectOpsAlloc.alloc(*this);
// if (!ptr) {
// errorLog("Cannot allocate new disconnect operation.");
// updateEc(ec, CC_MqttsnErrorCode_OutOfMemory);
// break;
// }
auto ptr = m_disconnectOpsAlloc.alloc(*this);
if (!ptr) {
errorLog("Cannot allocate new disconnect operation.");
updateEc(ec, CC_MqttsnErrorCode_OutOfMemory);
break;
}

// m_preparationLocked = true;
// m_ops.push_back(ptr.get());
// m_disconnectOps.push_back(std::move(ptr));
// disconnectOp = m_disconnectOps.back().get();
// updateEc(ec, CC_MqttsnErrorCode_Success);
// } while (false);
m_preparationLocked = true;
m_ops.push_back(ptr.get());
m_disconnectOps.push_back(std::move(ptr));
op = m_disconnectOps.back().get();
updateEc(ec, CC_MqttsnErrorCode_Success);
} while (false);

// return disconnectOp;
// }
return op;
}

// op::SubscribeOp* ClientImpl::subscribePrepare(CC_MqttsnErrorCode* ec)
// {
Expand Down Expand Up @@ -829,7 +837,7 @@ void ClientImpl::opComplete(const op::Op* op)
/* Type_Search */ &ClientImpl::opComplete_Search,
/* Type_Connect */ &ClientImpl::opComplete_Connect,
/* Type_KeepAlive */ &ClientImpl::opComplete_KeepAlive,
// /* Type_Disconnect */ &ClientImpl::opComplete_Disconnect,
/* Type_Disconnect */ &ClientImpl::opComplete_Disconnect,
// /* Type_Subscribe */ &ClientImpl::opComplete_Subscribe,
// /* Type_Unsubscribe */ &ClientImpl::opComplete_Unsubscribe,
// /* Type_Recv */ &ClientImpl::opComplete_Recv,
Expand Down Expand Up @@ -1172,10 +1180,10 @@ void ClientImpl::opComplete_KeepAlive(const op::Op* op)
eraseFromList(op, m_keepAliveOps);
}

// void ClientImpl::opComplete_Disconnect(const op::Op* op)
// {
// eraseFromList(op, m_disconnectOps);
// }
void ClientImpl::opComplete_Disconnect(const op::Op* op)
{
eraseFromList(op, m_disconnectOps);
}

// void ClientImpl::opComplete_Subscribe(const op::Op* op)
// {
Expand Down
14 changes: 7 additions & 7 deletions client/lib/src/ClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "TimerMgr.h"

#include "op/ConnectOp.h"
// #include "op/DisconnectOp.h"
#include "op/DisconnectOp.h"
#include "op/KeepAliveOp.h"
#include "op/Op.h"
// #include "op/RecvOp.h"
Expand Down Expand Up @@ -70,7 +70,7 @@ class ClientImpl final : public ProtMsgHandler

op::SearchOp* searchPrepare(CC_MqttsnErrorCode* ec);
op::ConnectOp* connectPrepare(CC_MqttsnErrorCode* ec);
// op::DisconnectOp* disconnectPrepare(CC_MqttsnErrorCode* ec);
op::DisconnectOp* disconnectPrepare(CC_MqttsnErrorCode* ec);
// op::SubscribeOp* subscribePrepare(CC_MqttsnErrorCode* ec);
// op::UnsubscribeOp* unsubscribePrepare(CC_MqttsnErrorCode* ec);
// op::SendOp* publishPrepare(CC_MqttsnErrorCode* ec);
Expand Down Expand Up @@ -240,8 +240,8 @@ class ClientImpl final : public ProtMsgHandler
using KeepAliveOpAlloc = ObjAllocator<op::KeepAliveOp, ExtConfig::KeepAliveOpsLimit>;
using KeepAliveOpsList = ObjListType<KeepAliveOpAlloc::Ptr, ExtConfig::KeepAliveOpsLimit>;

// using DisconnectOpAlloc = ObjAllocator<op::DisconnectOp, ExtConfig::DisconnectOpsLimit>;
// using DisconnectOpsList = ObjListType<DisconnectOpAlloc::Ptr, ExtConfig::DisconnectOpsLimit>;
using DisconnectOpAlloc = ObjAllocator<op::DisconnectOp, ExtConfig::DisconnectOpsLimit>;
using DisconnectOpsList = ObjListType<DisconnectOpAlloc::Ptr, ExtConfig::DisconnectOpsLimit>;

// using SubscribeOpAlloc = ObjAllocator<op::SubscribeOp, ExtConfig::SubscribeOpsLimit>;
// using SubscribeOpsList = ObjListType<SubscribeOpAlloc::Ptr, ExtConfig::SubscribeOpsLimit>;
Expand Down Expand Up @@ -282,7 +282,7 @@ class ClientImpl final : public ProtMsgHandler
void opComplete_Search(const op::Op* op);
void opComplete_Connect(const op::Op* op);
void opComplete_KeepAlive(const op::Op* op);
// void opComplete_Disconnect(const op::Op* op);
void opComplete_Disconnect(const op::Op* op);
// void opComplete_Subscribe(const op::Op* op);
// void opComplete_Unsubscribe(const op::Op* op);
// void opComplete_Recv(const op::Op* op);
Expand Down Expand Up @@ -346,8 +346,8 @@ class ClientImpl final : public ProtMsgHandler
KeepAliveOpAlloc m_keepAliveOpsAlloc;
KeepAliveOpsList m_keepAliveOps;

// DisconnectOpAlloc m_disconnectOpsAlloc;
// DisconnectOpsList m_disconnectOps;
DisconnectOpAlloc m_disconnectOpsAlloc;
DisconnectOpsList m_disconnectOps;

// SubscribeOpAlloc m_subscribeOpsAlloc;
// SubscribeOpsList m_subscribeOps;
Expand Down
8 changes: 5 additions & 3 deletions client/lib/src/ExtConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct ExtConfig : public Config
static constexpr unsigned ConnectOpTimers = 1U;
static constexpr unsigned KeepAliveOpTimers = 3U;
static constexpr unsigned DisconnectOpsLimit = HasDynMemAlloc ? 0 : 1U;
static constexpr unsigned DisconnectOpTimers = 0U;
static constexpr unsigned DisconnectOpTimers = 1U;
static constexpr unsigned SubscribeOpTimers = 1U;
static constexpr unsigned UnsubscribeOpTimers = 1U;
static constexpr unsigned RecvOpsLimit = MaxQos < 2 ? 1U : (ReceiveMaxLimit == 0U ? 0U : ReceiveMaxLimit + 1U);
Expand All @@ -34,8 +34,8 @@ struct ExtConfig : public Config
static constexpr bool HasOpsLimit =
(SearchOpsLimit > 0U) &&
(ConnectOpsLimit > 0U) &&
(KeepAliveOpsLimit > 0U) /* &&
(DisconnectOpsLimit > 0U) &&
(KeepAliveOpsLimit > 0U) &&
(DisconnectOpsLimit > 0U) /* &&
(SubscribeOpsLimit > 0U) &&
(UnsubscribeOpsLimit > 0U) &&
(RecvOpsLimit > 0U) &&
Expand All @@ -44,6 +44,7 @@ struct ExtConfig : public Config
(DiscoveryTimers) +
(SearchOpsLimit * SearchOpTimers) +
(ConnectOpsLimit * ConnectOpTimers) +
(DisconnectOpsLimit * DisconnectOpTimers) +
(KeepAliveOpsLimit * KeepAliveOpTimers) +
(DisconnectOpsLimit * DisconnectOpTimers) +
(SubscribeOpsLimit * SubscribeOpTimers) +
Expand Down Expand Up @@ -73,6 +74,7 @@ struct ExtConfig : public Config
static_assert(HasDynMemAlloc || (TimersLimit > 0U));
static_assert(HasDynMemAlloc || (ConnectOpsLimit > 0U));
static_assert(HasDynMemAlloc || (KeepAliveOpsLimit > 0U));
static_assert(HasDynMemAlloc || (DisconnectOpsLimit > 0U));
// static_assert(HasDynMemAlloc || (RecvOpsLimit > 0U));
// static_assert(HasDynMemAlloc || (SendOpsLimit > 0U));
static_assert(HasDynMemAlloc || (OpsLimit > 0U));
Expand Down
Loading

0 comments on commit 72582d6

Please sign in to comment.