From 5f69cb2a5bc6009dc7f677e735f49afc30f76ec6 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 26 Oct 2016 23:31:48 -0700 Subject: [PATCH] SinkImpl: Refactor GetDescription, add GetError, SetEnabled. --- src/MJPEGServerImpl.cpp | 10 ++-------- src/MJPEGServerImpl.h | 8 -------- src/SinkImpl.cpp | 29 +++++++++++++++++++++++++++++ src/SinkImpl.h | 24 +++++++++++++++++++++--- 4 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/MJPEGServerImpl.cpp b/src/MJPEGServerImpl.cpp index abbca3f4e6..9d95b3779d 100644 --- a/src/MJPEGServerImpl.cpp +++ b/src/MJPEGServerImpl.cpp @@ -332,20 +332,14 @@ void MJPEGServerImpl::SendJSON(llvm::raw_ostream& os, SourceImpl& source, MJPEGServerImpl::MJPEGServerImpl(llvm::StringRef name, llvm::StringRef description, std::unique_ptr acceptor) - : SinkImpl{name}, - m_description(description), - m_acceptor{std::move(acceptor)} { + : SinkImpl{name}, m_acceptor{std::move(acceptor)} { m_active = true; + SetDescription(description); m_serverThread = std::thread(&MJPEGServerImpl::ServerThreadMain, this); } MJPEGServerImpl::~MJPEGServerImpl() { Stop(); } -llvm::StringRef MJPEGServerImpl::GetDescription( - llvm::SmallVectorImpl& buf) const { - return m_description; -} - void MJPEGServerImpl::Stop() { m_active = false; diff --git a/src/MJPEGServerImpl.h b/src/MJPEGServerImpl.h index f51ee42b32..c590582d37 100644 --- a/src/MJPEGServerImpl.h +++ b/src/MJPEGServerImpl.h @@ -9,9 +9,7 @@ #define CAMERASERVER_MJPEGSERVERIMPL_H_ #include -#include #include -#include #include #include @@ -35,9 +33,6 @@ class MJPEGServerImpl : public SinkImpl { std::unique_ptr acceptor); ~MJPEGServerImpl() override; - llvm::StringRef GetDescription( - llvm::SmallVectorImpl& buf) const override; - void Stop(); static void SendHeader(llvm::raw_ostream& os, int code, @@ -59,13 +54,10 @@ class MJPEGServerImpl : public SinkImpl { void ServerThreadMain(); void ConnThreadMain(wpi::NetworkStream* stream); - std::string m_description; - std::unique_ptr m_acceptor; std::atomic_bool m_active; // set to false to terminate threads std::thread m_serverThread; - std::mutex m_mutex; std::vector m_connThreads; std::vector> m_connStreams; }; diff --git a/src/SinkImpl.cpp b/src/SinkImpl.cpp index 7104a6d390..fa76750247 100644 --- a/src/SinkImpl.cpp +++ b/src/SinkImpl.cpp @@ -20,6 +20,18 @@ SinkImpl::~SinkImpl() { } } +void SinkImpl::SetDescription(llvm::StringRef description) { + std::lock_guard lock(m_mutex); + m_description = description; +} + +llvm::StringRef SinkImpl::GetDescription( + llvm::SmallVectorImpl& buf) const { + std::lock_guard lock(m_mutex); + buf.append(m_description.begin(), m_description.end()); + return llvm::StringRef{buf.data(), buf.size()}; +} + void SinkImpl::SetSource(std::shared_ptr source) { std::lock_guard lock(m_mutex); if (m_source) { @@ -30,3 +42,20 @@ void SinkImpl::SetSource(std::shared_ptr source) { m_source->AddSink(); if (m_enabledCount > 0) m_source->EnableSink(); } + +std::string SinkImpl::GetError() const { + std::lock_guard lock(m_mutex); + if (!m_source) return "no source connected"; + auto frame = m_source->GetCurFrame(); + if (frame) return std::string{}; // no error + return llvm::StringRef{frame}; +} + +llvm::StringRef SinkImpl::GetError(llvm::SmallVectorImpl& buf) const { + std::lock_guard lock(m_mutex); + if (!m_source) return "no source connected"; + auto frame = m_source->GetCurFrame(); + if (frame) return llvm::StringRef{}; // no error + buf.append(frame.data(), frame.data() + frame.size()); + return llvm::StringRef{buf.data(), buf.size()}; +} diff --git a/src/SinkImpl.h b/src/SinkImpl.h index 95da265068..03fef945b2 100644 --- a/src/SinkImpl.h +++ b/src/SinkImpl.h @@ -28,8 +28,9 @@ class SinkImpl { SinkImpl& operator=(const SinkImpl& queue) = delete; llvm::StringRef GetName() const { return m_name; } - virtual llvm::StringRef GetDescription( - llvm::SmallVectorImpl& buf) const = 0; + + void SetDescription(llvm::StringRef description); + llvm::StringRef GetDescription(llvm::SmallVectorImpl& buf) const; void Enable() { std::lock_guard lock(m_mutex); @@ -43,6 +44,17 @@ class SinkImpl { if (m_enabledCount == 0 && m_source) m_source->DisableSink(); } + void SetEnabled(bool enabled) { + std::lock_guard lock(m_mutex); + if (enabled && m_enabledCount == 0) { + if (m_source) m_source->EnableSink(); + m_enabledCount = 1; + } else if (!enabled && m_enabledCount > 0) { + if (m_source) m_source->DisableSink(); + m_enabledCount = 0; + } + } + void SetSource(std::shared_ptr source); std::shared_ptr GetSource() const { @@ -50,9 +62,15 @@ class SinkImpl { return m_source; } + std::string GetError() const; + llvm::StringRef GetError(llvm::SmallVectorImpl& buf) const; + + protected: + mutable std::mutex m_mutex; + private: std::string m_name; - mutable std::mutex m_mutex; + std::string m_description; std::shared_ptr m_source; int m_enabledCount{0}; };