SinkImpl: Refactor GetDescription, add GetError, SetEnabled.

This commit is contained in:
Peter Johnson
2016-10-26 23:31:48 -07:00
parent a5f63c3ae3
commit 5f69cb2a5b
4 changed files with 52 additions and 19 deletions

View File

@@ -332,20 +332,14 @@ void MJPEGServerImpl::SendJSON(llvm::raw_ostream& os, SourceImpl& source,
MJPEGServerImpl::MJPEGServerImpl(llvm::StringRef name,
llvm::StringRef description,
std::unique_ptr<wpi::NetworkAcceptor> 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<char>& buf) const {
return m_description;
}
void MJPEGServerImpl::Stop() {
m_active = false;

View File

@@ -9,9 +9,7 @@
#define CAMERASERVER_MJPEGSERVERIMPL_H_
#include <atomic>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
@@ -35,9 +33,6 @@ class MJPEGServerImpl : public SinkImpl {
std::unique_ptr<wpi::NetworkAcceptor> acceptor);
~MJPEGServerImpl() override;
llvm::StringRef GetDescription(
llvm::SmallVectorImpl<char>& 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<wpi::NetworkAcceptor> m_acceptor;
std::atomic_bool m_active; // set to false to terminate threads
std::thread m_serverThread;
std::mutex m_mutex;
std::vector<std::thread> m_connThreads;
std::vector<std::unique_ptr<wpi::NetworkStream>> m_connStreams;
};

View File

@@ -20,6 +20,18 @@ SinkImpl::~SinkImpl() {
}
}
void SinkImpl::SetDescription(llvm::StringRef description) {
std::lock_guard<std::mutex> lock(m_mutex);
m_description = description;
}
llvm::StringRef SinkImpl::GetDescription(
llvm::SmallVectorImpl<char>& buf) const {
std::lock_guard<std::mutex> lock(m_mutex);
buf.append(m_description.begin(), m_description.end());
return llvm::StringRef{buf.data(), buf.size()};
}
void SinkImpl::SetSource(std::shared_ptr<SourceImpl> source) {
std::lock_guard<std::mutex> lock(m_mutex);
if (m_source) {
@@ -30,3 +42,20 @@ void SinkImpl::SetSource(std::shared_ptr<SourceImpl> source) {
m_source->AddSink();
if (m_enabledCount > 0) m_source->EnableSink();
}
std::string SinkImpl::GetError() const {
std::lock_guard<std::mutex> 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<char>& buf) const {
std::lock_guard<std::mutex> 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()};
}

View File

@@ -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<char>& buf) const = 0;
void SetDescription(llvm::StringRef description);
llvm::StringRef GetDescription(llvm::SmallVectorImpl<char>& buf) const;
void Enable() {
std::lock_guard<std::mutex> 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<std::mutex> 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<SourceImpl> source);
std::shared_ptr<SourceImpl> GetSource() const {
@@ -50,9 +62,15 @@ class SinkImpl {
return m_source;
}
std::string GetError() const;
llvm::StringRef GetError(llvm::SmallVectorImpl<char>& 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<SourceImpl> m_source;
int m_enabledCount{0};
};