30 #include "../base/cpu_profiler_guard.h"
31 #include "../base/logger.h"
32 #include "../impl/url.h"
33 #include "../publisher.h"
34 #include "../serializer.h"
38 template <
typename MsgT, SecurityType SecT>
41 return std::make_unique<Publisher<MsgT, SecT>>(url_str, type);
44 template <
typename MsgT, SecurityType SecT>
47 return std::make_shared<Publisher<MsgT, SecT>>(url_str, type);
50 template <
typename MsgT, SecurityType SecT>
51 template <
typename ConfT,
typename>
53 static_assert(ConfT::get_allow_impl_type() & kImplType,
"Conf does not support publisher mode.");
55 if VUNLIKELY (!conf.parse(kImplType) || !conf.is_valid()) {
56 VLOG_F(conf,
" publisher configuration is invalid or could not be parsed.");
60 this->impl_ = conf.create_publisher();
63 VLOG_F(conf,
" publisher implementation not available for this transport.");
67 this->impl_->transport_type = conf.get_transport_type();
68 this->impl_->ser_type = Serializer::get_serialized_type<kMsgType, MsgT>();
69 this->impl_->schema_type = Serializer::get_schema_type<kMsgType, MsgT>();
70 this->impl_->is_cdr_type = Serializer::is_cdr_type<MsgT>();
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 MsgT, SecurityType SecT>
89 template <
typename MsgT, SecurityType SecT>
91 return this->impl_->detect_subscribers(std::move(callback));
94 template <
typename MsgT, SecurityType SecT>
97 VLOG_W(
"Publisher: Timeout value is 0, using infinite wait instead.");
101 return this->impl_->wait_for_subscribers(timeout);
104 template <
typename MsgT, SecurityType SecT>
106 return this->impl_->has_subscribers();
109 template <
typename MsgT, SecurityType SecT>
111 #ifndef VLINK_DISABLE_PROFILER
116 if (!this->impl_->has_subscribers()) {
122 if constexpr (std::is_base_of_v<IntraDataType, typename MsgT::element_type>) {
126 return write_intra(msg);
131 if constexpr (std::is_same_v<MsgT, Bytes>) {
132 return write_bytes(msg);
137 if (this->is_support_loan_) {
138 size_t ser_size = Serializer::get_serialized_size<kMsgType>(msg);
140 msg_data = this->impl_->loan(ser_size);
148 if VUNLIKELY (!Serializer::serialize<kMsgType>(msg, msg_data, this->impl_->transport_type)) {
149 VLOG_T(
"Publisher serialize failed, url: ", this->impl_->url,
".");
152 if (this->is_support_loan_) {
153 this->impl_->return_loan(msg_data);
160 bool ret = write_bytes(msg_data);
166 template <
typename MsgT, SecurityType SecT>
170 #ifndef VLINK_DISABLE_PROFILER
175 if (!this->impl_->has_subscribers()) {
183 template <
typename MsgT, SecurityType SecT>
185 if VUNLIKELY (this->has_inited_.load(std::memory_order_acquire)) {
186 this->impl_->deinit_ext();
188 this->impl_->init_ext();
190 this->impl_->impl_type =
kSetter;
194 template <
typename MsgT, SecurityType SecT>
195 inline bool Publisher<MsgT, SecT>::write_bytes(
const Bytes& data) {
199 if VUNLIKELY (!this->impl_->security || !this->impl_->security->encrypt(data, sec_data)) {
200 VLOG_T(
"Publisher encrypt failed, url: ", this->impl_->url,
".");
204 return this->impl_->write(sec_data);
208 return this->impl_->write(data);
212 template <
typename MsgT, SecurityType SecT>
214 return this->impl_->write(intra_data);
217 template <
typename MsgT>
218 template <
typename SecurityConfigT>
220 SecurityConfigT&& sec_cfg,
222 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
223 "SecurityConfigT must be Security::Config.");
225 return std::make_unique<SecurityPublisher<MsgT>>(url_str, std::forward<SecurityConfigT>(sec_cfg), type);
228 template <
typename MsgT>
229 template <
typename SecurityConfigT>
231 SecurityConfigT&& sec_cfg,
233 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
234 "SecurityConfigT must be Security::Config.");
236 return std::make_shared<SecurityPublisher<MsgT>>(url_str, std::forward<SecurityConfigT>(sec_cfg), type);
239 template <
typename MsgT>
240 template <
typename ConfT,
typename SecurityConfigT,
typename>
243 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
244 "SecurityConfigT must be Security::Config.");
253 template <
typename MsgT>
254 template <
typename SecurityConfigT>
257 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
258 "SecurityConfigT must be Security::Config.");
Fixed-size 128-byte buffer holder with SBO, five ownership modes and integrated codecs.
Definition: bytes.h:120
bool empty() const noexcept
Reports whether the buffer is logically empty.
Definition: bytes.h:861
static Bytes shallow_copy(uint8_t *data, size_t size) noexcept
Wraps an external mutable buffer as a non-owning alias.
Scope guard that opens and closes a CpuProfiler active interval.
Definition: cpu_profiler_guard.h:80
Copyable type-erased callable analogue of std::function with a tunable SBO and pool spill.
Definition: functional.h:131
bool enable_security(const Security::Config &cfg)
Installs a Security configuration before transport initialisation.
Definition: node-inl.h:321
virtual bool init()
Initialises the node and its transport back-end.
Definition: node-inl.h:39
Type-safe topic emitter for the VLink event communication model.
Definition: publisher.h:130
void mark_as_setter()
Promotes this publisher to behave as a Setter (field-writer) at the transport layer.
Definition: publisher-inl.h:184
void detect_subscribers(ConnectCallback &&callback)
Registers a callback invoked whenever subscriber presence transitions.
Definition: publisher-inl.h:90
std::unique_ptr< Publisher< MsgT, SecT > > UniquePtr
Owning unique-pointer alias.
Definition: publisher.h:132
Publisher(const ConfT &conf, InitType type=InitType::kWithInit)
Constructs a publisher from a typed transport configuration object.
Definition: publisher-inl.h:52
bool publish(const MsgT &msg, bool force=false)
Serialises and emits msg to every connected subscriber.
Definition: publisher-inl.h:110
bool has_subscribers() const
Non-blocking query of subscriber presence.
Definition: publisher-inl.h:105
bool publish_fbb(const void *fbb, bool force=false)
Publishes the raw payload of a pre-finished flatbuffers::FlatBufferBuilder.
Definition: publisher-inl.h:167
std::shared_ptr< Publisher< MsgT, SecT > > SharedPtr
Owning shared-pointer alias.
Definition: publisher.h:133
bool wait_for_subscribers(std::chrono::milliseconds timeout=Timeout::kDefaultInterval)
Blocks until at least one subscriber is detected or timeout expires.
Definition: publisher-inl.h:95
static SharedPtr create_shared(const std::string &url_str, InitType type=InitType::kWithInit)
Heap-allocates a Publisher and wraps it in a std::shared_ptr.
Definition: publisher-inl.h:45
static UniquePtr create_unique(const std::string &url_str, InitType type=InitType::kWithInit)
Heap-allocates a Publisher and wraps it in a std::unique_ptr.
Definition: publisher-inl.h:39
SecurityPublisher(const ConfT &conf, SecurityConfigT &&sec_cfg={}, InitType type=InitType::kWithInit)
Constructs a SecurityPublisher from a typed configuration object.
Definition: publisher-inl.h:241
static SharedPtr create_shared(const std::string &url_str, SecurityConfigT &&sec_cfg={}, InitType type=InitType::kWithInit)
Heap-allocates a SecurityPublisher and wraps it in a std::shared_ptr.
Definition: publisher-inl.h:230
std::unique_ptr< SecurityPublisher< MsgT > > UniquePtr
Owning unique-pointer alias.
Definition: publisher.h:296
std::shared_ptr< SecurityPublisher< MsgT > > SharedPtr
Owning shared-pointer alias.
Definition: publisher.h:297
static UniquePtr create_unique(const std::string &url_str, SecurityConfigT &&sec_cfg={}, InitType type=InitType::kWithInit)
Heap-allocates a SecurityPublisher and wraps it in a std::unique_ptr.
Definition: publisher-inl.h:219
#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.
@ kPublish
Message emitted by a Publisher node.
std::shared_ptr< IntraDataType > IntraData
Shared-ownership handle for an IntraDataType payload.
Definition: intra_data.h:112
@ kIntra
In-process queue (intra://).
@ kSetter
Field setter (writes latest value).
Definition: types.h:116
SecurityType
Compile-time selector for the per-node message security variant.
Definition: types.h:172
@ kWithSecurity
Encrypted and authenticated transport.
Definition: serializer-inl.h:126
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
Detects whether T is (or derives from) a std::shared_ptr specialisation.
Definition: traits.h:193
Conf subclass that routes virtual calls to the transport selected by a URL string.
Definition: url.h:213