From ef0793a458228a55547fd4397875088a84fc2f3b Mon Sep 17 00:00:00 2001 From: Grzegorz Rychlik Date: Thu, 31 Oct 2019 16:20:38 +0100 Subject: [PATCH] Introduce MockDeviceBridge to enable logging from channel --- Src/ChannelLinter/ChannelLinter.cpp | 9 +- Src/ChannelLinter/ChannelLinter.vcxproj | 2 + .../ChannelLinter.vcxproj.filters | 22 +++++ Src/ChannelLinter/MockDeviceBridge.cpp | 93 +++++++++++++++++++ Src/ChannelLinter/MockDeviceBridge.h | 46 +++++++++ Src/ChannelLinter/StdAfx.h | 1 + 6 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 Src/ChannelLinter/ChannelLinter.vcxproj.filters create mode 100644 Src/ChannelLinter/MockDeviceBridge.cpp create mode 100644 Src/ChannelLinter/MockDeviceBridge.h diff --git a/Src/ChannelLinter/ChannelLinter.cpp b/Src/ChannelLinter/ChannelLinter.cpp index d70ea83..eeba16d 100644 --- a/Src/ChannelLinter/ChannelLinter.cpp +++ b/Src/ChannelLinter/ChannelLinter.cpp @@ -35,7 +35,9 @@ namespace MWR::C3::Linter auto MakeDevice(MWR::json const& createParams, const T& chInfo) { auto blob = MWR::C3::Core::Profiler::TranslateArguments(createParams); - return chInfo.m_Builder(blob); + auto channelBridge = std::make_shared(chInfo.m_Builder(blob)); + channelBridge->OnAttach(); + return channelBridge; } } @@ -68,6 +70,7 @@ try auto createParams = form.FillForm(config.m_ChannelArguments); auto channel = C3::Linter::MakeDevice(createParams, chInfo); + std::cout << "Create channel 2" << std::endl; auto const& ch2Args = config.m_ComplementaryChannelArguments ? *config.m_ComplementaryChannelArguments : form.GetComplementaryArgs(config.m_ChannelArguments); json createParams2 = form.FillForm(ch2Args); @@ -75,8 +78,8 @@ try // test write and read auto data = ByteVector(ByteView(MWR::Utils::GenerateRandomString(64))); - channel->OnSendToChannel(data); - auto rcv = ch2->OnReceiveFromChannel(); + channel->GetDevice()->OnSendToChannel(data); + auto rcv = std::static_pointer_cast(ch2->GetDevice())->OnReceiveFromChannel(); if (data != rcv) throw std::exception("data sent and received mismatch"); } diff --git a/Src/ChannelLinter/ChannelLinter.vcxproj b/Src/ChannelLinter/ChannelLinter.vcxproj index ce43db9..b38ac7b 100644 --- a/Src/ChannelLinter/ChannelLinter.vcxproj +++ b/Src/ChannelLinter/ChannelLinter.vcxproj @@ -300,6 +300,7 @@ + @@ -307,6 +308,7 @@ + Create Create diff --git a/Src/ChannelLinter/ChannelLinter.vcxproj.filters b/Src/ChannelLinter/ChannelLinter.vcxproj.filters new file mode 100644 index 0000000..d13c3df --- /dev/null +++ b/Src/ChannelLinter/ChannelLinter.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Src/ChannelLinter/MockDeviceBridge.cpp b/Src/ChannelLinter/MockDeviceBridge.cpp new file mode 100644 index 0000000..b6ed95c --- /dev/null +++ b/Src/ChannelLinter/MockDeviceBridge.cpp @@ -0,0 +1,93 @@ +#include "stdafx.h" +#include "MockDeviceBridge.h" + +namespace MWR::C3::Linter +{ + MockDeviceBridge::MockDeviceBridge(std::shared_ptr device ) : + m_Device(move(device)) + { + } + + void MockDeviceBridge::OnAttach() + { + GetDevice()->OnAttach(shared_from_this()); + } + + void MockDeviceBridge::Detach() + { + throw std::logic_error("The method or operation is not implemented."); + } + + void MockDeviceBridge::Close() + { + throw std::logic_error("The method or operation is not implemented."); + } + + void MockDeviceBridge::OnReceive() + { + throw std::logic_error("The method or operation is not implemented."); + } + + void MockDeviceBridge::PassNetworkPacket(ByteView packet) + { + throw std::logic_error("The method or operation is not implemented."); + } + + void MockDeviceBridge::OnPassNetworkPacket(ByteView packet) + { + throw std::logic_error("The method or operation is not implemented."); + } + + void MockDeviceBridge::PostCommandToConnector(ByteView command) + { + throw std::logic_error("The method or operation is not implemented."); + } + + void MockDeviceBridge::OnCommandFromConnector(ByteView command) + { + throw std::logic_error("The method or operation is not implemented."); + } + + MWR::ByteVector MockDeviceBridge::RunCommand(ByteView command) + { + throw std::logic_error("The method or operation is not implemented."); + } + + MWR::ByteVector MockDeviceBridge::WhoAreYou() + { + throw std::logic_error("The method or operation is not implemented."); + } + + void MockDeviceBridge::Log(LogMessage const& message) + { + static std::mutex mutex; + std::lock_guard lock(mutex); + std::cout << MWR::C3::Utils::ConvertLogMessageToConsoleText("", message, nullptr) << std::endl; + } + + void MockDeviceBridge::SetUpdateFrequency(std::chrono::milliseconds minUpdateFrequencyInMs, std::chrono::milliseconds maxUpdateFrequencyInMs) + { + throw std::logic_error("The method or operation is not implemented."); + } + + void MockDeviceBridge::SetUpdateFrequency(std::chrono::milliseconds frequencyInMs) + { + throw std::logic_error("The method or operation is not implemented."); + } + + void MockDeviceBridge::SetErrorStatus(std::string_view errorMessage) + { + throw std::logic_error("The method or operation is not implemented."); + } + + std::string MockDeviceBridge::GetErrorStatus() + { + throw std::logic_error("The method or operation is not implemented."); + } + + std::shared_ptr MockDeviceBridge::GetDevice() const + { + return m_Device; + } + +} diff --git a/Src/ChannelLinter/MockDeviceBridge.h b/Src/ChannelLinter/MockDeviceBridge.h new file mode 100644 index 0000000..39f29d7 --- /dev/null +++ b/Src/ChannelLinter/MockDeviceBridge.h @@ -0,0 +1,46 @@ +#pragma once + +namespace MWR::C3::Linter +{ + class MockDeviceBridge : public MWR::C3::AbstractDeviceBridge, public std::enable_shared_from_this + { + public: + MockDeviceBridge(std::shared_ptr device); + + void OnAttach() override; + + void Detach() override; + + void Close() override; + + void OnReceive() override; + + void PassNetworkPacket(ByteView packet) override; + + void OnPassNetworkPacket(ByteView packet) override; + + void PostCommandToConnector(ByteView command) override; + + void OnCommandFromConnector(ByteView command) override; + + ByteVector RunCommand(ByteView command) override; + + ByteVector WhoAreYou() override; + + void Log(LogMessage const& message) override; + + void SetUpdateFrequency(std::chrono::milliseconds minUpdateFrequencyInMs, std::chrono::milliseconds maxUpdateFrequencyInMs) override; + + void SetUpdateFrequency(std::chrono::milliseconds frequencyInMs) override; + + void SetErrorStatus(std::string_view errorMessage) override; + + std::string GetErrorStatus() override; + + std::shared_ptr GetDevice() const; + + private: + std::shared_ptr m_Device; + }; +} + diff --git a/Src/ChannelLinter/StdAfx.h b/Src/ChannelLinter/StdAfx.h index ad9dc53..689ba69 100644 --- a/Src/ChannelLinter/StdAfx.h +++ b/Src/ChannelLinter/StdAfx.h @@ -21,3 +21,4 @@ using json = nlohmann::json; #include "InputVector.h" #include "FormElement.hpp" #include "Form.h" +#include "MockDeviceBridge.h"