6#include <condition_variable>
9#include <unordered_set>
13#define PANO_DETECTOR_QUEUE_MAX 16384
17#define PANO_MAX_DETECTOR_SUBSCRIBERS 4
21 std::condition_variable
Wake;
22 std::deque<DetectorEventRecord>
Queue;
27static std::mutex g_SubscribersLock;
28static std::vector<std::shared_ptr<DetectorSubscriberState>> g_Subscribers;
29static bool g_Stopping =
false;
31static std::atomic<size_t> g_SubscriberCount{ 0 };
35 auto state = std::make_shared<DetectorSubscriberState>();
36 state->EventNames.insert(eventNames.begin(), eventNames.end());
38 std::lock_guard<std::mutex> guard(g_SubscribersLock);
42 g_Subscribers.push_back(state);
43 g_SubscriberCount = g_Subscribers.size();
48 : m_state(std::move(state))
54 std::lock_guard<std::mutex> guard(g_SubscribersLock);
55 g_Subscribers.erase(std::remove(g_Subscribers.begin(), g_Subscribers.end(), m_state), g_Subscribers.end());
56 g_SubscriberCount = g_Subscribers.size();
62 std::unique_lock<std::mutex> lock(m_state->Lock);
63 m_state->Wake.wait_for(lock, std::chrono::milliseconds(timeoutMs),
64 [
this] {
return !m_state->Queue.empty() || m_state->Stopping; });
65 if (m_state->Stopping) {
68 out.assign(std::make_move_iterator(m_state->Queue.begin()), std::make_move_iterator(m_state->Queue.end()));
69 m_state->Queue.clear();
73static void PublishDetectorEvent(
const std::string& eventName, UINT64 sequence,
const std::string& json,
const FILETIME& time)
75 if (g_SubscriberCount.load(std::memory_order_relaxed) == 0) {
79 INT64 timestamp = ((INT64)time.dwHighDateTime << 32) | time.dwLowDateTime;
80 std::lock_guard<std::mutex> guard(g_SubscribersLock);
81 for (
auto& subscriber : g_Subscribers) {
83 std::lock_guard<std::mutex> lock(subscriber->Lock);
84 if (!subscriber->EventNames.empty() && subscriber->EventNames.count(eventName) == 0) {
91 subscriber->Queue.push_back({ sequence, timestamp, json });
93 subscriber->Wake.notify_one();
98static UINT64 FirstSequence()
102 now.LowPart = time.dwLowDateTime;
103 now.HighPart = time.dwHighDateTime;
104 const UINT64 unixEpoch = 116444736000000000ULL;
105 return (now.QuadPart - unixEpoch) / 10;
110static std::mutex g_RecordLock;
111static UINT64 g_NextSequence = FirstSequence();
113std::string
RecordEvent(nlohmann::ordered_json& event,
const FILETIME& time)
115 std::lock_guard<std::mutex> guard(g_RecordLock);
116 UINT64 sequence = g_NextSequence++;
117 event[
"Seq"] = sequence;
118 std::string line =
event.dump();
120 PublishDetectorEvent(event.value(
"Event",
""), sequence, line, time);
127 GetSystemTimeAsFileTime(&now);
133 std::lock_guard<std::mutex> guard(g_SubscribersLock);
135 for (
auto& subscriber : g_Subscribers) {
137 std::lock_guard<std::mutex> lock(subscriber->Lock);
138 subscriber->Stopping =
true;
140 subscriber->Wake.notify_all();
static std::unique_ptr< DetectorSubscription > Open(const std::vector< std::string > &eventNames)
Registers a subscriber. Returns nullptr when the service is stopping or too many subscribers are conn...
DetectorSubscription(const DetectorSubscription &)=delete
bool Wait(std::vector< DetectorEventRecord > &out, DWORD timeoutMs)
Waits up to timeoutMs for events and moves the queued ones into out (which it clears first)....
void StopDetectorStreams()
Wakes every subscriber's Wait with false and refuses new subscribers, so the SubscribeEvents calls re...
#define PANO_DETECTOR_QUEUE_MAX
std::string RecordEvent(nlohmann::ordered_json &event, const FILETIME &time)
Gives event the next sequence number (appended as "Seq"), writes it to pano.log and queues it for eve...
FILETIME CurrentFileTime()
The current time as a FILETIME, for events that carry no timestamp of their own.
#define PANO_MAX_DETECTOR_SUBSCRIBERS
void WriteToLogFile(const std::string &message)
Appends a line to the event log: LOG_FOLDER\pano.log, continuing in pano1.log, pano2....
std::deque< DetectorEventRecord > Queue
std::unordered_set< std::string > EventNames
std::condition_variable Wake