Skip to content

Commit

Permalink
ToolsSocket starts using signals instead of callbacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed Nov 7, 2024
1 parent f5361b6 commit c25db76
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 101 deletions.
4 changes: 2 additions & 2 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ function (lib_tools)
target_link_libraries(${name} PUBLIC cc::comms Qt::Core ${CC_PLATFORM_SPECIFIC})
target_include_directories(${name} BEFORE
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/src
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
INTERFACE
$<INSTALL_INTERFACE:include>
)
Expand Down
52 changes: 3 additions & 49 deletions lib/include/cc_tools_qt/ToolsSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,55 +112,14 @@ class CC_TOOLS_API ToolsSocket : public QObject
/// @param[in] props Properties map.
void applyInterPluginConfig(const QVariantMap& props);

/// @brief Callback to report incoming data.
using DataReceivedCallback = std::function<void (ToolsDataInfoPtr)>;

/// @brief Set callback to report incoming data
/// @details The callback must have the same signature as @ref DataReceivedCallback.
template <typename TFunc>
void setDataReceivedCallback(TFunc&& func)
{
m_dataReceivedCallback = std::forward<TFunc>(func);
}

/// @brief Callback to report errors
using ErrorReportCallback = std::function<void (const QString& msg)>;

/// @brief Set callback to report errors
/// @details The callback must have the same signature as @ref ErrorReportCallback
template <typename TFunc>
void setErrorReportCallback(TFunc&& func)
{
m_errorReportCallback = std::forward<TFunc>(func);
}

/// @brief Callback to report socket connection status
using ConnectionStatusReportCallback = std::function <void (bool)>;

/// @brief Set callback to report socket connection
/// @details The callback must have the same signature as @ref ConnectionStatusReportCallback.
template <typename TFunc>
void setConnectionStatusReportCallback(TFunc&& func)
{
m_connectionStatusReportCallback = std::forward<TFunc>(func);
}

/// @brief Callback to report inter-plugin configuration updates
using InterPluginConfigReportCallback = std::function <void (const QVariantMap&)>;

/// @brief Set callback to report inter-plugin configuration.
/// @details The callback must have the same signature as @ref InterPluginConfigReportCallback.
template <typename TFunc>
void setInterPluginConfigReportCallback(TFunc&& func)
{
m_interPluginConfigReportCallback = std::forward<TFunc>(func);
}

/// @brief Set debug output level
/// @param[in] level Debug level. If @b 0, debug output is disabled
void setDebugOutputLevel(unsigned level = 0U);

signals:
void sigDataReceivedReport(ToolsDataInfoPtr data);
void sigErrorReport(const QString& msg);
void sigConnectionStatusReport(bool connected);
void sigInterPluginConfigReport(const QVariantMap& props) ;

protected:
Expand Down Expand Up @@ -240,11 +199,6 @@ class CC_TOOLS_API ToolsSocket : public QObject
private:
struct InnerState;
std::unique_ptr<InnerState> m_state;

DataReceivedCallback m_dataReceivedCallback;
ErrorReportCallback m_errorReportCallback;
ConnectionStatusReportCallback m_connectionStatusReportCallback;
InterPluginConfigReportCallback m_interPluginConfigReportCallback;
};

/// @brief Pointer to @ref ToolsSocket object.
Expand Down
71 changes: 42 additions & 29 deletions lib/src/ToolsMsgMgrImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,42 +266,25 @@ void ToolsMsgMgrImpl::addMsgs(const MessagesList& msgs, bool reportAdded)

void ToolsMsgMgrImpl::setSocket(ToolsSocketPtr socket)
{
if (m_socket) {
m_socket->setDataReceivedCallback(nullptr);
m_socket->setErrorReportCallback(nullptr);
m_socket->setConnectionStatusReportCallback(nullptr);
}

if (!socket) {
m_socket.reset();
return;
}

socket->setDataReceivedCallback(
[this](ToolsDataInfoPtr dataPtr)
{
socketDataReceived(std::move(dataPtr));
});
connect(
socket.get(), &ToolsSocket::sigDataReceivedReport,
this, &ToolsMsgMgrImpl::socketDataReceived
);

socket->setErrorReportCallback(
[this](const QString& msg)
{
reportError(msg);
});
connect(
socket.get(), &ToolsSocket::sigErrorReport,
this, &ToolsMsgMgrImpl::socketErrorReport
);

socket->setConnectionStatusReportCallback(
[this](bool connected)
{
for (auto& filter : m_filters) {
filter->socketConnectionReport(connected);
}

if (m_protocol) {
m_protocol->socketConnectionReport(connected);
}

reportSocketConnectionStatus(connected);
});
connect(
socket.get(), &ToolsSocket::sigConnectionStatusReport,
this, &ToolsMsgMgrImpl::socketConnectionReport
);

m_socket = std::move(socket);
}
Expand Down Expand Up @@ -384,8 +367,38 @@ void ToolsMsgMgrImpl::addFilter(ToolsFilterPtr filter)
m_filters.push_back(std::move(filter));
}

