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 std::lock_guard lock(mtx_);
132 if VUNLIKELY (this->impl_->is_listened) {
133 VLOG_F(
"Getter has already been listened.");
137 callback_ = std::move(callback);
139 this->impl_->is_listened =
true;
144 template <
typename ValueT, SecurityType SecT>
146 std::lock_guard lock(mtx_);
147 change_reporting_ = enable;
150 template <
typename ValueT, SecurityType SecT>
152 this->impl_->set_latency_and_lost_enabled(enable);
155 template <
typename ValueT, SecurityType SecT>
157 return this->impl_->is_latency_and_lost_enabled();
160 template <
typename ValueT, SecurityType SecT>
162 return this->impl_->get_latency();
165 template <
typename ValueT, SecurityType SecT>
167 return this->impl_->get_lost();
170 template <
typename ValueT, SecurityType SecT>
172 std::lock_guard lock(mtx_);
173 return change_reporting_;
176 template <
typename ValueT, SecurityType SecT>
182 listen_bytes([
this](
const Bytes& data) {
183 #ifndef VLINK_DISABLE_PROFILER
187 bool has_callback =
false;
190 std::lock_guard lock(mtx_);
192 if (change_reporting_) {
193 if (value_.has_value() && last_cache_ == data) {
200 has_callback =
static_cast<bool>(callback_);
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 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(std::move(value));
245 template <
typename ValueT, SecurityType SecT>
248 std::lock_guard lock(mtx_);
255 template <
typename ValueT, SecurityType SecT>
257 if VUNLIKELY (this->has_inited_.load(std::memory_order_acquire)) {
258 this->impl_->deinit_ext();
260 this->impl_->init_ext();
266 template <
typename ValueT, SecurityType SecT>
268 if (!this->has_inited_.load(std::memory_order_acquire)) {
272 this->impl_->listen([
this, callback = std::move(callback)](
const Bytes& data) {
276 if VUNLIKELY (!this->impl_->security || !this->impl_->security->decrypt(data, sec_data)) {
277 VLOG_T(
"Getter decrypt failed, url: ", this->impl_->url,
".");
281 this->invoke_callback(callback, sec_data);
285 this->invoke_callback(callback, data);
290 template <
typename ValueT>
291 template <
typename SecurityConfigT>
293 SecurityConfigT&& sec_cfg,
295 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
296 "SecurityConfigT must be Security::Config.");
298 return std::make_unique<SecurityGetter<ValueT>>(url_str, std::forward<SecurityConfigT>(sec_cfg), type);
301 template <
typename ValueT>
302 template <
typename SecurityConfigT>
304 SecurityConfigT&& sec_cfg,
306 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
307 "SecurityConfigT must be Security::Config.");
309 return std::make_shared<SecurityGetter<ValueT>>(url_str, std::forward<SecurityConfigT>(sec_cfg), type);
312 template <
typename ValueT>
313 template <
typename ConfT,
typename SecurityConfigT,
typename>
316 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
317 "SecurityConfigT must be Security::Config.");
326 template <
typename ValueT>
327 template <
typename SecurityConfigT>
330 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
331 "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:177
void set_change_reporting(bool enable)
Toggles change-only reporting (duplicate suppression).
Definition: getter-inl.h:145
~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:161
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
SampleLostInfo get_lost() const
Returns cumulative sample-delivery statistics.
Definition: getter-inl.h:166
bool get_change_reporting() const
Reports whether change-only reporting is currently active.
Definition: getter-inl.h:171
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:256
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:156
void set_latency_and_lost_enabled(bool enable)
Toggles per-value latency and sample-loss measurement.
Definition: getter-inl.h:151
Function< void(const Bytes &)> MsgCallback
Callback delivering a serialised payload to a subscriber or getter.
Definition: node_impl.h:200
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:139
bool enable_security(const Security::Config &cfg)
Installs a Security configuration before transport initialisation.
Definition: node-inl.h:327
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:292
std::shared_ptr< SecurityGetter< ValueT > > SharedPtr
Owning shared-pointer alias.
Definition: getter.h:329
std::unique_ptr< SecurityGetter< ValueT > > UniquePtr
Owning unique-pointer alias.
Definition: getter.h:328
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:303
SecurityGetter(const ConfT &conf, SecurityConfigT &&sec_cfg={}, InitType type=InitType::kWithInit)
Constructs a SecurityGetter from a typed configuration object.
Definition: getter-inl.h:314
#define VLOG_F(...)
Definition: logger.h:843
#define VLOG_W(...)
Definition: logger.h:839
#define VLOG_T(...)
Definition: logger.h:833
#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:150
@ 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:113
SecurityType
Compile-time selector for the per-node message security variant.
Definition: types.h:168
@ kWithSecurity
Encrypted and authenticated transport.
Aggregate of cumulative delivered / lost sample counts.
Definition: types.h:269
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:256
Conf subclass that routes virtual calls to the transport selected by a URL string.
Definition: url.h:216