From 7af1bf51dee3fd3834895170e90ebbd87efe2edc Mon Sep 17 00:00:00 2001 From: Sid Dahiya Date: Mon, 19 Aug 2019 11:32:01 -0700 Subject: [PATCH] - Rename `DataViewerCollectionImpl` to `DataViewerCollection` - Updated Locks to make them C++11 compatible. --- .../Clienttelemetry/Clienttelemetry.vcxitems | 4 +- .../Clienttelemetry.vcxitems.filters | 8 +- lib/api/DataViewerCollection.cpp | 82 ++++++++++++++++ ...ctionImpl.hpp => DataViewerCollection.hpp} | 12 +-- lib/api/DataViewerCollectionImpl.cpp | 94 ------------------- lib/api/LogManagerImpl.cpp | 7 +- ...ests.cpp => DataViewerCollectionTests.cpp} | 14 +-- tests/unittests/UnitTests.vcxproj | 2 +- tests/unittests/UnitTests.vcxproj.filters | 2 +- 9 files changed, 106 insertions(+), 119 deletions(-) create mode 100644 lib/api/DataViewerCollection.cpp rename lib/api/{DataViewerCollectionImpl.hpp => DataViewerCollection.hpp} (86%) delete mode 100644 lib/api/DataViewerCollectionImpl.cpp rename tests/unittests/{DataViewerCollectionImplTests.cpp => DataViewerCollectionTests.cpp} (96%) diff --git a/Solutions/Clienttelemetry/Clienttelemetry.vcxitems b/Solutions/Clienttelemetry/Clienttelemetry.vcxitems index cac123cd..0ba8fa42 100644 --- a/Solutions/Clienttelemetry/Clienttelemetry.vcxitems +++ b/Solutions/Clienttelemetry/Clienttelemetry.vcxitems @@ -24,7 +24,7 @@ - + @@ -68,7 +68,7 @@ - + diff --git a/Solutions/Clienttelemetry/Clienttelemetry.vcxitems.filters b/Solutions/Clienttelemetry/Clienttelemetry.vcxitems.filters index 14602471..8f87a612 100644 --- a/Solutions/Clienttelemetry/Clienttelemetry.vcxitems.filters +++ b/Solutions/Clienttelemetry/Clienttelemetry.vcxitems.filters @@ -204,7 +204,7 @@ system - + api @@ -569,12 +569,12 @@ system - - api - include\public + + api + diff --git a/lib/api/DataViewerCollection.cpp b/lib/api/DataViewerCollection.cpp new file mode 100644 index 00000000..ab09be08 --- /dev/null +++ b/lib/api/DataViewerCollection.cpp @@ -0,0 +1,82 @@ +#ifdef _MSC_VER +// evntprov.h(838) : warning C4459 : declaration of 'Version' hides global declaration +#pragma warning( disable : 4459 ) +#endif + +#include "DataViewerCollection.hpp" +#include + +namespace ARIASDK_NS_BEGIN { + + MATSDK_LOG_INST_COMPONENT_CLASS(DataViewerCollection, "EventsSDK.DataViewerCollection", "Microsoft Telemetry Client - DataViewerCollection class"); + + void DataViewerCollection::DispatchDataViewerEvent(const std::vector& packetData) noexcept + { + if (IsViewerEnabled() == false) + return; + + LOCKGUARD(m_dataViewerMapLock); + auto dataViewerIterator = m_dataViewerCollection.cbegin(); + while (dataViewerIterator != m_dataViewerCollection.cend()) + { + //ToDo: Send data asynchronously to individual viewers + dataViewerIterator->second->RecieveData(packetData); + } + }; + + void DataViewerCollection::RegisterViewer(const std::shared_ptr& dataViewer) + { + + if (dataViewer == nullptr) + { + throw std::invalid_argument("nullptr passed for data viewer"); + } + + LOCKGUARD(m_dataViewerMapLock); + if (m_dataViewerCollection.find(dataViewer->GetName()) != m_dataViewerCollection.end()) + { + throw std::invalid_argument(std::string { "Viewer: '%s' is already registered", dataViewer->GetName() }); + } + + m_dataViewerCollection.emplace(dataViewer->GetName(), std::move(dataViewer)); + } + + void DataViewerCollection::UnregisterViewer(const char* viewerName) + { + if (viewerName == nullptr) + { + throw std::invalid_argument("nullptr passed for viewer name"); + } + + LOCKGUARD(m_dataViewerMapLock); + if (m_dataViewerCollection.find(viewerName) == m_dataViewerCollection.end()) + { + throw std::invalid_argument(std::string { "Viewer: '%s' is not currently registered", viewerName }); + } + + m_dataViewerCollection.erase(viewerName); + } + + void DataViewerCollection::UnregisterAllViewers() + { + LOCKGUARD(m_dataViewerMapLock); + m_dataViewerCollection.clear(); + } + + bool DataViewerCollection::IsViewerEnabled(const char* viewerName) + { + if (viewerName == nullptr) + { + throw std::invalid_argument("nullptr passed for viewer name"); + } + + LOCKGUARD(m_dataViewerMapLock); + return m_dataViewerCollection.find(viewerName) != m_dataViewerCollection.end(); + } + + bool DataViewerCollection::IsViewerEnabled() noexcept + { + LOCKGUARD(m_dataViewerMapLock); + return m_dataViewerCollection.empty() == false; + } +} ARIASDK_NS_END \ No newline at end of file diff --git a/lib/api/DataViewerCollectionImpl.hpp b/lib/api/DataViewerCollection.hpp similarity index 86% rename from lib/api/DataViewerCollectionImpl.hpp rename to lib/api/DataViewerCollection.hpp index 795d81c9..d6bfe03c 100644 --- a/lib/api/DataViewerCollectionImpl.hpp +++ b/lib/api/DataViewerCollection.hpp @@ -6,12 +6,12 @@ #include "pal/DebugTrace.hpp" #include "public/ctmacros.hpp" -#include +#include #include namespace ARIASDK_NS_BEGIN { - class DataViewerCollectionImpl : public IDataViewerCollection + class DataViewerCollection : public IDataViewerCollection { public: virtual void DispatchDataViewerEvent(const std::vector& packetData) noexcept override; @@ -26,13 +26,13 @@ namespace ARIASDK_NS_BEGIN { virtual bool IsViewerEnabled() noexcept override; - protected: - std::map> m_dataViewerCollection; - private: MATSDK_LOG_DECL_COMPONENT_CLASS(); - mutable std::shared_mutex m_dataViewerMapLock; + mutable std::recursive_mutex m_dataViewerMapLock; + + protected: + std::map> m_dataViewerCollection; }; } ARIASDK_NS_END diff --git a/lib/api/DataViewerCollectionImpl.cpp b/lib/api/DataViewerCollectionImpl.cpp deleted file mode 100644 index cf023c0b..00000000 --- a/lib/api/DataViewerCollectionImpl.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#ifdef _MSC_VER -// evntprov.h(838) : warning C4459 : declaration of 'Version' hides global declaration -#pragma warning( disable : 4459 ) -#endif - -#include "DataViewerCollectionImpl.hpp" -#include - -namespace ARIASDK_NS_BEGIN { - - MATSDK_LOG_INST_COMPONENT_CLASS(DataViewerCollectionImpl, "EventsSDK.DataViewerCollection", "Microsoft Telemetry Client - DataViewerCollection class"); - - void DataViewerCollectionImpl::DispatchDataViewerEvent(const std::vector& packetData) noexcept - { - if (IsViewerEnabled() == false) - return; - - LOG_DEBUG("LOCKGUARD locking at %s:%d", __FILE__, __LINE__); - std::shared_lock lock(m_dataViewerMapLock); - - auto dataViewerIterator = m_dataViewerCollection.cbegin(); - while (dataViewerIterator != m_dataViewerCollection.cend()) - { - //ToDo: Send data asynchronously to individual viewers - dataViewerIterator->second->RecieveData(packetData); - } - }; - - void DataViewerCollectionImpl::RegisterViewer(const std::shared_ptr& dataViewer) - { - LOG_DEBUG("LOCKGUARD locking at %s:%d", __FILE__, __LINE__); - std::unique_lock lock(m_dataViewerMapLock); - - if (dataViewer == nullptr) - { - throw std::invalid_argument("nullptr passed for data viewer"); - } - - if (m_dataViewerCollection.find(dataViewer->GetName()) == m_dataViewerCollection.end()) - { - m_dataViewerCollection.emplace(dataViewer->GetName(), std::move(dataViewer)); - } - else - { - throw std::invalid_argument(std::string { "Viewer: '%s' is already registered", dataViewer->GetName() }); - } - } - - void DataViewerCollectionImpl::UnregisterViewer(const char* viewerName) - { - LOG_DEBUG("LOCKGUARD locking at %s:%d", __FILE__, __LINE__); - std::unique_lock lock(m_dataViewerMapLock); - - if (viewerName == nullptr) - { - throw std::invalid_argument("nullptr passed for viewer name"); - } - - if (m_dataViewerCollection.find(viewerName) == m_dataViewerCollection.end()) - { - throw std::invalid_argument(std::string { "Viewer: '%s' is not currently registered", viewerName }); - } - - m_dataViewerCollection.erase(viewerName); - } - - void DataViewerCollectionImpl::UnregisterAllViewers() - { - LOG_DEBUG("LOCKGUARD locking at %s:%d", __FILE__, __LINE__); - std::unique_lock lock(m_dataViewerMapLock); - m_dataViewerCollection.clear(); - } - - bool DataViewerCollectionImpl::IsViewerEnabled(const char* viewerName) - { - LOG_DEBUG("LOCKGUARD locking at %s:%d", __FILE__, __LINE__); - std::shared_lock lock(m_dataViewerMapLock); - - if (viewerName == nullptr) - { - throw std::invalid_argument("nullptr passed for viewer name"); - } - - return m_dataViewerCollection.find(viewerName) != m_dataViewerCollection.end(); - } - - bool DataViewerCollectionImpl::IsViewerEnabled() noexcept - { - LOG_DEBUG("LOCKGUARD locking at %s:%d", __FILE__, __LINE__); - std::shared_lock lock(m_dataViewerMapLock); - - return m_dataViewerCollection.empty() == false; - } -} ARIASDK_NS_END \ No newline at end of file diff --git a/lib/api/LogManagerImpl.cpp b/lib/api/LogManagerImpl.cpp index fa96bec6..ab74cbf3 100644 --- a/lib/api/LogManagerImpl.cpp +++ b/lib/api/LogManagerImpl.cpp @@ -14,7 +14,7 @@ #include "TransmitProfiles.hpp" #include "EventProperty.hpp" #include "http/HttpClientFactory.hpp" -#include "api/DataViewerCollectionImpl.hpp" +#include "api/DataViewerCollection.hpp" #ifdef HAVE_MAT_UTC #if defined __has_include @@ -170,14 +170,12 @@ namespace ARIASDK_NS_BEGIN m_context.SetCommonField(SESSION_ID_LEGACY, PAL::generateUuidString()); LOG_TRACE("Setting up the Data Viewer Collection implementation..."); - m_dataViewerCollection = std::make_unique(); + m_dataViewerCollection = std::unique_ptr(new DataViewerCollection()); -#ifdef HAVE_MAT_DEFAULTDATAVIEWER if (dataViewer != nullptr) { m_dataViewerCollection->RegisterViewer(dataViewer); } -#endif #ifdef HAVE_MAT_UTC // UTC is not active @@ -286,6 +284,7 @@ namespace ARIASDK_NS_BEGIN m_ownHttpClient.reset(); m_httpClient = nullptr; + m_dataViewerCollection = nullptr; // Reset the contents of m_eventFilterRegulator, but keep the object m_eventFilterRegulator.Reset(); diff --git a/tests/unittests/DataViewerCollectionImplTests.cpp b/tests/unittests/DataViewerCollectionTests.cpp similarity index 96% rename from tests/unittests/DataViewerCollectionImplTests.cpp rename to tests/unittests/DataViewerCollectionTests.cpp index 2319e9de..93c6f851 100644 --- a/tests/unittests/DataViewerCollectionImplTests.cpp +++ b/tests/unittests/DataViewerCollectionTests.cpp @@ -16,7 +16,7 @@ public: void RecieveData(const std::vector& packetData) noexcept override { - localPacketData.assign(packetData.cbegin(), packetData.cend() ); + localPacketData = packetData; } const char* const GetName() const noexcept override @@ -28,15 +28,15 @@ public: const char* m_name; }; -class TestDataViewerCollectionImpl : public DataViewerCollectionImpl +class TestDataViewerCollectionImpl : public DataViewerCollection { public: - using DataViewerCollectionImpl::DispatchDataViewerEvent; - using DataViewerCollectionImpl::RegisterViewer; - using DataViewerCollectionImpl::UnregisterViewer; - using DataViewerCollectionImpl::UnregisterAllViewers; - using DataViewerCollectionImpl::IsViewerEnabled; + using DataViewerCollection::DispatchDataViewerEvent; + using DataViewerCollection::RegisterViewer; + using DataViewerCollection::UnregisterViewer; + using DataViewerCollection::UnregisterAllViewers; + using DataViewerCollection::IsViewerEnabled; std::map>& GetCollection() { diff --git a/tests/unittests/UnitTests.vcxproj b/tests/unittests/UnitTests.vcxproj index 76420569..8ae15a09 100644 --- a/tests/unittests/UnitTests.vcxproj +++ b/tests/unittests/UnitTests.vcxproj @@ -417,7 +417,7 @@ - + diff --git a/tests/unittests/UnitTests.vcxproj.filters b/tests/unittests/UnitTests.vcxproj.filters index 42ced2a9..e5f55a0a 100644 --- a/tests/unittests/UnitTests.vcxproj.filters +++ b/tests/unittests/UnitTests.vcxproj.filters @@ -39,8 +39,8 @@ - +