void ToolsMsgMgrImpl::socketErrorReport(const QString& msg)
{
if (m_socket.get() != sender()) {
return;
}

reportError(msg);
}

void ToolsMsgMgrImpl::socketConnectionReport(bool connected)
{
if (m_socket.get() != sender()) {
return;
}

for (auto& filter : m_filters) {
filter->socketConnectionReport(connected);
}

if (m_protocol) {
m_protocol->socketConnectionReport(connected);
}

reportSocketConnectionStatus(connected);
}

void ToolsMsgMgrImpl::socketDataReceived(ToolsDataInfoPtr dataInfoPtr)
{
if (m_socket.get() != sender()) {
return;
}

if ((!m_recvEnabled) || !(m_protocol) || (!dataInfoPtr)) {
return;
}
Expand Down
15 changes: 11 additions & 4 deletions lib/src/ToolsMsgMgrImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@

#pragma once

#include <vector>

#include "cc_tools_qt/ToolsMsgMgr.h"

#include <QtCore/QObject>

#include <vector>

namespace cc_tools_qt
{

class ToolsMsgMgrImpl
class ToolsMsgMgrImpl : public QObject
{
Q_OBJECT
public:
using MessagesList = ToolsMsgMgr::MessagesList;
using MsgType = ToolsMsgMgr::MsgType;
Expand Down Expand Up @@ -84,11 +87,15 @@ class ToolsMsgMgrImpl
m_socketConnectionStatusReportCallback = std::forward<TFunc>(func);
}

private slots:
void socketErrorReport(const QString& msg);
void socketConnectionReport(bool connected);
void socketDataReceived(ToolsDataInfoPtr dataInfoPtr);

private:
using MsgNumberType = unsigned long long;
using FiltersList = std::vector<ToolsFilterPtr>;

void socketDataReceived(ToolsDataInfoPtr dataInfoPtr);
void updateInternalId(ToolsMessage& msg);
void reportMsgAdded(ToolsMessagePtr msg);
void reportError(const QString& error);
Expand Down
24 changes: 7 additions & 17 deletions lib/src/ToolsSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,15 @@ bool ToolsSocket::isRunning() const
bool ToolsSocket::socketConnect()
{
m_state->m_connected = socketConnectImpl();
if (m_connectionStatusReportCallback) {
m_connectionStatusReportCallback(m_state->m_connected);
}
emit sigConnectionStatusReport(m_state->m_connected);
return m_state->m_connected;
}

void ToolsSocket::socketDisconnect()
{
socketDisconnectImpl();
m_state->m_connected = false;
if (m_connectionStatusReportCallback) {
m_connectionStatusReportCallback(false);
}
emit sigConnectionStatusReport(m_state->m_connected);
}

bool ToolsSocket::isSocketConnected() const
Expand Down Expand Up @@ -174,7 +170,7 @@ void ToolsSocket::applyInterPluginConfigImpl([[maybe_unused]] const QVariantMap&

void ToolsSocket::reportDataReceived(ToolsDataInfoPtr dataPtr)
{
if ((!m_state->m_running) || (!m_dataReceivedCallback)) {
if (!m_state->m_running) {
return;
}

Expand All @@ -192,31 +188,25 @@ void ToolsSocket::reportDataReceived(ToolsDataInfoPtr dataPtr)
std::cout << std::endl;
}

m_dataReceivedCallback(std::move(dataPtr));
emit sigDataReceivedReport(std::move(dataPtr));
}

void ToolsSocket::reportError(const QString& msg)
{
if (m_state->m_running && m_errorReportCallback) {
m_errorReportCallback(msg);
if (m_state->m_running) {
emit sigErrorReport(msg);
}
}

void ToolsSocket::reportDisconnected()
{
m_state->m_connected = false;
if (m_state->m_running && m_connectionStatusReportCallback) {
m_connectionStatusReportCallback(false);
}
emit sigConnectionStatusReport(false);
}

void ToolsSocket::reportInterPluginConfig(const QVariantMap& props)
{
emit sigInterPluginConfigReport(props);

if (m_interPluginConfigReportCallback) {
m_interPluginConfigReportCallback(props);
}
}

unsigned long long ToolsSocket::currTimestamp()
Expand Down

0 comments on commit c25db76

Please sign in to comment.