From de5fc21ccc8f3f8f5cb8866393c9ba5c6635d887 Mon Sep 17 00:00:00 2001 From: Gordon McCann Date: Sun, 23 Jan 2022 13:48:14 -0500 Subject: [PATCH] Added asio submodule for socket and created CompassOnlineSource. Needs testing. Some refinement to come for the source classes I think --- .gitmodules | 3 + Navigator/src/Navigator/Application.cpp | 13 --- .../Navigator/Physics/CompassOnlineSource.cpp | 96 +++++++++++++++++++ .../Navigator/Physics/CompassOnlineSource.h | 34 +++++++ .../src/Navigator/Physics/CompassRun.cpp | 5 +- Navigator/src/Navigator/Physics/CompassRun.h | 2 +- .../src/Navigator/Physics/DataSource.cpp | 3 +- Navigator/src/Navigator/Physics/DataSource.h | 2 +- Navigator/vendor/asio | 1 + premake5.lua | 11 ++- spectra.nav | 57 +++++++++++ 11 files changed, 205 insertions(+), 22 deletions(-) create mode 100644 Navigator/src/Navigator/Physics/CompassOnlineSource.cpp create mode 100644 Navigator/src/Navigator/Physics/CompassOnlineSource.h create mode 160000 Navigator/vendor/asio create mode 100644 spectra.nav diff --git a/.gitmodules b/.gitmodules index b9811c3..7bbf992 100644 --- a/.gitmodules +++ b/.gitmodules @@ -14,3 +14,6 @@ [submodule "Navigator/vendor/glm"] path = Navigator/vendor/glm url = https://github.com/g-truc/glm.git +[submodule "Navigator\\vendor\\asio"] + path = Navigator\\vendor\\asio + url = https://github.com/chriskohlhoff/asio.git diff --git a/Navigator/src/Navigator/Application.cpp b/Navigator/src/Navigator/Application.cpp index 7ba761f..ceffc7c 100644 --- a/Navigator/src/Navigator/Application.cpp +++ b/Navigator/src/Navigator/Application.cpp @@ -23,19 +23,6 @@ namespace Navigator { PushLayer(new EditorLayer()); m_imgui_layer = new ImGuiLayer(); PushOverlay(m_imgui_layer); - - /* - HistogramMap& histMap = HistogramMap::GetInstance(); - histMap.AddHistogram(HistogramParameters("myHisto", "joseph", 100, 0, 10)); - histMap.AddHistogram(HistogramParameters("myHisto2D", "joseph", "joseph", 100, 0, 10, 100, 0, 10)); - - CutMap::GetInstance().AddCut(CutParams("joe_cut","joseph"),0.0, 7.0); - CutMap::GetInstance().AddCut(CutParams("joe2D_cut", "joseph", "joseph"), { 1.0, 3.0, 3.0, 1.0, 1.0}, { 1.0, 1.0, 3.0, 3.0, 1.0}); - - histMap.AddCutToHistogramDraw("joe_cut", "myHisto"); - histMap.AddCutToHistogramDraw("joe2D_cut", "myHisto2D"); - - */ } Application::~Application() diff --git a/Navigator/src/Navigator/Physics/CompassOnlineSource.cpp b/Navigator/src/Navigator/Physics/CompassOnlineSource.cpp new file mode 100644 index 0000000..7ffdc94 --- /dev/null +++ b/Navigator/src/Navigator/Physics/CompassOnlineSource.cpp @@ -0,0 +1,96 @@ +#include "CompassOnlineSource.h" + +namespace Navigator { + + CompassOnlineSource::CompassOnlineSource(const std::string& hostname, const std::string& port) : + DataSource(), m_bufferIter(nullptr), m_bufferEnd(nullptr), m_socket(m_socketContext) + { + m_buffer.resize(m_bufferSize); + InitSocket(hostname, port); + } + + CompassOnlineSource::~CompassOnlineSource() {} + + void CompassOnlineSource::InitSocket(const std::string& hostname, const std::string& port) + { + m_validFlag = false; + try + { + asio::ip::tcp::resolver resolver(m_socketContext); + auto end_points = resolver.resolve(hostname, port); + asio::connect(m_socket, end_points); + } + catch (asio::system_error& error) + { + NAV_ERROR("Unable to connect to remote port for CompassOnlineSource! Error Code: {0}", error.what()); + return; + } + + NAV_INFO("Successfully connected to host {0} on port {1}", hostname, port); + m_validFlag = true; + } + + const CompassHit& CompassOnlineSource::GetData() + { + if (!IsValid()) + { + NAV_ERROR("Attempting to access invalid source at CompassOnlineSource!"); + m_currentHit = CompassHit(); + return m_currentHit; + } + else if (m_bufferIter == nullptr || m_bufferIter == m_bufferEnd) + { + FillBuffer(); + } + + if (m_bufferIter != m_bufferEnd) + { + GetHit(); + return m_currentHit; + } + else + { + m_currentHit = CompassHit(); + return m_currentHit; + } + } + + void CompassOnlineSource::FillBuffer() + { + asio::error_code code; + + size_t length = m_socket.read_some(asio::buffer(m_buffer), code); + m_bufferEnd = m_buffer.data() + length; + m_bufferIter = m_buffer.data(); + if (code == asio::error::eof) + { + NAV_WARN("CompassOnlineSource invalidated by host. Invalidating and detaching source."); + m_validFlag = false; + } + else if (code) + { + NAV_ERROR("CompassOnlineSource recieved unexpected error from host. Error message: {0}", code.message()); + NAV_WARN("Invalidating and detaching source."); + m_validFlag = false; + } + } + + void CompassOnlineSource::GetHit() + { + m_currentHit.board = *((uint16_t*)m_bufferIter); + m_bufferIter += 2; + m_currentHit.channel = *((uint16_t*)m_bufferIter); + m_bufferIter += 2; + m_currentHit.timestamp = *((uint64_t*)m_bufferIter); + m_bufferIter += 8; + m_currentHit.lgate = *((uint16_t*)m_bufferIter); + m_bufferIter += 2; + m_currentHit.sgate = *((uint16_t*)m_bufferIter); + m_bufferIter += 2; + m_currentHit.flags = *((uint32_t*)m_bufferIter); + m_bufferIter += 4; + m_currentHit.Ns = *((uint32_t*)m_bufferIter); + m_bufferIter += 4; + } + +} \ No newline at end of file diff --git a/Navigator/src/Navigator/Physics/CompassOnlineSource.h b/Navigator/src/Navigator/Physics/CompassOnlineSource.h new file mode 100644 index 0000000..a63ac86 --- /dev/null +++ b/Navigator/src/Navigator/Physics/CompassOnlineSource.h @@ -0,0 +1,34 @@ +#ifndef COMPASS_ONLINE_SOURCE_H +#define COMPASS_ONLINE_SOURCE_H + +#include "DataSource.h" +#include "asio/include/asio.hpp" + +namespace Navigator { + + class CompassOnlineSource : public DataSource + { + public: + CompassOnlineSource(const std::string& hostname, const std::string& port); + virtual ~CompassOnlineSource() override; + + virtual const CompassHit& GetData() override; + + private: + void InitSocket(const std::string& hostname, const std::string& port); + void FillBuffer(); + void GetHit(); + std::vector m_buffer; + static constexpr size_t m_bufferSize = 1000000; + char* m_bufferIter; + char* m_bufferEnd; + CompassHit m_currentHit; + + asio::io_context m_socketContext; + asio::ip::tcp::socket m_socket; + + }; + +} + +#endif \ No newline at end of file diff --git a/Navigator/src/Navigator/Physics/CompassRun.cpp b/Navigator/src/Navigator/Physics/CompassRun.cpp index d6050a5..2393a2b 100644 --- a/Navigator/src/Navigator/Physics/CompassRun.cpp +++ b/Navigator/src/Navigator/Physics/CompassRun.cpp @@ -114,12 +114,13 @@ namespace Navigator { return true; } - CompassHit CompassRun::GetData() + const CompassHit& CompassRun::GetData() { if(!IsValid()) { NAV_ERROR("Trying to access CompassRun data when invalid, bug detected!"); - return CompassHit(); + m_hit = CompassHit(); + return m_hit; } if(GetHitsFromFiles()) diff --git a/Navigator/src/Navigator/Physics/CompassRun.h b/Navigator/src/Navigator/Physics/CompassRun.h index b2fec0f..4738f1b 100644 --- a/Navigator/src/Navigator/Physics/CompassRun.h +++ b/Navigator/src/Navigator/Physics/CompassRun.h @@ -25,7 +25,7 @@ namespace Navigator { CompassRun(); CompassRun(const std::string& dir); virtual ~CompassRun(); - virtual CompassHit GetData() override; + virtual const CompassHit& GetData() override; inline void SetDirectory(const std::string& dir) { m_directory = dir; CollectFiles(); } inline void SetShiftMap(const std::string& filename) { m_smap.SetFile(filename); } diff --git a/Navigator/src/Navigator/Physics/DataSource.cpp b/Navigator/src/Navigator/Physics/DataSource.cpp index 16b4bb9..6a5c98b 100644 --- a/Navigator/src/Navigator/Physics/DataSource.cpp +++ b/Navigator/src/Navigator/Physics/DataSource.cpp @@ -1,5 +1,6 @@ #include "DataSource.h" #include "CompassRun.h" +#include "CompassOnlineSource.h" namespace Navigator { @@ -8,7 +9,7 @@ namespace Navigator { switch(type) { case DataSource::SourceType::CompassOffline : return new CompassRun(loc); - case DataSource::SourceType::CompassOnline : return nullptr; + case DataSource::SourceType::CompassOnline : return new CompassOnlineSource(loc, "5346"); case DataSource::SourceType::None : return nullptr; } } diff --git a/Navigator/src/Navigator/Physics/DataSource.h b/Navigator/src/Navigator/Physics/DataSource.h index 2fc9180..6c248f6 100644 --- a/Navigator/src/Navigator/Physics/DataSource.h +++ b/Navigator/src/Navigator/Physics/DataSource.h @@ -22,7 +22,7 @@ namespace Navigator { } virtual ~DataSource() {}; - virtual CompassHit GetData() = 0; + virtual const CompassHit& GetData() = 0; inline bool IsValid() { return m_validFlag; } protected: diff --git a/Navigator/vendor/asio b/Navigator/vendor/asio new file mode 160000 index 0000000..f70f65a --- /dev/null +++ b/Navigator/vendor/asio @@ -0,0 +1 @@ +Subproject commit f70f65ae54351c209c3a24704624144bfe8e70a3 diff --git a/premake5.lua b/premake5.lua index 908cdad..1febdf8 100644 --- a/premake5.lua +++ b/premake5.lua @@ -16,6 +16,7 @@ IncludeDirs["ImGui"] = "Navigator/vendor/imgui" IncludeDirs["glad"] = "Navigator/vendor/glad/include" IncludeDirs["ImPlot"] = "Navigator/vendor/implot" IncludeDirs["glm"] = "Navigator/vendor/glm" +IncludeDirs["asio"] = "Navigator/vendor/asio" include "Navigator/vendor/glfw" include "Navigator/vendor/imgui" @@ -44,7 +45,8 @@ project "Navigator" "%{IncludeDirs.ImGui}", "%{IncludeDirs.glad}", "%{IncludeDirs.ImPlot}", - "%{IncludeDirs.glm}" + "%{IncludeDirs.glm}", + "%{IncludeDirs.asio}" } filter {} @@ -55,7 +57,8 @@ project "Navigator" "%{IncludeDirs.ImGui}", "%{IncludeDirs.glad}", "%{IncludeDirs.ImPlot}", - "%{IncludeDirs.glm}" + "%{IncludeDirs.glm}", + "%{IncludeDirs.asio}" } @@ -145,8 +148,8 @@ project "NavProject" "Navigator/vendor/spdlog/include/", "Navigator/vendor/implot/", "Navigator/vendor", - "%{IncludeDirs.glm}" - + "%{IncludeDirs.glm}", + "%{IncludeDirs.asio}" } links { diff --git a/spectra.nav b/spectra.nav new file mode 100644 index 0000000..0712826 --- /dev/null +++ b/spectra.nav @@ -0,0 +1,57 @@ +begin_cuts + begin_cut1D + name: joe_cut + xparam: joseph + minValue: 0 + maxValue: 7 + end_cut1D + begin_cut2D + name: joe2D_cut + xparam: joseph + yparam: joseph + begin_xvalues + 1 + 3 + 3 + 1 + 1 + end_xvalues + begin_yvalues + 1 + 1 + 3 + 3 + 1 + end_yvalues + end_cut2D +end_cuts +begin_histograms + begin_histogram1D + name: myHisto + xparam: joseph + Nxbins: 100 + XMin: 0 + XMax: 10 + begin_cutsdrawn + joe_cut + end_cutsdrawn + begin_cutsapplied + end_cutsapplied + end_histogram1D + begin_histogram2D + name: myHisto2D + xparam: joseph + yparam: joseph + Nxbins: 100 + XMin: 0 + XMax: 10 + Nybins: 100 + YMin: 0 + YMax: 10 + begin_cutsdrawn + joe2D_cut + end_cutsdrawn + begin_cutsapplied + end_cutsapplied + end_histogram2D +end_histograms