Commit 22931a04 authored by Micha Müller's avatar Micha Müller
Browse files

Pusher: ensure entities are only initialized once

parent 30a60213
......@@ -55,6 +55,7 @@ public:
EntityInterface(const std::string& name) :
_name(name),
_mqttPart(""),
_initialized(false),
_strand(nullptr) {}
/**
......@@ -67,6 +68,7 @@ public:
EntityInterface(const EntityInterface& other) :
_name(other._name),
_mqttPart(other._mqttPart),
_initialized(false),
_strand(nullptr) {}
/**
......@@ -86,6 +88,7 @@ public:
EntityInterface& operator=(const EntityInterface& other) {
_name = other._name;
_mqttPart = other._mqttPart;
_initialized = false;
_strand = nullptr;
return *this;
......@@ -123,10 +126,12 @@ public:
* @param io IO service to initialize _strand with.
*/
void init(boost::asio::io_service& io) {
if (!_strand) {
_strand.reset(new strand(io));
if(_initialized) {
return;
}
_strand.reset(new strand(io));
this->execOnInit();
_initialized = true;
}
/**
......@@ -163,12 +168,13 @@ protected:
*/
virtual void printEntityConfig(LOG_LEVEL ll) { /* do nothing if not overwritten */ }
std::string _name; /**< Name of the entity */
std::string _mqttPart; /**< Partial MQTT topic identifying this entity */
std::string _name; ///< Name of the entity
std::string _mqttPart; ///< Partial MQTT topic identifying this entity
std::unique_ptr<strand> _strand; /**< Provides serialized handler execution
to avoid race conditions */
LOGGER lg; /**< Logging instance */
bool _initialized; ///< An entity should only be initialized once
std::unique_ptr<strand> _strand; ///< Provides serialized handler execution to avoid race conditions
LOGGER lg; ///< Logging instance
};
#endif /* DCDBPUSHER_INCLUDES_ENTITYINTERFACE_H_ */
......@@ -30,7 +30,6 @@
SNMPConnection::SNMPConnection(const std::string& name) :
EntityInterface(name),
_isInitialized(false),
_snmpCommunity(""),
_oidPrefix(""),
_username(""),
......@@ -51,7 +50,6 @@ SNMPConnection::SNMPConnection(const std::string& name) :
SNMPConnection::SNMPConnection(const SNMPConnection& other) :
EntityInterface(other),
_isInitialized(false),
_snmpCommunity(other._snmpCommunity),
_oidPrefix(other._oidPrefix),
_username(other._username),
......@@ -84,7 +82,6 @@ SNMPConnection::~SNMPConnection() {
SNMPConnection& SNMPConnection::operator=(const SNMPConnection& other) {
EntityInterface::operator=(other);
_isInitialized = false;
_snmpCommunity = other._snmpCommunity;
_oidPrefix = other._oidPrefix;
_username = other._username;
......@@ -104,10 +101,6 @@ SNMPConnection& SNMPConnection::operator=(const SNMPConnection& other) {
}
void SNMPConnection::execOnInit() {
if(_isInitialized) {
return;
}
snmp_sess_init(&_snmpSession);
int liberr, syserr;
char *errstr;
......@@ -175,7 +168,6 @@ void SNMPConnection::execOnInit() {
} else {
LOG(warning) << "SNMP Version " << _version << " not supported!";
}
_isInitialized = true;
}
bool SNMPConnection::open() {
......
......@@ -184,8 +184,6 @@ public:
int64_t get(const oid* const OID, size_t OIDLen);
private:
bool _isInitialized;
std::string _snmpCommunity;
std::string _oidPrefix;
std::string _username;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment