-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathDataViewerCollection.cpp
More file actions
116 lines (94 loc) · 3.96 KB
/
DataViewerCollection.cpp
File metadata and controls
116 lines (94 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
#include "DataViewerCollection.hpp"
#include <algorithm>
#include <mutex>
namespace MAT_NS_BEGIN {
MATSDK_LOG_INST_COMPONENT_CLASS(DataViewerCollection, "EventsSDK.DataViewerCollection", "Microsoft Telemetry Client - DataViewerCollection class")
void DataViewerCollection::DispatchDataViewerEvent(const std::vector<uint8_t>& packetData) const noexcept
{
if (IsViewerEnabled() == false)
return;
LOCKGUARD(m_dataViewerMapLock);
for(const auto& viewer : m_dataViewerCollection)
{
// Task 3568800: Integrate ThreadPool to IDataViewerCollection
viewer->ReceiveData(packetData);
}
}
void DataViewerCollection::RegisterViewer(const std::shared_ptr<IDataViewer>& dataViewer)
{
if (dataViewer == nullptr)
{
MATSDK_THROW(std::invalid_argument("nullptr passed for data viewer"));
}
LOCKGUARD(m_dataViewerMapLock);
if (GetViewerFromCollection(dataViewer->GetName()) != nullptr)
{
std::stringstream errorMessage;
errorMessage << "Viewer: '" << dataViewer->GetName() << "' is already registered";
MATSDK_THROW(std::invalid_argument(errorMessage.str()));
}
m_dataViewerCollection.push_back(dataViewer);
}
void DataViewerCollection::UnregisterViewer(const char* viewerName)
{
if (viewerName == nullptr)
{
MATSDK_THROW(std::invalid_argument("nullptr passed for viewer name"));
}
LOCKGUARD(m_dataViewerMapLock);
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())
{
std::stringstream errorMessage;
errorMessage << "Viewer: '" << viewerName << "' is not currently registered";
MATSDK_THROW(std::invalid_argument(errorMessage.str()));
}
m_dataViewerCollection.erase(toErase);
}
void DataViewerCollection::UnregisterAllViewers()
{
LOCKGUARD(m_dataViewerMapLock);
m_dataViewerCollection.clear();
}
bool DataViewerCollection::IsViewerEnabled(const char* viewerName) const
{
auto viewerFetched = GetViewerFromCollection(viewerName);
return viewerFetched != nullptr && viewerFetched->IsTransmissionEnabled();
}
bool DataViewerCollection::IsViewerEnabled() const noexcept
{
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();
}
bool DataViewerCollection::IsViewerRegistered(const char* viewerName) const
{
return GetViewerFromCollection(viewerName) != nullptr;
}
std::shared_ptr<IDataViewer> DataViewerCollection::GetViewerFromCollection(const char* viewerName) const
{
if (viewerName == nullptr)
{
MATSDK_THROW(std::invalid_argument("nullptr passed for viewer name"));
}
LOCKGUARD(m_dataViewerMapLock);
auto lookupResult = std::find_if(m_dataViewerCollection.begin(),
m_dataViewerCollection.end(),
[&viewerName](std::shared_ptr<IDataViewer> viewer)
{
return strcmp(viewer->GetName(), viewerName) == 0;
});
if (lookupResult != m_dataViewerCollection.end())
{
return *lookupResult;
}
return nullptr;
}
} MAT_NS_END