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 VUNLIKELY (!Serializer::serialize_to_transport<kMsgType>(
138 msg, msg_data, this->impl_->transport_type, use_loan,
139 [
this](
size_t size) { return this->impl_->loan(size); })) {
140 VLOG_T(
"Publisher serialize failed, url: ", this->impl_->url,
".");
143 if (this->is_support_loan_) {
144 this->impl_->return_loan(msg_data);
151 bool ret = write_bytes(msg_data);
157 template <
typename MsgT, SecurityType SecT>
159 #ifdef VLINK_HAS_FLATBUFFERS
160 const auto* fbb_ptr =
static_cast<const flatbuffers::FlatBufferBuilder*
>(fbb);
162 #ifndef VLINK_DISABLE_PROFILER
167 if (!this->impl_->has_subscribers()) {
180 template <
typename MsgT, SecurityType SecT>
182 if VUNLIKELY (this->has_inited_.load(std::memory_order_acquire)) {
183 this->impl_->deinit_ext();
184 this->impl_->impl_type =
kSetter;
185 this->impl_->init_ext();
191 template <
typename MsgT, SecurityType SecT>
192 inline bool Publisher<MsgT, SecT>::write_bytes(
const Bytes& data) {
196 if VUNLIKELY (!this->impl_->security || !this->impl_->security->encrypt(data, sec_data)) {
197 VLOG_T(
"Publisher encrypt failed, url: ", this->impl_->url,
".");
201 return this->impl_->write(sec_data);
205 return this->impl_->write(data);
209 template <
typename MsgT, SecurityType SecT>
210 inline bool Publisher<MsgT, SecT>::write_intra(
const IntraData& intra_data) {
211 return this->impl_->write(intra_data);
214 template <
typename MsgT>
215 template <
typename SecurityConfigT>
217 SecurityConfigT&& sec_cfg,
219 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
220 "SecurityConfigT must be Security::Config.");
222 return std::make_unique<SecurityPublisher<MsgT>>(url_str, std::forward<SecurityConfigT>(sec_cfg), type);
225 template <
typename MsgT>
226 template <
typename SecurityConfigT>
228 SecurityConfigT&& sec_cfg,
230 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
231 "SecurityConfigT must be Security::Config.");
233 return std::make_shared<SecurityPublisher<MsgT>>(url_str, std::forward<SecurityConfigT>(sec_cfg), type);
236 template <
typename MsgT>
237 template <
typename ConfT,
typename SecurityConfigT,
typename>
240 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
241 "SecurityConfigT must be Security::Config.");
250 template <
typename MsgT>
251 template <
typename SecurityConfigT>
254 static_assert(std::is_same_v<std::decay_t<SecurityConfigT>,
Security::Config>,
255 "SecurityConfigT must be Security::Config.");
Fixed-size 128-byte buffer holder with SBO, five ownership modes and integrated codecs.
Definition: bytes.h:120
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:132
bool enable_security(const Security::Config &cfg)
Installs a Security configuration before transport initialisation.
Definition: node-inl.h:327
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:181
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:158
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:238
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:227
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:216
#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.
@ 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:114
SecurityType
Compile-time selector for the per-node message security variant.
Definition: types.h:168
@ kWithSecurity
Encrypted and authenticated transport.
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
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:216