Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
volume_detector.hpp
Go to the documentation of this file.
1#pragma once
2
3// MountedVolumeListChangeDetector — monitors filesystem for volume mount/unmount.
4// ScopedLowPowerModeDisabler is in coalesced_updater.hpp.
5// LockingCoalescedUpdater is in coalesced_updater.hpp.
6// NetworkServiceDiscovery — mDNS-based service discovery.
7
8#include <string>
9#include <string_view>
10#include <vector>
11#include <functional>
12#include <atomic>
13#include <memory>
14#include <thread>
15#include <chrono>
16#include <mutex>
17#include <condition_variable>
18#include <unordered_map>
19
20namespace pulp::events {
21
25public:
28
30 void start(std::chrono::milliseconds interval = std::chrono::seconds(2));
31
33 void stop();
34
36 bool is_running() const { return running_.load(); }
37
39 static std::vector<std::string> get_mounted_volumes();
40
42 std::function<void(const std::vector<std::string>& volumes)> on_change;
43
44private:
45 std::atomic<bool> running_{false};
46 std::thread thread_;
47 std::mutex mutex_;
48 std::condition_variable cv_;
49 std::vector<std::string> last_volumes_;
50};
51
65public:
72 using TxtRecords = std::unordered_map<std::string, std::string>;
73
74 struct Service {
75 std::string name;
76 std::string type; // e.g., "_http._tcp"
77 std::string hostname;
78 std::string address;
79 uint16_t port = 0;
81 };
82
88 class Backend {
89 public:
90 virtual ~Backend() = default;
91 virtual void browse(std::string_view service_type,
92 NetworkServiceDiscovery& owner) = 0;
93 virtual void stop() = 0;
94 virtual bool register_service(std::string_view name,
95 std::string_view type,
96 uint16_t port) = 0;
100 virtual bool register_service(std::string_view name,
101 std::string_view type,
102 uint16_t port,
103 const TxtRecords& /*txt_records*/) {
104 return register_service(name, type, port);
105 }
106 virtual void unregister_service() = 0;
107 };
108
111
114 void install_backend(std::unique_ptr<Backend> backend);
115
119 bool has_backend() const noexcept { return backend_ != nullptr; }
120
123 void browse(std::string_view service_type);
124
126 void stop();
127
130 bool register_service(std::string_view name, std::string_view type, uint16_t port);
131
135 bool register_service(std::string_view name,
136 std::string_view type,
137 uint16_t port,
138 const TxtRecords& txt_records);
139
142
147
151 void notify_service_lost(const Service& svc);
152
154 std::function<void(const Service&)> on_service_found;
155
157 std::function<void(const Service&)> on_service_lost;
158
160 const std::vector<Service>& discovered() const { return services_; }
161
162private:
163 std::vector<Service> services_;
164 std::atomic<bool> running_{false};
165 std::thread browse_thread_;
166 std::unique_ptr<Backend> backend_;
167};
168
172public:
173 virtual ~LockingCoalescedUpdater() = default;
174
177
179 virtual void on_update() = 0;
180
181private:
182 std::atomic<bool> pending_{false};
183 std::mutex mutex_;
184 std::condition_variable cv_;
185};
186
187} // namespace pulp::events
Locking variant of CoalescedUpdater — blocks the trigger thread until the update is processed on the ...
Definition volume_detector.hpp:171
virtual ~LockingCoalescedUpdater()=default
virtual void on_update()=0
Override to handle the update.
void trigger_and_wait()
Trigger and wait for the update to be processed.
void start(std::chrono::milliseconds interval=std::chrono::seconds(2))
Start monitoring with the given poll interval.
bool is_running() const
Whether monitoring is active.
Definition volume_detector.hpp:36
std::function< void(const std::vector< std::string > &volumes)> on_change
Called when the volume list changes.
Definition volume_detector.hpp:42
static std::vector< std::string > get_mounted_volumes()
Get current list of mounted volumes.
Backend interface the host installs to get real mDNS.
Definition volume_detector.hpp:88
virtual void browse(std::string_view service_type, NetworkServiceDiscovery &owner)=0
virtual bool register_service(std::string_view name, std::string_view type, uint16_t port, const TxtRecords &)
Default implementation drops the TXT records and forwards to the base three-argument register_service...
Definition volume_detector.hpp:100
virtual bool register_service(std::string_view name, std::string_view type, uint16_t port)=0
void notify_service_found(const Service &svc)
Push a discovered service into the core layer.
void notify_service_lost(const Service &svc)
Push a lost service into the core layer.
std::function< void(const Service &)> on_service_lost
Called when a service is lost.
Definition volume_detector.hpp:157
std::function< void(const Service &)> on_service_found
Called when a service is discovered.
Definition volume_detector.hpp:154
bool has_backend() const noexcept
Whether a concrete backend is installed.
Definition volume_detector.hpp:119
void browse(std::string_view service_type)
Start browsing for services of the given type.
bool register_service(std::string_view name, std::string_view type, uint16_t port)
Register a service on this machine.
void install_backend(std::unique_ptr< Backend > backend)
Install a concrete backend.
std::unordered_map< std::string, std::string > TxtRecords
TXT records carry arbitrary key=value metadata published with a service registration (RFC 6763 §6).
Definition volume_detector.hpp:72
bool register_service(std::string_view name, std::string_view type, uint16_t port, const TxtRecords &txt_records)
Register a service with TXT records (RFC 6763 §6 metadata).
void unregister_service()
Unregister a previously registered service.
const std::vector< Service > & discovered() const
Currently discovered services.
Definition volume_detector.hpp:160
Definition child_process_manager.hpp:18
Definition volume_detector.hpp:74
std::string type
Definition volume_detector.hpp:76
uint16_t port
Definition volume_detector.hpp:79
TxtRecords txt_records
Definition volume_detector.hpp:80
std::string hostname
Definition volume_detector.hpp:77
std::string address
Definition volume_detector.hpp:78
std::string name
Definition volume_detector.hpp:75