2020-10-07 10:51:29 +03:00
|
|
|
//
|
2021-09-01 01:19:56 +03:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
2020-10-07 10:51:29 +03:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
2019-08-19 21:32:01 +03:00
|
|
|
#include "DataViewerCollection.hpp"
|
2019-08-28 00:13:13 +03:00
|
|
|
#include <algorithm>
|
2019-08-19 21:32:01 +03:00
|
|
|
#include <mutex>
|
|
|
|
|
2020-08-14 08:30:45 +03:00
|
|
|
namespace MAT_NS_BEGIN {
|
2019-08-19 21:32:01 +03:00
|
|
|
|
|
|
|
MATSDK_LOG_INST_COMPONENT_CLASS(DataViewerCollection, "EventsSDK.DataViewerCollection", "Microsoft Telemetry Client - DataViewerCollection class");
|
|
|
|
|
2019-08-31 00:23:19 +03:00
|
|
|
void DataViewerCollection::DispatchDataViewerEvent(const std::vector<uint8_t>& packetData) const noexcept
|
2019-08-19 21:32:01 +03:00
|
|
|
{
|
|
|
|
if (IsViewerEnabled() == false)
|
|
|
|
return;
|
|
|
|
|
2020-06-22 21:45:37 +03:00
|
|
|
LOCKGUARD(m_dataViewerMapLock);
|
2019-08-27 21:49:42 +03:00
|
|
|
for(const auto& viewer : m_dataViewerCollection)
|
2019-08-19 21:32:01 +03:00
|
|
|
{
|
2019-08-27 21:49:42 +03:00
|
|
|
// Task 3568800: Integrate ThreadPool to IDataViewerCollection
|
2019-08-30 18:52:19 +03:00
|
|
|
viewer->ReceiveData(packetData);
|
2019-08-19 21:32:01 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void DataViewerCollection::RegisterViewer(const std::shared_ptr<IDataViewer>& dataViewer)
|
|
|
|
{
|
|
|
|
if (dataViewer == nullptr)
|
|
|
|
{
|
2019-09-18 02:45:22 +03:00
|
|
|
MATSDK_THROW(std::invalid_argument("nullptr passed for data viewer"));
|
2019-08-19 21:32:01 +03:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:45:37 +03:00
|
|
|
LOCKGUARD(m_dataViewerMapLock);
|
2019-08-28 00:13:13 +03:00
|
|
|
|
2020-06-22 20:48:37 +03:00
|
|
|
if (GetViewerFromCollection(dataViewer->GetName()) != nullptr)
|
2019-08-19 21:32:01 +03:00
|
|
|
{
|
2019-09-30 20:55:17 +03:00
|
|
|
std::stringstream errorMessage;
|
|
|
|
errorMessage << "Viewer: '" << dataViewer->GetName() << "' is already registered";
|
|
|
|
MATSDK_THROW(std::invalid_argument(errorMessage.str()));
|
2019-08-19 21:32:01 +03:00
|
|
|
}
|
|
|
|
|
2019-08-28 00:13:13 +03:00
|
|
|
m_dataViewerCollection.push_back(dataViewer);
|
2019-08-19 21:32:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataViewerCollection::UnregisterViewer(const char* viewerName)
|
|
|
|
{
|
|
|
|
if (viewerName == nullptr)
|
|
|
|
{
|
2019-09-18 02:45:22 +03:00
|
|
|
MATSDK_THROW(std::invalid_argument("nullptr passed for viewer name"));
|
2019-08-19 21:32:01 +03:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:45:37 +03:00
|
|
|
LOCKGUARD(m_dataViewerMapLock);
|
2019-08-28 00:13:13 +03:00
|
|
|
auto toErase = std::find_if(m_dataViewerCollection.begin(), m_dataViewerCollection.end(), [&viewerName](std::shared_ptr<IDataViewer> viewer)
|
|
|
|
{
|
|
|
|
return viewer->GetName() == viewerName;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (toErase == m_dataViewerCollection.end())
|
2019-08-19 21:32:01 +03:00
|
|
|
{
|
2019-09-30 20:55:17 +03:00
|
|
|
std::stringstream errorMessage;
|
|
|
|
errorMessage << "Viewer: '" << viewerName << "' is not currently registered";
|
|
|
|
MATSDK_THROW(std::invalid_argument(errorMessage.str()));
|
2019-08-19 21:32:01 +03:00
|
|
|
}
|
|
|
|
|
2019-08-28 00:13:13 +03:00
|
|
|
m_dataViewerCollection.erase(toErase);
|
2019-08-19 21:32:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataViewerCollection::UnregisterAllViewers()
|
|
|
|
{
|
2020-06-22 21:45:37 +03:00
|
|
|
LOCKGUARD(m_dataViewerMapLock);
|
2019-08-19 21:32:01 +03:00
|
|
|
m_dataViewerCollection.clear();
|
|
|
|
}
|
|
|
|
|
2019-08-19 21:56:26 +03:00
|
|
|
bool DataViewerCollection::IsViewerEnabled(const char* viewerName) const
|
2020-06-19 00:00:26 +03:00
|
|
|
{
|
2020-06-22 20:48:37 +03:00
|
|
|
auto viewerFetched = GetViewerFromCollection(viewerName);
|
2020-06-19 00:00:26 +03:00
|
|
|
return viewerFetched != nullptr && viewerFetched->IsTransmissionEnabled();
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:45:37 +03:00
|
|
|
bool DataViewerCollection::IsViewerEnabled() const noexcept
|
2020-06-19 00:00:26 +03:00
|
|
|
{
|
2020-06-22 21:45:37 +03:00
|
|
|
LOCKGUARD(m_dataViewerMapLock);
|
|
|
|
return !m_dataViewerCollection.empty() &&
|
|
|
|
std::find_if(m_dataViewerCollection.begin(), m_dataViewerCollection.end(), [](std::shared_ptr<IDataViewer> viewer) { return viewer->IsTransmissionEnabled(); }) != m_dataViewerCollection.end();
|
2020-06-19 00:00:26 +03:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:45:37 +03:00
|
|
|
bool DataViewerCollection::IsViewerRegistered(const char* viewerName) const
|
2020-06-19 00:00:26 +03:00
|
|
|
{
|
2020-06-22 21:45:37 +03:00
|
|
|
return GetViewerFromCollection(viewerName) != nullptr;
|
2020-06-19 00:00:26 +03:00
|
|
|
}
|
|
|
|
|
2020-06-22 20:48:37 +03:00
|
|
|
std::shared_ptr<IDataViewer> DataViewerCollection::GetViewerFromCollection(const char* viewerName) const
|
2019-08-19 21:32:01 +03:00
|
|
|
{
|
|
|
|
if (viewerName == nullptr)
|
|
|
|
{
|
2019-09-18 02:45:22 +03:00
|
|
|
MATSDK_THROW(std::invalid_argument("nullptr passed for viewer name"));
|
2019-08-19 21:32:01 +03:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:45:37 +03:00
|
|
|
LOCKGUARD(m_dataViewerMapLock);
|
2019-09-13 01:10:21 +03:00
|
|
|
|
2019-09-13 04:09:42 +03:00
|
|
|
auto lookupResult = std::find_if(m_dataViewerCollection.begin(),
|
|
|
|
m_dataViewerCollection.end(),
|
|
|
|
[&viewerName](std::shared_ptr<IDataViewer> viewer)
|
|
|
|
{
|
|
|
|
return strcmp(viewer->GetName(), viewerName) == 0;
|
|
|
|
});
|
|
|
|
|
2020-06-19 00:00:26 +03:00
|
|
|
if (lookupResult != m_dataViewerCollection.end())
|
|
|
|
{
|
|
|
|
return *lookupResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2019-08-28 00:13:13 +03:00
|
|
|
}
|
2020-08-14 08:30:45 +03:00
|
|
|
} MAT_NS_END
|
2020-10-07 10:51:29 +03:00
|
|
|
|