MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
DomainWatcher.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include <cstdint>
7#include <filesystem>
8#include <memory>
9#include <string>
10#include <unistd.h>
11#include <uuid.h>
12#include <mxl/platform.h>
13#include "mxl-internal/Flow.hpp"
15
16#if defined __linux__
17# include <sys/eventfd.h>
18# include <sys/inotify.h>
19#elif defined __APPLE__
20# include <sys/event.h>
21#endif
22
23namespace mxl::lib
24{
25#if defined __APPLE__
26 constexpr static std::uintptr_t USER_IDENT = 0x13acab2142;
27#endif
28
29 class FlowWriter;
30
33 {
34 using ptr = std::shared_ptr<DomainWatcherRecord>;
35
37 uuids::uuid id;
39 std::string fileName;
40
42
43 std::shared_ptr<SharedMemoryInstance<Flow>> flowData;
44
45 [[nodiscard]]
46 bool operator==(DomainWatcherRecord const& other) const noexcept
47 {
48 return (id == other.id) && (fw == other.fw);
49 }
50 };
51
59 {
60 public:
61 using ptr = std::shared_ptr<DomainWatcher>;
62
68 explicit DomainWatcher(std::filesystem::path const& in_domain);
69
73 ~DomainWatcher();
74
80 void addFlow(FlowWriter* writer, uuids::uuid id);
81
88 void removeFlow(FlowWriter* writer, uuids::uuid id);
89
93 void stop()
94 {
95 _running = false;
96 if (_watchThread.joinable())
97 {
98#if defined __linux__
99 auto value = ::eventfd_t{1};
100 if (::write(_eventFd, &value, sizeof(::eventfd_t)) < 0)
101 {
102 auto const error = errno;
103 MXL_ERROR("Failed to signal DomainWatcher stop request: {}", ::strerror(error));
104 }
105#elif defined __APPLE__
106 struct kevent kev{};
107 EV_SET(&kev, USER_IDENT, EVFILT_USER, 0, NOTE_TRIGGER, 0, nullptr);
108 if (::kevent(_kq, &kev, 1, nullptr, 0, nullptr) < 0)
109 {
110 auto const error = errno;
111 MXL_WARN("Failed so signal DomainWatcher stop request: {}", std::strerror(error));
112 }
113#endif
114
115 _watchThread.join();
116 }
117 }
118
121 [[nodiscard]]
122 std::size_t count(uuids::uuid id) const noexcept;
123
126 [[nodiscard]]
127 std::size_t size() const noexcept;
128
129 private:
132 void processEvents();
133
134#if defined __linux__
135 void processEventBuffer(struct ::inotify_event const* buffer, std::size_t count);
136#elif defined __APPLE__
137 void setWatch();
138 void processPendingEvents(int numEvents);
139#endif
140
142 std::filesystem::path _domain;
143
144#if defined __APPLE__
145 int _kq;
146 std::vector<struct ::kevent> _eventsToMonitor;
147 std::vector<struct ::kevent> _eventData;
148#elif defined __linux__
150 int _inotifyFd;
152 int _epollFd;
154 int _eventFd;
155#endif
156
158 std::unordered_multimap<int, DomainWatcherRecord> _watches;
160 mutable std::mutex _mutex;
162 std::atomic<bool> _running;
164 std::thread _watchThread;
165 };
166
167} // namespace mxl::lib
Internal shared-memory headers for discrete grains and event slots.
#define MXL_ERROR(...)
Definition Logging.hpp:67
#define MXL_WARN(...)
Definition Logging.hpp:56
std::filesystem::path _domain
The monitored domain.
std::size_t size() const noexcept
Returns the total number of writers that are registered in the DomainWatcher.
DomainWatcher(std::filesystem::path const &in_domain)
std::unordered_multimap< int, DomainWatcherRecord > _watches
Map of watch descriptors to file records. Multiple records could use the same watchfd.
std::atomic< bool > _running
Controls the event processing thread.
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
std::mutex _mutex
Prodect maps.
std::thread _watchThread
Event processing thread.
void processEvents()
#define MXL_EXPORT
Definition platform.h:9
Entry stored in the unordered_maps.
FlowWriter * fw
Non-owning event or discrete writer registered with the watcher.
std::string fileName
file being watched
std::shared_ptr< DomainWatcherRecord > ptr
Shared ownership of a watch record.
bool operator==(DomainWatcherRecord const &other) const noexcept
std::shared_ptr< SharedMemoryInstance< Flow > > flowData
Shared mapping used to update the flow's last-read time.
uint8_t * buffer
Set a mark at the beginning and the end of the grain payload.
mxlFlowWriter writer