30 #include "../base/cpu_profiler_guard.h"
31 #include "../base/logger.h"
32 #include "../getter.h"
33 #include "../impl/url.h"
34 #include "../serializer.h"
38 template <
typename ValueT, SecurityType SecT>
41 return std::make_unique<Getter<ValueT, SecT>>(url_str, type);
44 template <
typename ValueT, SecurityType SecT>
47 return std::make_shared<Getter<ValueT, SecT>>(url_str, type);
50 template <
typename ValueT, SecurityType SecT>
51 template <
typename ConfT,
typename>
53 static_assert(ConfT::get_allow_impl_type() & kImplType,
"Conf does not support getter mode.");
55 if VUNLIKELY (!conf.parse(kImplType) || !conf.is_valid()) {
56 VLOG_F(conf,
" getter configuration is invalid or could not be parsed.");
60 this->impl_ = conf.create_getter();
63 VLOG_F(conf,
" getter implementation not available for this transport.");
67 this->impl_->transport_type = conf.get_transport_type();
68 this->impl_->ser_type = Serializer::get_serialized_type<kValueType, ValueT>();
69 this->impl_->schema_type = Serializer::get_schema_type<kValueType, ValueT>();
70 this->impl_->is_cdr_type = Serializer::is_cdr_type<ValueT>();
72 if constexpr (std::is_same_v<ConfT, Url>) {
73 this->impl_->url = conf.get_str();
77 this->impl_->is_security_type =
true;
85 template <
typename ValueT, SecurityType SecT>
87 :
Getter<ValueT, SecT>(
Url(url_str), type) {}
89 template <
typename ValueT, SecurityType SecT>
95 template <
typename ValueT, SecurityType SecT>
97 std::lock_guard lock(mtx_);
101 template <
typename ValueT, SecurityType SecT>
104 VLOG_W(
"Getter: Timeout value is 0, using infinite wait instead.");
108 std::unique_lock lock(mtx_);
110 this->impl_->reset_interrupted();
112 if (value_.has_value()) {
116 has_value_notification_ =
false;
118 auto predicate = [
this]() ->
bool {
return has_value_notification_ || this->impl_->is_interrupted(); };
120 if (timeout.count() < 0) {
121 cv_.wait(lock, std::move(predicate));
122 return !this->impl_->is_interrupted();
124 return cv_.wait_for(lock, timeout, std::move(predicate)) && !this->impl_->is_interrupted();
128 template <
typename ValueT, SecurityType SecT>
130 if VUNLIKELY (this->impl_->is_listened) {
131 VLOG_F(
"Getter has already been listened.");
135 callback_ = std::move(callback);
137 this->impl_->is_listened =
true;
142 template <
typename ValueT, SecurityType SecT>
144 std::lock_guard lock(mtx_);
145 change_reporting_ = enable;
148 template <
typename ValueT, SecurityType SecT>
150 this->impl_->set_manual_unloan(manual_unloan);
151 this->is_manual_unloan_ = manual_unloan;
154 template <
typename ValueT, SecurityType SecT>
156 this->impl_->set_latency_and_lost_enabled(enable);
159 template <
typename ValueT, SecurityType SecT>
161 return this->impl_->is_latency_and_lost_enabled();
164 template <
typename ValueT, SecurityType SecT>
166 return this->impl_->get_latency();
169 template <
typename ValueT, SecurityType SecT>
171 return this->impl_->get_lost();
174 template <
typename ValueT, SecurityType SecT>
176 std::lock_guard lock(mtx_);
177 return change_reporting_;
180 template <
typename ValueT, SecurityType SecT>
186 listen_bytes([
this](
const Bytes& data) {
187 #ifndef VLINK_DISABLE_PROFILER
192 std::lock_guard lock(mtx_);
194 if (change_reporting_) {
195 if (value_.has_value() && last_cache_ == data) {
203 if constexpr (std::is_same_v<ValueT, Bytes>) {
209 std::lock_guard lock(mtx_);
211 has_value_notification_ =
true;
213 value_.emplace(data);
219 thread_local
auto value = this->
template get_default_value<ValueT>();
221 if VUNLIKELY (!Serializer::deserialize<kValueType>(data, value, this->impl_->transport_type)) {
222 VLOG_T(
"Getter deserialize failed, url: ", this->impl_->url,
".");
231 std::lock_guard lock(mtx_);
233 has_value_notification_ =
true;
235 value_.emplace(value);
245 template <
typename ValueT, SecurityType SecT>
252 template <
typename ValueT, SecurityType SecT>
254 if VUNLIKELY (this->has_inited_.load(std::memory_order_acquire)) {
255 this->impl_->deinit_ext();
257 this->impl_->init_ext();
263 template <
typename ValueT, SecurityType SecT>
265 if (!this->has_inited_.load(std::memory_order_acquire)) {
269 this->impl_->
listen([
this, callback = std::move(callback)](
const Bytes& data) {
273 if VUNLIKELY (!this->impl_->security || !this->impl_->security->decrypt(data, sec_data)) {
274 VLOG_T(
"Getter decrypt failed, url: ", this->impl_->url,
".");
278 this->invoke_callback(callback, sec_data);
282 this->invoke_callback(callback, data);
287 template <
typename ValueT>
288 template <
typename SecurityConfigT>
290 SecurityConfigT&& sec_cfg,
292 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
293 "SecurityConfigT must be Security::Config.");
295 return std::make_unique<SecurityGetter<ValueT>>(url_str, std::forward<SecurityConfigT>(sec_cfg), type);
298 template <
typename ValueT>
299 template <
typename SecurityConfigT>
301 SecurityConfigT&& sec_cfg,
303 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
304 "SecurityConfigT must be Security::Config.");
306 return std::make_shared<SecurityGetter<ValueT>>(url_str, std::forward<SecurityConfigT>(sec_cfg), type);
309 template <
typename ValueT>
310 template <
typename ConfT,
typename SecurityConfigT,
typename>
313 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
314 "SecurityConfigT must be Security::Config.");
323 template <
typename ValueT>
324 template <
typename SecurityConfigT>
327 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
328 "SecurityConfigT must be Security::Config.");
Fixed-size 128-byte buffer holder with SBO, five ownership modes and integrated codecs.
Definition: bytes.h:120
Scope guard that opens and closes a CpuProfiler active interval.
Definition: cpu_profiler_guard.h:80
Type-safe latest-value reader for the VLink field communication model.
Definition: getter.h:122
Getter(const ConfT &conf, InitType type=InitType::kWithInit)
Constructs a getter from a typed transport configuration object.
Definition: getter-inl.h:52
std::unique_ptr< Getter< ValueT, SecT > > UniquePtr
Owning unique-pointer alias.
Definition: getter.h:124
bool init() override
Initialises the getter and installs the internal delivery hook.
Definition: getter-inl.h:181
void set_change_reporting(bool enable)
Toggles change-only reporting (duplicate suppression).
Definition: getter-inl.h:143
~Getter() override
Destroys the getter and drains in-flight transport callbacks.
Definition: getter-inl.h:90
int64_t get_latency() const
Returns the most recent end-to-end latency measurement.
Definition: getter-inl.h:165
std::optional< ValueT > get() const
Returns the most recent cached value, if any has been received.
Definition: getter-inl.h:96
bool wait_for_value(std::chrono::milliseconds timeout=Timeout::kDefaultInterval)
Blocks until a value is received or timeout expires.
Definition: getter-inl.h:102
void set_manual_unloan(bool manual_unloan) override
Enables or disables manual-unloan mode for zero-copy receives.
Definition: getter-inl.h:149
SampleLostInfo get_lost() const
Returns cumulative sample-delivery statistics.
Definition: getter-inl.h:170
bool get_change_reporting() const
Reports whether change-only reporting is currently active.
Definition: getter-inl.h:175
static SharedPtr create_shared(const std::string &url_str, InitType type=InitType::kWithInit)
Heap-allocates a Getter and wraps it in a std::shared_ptr.
Definition: getter-inl.h:45
bool listen(MsgCallback &&callback)
Installs a callback invoked whenever a new value arrives.
Definition: getter-inl.h:129
void mark_as_subscriber()
Promotes this getter to behave as a Subscriber at the transport layer.
Definition: getter-inl.h:253
void interrupt() override
Aborts any blocking wait_for_value() call.
Definition: getter-inl.h:246
std::shared_ptr< Getter< ValueT, SecT > > SharedPtr
Owning shared-pointer alias.
Definition: getter.h:125
static UniquePtr create_unique(const std::string &url_str, InitType type=InitType::kWithInit)
Heap-allocates a Getter and wraps it in a std::unique_ptr.
Definition: getter-inl.h:39
bool is_latency_and_lost_enabled() const
Reports whether latency and sample-loss tracking is currently active.
Definition: getter-inl.h:160
void set_latency_and_lost_enabled(bool enable)
Toggles per-value latency and sample-loss measurement.
Definition: getter-inl.h:155
Transport-agnostic CRTP base for all VLink communication primitives.
Definition: node.h:164
virtual void interrupt()
Aborts any blocking wait on this node.
Definition: node-inl.h:150
bool enable_security(const Security::Config &cfg)
Installs a Security configuration before transport initialisation.
Definition: node-inl.h:321
static UniquePtr create_unique(const std::string &url_str, SecurityConfigT &&sec_cfg={}, InitType type=InitType::kWithInit)
Heap-allocates a SecurityGetter and wraps it in a std::unique_ptr.
Definition: getter-inl.h:289
std::shared_ptr< SecurityGetter< ValueT > > SharedPtr
Owning shared-pointer alias.
Definition: getter.h:336
std::unique_ptr< SecurityGetter< ValueT > > UniquePtr
Owning unique-pointer alias.
Definition: getter.h:335
static SharedPtr create_shared(const std::string &url_str, SecurityConfigT &&sec_cfg={}, InitType type=InitType::kWithInit)
Heap-allocates a SecurityGetter and wraps it in a std::shared_ptr.
Definition: getter-inl.h:300
SecurityGetter(const ConfT &conf, SecurityConfigT &&sec_cfg={}, InitType type=InitType::kWithInit)
Constructs a SecurityGetter from a typed configuration object.
Definition: getter-inl.h:311
#define VLOG_F(...)
Definition: logger.h:791
#define VLOG_W(...)
Definition: logger.h:787
#define VLOG_T(...)
Definition: logger.h:781
#define VUNLIKELY(...)
Short alias for VLINK_UNLIKELY.
Definition: macros.h:289
#define VLIKELY(...)
Short alias for VLINK_LIKELY.
Definition: macros.h:284
InitType
Selects between immediate and deferred node initialisation.
Definition: types.h:154
@ kWithoutInit
Defer initialisation; call init() manually.
@ kWithInit
Initialise immediately in the constructor.
@ kGet
Value observed by a Getter node.
@ kSubscriber
Event subscriber (receives broadcasts).
Definition: types.h:115
SecurityType
Compile-time selector for the per-node message security variant.
Definition: types.h:172
@ kWithSecurity
Encrypted and authenticated transport.
Aggregate of cumulative delivered / lost sample counts.
Definition: types.h:273
Aggregate of every parameter accepted by the Security constructor.
Definition: security.h:164
static constexpr std::chrono::milliseconds kInfinite
Wait indefinitely (negative timeout).
Definition: types.h:260
Conf subclass that routes virtual calls to the transport selected by a URL string.
Definition: url.h:213