16 #ifndef _SAG_CONNECTIVITY_PLUGINS_HPP_ 17 #define _SAG_CONNECTIVITY_PLUGINS_HPP_ 25 #ifndef __STDC_FORMAT_MACROS 26 #define __STDC_FORMAT_MACROS 1 31 namespace softwareag {
32 namespace _DATAT_INTERNAL_CPP_NAMESPACE {
39 void replace(std::string &input,
const std::string &from,
const std::string &to)
42 while((pos = input.find(from, pos)) != std::string::npos)
44 input.replace(pos, from.length(), to);
52 std::string
to_string(
const Message &m,
bool truncate=
true)
54 std::string payload =
to_string(m.getPayload());
55 if (truncate && payload.length() > 200) payload = payload.substr(0, 196) +
" ...";
57 replace(payload,
"\n",
"\\n");
58 replace(payload,
"\r",
"\\r");
59 return "Message<metadata="+
to_string(m.getMetadataMap())+
", payload="+payload+
">";
87 typedef std::unique_ptr<PluginHost> ptr_t;
104 if (SAG_ERROR_OK != sag_enable_reliability(chain, static_cast<int>(direction))) {
105 throw std::runtime_error(
"An error occurred while setting chain reliability");
111 bool isShuttingDown =
false;
112 if (SAG_ERROR_OK != sag_is_host_shutting_down(chain, isShuttingDown)) {
113 throw std::runtime_error(
"An error occurred while checking if host is shutting down");
115 return isShuttingDown;
121 PluginHost(
void* chain =
nullptr) :chain(chain) {}
168 : chainId(chainId), pluginName(pluginName), config(config.copy()), connectivityManager(connectivityManager), chain(chain)
173 std::string pluginName;
176 void* connectivityManager;
254 sag_delete_user_status_item(connectivityManager, underlying);
255 underlying.item =
nullptr;
267 std::unique_lock<std::mutex> ul(status_lock);
268 setStatusLocked(value);
281 std::unique_lock<std::mutex> ul(status_lock);
283 setStatusLocked(convert_to_details::integerToString(value));
294 std::unique_lock<std::mutex> ul(status_lock);
307 std::unique_lock<std::mutex> ul(status_lock);
308 intValue += incrementValue;
309 setStatusLocked(convert_to_details::integerToString(intValue));
314 const std::string &
key() {
return mkey; }
320 StatusItem(
void* connectivityManager,
const std::string &key,
const std::string &initialValue,
const int64_t intValue)
321 : intValue(intValue),
323 lastValue(initialValue),
324 connectivityManager(connectivityManager),
325 underlying(sag_create_user_status_item(connectivityManager, key.c_str(), initialValue.c_str()))
327 if (!underlying.item)
329 std::ostringstream oss;
330 oss <<
"Failed to create status item '" << key <<
"' (ensure the key is unique and that this plug-in has not been shutdown already)";
331 throw std::runtime_error(oss.str());
335 void setStatusLocked(
const std::string &value)
337 if (value == lastValue)
return;
340 sag_set_user_status_item(underlying, value.c_str());
344 const std::string mkey;
345 std::string lastValue;
346 void* connectivityManager;
347 sag_status_item_t underlying;
348 std::mutex status_lock;
370 return std::unique_ptr<StatusItem>(
new StatusItem(connectivityManager, key, initialValue, 0));
388 std::ostringstream oss;
390 return std::unique_ptr<StatusItem>(
new StatusItem(connectivityManager, key, oss.str(), initialValue));
406 sag_set_user_status_map(connectivityManager, reinterpret_cast<const sag_underlying_map_t&>(statusmap));
408 for (
auto it = statusmap.
cbegin(); it != statusmap.
cend(); it++)
411 if (it->second.empty())
412 mapKeysToCleanup.erase(it->first);
414 mapKeysToCleanup.insert(it->first.copy(),
data_t());
425 if (!mapKeysToCleanup.empty())
426 sag_set_user_status_map(connectivityManager, reinterpret_cast<const sag_underlying_map_t&>(mapKeysToCleanup));
432 explicit StatusReporter(
void *connectivityManager) : connectivityManager(connectivityManager), mapKeysToCleanup()
437 void* connectivityManager;
438 map_t mapKeysToCleanup;
441 StatusReporter() =
delete;
444 StatusReporter(
const StatusReporter& other) =
delete;
445 StatusReporter& operator=(
const StatusReporter&) =
delete;
464 host(
new PluginHost(params.chain)), logger(
"connectivity." + pluginName +
"." + chainId),
523 const std::string &
getName()
const {
return pluginName; }
547 if (statusReporter)
return *statusReporter;
548 throw std::runtime_error(
"Cannot call getStatusReporter when using the legacy constructor");
557 std::unique_ptr<StatusReporter> statusReporter;
570 typedef std::unique_ptr<HostSide>
ptr_t;
590 virtual void sendBatchTowardsHost(
Message *start,
Message *end) = 0;
600 sendBatchTowardsHost(&message, &message+1);
613 template<
typename IT>
615 !std::is_const<ampl::remove_ref_t<decltype(*begin)>>::value &&
616 ampl::is_same<
Message, ampl::remove_const_t<ampl::remove_ref_t<decltype(*begin)>>>::value
619 if(begin == end) sendBatchTowardsHost((
Message*)
nullptr, (
Message*)
nullptr);
620 else sendBatchTowardsHost(&(*begin), (&(*(end-1)))+1);
628 class RemoteHostSide:
public HostSide
631 RemoteHostSide(sag_plugin_t other, sag_send_fn_t fn): other(other), fn(fn) {}
632 virtual ~RemoteHostSide() {}
633 virtual void sendBatchTowardsHost(Message *start, Message *end);
649 typedef std::unique_ptr<TransportSide>
ptr_t;
670 virtual void sendBatchTowardsTransport(
Message *start,
Message *end) = 0;
680 sendBatchTowardsTransport(&message, &message+1);
692 template<
typename IT>
694 !std::is_const<ampl::remove_ref_t<decltype(*begin)>>::value &&
695 ampl::is_same<
Message, ampl::remove_const_t<ampl::remove_ref_t<decltype(*begin)>>>::value
698 if(begin == end) sendBatchTowardsTransport((
Message*)
nullptr, (
Message*)
nullptr);
699 else sendBatchTowardsTransport(&(*begin), (&(*(end-1)))+1);
707 class RemoteTransportSide:
public TransportSide
710 RemoteTransportSide(sag_plugin_t other, sag_send_fn_t fn): other(other), fn(fn) {}
711 virtual ~RemoteTransportSide() {}
712 virtual void sendBatchTowardsTransport(Message *start, Message *end);
761 hostSide = std::move(host);
764 virtual void setNextTowardsTransport(TransportSide::ptr_t &&transport)
766 transportSide = std::move(transport);
831 hostSide = std::move(host);
877 for (
Message *it = start; it != end; ++it) {
879 if (it->getPayload().empty()) {
880 deliverNullPayloadTowardsTransport(*it);
882 deliverMessageTowardsTransport(*it);
885 handleException(*it);
890 virtual void deliverMessageTowardsTransport(
Message &msg) = 0;
919 }
catch (
const std::exception &e) {
920 logger.warn(
"Error while delivering message: %s; %s will be dropped.", e.what(),
to_string(m).c_str());
922 logger.warn(
"Unknown error delivering message: %s",
to_string(m).c_str());
967 for (
Message *it = start; it != end; ++it) {
970 if (it->getPayload().empty()) {
971 rv = transformNullPayloadTowardsHost(*it);
973 rv = transformMessageTowardsHost(*it);
976 rv = handleException(*it,
false);
980 if (it != curr) it->swap(std::move(*curr));
984 if (hostSide.get()) hostSide->sendBatchTowardsHost(start, curr);
995 for (
Message *it = start; it != end; ++it) {
999 if (it->getPayload().empty()) {
1000 rv = transformNullPayloadTowardsTransport(*it);
1002 rv = transformMessageTowardsTransport(*it);
1005 rv = handleException(*it,
true);
1009 if (it != curr) it->swap(std::move(*curr));
1013 if (transportSide.get()) transportSide->sendBatchTowardsTransport(start, curr);
1020 virtual bool transformMessageTowardsHost(
Message &msg) = 0;
1025 virtual bool transformMessageTowardsTransport(
Message &msg) = 0;
1069 }
catch (
const std::exception &e) {
1070 logger.warn(
"Error while transforming message: %s; %s will be dropped.", e.what(),
to_string(m).c_str());
1072 logger.warn(
"Unknown error transforming message: %s",
to_string(m).c_str());
1080 namespace connectivity {
using namespace _DATAT_INTERNAL_CPP_NAMESPACE; }
1085 #include <sag_internal/exception.hpp> 1086 #include <sag_internal/remote_plugins.hpp> 1087 #include <sag_internal/plugin_macros.hpp> 1098 #define SAG_DECLARE_CONNECTIVITY_TRANSPORT_CLASS(Class) _SAG_DECLARE_CONNECTIVITY_TRANSPORT_CLASS(Class) 1109 #define SAG_DECLARE_CONNECTIVITY_CODEC_CLASS(Class) _SAG_DECLARE_CONNECTIVITY_CODEC_CLASS(Class) 1111 #endif // _SAG_CONNECTIVITY_PLUGINS_HPP_ A container for parameters passed to the constructor of a codec plug-in.
Definition: sag_connectivity_plugins.hpp:194
const std::string pluginName
The name used for this plug-in in the configuration file.
Definition: sag_connectivity_plugins.hpp:528
Base class that simplifies implementation of codec plug-ins that deal only with individual messages n...
Definition: sag_connectivity_plugins.hpp:942
AbstractTransport(const PluginConstructorParameters::TransportConstructorParameters ¶ms)
Constructor.
Definition: sag_connectivity_plugins.hpp:823
Base class that simplifies implementation of transport plug-ins that deal only with individual messag...
Definition: sag_connectivity_plugins.hpp:853
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:305
HostSide::ptr_t hostSide
The next plug-in in the chain towards host.
Definition: sag_connectivity_plugins.hpp:773
An interface to the next component (plugin or host) towards the host.
Definition: sag_connectivity_plugins.hpp:564
const_iterator cbegin() const
Forward const_iterator begin.
Definition: sag_connectivity_cpp.hpp:313
map_t config
The configuration of this plug-in.
Definition: sag_connectivity_plugins.hpp:532
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:386
Direction
The enumeration indicating the direction of message flow - towards the transport or towards the host.
Definition: sag_connectivity_plugins.hpp:68
Class for writing to the system logger.
Definition: sag_plugin_logging.hpp:71
const PluginHost::ptr_t host
Interface to support miscellaneous requests from this plug-in to the host system.
Definition: sag_connectivity_plugins.hpp:538
StatusReporter & getStatusReporter()
Allows reporting status information from this plug-in, such as online or failed status and number of ...
Definition: sag_connectivity_plugins.hpp:546
const_iterator cend() const
Forward const_iterator end.
Definition: sag_connectivity_cpp.hpp:315
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:404
virtual void sendBatchTowardsTransport(Message *start, Message *end)
Implements batch sending, calling deliverMessageTowardsTransport(Message&) for each message individua...
Definition: sag_connectivity_plugins.hpp:875
Base of the inheritance tree for Connectivity plugins.
Definition: sag_connectivity_plugins.hpp:456
TransportSide::ptr_t transportSide
The next plug-in in the chain towards transport.
Definition: sag_connectivity_plugins.hpp:779
std::unique_ptr< StatusItem > item_ptr
Unique pointer to a StatusItem.
Definition: sag_connectivity_plugins.hpp:352
void setStatus(const std::string &value)
Set a string status value.
Definition: sag_connectivity_plugins.hpp:265
AbstractSimpleTransport(const PluginConstructorParameters::TransportConstructorParameters ¶ms)
Constructor.
Definition: sag_connectivity_plugins.hpp:865
Logger logger
Logging for writing to the host log file.
Definition: sag_connectivity_plugins.hpp:555
virtual bool transformNullPayloadTowardsTransport(Message &msg)
Transform a message with a null payload in a transport-wards direction.
Definition: sag_connectivity_plugins.hpp:1039
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:486
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:693
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:221
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:649
virtual ~TransportSide()
Ensure virtual destruction.
Definition: sag_connectivity_plugins.hpp:647
Base class for transport plug-ins.
Definition: sag_connectivity_plugins.hpp:810
static const char * STATUS_ONLINE()
Returns a constant that should be used as the status value when a component is online,...
Definition: sag_connectivity_plugins.hpp:228
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:368
const std::string & getName() const
The name used for this plug-in in the configuration file.
Definition: sag_connectivity_plugins.hpp:523
bool isShuttingDown()
Check if host is shutting down.
Definition: sag_connectivity_plugins.hpp:110
virtual void handleException(Message &m)
Handle an exception thrown while delivering a message towards the transport.
Definition: sag_connectivity_plugins.hpp:915
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:236
virtual ~HostSide()
Ensure virtual destruction.
Definition: sag_connectivity_plugins.hpp:568
AbstractSimpleCodec(const PluginConstructorParameters::CodecConstructorParameters ¶ms)
Constructor.
Definition: sag_connectivity_plugins.hpp:954
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:495
Base class for codec plug-ins.
Definition: sag_connectivity_plugins.hpp:740
void sendBatchTowardsHost(Message &&message)
Overload for sending a batch containing a single message.
Definition: sag_connectivity_plugins.hpp:599
const std::string & getPluginName() const
Get the name used in the configuration file for this plug-in.
Definition: sag_connectivity_plugins.hpp:161
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:232
void setStatus(int64_t value)
Set an integer status value.
Definition: sag_connectivity_plugins.hpp:279
A map class which implements many of the functions on std::map.
Definition: sag_connectivity_cpp.hpp:35
const std::string & key()
Get the unique key specified when this status item was created.
Definition: sag_connectivity_plugins.hpp:314
A class that can be used to efficiently update the value associated with a single status key.
Definition: sag_connectivity_plugins.hpp:247
A container for an payload and associated metadata.
Definition: sag_connectivity_cpp.hpp:26
virtual void shutdown()
Stop processing messages and terminate and join any background threads.
Definition: sag_connectivity_plugins.hpp:521
virtual void sendBatchTowardsHost(Message *start, Message *end)
Implements batch sending, calling transformMessageTowardsHost(Message &) for each message individuall...
Definition: sag_connectivity_plugins.hpp:964
An interface to the next component (plugin or host) towards the transport.
Definition: sag_connectivity_plugins.hpp:643
AbstractCodec(const PluginConstructorParameters::CodecConstructorParameters ¶ms)
Constructor.
Definition: sag_connectivity_plugins.hpp:753
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:614
map_t copy() const
Return a deep copy of this map.
Definition: sag_connectivity_cpp.hpp:242
virtual void deliverNullPayloadTowardsTransport(Message &msg)
Deliver a message with a null payload.
Definition: sag_connectivity_plugins.hpp:892
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:103
Contains the headers needed to implement your own Connectivity Plugins.
const std::string & getChainId() const
Get the identifier used for the chain this plug-in is part of.
Definition: sag_connectivity_plugins.hpp:156
A container for parameters passed to the constructor of a transport plug-in.
Definition: sag_connectivity_plugins.hpp:182
The direction of messages flowing towards the host (from the transport).
Interface to support miscellaneous requests from a particular plug-in to the host system.
Definition: sag_connectivity_plugins.hpp:84
A base interface for parameters passed to the constructor of transport or codec plug-ins.
Definition: sag_connectivity_plugins.hpp:137
virtual bool handleException(Message &m, bool towardsTransport)
Handle an exception thrown while delivering a message.
Definition: sag_connectivity_plugins.hpp:1065
utf8-encoded const char*
Definition: sag_connectivity_c.h:45
const std::string chainId
The identifier used for the chain this plug-in is part of.
Definition: sag_connectivity_plugins.hpp:530
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:679
std::string getStatus()
Return the value this status item was set to most recently by this class.
Definition: sag_connectivity_plugins.hpp:293
A variant type which can be one of the following:
Definition: sag_connectivity_cpp.hpp:41
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:570
virtual void sendBatchTowardsTransport(Message *start, Message *end)
Implements batch sending, calling transformMessageTowardsTransport(Message &) for each message indivi...
Definition: sag_connectivity_plugins.hpp:992
const map_t & getConfig() const
Get the configuration for this plug-in.
Definition: sag_connectivity_plugins.hpp:151
virtual ~Plugin()
This destructor must be virtual.
Definition: sag_connectivity_plugins.hpp:474
HostSide::ptr_t hostSide
The next plug-in in the chain towards host.
Definition: sag_connectivity_plugins.hpp:839
virtual bool transformNullPayloadTowardsHost(Message &msg)
Transform a message with a null payload in a host-wards direction.
Definition: sag_connectivity_plugins.hpp:1030