17 #ifndef _SAG_CONNECTIVITY_PLUGINS_HPP_
18 #define _SAG_CONNECTIVITY_PLUGINS_HPP_
25 #ifndef __STDC_FORMAT_MACROS
26 #define __STDC_FORMAT_MACROS 1
31 namespace softwareag {
32 namespace connectivity {
55 typedef std::unique_ptr<PluginHost> ptr_t;
72 if (
SAG_ERROR_OK != sag_enable_reliability(chain, static_cast<int>(direction))) {
73 throw std::runtime_error(
"An error occurred while setting chain reliability");
80 if (
SAG_ERROR_OK != sag_is_host_shutting_down(chain, isShuttingDown)) {
81 throw std::runtime_error(
"An error occurred while checking if host is shutting down");
89 PluginHost(
void* chain = NULL) :chain(chain) {}
135 : chainId(chainId), pluginName(pluginName), config(config.copy()), connectivityManager(connectivityManager), chain(chain)
140 std::string pluginName;
143 void* connectivityManager;
220 sag_delete_user_status_item(connectivityManager, underlying);
231 if (!underlying.item)
throw std::runtime_error(
"Cannot call setStatus after StatusItem has been deleted!");
233 if (value == lastValue)
return;
236 sag_set_user_status_item(underlying, value.c_str());
269 intValue += incrementValue;
275 const std::string &
key() {
return mkey; }
280 static const std::string toString(int64_t value)
285 #if defined(_WIN32) || defined(__WIN32) || defined(WIN32)
287 _snprintf_s(result,
sizeof(result), _TRUNCATE,
"%" PRId64, value);
289 snprintf(result,
sizeof(result),
"%" PRId64, value);
291 return std::string(result);
294 StatusItem(
const StatusItem& other) =
delete;
295 StatusItem& operator=(
const StatusItem&) =
delete;
297 StatusItem(
void* connectivityManager,
const std::string &
key,
const std::string &initialValue,
const int64_t intValue)
298 : intValue(intValue),
300 lastValue(initialValue),
301 connectivityManager(connectivityManager),
302 underlying(sag_create_user_status_item(connectivityManager, key.c_str(), initialValue.c_str()))
304 if (!underlying.item)
306 std::ostringstream oss;
307 oss <<
"Failed to create status item '" << key <<
"' (ensure the key is unique and that this plug-in has not been shutdown already)";
308 throw std::runtime_error(oss.str());
313 const std::string mkey;
314 std::string lastValue;
315 void* connectivityManager;
316 sag_status_item_t underlying;
332 std::unique_ptr<StatusItem>
createStatusItem(
const std::string &key,
const std::string &initialValue)
334 return std::unique_ptr<StatusItem>(
new StatusItem(connectivityManager, key, initialValue, 0));
352 std::ostringstream oss;
354 return std::unique_ptr<StatusItem>(
new StatusItem(connectivityManager, key, oss.str(), initialValue));
369 sag_set_user_status_map(connectivityManager, reinterpret_cast<const sag_underlying_map_t&>(statusmap));
371 for (
auto it = statusmap.
cbegin(); it != statusmap.
cend(); it++)
374 if (it->second.empty())
375 mapKeysToCleanup.
erase(it->first);
377 mapKeysToCleanup.
insert(it->first.copy(), std::move(
data_t()));
388 if (!mapKeysToCleanup.
empty())
389 sag_set_user_status_map(connectivityManager, reinterpret_cast<const sag_underlying_map_t&>(mapKeysToCleanup));
395 explicit StatusReporter(
void *connectivityManager) : connectivityManager(connectivityManager), mapKeysToCleanup()
400 void* connectivityManager;
401 map_t mapKeysToCleanup;
404 StatusReporter() =
delete;
407 StatusReporter(
const StatusReporter& other) =
delete;
408 StatusReporter& operator=(
const StatusReporter&) =
delete;
428 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.",
429 pluginName.c_str(), chainId.c_str());
519 if (statusReporter)
return *statusReporter;
520 throw std::runtime_error(
"Cannot call getStatusReporter when using the legacy constructor");
536 std::unique_ptr<StatusReporter> statusReporter;
549 typedef std::auto_ptr<HostSide>
ptr_t;
576 RemoteHostSide(sag_plugin_t other, sag_send_fn_t fn): other(other), fn(fn) {}
594 typedef std::auto_ptr<TransportSide>
ptr_t;
675 :
Plugin(pluginName, chainId, config)
766 :
Plugin(name, chainId, config)
852 for (
Message *it = start; it != end; ++it) {
854 if (it->getPayload().empty()) {
894 }
catch (
const std::exception &e) {
895 logger.
warn(
"Caught exception delivering message: %s", e.what());
897 logger.
warn(
"Caught unknown exception delivering message: %s");
961 for (
Message *it = start; it != end; ++it) {
964 if (it->getPayload().empty()) {
974 if (it != curr) it->swap(std::move(*curr));
988 for (
Message *it = start; it != end; ++it) {
992 if (it->getPayload().empty()) {
1002 if (it != curr) it->swap(std::move(*curr));
1058 }
catch (
const std::exception &e) {
1059 logger.
warn(
"Caught exception transforming message: %s", e.what());
1061 logger.
warn(
"Caught unknown exception transforming message");
1070 #include <sag_internal/exception.hpp>
1071 #include <sag_internal/remote_plugins.hpp>
1072 #include <sag_internal/plugin_macros.hpp>
1083 #define SAG_DECLARE_CONNECTIVITY_TRANSPORT_CLASS(Class) _SAG_DECLARE_CONNECTIVITY_TRANSPORT_CLASS(Class)
1096 #define SAG_DECLARE_CONNECTIVITY_TRANSPORT(Class) _SAG_DECLARE_CONNECTIVITY_TRANSPORT_LEGACY(Class)
1107 #define SAG_DECLARE_CONNECTIVITY_CODEC_CLASS(Class) _SAG_DECLARE_CONNECTIVITY_CODEC_CLASS(Class)
1119 #define SAG_DECLARE_CONNECTIVITY_CODEC(Class) _SAG_DECLARE_CONNECTIVITY_CODEC_LEGACY(Class)
1121 #endif // _SAG_CONNECTIVITY_PLUGINS_HPP_
A container for parameters passed to the constructor of a codec plug-in.
Definition: sag_connectivity_plugins.hpp:161
Logger LOGGER
Legacy logging for writing to the host log file [DEPRECATED].
Definition: sag_connectivity_plugins.hpp:529
bool empty() const
Returns true if the map is empty (size() == 0)
Definition: sag_connectivity_cpp.hpp:249
const std::string pluginName
The name used for this plug-in in the configuration file.
Definition: sag_connectivity_plugins.hpp:501
Base class that simplifies implementation of codec plug-ins that deal only with individual messages n...
Definition: sag_connectivity_plugins.hpp:920
AbstractTransport(const PluginConstructorParameters::TransportConstructorParameters ¶ms)
Constructor.
Definition: sag_connectivity_plugins.hpp:777
Base class that simplifies implementation of transport plug-ins that deal only with individual messag...
Definition: sag_connectivity_plugins.hpp:811
Wrap the next plugin in order as a TransportSide object.
Definition: sag_connectivity_plugins.hpp:618
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:267
HostSide::ptr_t hostSide
The next plug-in in the chain towards host.
Definition: sag_connectivity_plugins.hpp:706
An interface to the next component (plugin or host) towards the host.
Definition: sag_connectivity_plugins.hpp:543
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:505
Direction
The enumeration indicating the direction of message flow - towards the transport or towards the host...
Definition: sag_connectivity_plugins.hpp:37
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:511
StatusReporter & getStatusReporter()
Allows reporting status information from this plug-in, such as connected/disconnected status and numb...
Definition: sag_connectivity_plugins.hpp:518
A map class which implements many of the functions on std::map.
Definition: map_t.hpp:36
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:367
virtual void sendBatchTowardsTransport(Message *start, Message *end)
Implements batch sending, calling deliverMessageTowardsTransport(Message&) for each message individua...
Definition: sag_connectivity_plugins.hpp:850
Base of the inheritance tree for Connectivity plugins.
Definition: sag_connectivity_plugins.hpp:419
TransportSide::ptr_t transportSide
The next plug-in in the chain towards transport.
Definition: sag_connectivity_plugins.hpp:712
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:229
Definition: sag_connectivity_threading.h:178
std::auto_ptr< TransportSide > ptr_t
Pointers to TransportSides should always be this ptr_t type, which is a std::auto_ptr.
Definition: sag_connectivity_plugins.hpp:594
AbstractSimpleTransport(const PluginConstructorParameters::TransportConstructorParameters ¶ms)
Constructor.
Definition: sag_connectivity_plugins.hpp:841
Logger logger
Logging for writing to the host log file.
Definition: sag_connectivity_plugins.hpp:534
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:1028
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:459
std::unique_ptr< StatusItem > 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:332
A class allowing a plug-in to report status values to the host.
Definition: sag_connectivity_plugins.hpp:186
size_t erase(const data_t &k)
Remove the item with the specified key.
virtual ~TransportSide()
Ensure virtual destruction.
Definition: sag_connectivity_plugins.hpp:592
Base class for transport plug-ins.
Definition: sag_connectivity_plugins.hpp:746
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:193
const_iterator cbegin() const
Forward const_iterator begin.
Definition: sag_connectivity_cpp.hpp:241
AbstractSimpleTransport(const std::string &name, const std::string &chainId, const map_t &config)
Legacy constructor [DEPRECATED].
Definition: sag_connectivity_plugins.hpp:829
bool isShuttingDown()
Check if host is shutting down.
Definition: sag_connectivity_plugins.hpp:78
const_iterator cend() const
Forward const_iterator end.
Definition: sag_connectivity_cpp.hpp:243
virtual void handleException(Message &m)
Handle an exception thrown while delivering a message towards the transport.
Definition: sag_connectivity_plugins.hpp:890
Wrap the next plugin in order as a HostSide object.
Definition: sag_connectivity_plugins.hpp:573
const std::string name
The name used for this plug-in in the configuration file [DEPRECATED].
Definition: sag_connectivity_plugins.hpp:498
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:201
virtual ~HostSide()
Ensure virtual destruction.
Definition: sag_connectivity_plugins.hpp:547
AbstractSimpleCodec(const PluginConstructorParameters::CodecConstructorParameters ¶ms)
Constructor.
Definition: sag_connectivity_plugins.hpp:949
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:468
std::pair< iterator, bool > insert(const std::pair< data_t &&, data_t && > &v)
Insert a new key/value pair into the map.
Base class for codec plug-ins.
Definition: sag_connectivity_plugins.hpp:654
No error.
Definition: sag_connectivity_c.h:62
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:197
std::unique_ptr< StatusItem > 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:350
virtual bool transformMessageTowardsTransport(Message &msg)=0
Abstract method that must be implemented to handle transformation of an individual message in a trans...
AbstractSimpleCodec(const std::string &name, const std::string &chainId, const map_t &config)
Legacy constructor [DEPRECATED].
Definition: sag_connectivity_plugins.hpp:937
void setStatus(int64_t value)
Set an integer status value.
Definition: sag_connectivity_plugins.hpp:247
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:37
const std::string & key()
Get the unique key specified when this status item was created.
Definition: sag_connectivity_plugins.hpp:275
const std::string & getPluginName() const
Get the name used in the configuration file for this plug-in.
Definition: sag_connectivity_plugins.hpp:128
A class that can be used to efficiently update the value associated with a single status key...
Definition: sag_connectivity_plugins.hpp:213
A container for an payload and associated metadata.
Definition: sag_connectivity_cpp.hpp:28
virtual void shutdown()
Stop processing messages and terminate and join any background threads.
Definition: sag_connectivity_plugins.hpp:492
virtual void sendBatchTowardsHost(Message *start, Message *end)
Implements batch sending, calling transformMessageTowardsHost(Message &) for each message individuall...
Definition: sag_connectivity_plugins.hpp:958
An interface to the next component (plugin or host) towards the transport.
Definition: sag_connectivity_plugins.hpp:588
AbstractCodec(const PluginConstructorParameters::CodecConstructorParameters ¶ms)
Constructor.
Definition: sag_connectivity_plugins.hpp:686
virtual void deliverMessageTowardsTransport(Message &msg)=0
Abstract method that must be implemented to handle delivery of an individual message.
virtual void deliverNullPayloadTowardsTransport(Message &msg)
Deliver a message with a null payload.
Definition: sag_connectivity_plugins.hpp:867
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:71
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:674
A container for parameters passed to the constructor of a transport plug-in.
Definition: sag_connectivity_plugins.hpp:149
The direction of messages flowing towards the host (from the transport).
map_t copy() const
Return a deep copy of this map.
Definition: sag_connectivity_cpp.hpp:197
AbstractTransport(const std::string &name, const std::string &chainId, const map_t &config)
Legacy constructor [DEPRECATED].
Definition: sag_connectivity_plugins.hpp:765
const std::string & getChainId() const
Get the identifier used for the chain this plug-in is part of.
Definition: sag_connectivity_plugins.hpp:123
Interface to support miscellaneous requests from a particular plug-in to the host system...
Definition: sag_connectivity_plugins.hpp:52
A base interface for parameters passed to the constructor of transport or codec plug-ins.
Definition: sag_connectivity_plugins.hpp:105
virtual bool handleException(Message &m, bool towardsTransport)
Handle an exception thrown while delivering a message.
Definition: sag_connectivity_plugins.hpp:1054
utf8-encoded const char*
Definition: sag_connectivity_c.h:46
virtual void sendBatchTowardsHost(Message *start, Message *end)
Abstract method that must be implemented to handle delivery of a batch of messages heading towards th...
const std::string chainId
The identifier used for the chain this plug-in is part of.
Definition: sag_connectivity_plugins.hpp:503
The direction of messages flowing towards the transport (from the host).
std::string getStatus()
Return the value this status item was set to most recently by this class.
Definition: sag_connectivity_plugins.hpp:258
A variant type which can be one of the following:
Definition: sag_connectivity_cpp.hpp:43
const map_t & getConfig() const
Get the configuration for this plug-in.
Definition: sag_connectivity_plugins.hpp:118
virtual void sendBatchTowardsTransport(Message *start, Message *end)
Implements batch sending, calling transformMessageTowardsTransport(Message &) for each message indivi...
Definition: sag_connectivity_plugins.hpp:985
std::auto_ptr< HostSide > ptr_t
Pointers to HostSides should always be this ptr_t type, which is a std::auto_ptr. ...
Definition: sag_connectivity_plugins.hpp:549
virtual ~Plugin()
This destructor must be virtual.
Definition: sag_connectivity_plugins.hpp:447
HostSide::ptr_t hostSide
The next plug-in in the chain towards host.
Definition: sag_connectivity_plugins.hpp:793
virtual void sendBatchTowardsTransport(Message *start, Message *end)
Abstract method that must be implemented to handle delivery of a batch of messages heading towards th...
const std::string & getName() const
Return the name of this plugin instance.
Definition: sag_connectivity_plugins.hpp:494
virtual bool transformNullPayloadTowardsHost(Message &msg)
Transform a message with a null payload in a host-wards direction.
Definition: sag_connectivity_plugins.hpp:1020