Introduce MockDeviceBridge to enable logging from channel

dependabot/npm_and_yarn/Src/WebController/UI/websocket-extensions-0.1.4
Grzegorz Rychlik 2019-10-31 16:20:38 +01:00
parent d95a7dd653
commit ef0793a458
6 changed files with 170 additions and 3 deletions

View File

@ -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<C3::Linter::MockDeviceBridge>(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<C3::AbstractChannel>(ch2->GetDevice())->OnReceiveFromChannel();
if (data != rcv)
throw std::exception("data sent and received mismatch");
}

View File

@ -300,6 +300,7 @@
<ClInclude Include="FormElement.hpp" />
<ClInclude Include="InputContext.h" />
<ClInclude Include="InputVector.h" />
<ClInclude Include="MockDeviceBridge.h" />
<ClInclude Include="StdAfx.h" />
</ItemGroup>
<ItemGroup>
@ -307,6 +308,7 @@
<ClCompile Include="Form.cpp" />
<ClCompile Include="InputContext.cpp" />
<ClCompile Include="InputVector.cpp" />
<ClCompile Include="MockDeviceBridge.cpp" />
<ClCompile Include="StdAfx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Manifest Include="..\Common\MWR\WinTools\OsVersion.manifest" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="ChannelLinter.cpp" />
<ClCompile Include="Form.cpp" />
<ClCompile Include="InputContext.cpp" />
<ClCompile Include="InputVector.cpp" />
<ClCompile Include="StdAfx.cpp" />
<ClCompile Include="MockDeviceBridge.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Form.h" />
<ClInclude Include="FormElement.hpp" />
<ClInclude Include="InputContext.h" />
<ClInclude Include="InputVector.h" />
<ClInclude Include="StdAfx.h" />
<ClInclude Include="MockDeviceBridge.h" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,93 @@
#include "stdafx.h"
#include "MockDeviceBridge.h"
namespace MWR::C3::Linter
{
MockDeviceBridge::MockDeviceBridge(std::shared_ptr<Device> 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<MWR::C3::Device> MockDeviceBridge::GetDevice() const
{
return m_Device;
}
}

View File

@ -0,0 +1,46 @@
#pragma once
namespace MWR::C3::Linter
{
class MockDeviceBridge : public MWR::C3::AbstractDeviceBridge, public std::enable_shared_from_this<MockDeviceBridge>
{
public:
MockDeviceBridge(std::shared_ptr<Device> 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<MWR::C3::Device> GetDevice() const;
private:
std::shared_ptr<Device> m_Device;
};
}

View File

@ -21,3 +21,4 @@ using json = nlohmann::json;
#include "InputVector.h"
#include "FormElement.hpp"
#include "Form.h"
#include "MockDeviceBridge.h"