17 #ifndef _SAG_CONNECTIVITY_PLUGINS_HPP_
18 #define _SAG_CONNECTIVITY_PLUGINS_HPP_
26 #ifndef __STDC_FORMAT_MACROS
27 #define __STDC_FORMAT_MACROS 1
32 namespace softwareag {
33 namespace connectivity {
40 void replace(std::string &input,
const std::string &from,
const std::string &to)
43 while((pos = input.find(from, pos)) != std::string::npos)
45 input.replace(pos, from.length(), to);
53 std::string
to_string(
const Message &m,
bool truncate=
true)
55 std::string payload =
to_string(m.getPayload());
56 if (truncate && payload.length() > 200) payload = payload.substr(0, 196) +
" ...";
58 replace(payload,
"\n",
"\\n");
59 replace(payload,
"\r",
"\\r");
60 return "Message<metadata="+
to_string(m.getMetadataMap())+
", payload="+payload+
">";
88 typedef std::unique_ptr<PluginHost> ptr_t;
105 if (
SAG_ERROR_OK != sag_enable_reliability(chain, static_cast<int>(direction))) {
106 throw std::runtime_error(
"An error occurred while setting chain reliability");
113 if (
SAG_ERROR_OK != sag_is_host_shutting_down(chain, isShuttingDown)) {
114 throw std::runtime_error(
"An error occurred while checking if host is shutting down");
122 PluginHost(
void* chain =
nullptr) :chain(chain) {}
169 : chainId(chainId), pluginName(pluginName), config(config.copy()), connectivityManager(connectivityManager), chain(chain)
174 std::string pluginName;
177 void* connectivityManager;
259 sag_delete_user_status_item(connectivityManager, underlying);
260 underlying.item =
nullptr;
270 std::unique_lock<std::mutex> ul(status_lock);
271 setStatusLocked(value);
284 std::unique_lock<std::mutex> ul(status_lock);
286 setStatusLocked(convert_to_details::integerToString(value));
305 std::unique_lock<std::mutex> ul(status_lock);
306 intValue += incrementValue;
307 setStatusLocked(convert_to_details::integerToString(intValue));
312 const std::string &
key() {
return mkey; }
318 StatusItem(
void* connectivityManager,
const std::string &
key,
const std::string &initialValue,
const int64_t intValue)
319 : intValue(intValue),
321 lastValue(initialValue),
322 connectivityManager(connectivityManager),
323 underlying(sag_create_user_status_item(connectivityManager, key.c_str(), initialValue.c_str()))
325 if (!underlying.item)
327 std::ostringstream oss;
328 oss <<
"Failed to create status item '" << key <<
"' (ensure the key is unique and that this plug-in has not been shutdown already)";
329 throw std::runtime_error(oss.str());
333 void setStatusLocked(
const std::string &value)
335 if (value == lastValue)
return;
338 sag_set_user_status_item(underlying, value.c_str());
342 const std::string mkey;
343 std::string lastValue;
344 void* connectivityManager;
345 sag_status_item_t underlying;
346 std::mutex status_lock;
367 return std::unique_ptr<StatusItem>(
new StatusItem(connectivityManager, key, initialValue, 0));
384 std::ostringstream oss;
386 return std::unique_ptr<StatusItem>(
new StatusItem(connectivityManager, key, oss.str(), initialValue));
401 sag_set_user_status_map(connectivityManager, reinterpret_cast<const sag_underlying_map_t&>(statusmap));
403 for (
auto it = statusmap.
cbegin(); it != statusmap.
cend(); it++)
406 if (it->second.empty())
407 mapKeysToCleanup.
erase(it->first);
409 mapKeysToCleanup.
insert(it->first.copy(), std::move(
data_t()));
420 if (!mapKeysToCleanup.
empty())
421 sag_set_user_status_map(connectivityManager, reinterpret_cast<const sag_underlying_map_t&>(mapKeysToCleanup));
427 explicit StatusReporter(
void *connectivityManager) : connectivityManager(connectivityManager), mapKeysToCleanup()
432 void* connectivityManager;
433 map_t mapKeysToCleanup;
436 StatusReporter() =
delete;
439 StatusReporter(
const StatusReporter& other) =
delete;
440 StatusReporter& operator=(
const StatusReporter&) =
delete;
458 :
name(pluginName), pluginName(pluginName), chainId(chainId), config(config.copy()),
host(new
PluginHost()),
LOGGER(
"connectivity." + pluginName +
"." + chainId),
logger(
"connectivity." +
name +
"." + chainId), statusReporter()
460 logger.
info(
"The %s connectivity plug-in in chain '%s' is using the deprecated legacy (std::string&,std::string&,map_t&) constructor. We recommend changing to use the recommended (PluginConstructorParameters&) constructor instead.",
461 pluginName.c_str(), chainId.c_str());
468 :
name(params.getPluginName()),
pluginName(params.getPluginName()),
chainId(params.getChainId()),
config(params.getConfig().copy()),
553 if (statusReporter)
return *statusReporter;
554 throw std::runtime_error(
"Cannot call getStatusReporter when using the legacy constructor");
570 std::unique_ptr<StatusReporter> statusReporter;
583 typedef std::unique_ptr<HostSide>
ptr_t;
625 template<
typename IT>
627 !std::is_const<ampl::remove_ref_t<decltype(*begin)>>::value &&
628 ampl::is_same<Message, ampl::remove_const_t<ampl::remove_ref_t<decltype(*begin)>>>::value
640 class RemoteHostSide:
public HostSide
643 RemoteHostSide(sag_plugin_t other, sag_send_fn_t fn): other(other), fn(fn) {}
644 virtual ~RemoteHostSide() {}
645 virtual void sendBatchTowardsHost(Message *start, Message *end);
661 typedef std::unique_ptr<TransportSide>
ptr_t;
704 template<
typename IT>
706 !std::is_const<ampl::remove_ref_t<decltype(*begin)>>::value &&
707 ampl::is_same<Message, ampl::remove_const_t<ampl::remove_ref_t<decltype(*begin)>>>::value
719 class RemoteTransportSide:
public TransportSide
722 RemoteTransportSide(sag_plugin_t other, sag_send_fn_t fn): other(other), fn(fn) {}
723 virtual ~RemoteTransportSide() {}
724 virtual void sendBatchTowardsTransport(Message *start, Message *end);
776 :
Plugin(pluginName, chainId, config)
867 :
Plugin(name, chainId, config)
953 for (
Message *it = start; it != end; ++it) {
955 if (it->getPayload().empty()) {
995 }
catch (
const std::exception &e) {
996 logger.
warn(
"Error while delivering message: %s; %s will be dropped.", e.what(),
to_string(m).c_str());
1063 for (
Message *it = start; it != end; ++it) {
1066 if (it->getPayload().empty()) {
1076 if (it != curr) it->swap(std::move(*curr));
1091 for (
Message *it = start; it != end; ++it) {
1095 if (it->getPayload().empty()) {
1105 if (it != curr) it->swap(std::move(*curr));
1165 }
catch (
const std::exception &e) {
1166 logger.
warn(
"Error while transforming message: %s; %s will be dropped.", e.what(),
to_string(m).c_str());
1177 #include <sag_internal/exception.hpp>
1178 #include <sag_internal/remote_plugins.hpp>
1179 #include <sag_internal/plugin_macros.hpp>
1190 #define SAG_DECLARE_CONNECTIVITY_TRANSPORT_CLASS(Class) _SAG_DECLARE_CONNECTIVITY_TRANSPORT_CLASS(Class)
1203 #define SAG_DECLARE_CONNECTIVITY_TRANSPORT(Class) _SAG_DECLARE_CONNECTIVITY_TRANSPORT_LEGACY(Class)
1214 #define SAG_DECLARE_CONNECTIVITY_CODEC_CLASS(Class) _SAG_DECLARE_CONNECTIVITY_CODEC_CLASS(Class)
1226 #define SAG_DECLARE_CONNECTIVITY_CODEC(Class) _SAG_DECLARE_CONNECTIVITY_CODEC_LEGACY(Class)
1228 #endif // _SAG_CONNECTIVITY_PLUGINS_HPP_
A container for parameters passed to the constructor of a codec plug-in.
Definition: sag_connectivity_plugins.hpp:195
Logger LOGGER
Legacy logging for writing to the host log file [DEPRECATED].
Definition: sag_connectivity_plugins.hpp:563
bool empty() const
Returns true if the map is empty (size() == 0)
Definition: sag_connectivity_cpp.hpp:290
const std::string pluginName
The name used for this plug-in in the configuration file.
Definition: sag_connectivity_plugins.hpp:534
Base class that simplifies implementation of codec plug-ins that deal only with individual messages n...
Definition: sag_connectivity_plugins.hpp:1021
AbstractTransport(const PluginConstructorParameters::TransportConstructorParameters ¶ms)
Constructor.
Definition: sag_connectivity_plugins.hpp:878
Base class that simplifies implementation of transport plug-ins that deal only with individual messag...
Definition: sag_connectivity_plugins.hpp:911
void increment(int64_t incrementValue=1)
Set an integer status value by incrementing the previous integer value that was set by this object...
Definition: sag_connectivity_plugins.hpp:303
HostSide::ptr_t hostSide
The next plug-in in the chain towards host.
Definition: sag_connectivity_plugins.hpp:807
An interface to the next component (plugin or host) towards the host.
Definition: sag_connectivity_plugins.hpp:577
virtual void sendBatchTowardsHost(Message *start, Message *end)=0
Abstract method that must be implemented to handle delivery of a batch of messages heading towards th...
map_t config
The configuration of this plug-in.
Definition: sag_connectivity_plugins.hpp:538
item_ptr createStatusItem(const std::string &key, int64_t initialValue)
Creates a StatusItem instance that can be used to report status for a given key, using an integral in...
Definition: sag_connectivity_plugins.hpp:382
Direction
The enumeration indicating the direction of message flow - towards the transport or towards the host...
Definition: sag_connectivity_plugins.hpp:69
void warn(const char *format, ARGS...args) const
Log a message at WARN level.
Definition: sag_plugin_logging.hpp:153
Class for writing to the system logger.
Definition: sag_plugin_logging.hpp:72
const PluginHost::ptr_t host
Interface to support miscellaneous requests from this plug-in to the host system. ...
Definition: sag_connectivity_plugins.hpp:544
StatusReporter & getStatusReporter()
Allows reporting status information from this plug-in, such as online or failed status and number of ...
Definition: sag_connectivity_plugins.hpp:552
Contains the C++ implementation of the underlying datatypes used by connectivity plugins and their ac...
void setStatus(const map_t &statusmap)
Set multiple related string status values at the same time (atomically).
Definition: sag_connectivity_plugins.hpp:399
virtual void sendBatchTowardsTransport(Message *start, Message *end)
Implements batch sending, calling deliverMessageTowardsTransport(Message&) for each message individua...
Definition: sag_connectivity_plugins.hpp:951
Base of the inheritance tree for Connectivity plugins.
Definition: sag_connectivity_plugins.hpp:451
TransportSide::ptr_t transportSide
The next plug-in in the chain towards transport.
Definition: sag_connectivity_plugins.hpp:813
std::unique_ptr< StatusItem > item_ptr
Unique pointer to a StatusItem.
Definition: sag_connectivity_plugins.hpp:350
virtual void sendBatchTowardsTransport(Message *start, Message *end)=0
Abstract method that must be implemented to handle delivery of a batch of messages heading towards th...
void setStatus(const std::string &value)
Set a string status value.
Definition: sag_connectivity_plugins.hpp:268
Definition: sag_connectivity_threading.h:178
AbstractSimpleTransport(const PluginConstructorParameters::TransportConstructorParameters ¶ms)
Constructor.
Definition: sag_connectivity_plugins.hpp:941
Logger logger
Logging for writing to the host log file.
Definition: sag_connectivity_plugins.hpp:568
void info(const char *format, ARGS...args) const
Log a message at INFO level.
Definition: sag_plugin_logging.hpp:155
virtual bool transformNullPayloadTowardsTransport(Message &msg)
Transform a message with a null payload in a transport-wards direction.
Definition: sag_connectivity_plugins.hpp:1135
virtual void start()
Called when an entire chain has been created and the plugin is allowed to start up (after all plugins...
Definition: sag_connectivity_plugins.hpp:491
auto sendBatchTowardsTransport(const IT &begin, const IT &end) -> typename std::enable_if< !std::is_const< ampl::remove_ref_t< decltype(*begin)>>::value &&l::is_same< Message, ampl::remove_const_t< ampl::remove_ref_t< decltype(*begin)>>>::value, void >::type
Overload for sending messages using an iterator range.
Definition: sag_connectivity_plugins.hpp:705
std::enable_if< get_underlying< T >::value, std::string >::type to_string(const T &t)
Get a string representation of t.
A class allowing a plug-in to report status values to the host.
Definition: sag_connectivity_plugins.hpp:226
std::unique_ptr< TransportSide > ptr_t
Pointers to TransportSides should always be this ptr_t type, which is a std::unique_ptr.
Definition: sag_connectivity_plugins.hpp:661
size_t erase(const data_t &k)
Remove the item with the specified key.
virtual ~TransportSide()
Ensure virtual destruction.
Definition: sag_connectivity_plugins.hpp:659
Base class for transport plug-ins.
Definition: sag_connectivity_plugins.hpp:847
static const char * STATUS_ONLINE()
Returns a constant that should be used as the status value when a component is online, operational, connected, and ready to handles messages.
Definition: sag_connectivity_plugins.hpp:233
item_ptr createStatusItem(const std::string &key, const std::string &initialValue)
Creates a StatusItem instance that can be used to report status for a given key.
Definition: sag_connectivity_plugins.hpp:365
const_iterator cbegin() const
Forward const_iterator begin.
Definition: sag_connectivity_cpp.hpp:282
AbstractSimpleTransport(const std::string &name, const std::string &chainId, const map_t &config)
Legacy constructor [DEPRECATED].
Definition: sag_connectivity_plugins.hpp:929
bool isShuttingDown()
Check if host is shutting down.
Definition: sag_connectivity_plugins.hpp:111
const_iterator cend() const
Forward const_iterator end.
Definition: sag_connectivity_cpp.hpp:284
virtual void handleException(Message &m)
Handle an exception thrown while delivering a message towards the transport.
Definition: sag_connectivity_plugins.hpp:991
const std::string name
The name used for this plug-in in the configuration file.
Definition: sag_connectivity_plugins.hpp:530
static const char * STATUS_FAILED()
Returns a constant that should be used as the status value when a component is not currently operatio...
Definition: sag_connectivity_plugins.hpp:241
virtual ~HostSide()
Ensure virtual destruction.
Definition: sag_connectivity_plugins.hpp:581
AbstractSimpleCodec(const PluginConstructorParameters::CodecConstructorParameters ¶ms)
Constructor.
Definition: sag_connectivity_plugins.hpp:1050
virtual void hostReady()
Called some time after start(), when the host is ready to start receiving input (sends will be queued...
Definition: sag_connectivity_plugins.hpp:500
Base class for codec plug-ins.
Definition: sag_connectivity_plugins.hpp:755
No error.
Definition: sag_connectivity_c.h:64
void sendBatchTowardsHost(Message &&message)
Overload for sending a batch containing a single message.
Definition: sag_connectivity_plugins.hpp:612
static const char * STATUS_STARTING()
Returns a constant that should be used as the status value when a component is still starting...
Definition: sag_connectivity_plugins.hpp:237
virtual bool transformMessageTowardsTransport(Message &msg)=0
Abstract method that must be implemented to handle transformation of an individual message in a trans...
std::pair< iterator, bool > insert(insertion_t &&v)
Insert a new key/value pair into the map.
AbstractSimpleCodec(const std::string &name, const std::string &chainId, const map_t &config)
Legacy constructor [DEPRECATED].
Definition: sag_connectivity_plugins.hpp:1038
void setStatus(int64_t value)
Set an integer status value.
Definition: sag_connectivity_plugins.hpp:282
virtual bool transformMessageTowardsHost(Message &msg)=0
Abstract method that must be implemented to handle transformation of an individual message...
A map class which implements many of the functions on std::map.
Definition: sag_connectivity_cpp.hpp:36
const std::string & key()
Get the unique key specified when this status item was created.
Definition: sag_connectivity_plugins.hpp:312
const std::string & getPluginName() const
Get the name used in the configuration file for this plug-in.
Definition: sag_connectivity_plugins.hpp:162
A class that can be used to efficiently update the value associated with a single status key...
Definition: sag_connectivity_plugins.hpp:252
A container for an payload and associated metadata.
Definition: sag_connectivity_cpp.hpp:27
virtual void shutdown()
Stop processing messages and terminate and join any background threads.
Definition: sag_connectivity_plugins.hpp:524
virtual void sendBatchTowardsHost(Message *start, Message *end)
Implements batch sending, calling transformMessageTowardsHost(Message &) for each message individuall...
Definition: sag_connectivity_plugins.hpp:1060
An interface to the next component (plugin or host) towards the transport.
Definition: sag_connectivity_plugins.hpp:655
AbstractCodec(const PluginConstructorParameters::CodecConstructorParameters ¶ms)
Constructor.
Definition: sag_connectivity_plugins.hpp:787
virtual void deliverMessageTowardsTransport(Message &msg)=0
Abstract method that must be implemented to handle delivery of an individual message.
auto sendBatchTowardsHost(const IT &begin, const IT &end) -> typename std::enable_if< !std::is_const< ampl::remove_ref_t< decltype(*begin)>>::value &&l::is_same< Message, ampl::remove_const_t< ampl::remove_ref_t< decltype(*begin)>>>::value, void >::type
Overload for sending messages using an iterator range.
Definition: sag_connectivity_plugins.hpp:626
virtual void deliverNullPayloadTowardsTransport(Message &msg)
Deliver a message with a null payload.
Definition: sag_connectivity_plugins.hpp:968
void enableReliability(Direction direction)
Enable reliable messaging for the chain that this plug-in belongs to, in a particular direction i...
Definition: sag_connectivity_plugins.hpp:104
Contains the headers needed to implement your own Connectivity Plugins.
AbstractCodec(const std::string &pluginName, const std::string &chainId, const map_t &config)
Legacy constructor [DEPRECATED].
Definition: sag_connectivity_plugins.hpp:775
A container for parameters passed to the constructor of a transport plug-in.
Definition: sag_connectivity_plugins.hpp:183
The direction of messages flowing towards the host (from the transport).
AbstractTransport(const std::string &name, const std::string &chainId, const map_t &config)
Legacy constructor [DEPRECATED].
Definition: sag_connectivity_plugins.hpp:866
const std::string & getChainId() const
Get the identifier used for the chain this plug-in is part of.
Definition: sag_connectivity_plugins.hpp:157
Interface to support miscellaneous requests from a particular plug-in to the host system...
Definition: sag_connectivity_plugins.hpp:85
A base interface for parameters passed to the constructor of transport or codec plug-ins.
Definition: sag_connectivity_plugins.hpp:138
Plugin(const std::string &pluginName, const std::string &chainId, const map_t &config)
Legacy constructor [DEPRECATED].
Definition: sag_connectivity_plugins.hpp:457
virtual bool handleException(Message &m, bool towardsTransport)
Handle an exception thrown while delivering a message.
Definition: sag_connectivity_plugins.hpp:1161
utf8-encoded const char*
Definition: sag_connectivity_c.h:46
const std::string chainId
The identifier used for the chain this plug-in is part of.
Definition: sag_connectivity_plugins.hpp:536
The direction of messages flowing towards the transport (from the host).
void sendBatchTowardsTransport(Message &&message)
Overload for sending a batch containing a single message.
Definition: sag_connectivity_plugins.hpp:691
std::string getStatus()
Return the value this status item was set to most recently by this class.
Definition: sag_connectivity_plugins.hpp:294
A variant type which can be one of the following:
Definition: sag_connectivity_cpp.hpp:42
std::unique_ptr< HostSide > ptr_t
Pointers to HostSides should always be this ptr_t type, which is a std::unique_ptr.
Definition: sag_connectivity_plugins.hpp:583
const map_t & getConfig() const
Get the configuration for this plug-in.
Definition: sag_connectivity_plugins.hpp:152
virtual void sendBatchTowardsTransport(Message *start, Message *end)
Implements batch sending, calling transformMessageTowardsTransport(Message &) for each message indivi...
Definition: sag_connectivity_plugins.hpp:1088
virtual ~Plugin()
This destructor must be virtual.
Definition: sag_connectivity_plugins.hpp:479
HostSide::ptr_t hostSide
The next plug-in in the chain towards host.
Definition: sag_connectivity_plugins.hpp:894
const std::string & getName() const
The name used for this plug-in in the configuration file.
Definition: sag_connectivity_plugins.hpp:526
virtual bool transformNullPayloadTowardsHost(Message &msg)
Transform a message with a null payload in a host-wards direction.
Definition: sag_connectivity_plugins.hpp:1126