From 8e471d3534e4ea73b6cc9aa5fc9fa623f74e1d77 Mon Sep 17 00:00:00 2001 From: unai_71 Date: Tue, 10 Mar 2026 19:07:22 +0100 Subject: [PATCH] feat: Consumer header file, consumes socket to connect to an passes signal to main window (in theory) --- include/Consumer.hpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 include/Consumer.hpp diff --git a/include/Consumer.hpp b/include/Consumer.hpp new file mode 100644 index 0000000..7577741 --- /dev/null +++ b/include/Consumer.hpp @@ -0,0 +1,44 @@ +#pragma once +// ConsumerThread.hpp +// SPDX-License-Identifier: GPL-3.0-or-later +// Author: Unai Blazquez +#include +#include +#include +#include + +/// @brief Listens on a UNIX domain socket, receives integers from the +/// producer via IPC, prints them to console, and emits a Qt signal. +class ConsumerThread : public QObject +{ + Q_OBJECT + + public: + /// @brief Construct the consumer bound to a socket path. + /// @param socket_path UNIX domain socket path to listen on. + /// @param parent Optional QObject parent for memory management. + explicit ConsumerThread(const std::string& socket_path, + QObject* parent = nullptr); + + ~ConsumerThread() override; + + /// @brief Start the listener thread. The server socket is ready + /// when this function returns. + void start(); + + /// @brief Stop the listener thread gracefully. Safe to call multiple times. + void stop(); + + signals: + /// @brief Emitted every time an integer is received from the producer. + void valueReceived(int value); + + private: + /// @brief Main loop: accept → recv → print → emit. Runs in m_thread. + void run_loop(); + + std::string m_socket_path; + int m_server_fd = -1; + std::atomic m_running{false}; + std::thread m_thread; +};