From 0fdae0386e2e55f489853561dc15055a168e5df1 Mon Sep 17 00:00:00 2001 From: Nikita Kostovsky Date: Mon, 17 Nov 2025 16:28:45 +0100 Subject: introduce Scanner --- src/iscanner.h | 4 ++++ src/main.cpp | 2 ++ src/protocols/httpserver.cpp | 23 +++++++++++++++++++++-- src/protocols/httpserver.h | 5 +++++ src/protocols/iprotocol.h | 4 ++++ src/protocols/pixelsudpstreamer.cpp | 7 +++++++ src/protocols/pixelsudpstreamer.h | 14 ++++++++++++++ src/scanner.cpp | 22 ++++++++++++++++++++++ src/scanner.h | 4 ++++ 9 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 src/protocols/pixelsudpstreamer.cpp create mode 100644 src/protocols/pixelsudpstreamer.h (limited to 'src') diff --git a/src/iscanner.h b/src/iscanner.h index 9ce03b7..75300e1 100644 --- a/src/iscanner.h +++ b/src/iscanner.h @@ -14,6 +14,10 @@ public: std::vector> protocols); virtual ~IScanner() = default; +public: + virtual bool startAllProtocols() = 0; + virtual void stopAllProtocols() = 0; + protected: std::shared_ptr m_camera; std::vector> m_protocols; diff --git a/src/main.cpp b/src/main.cpp index 6986751..01e4eac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -209,6 +209,8 @@ int main(int argc, char *argv[]) std::vector>{ httpServer}); + scanner->startAllProtocols(); + QHttpServer qHttpServer; qHttpServer.route("/v1/profile", [&]() -> QHttpServerResponse { diff --git a/src/protocols/httpserver.cpp b/src/protocols/httpserver.cpp index f1b562e..d10518b 100644 --- a/src/protocols/httpserver.cpp +++ b/src/protocols/httpserver.cpp @@ -28,8 +28,14 @@ HttpServer::HttpServer(std::shared_ptr camera, : IProtocol{camera} , INIT_FIELD(address) , INIT_FIELD(port) - , m_server{std::make_shared()} +// , m_server{std::make_shared()} { +} + +bool HttpServer::start() +{ + m_server = std::make_shared(); + // TODO: move these vars outside const auto apiPrefix = QStringLiteral("/v1"); const auto pixelsPath = apiPrefix + "/pixels"; @@ -52,7 +58,20 @@ HttpServer::HttpServer(std::shared_ptr camera, QHttpServerRequest::Method::Get, [this]() { return GET_image(); }); - qDebug().noquote() << Q_FUNC_INFO << ": listen: " << m_server->listen(m_address, m_port); + const auto result = m_server->listen(m_address, m_port); + + qDebug().noquote() << Q_FUNC_INFO << ": listen: " << result; + + if (!result) { + m_server.reset(); + } + + return result; +} + +void HttpServer::stop() +{ + m_server.reset(); } QHttpServerResponse HttpServer::GET_image() diff --git a/src/protocols/httpserver.h b/src/protocols/httpserver.h index 600ead4..bfc1e56 100644 --- a/src/protocols/httpserver.h +++ b/src/protocols/httpserver.h @@ -29,6 +29,11 @@ public: const uint16_t port = DefaultPort); ~HttpServer() override = default; + // IProtocol +public: + bool start() override; + void stop() override; + // TODO: methods starting with GET_/POST_ will be routed automatically public: QHttpServerResponse GET_image(); diff --git a/src/protocols/iprotocol.h b/src/protocols/iprotocol.h index 2d65dac..75259bc 100644 --- a/src/protocols/iprotocol.h +++ b/src/protocols/iprotocol.h @@ -10,6 +10,10 @@ public: explicit IProtocol(std::shared_ptr camera); virtual ~IProtocol() = default; +public: + virtual bool start() = 0; + virtual void stop() = 0; + protected: std::shared_ptr m_camera; }; diff --git a/src/protocols/pixelsudpstreamer.cpp b/src/protocols/pixelsudpstreamer.cpp new file mode 100644 index 0000000..d7c66c7 --- /dev/null +++ b/src/protocols/pixelsudpstreamer.cpp @@ -0,0 +1,7 @@ +#include "pixelsudpstreamer.h" + +PixelsUdpStreamer::PixelsUdpStreamer(std::shared_ptr camera) + : IProtocol{camera} +{} + +bool PixelsUdpStreamer::start() {} diff --git a/src/protocols/pixelsudpstreamer.h b/src/protocols/pixelsudpstreamer.h new file mode 100644 index 0000000..db8acc3 --- /dev/null +++ b/src/protocols/pixelsudpstreamer.h @@ -0,0 +1,14 @@ +#pragma once + +#include "iprotocol.h" + +class PixelsUdpStreamer : public IProtocol +{ +public: + explicit PixelsUdpStreamer(std::shared_ptr camera); + ~PixelsUdpStreamer() override = default; + +public: + bool start() override; + void stop() override; +}; diff --git a/src/scanner.cpp b/src/scanner.cpp index 1f14ca0..07d3c0f 100644 --- a/src/scanner.cpp +++ b/src/scanner.cpp @@ -1,8 +1,30 @@ #include "scanner.h" +#include "protocols/iprotocol.h" + Scanner::Scanner(std::shared_ptr camera, std::vector> protocols) : IScanner{camera, protocols} { // m_protocols.push_back() } + +bool Scanner::startAllProtocols() +{ + for (const auto& protocol : m_protocols) { + if (!protocol->start()) { + stopAllProtocols(); + + return false; + } + } + + return true; +} + +void Scanner::stopAllProtocols() +{ + for (const auto& protocol : m_protocols) { + protocol->stop(); + } +} diff --git a/src/scanner.h b/src/scanner.h index f1c1f52..8031dda 100644 --- a/src/scanner.h +++ b/src/scanner.h @@ -8,4 +8,8 @@ public: explicit Scanner(std::shared_ptr camera, std::vector> protocols); ~Scanner() override = default; + +public: + bool startAllProtocols() override; + void stopAllProtocols() override; }; -- cgit v1.2.3-70-g09d2