Currently job artifacts in CI/CD pipelines on LRZ GitLab never expire. Starting from Wed 26.1.2022 the default expiration time will be 30 days (GitLab default). Currently existing artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

Commit 294e1751 authored by Micha Mueller's avatar Micha Mueller
Browse files

WIP Use std::shared_ptr instead of raw ptrs where appropriate

parent 6330a60a
......@@ -273,8 +273,8 @@ bool Configuration::readPlugins() {
}
//check if an MQTT-suffix was assigned twice
for(auto g : dynLib.configurator->getSensorGroups()) {
for(auto s : g->getSensors()) {
for(const auto& g : dynLib.configurator->getSensorGroups()) {
for(const auto& s : g->getSensors()) {
bool ok = checkMqtt(s->getMqtt());
if(!ok) {
LOG(error) << "Problematic MQTT-Topics! Please check your config files";
......
......@@ -147,9 +147,9 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
if (p.id == pathStrs[0]) {
boost::property_tree::ptree root, sensors;
for(auto g : p.configurator->getSensorGroups()) {
for(const auto& g : p.configurator->getSensorGroups()) {
boost::property_tree::ptree group;
for(auto s : g->getSensors()) {
for(const auto& s : g->getSensors()) {
group.put(s->getName(), "");
}
sensors.add_child(g->getGroupName(), group);
......@@ -193,10 +193,10 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
for(auto& p : _httpsServer._plugins) {
if (p.id == pathStrs[0]) {
response = "Sensor not found!";
for(auto g : p.configurator->getSensorGroups()) {
for(auto s : g->getSensors()) {
for(const auto& g : p.configurator->getSensorGroups()) {
for(const auto& s : g->getSensors()) {
if (s->getName() == sensor) {
response = pathStrs[0] + "::" + sensor + _httpsServer.calcAvg(s, g->getCacheSize(), time);
response = pathStrs[0] + "::" + sensor + _httpsServer.calcAvg(*s, g->getCacheSize(), time);
connection->set_status(server::connection::ok);
break;
}
......@@ -231,7 +231,7 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
if (action == "start") {
for(auto& p : _httpsServer._plugins) {
if (p.id == pathStrs[0]) {
for(auto g : p.configurator->getSensorGroups()) {
for(const auto& g : p.configurator->getSensorGroups()) {
g->start();
}
response = "Plugin " + pathStrs[0] + ": Sensors started";
......@@ -242,7 +242,7 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
} else if (action == "stop") {
for(auto& p : _httpsServer._plugins) {
if (p.id == pathStrs[0]) {
for(auto g : p.configurator->getSensorGroups()) {
for(const auto& g : p.configurator->getSensorGroups()) {
g->stop();
}
response = "Plugin " + pathStrs[0] + ": Sensors stopped";
......@@ -269,7 +269,7 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
connection->set_status(server::connection::internal_server_error);
}
for(auto g : p.configurator->getSensorGroups()) {
for(const auto& g : p.configurator->getSensorGroups()) {
g->init(_httpsServer._io);
g->start();
}
......@@ -321,9 +321,9 @@ HttpsServer::~HttpsServer() {
delete _server;
}
std::string HttpsServer::calcAvg(SensorBase* const s, unsigned cacheSize, uint64_t time) {
std::string HttpsServer::calcAvg(SensorBase& s, unsigned cacheSize, uint64_t time) {
uint64_t avg = 0;
const reading_t * const cache = s->getCache();
const reading_t * const cache = s.getCache();
unsigned count = 0;
for(unsigned i = 0; i < cacheSize; i++) {
......
......@@ -94,7 +94,7 @@ private:
*
* @return Response message of the form " Average of last *count* values is *avg*"
*/
std::string calcAvg(SensorBase* const s, unsigned cacheSize, uint64_t time);
std::string calcAvg(SensorBase& s, unsigned cacheSize, uint64_t time);
/*
* Check if the authkey is valid and has the permission requiredPerm associated
......
......@@ -88,11 +88,11 @@ void MQTTPusher::push() {
//for faster response
break;
}
for(auto g : p.configurator->getSensorGroups()) {
for(auto s : g->getSensors()) {
for(const auto& g : p.configurator->getSensorGroups()) {
for(const auto& s : g->getSensors()) {
if (s->getSizeOfReadingQueue() >= g->getMinValues()) {
if(totalCount < _maxNumberOfMessages){
sendReadings(s, reads, totalCount);
sendReadings(*s, reads, totalCount);
} else {
break; //ultimately we will go to sleep 1 second
}
......@@ -106,7 +106,7 @@ void MQTTPusher::push() {
mosquitto_loop_stop(_mosq, false);
}
void MQTTPusher::sendReadings(SensorBase* s, reading_t* reads, std::size_t& totalCount) {
void MQTTPusher::sendReadings(SensorBase& s, reading_t* reads, std::size_t& totalCount) {
//there was a (unintended) disconnect in the meantime --> reconnect
if (!_connected) {
LOGM(error) << "Lost connection. Reconnecting...";
......@@ -122,7 +122,7 @@ void MQTTPusher::sendReadings(SensorBase* s, reading_t* reads, std::size_t& tota
if (_connected) {
//get all sensor values out of its queue
std::size_t count = s->popReadingQueue(reads, SensorBase::QUEUE_MAXLIMIT);
std::size_t count = s.popReadingQueue(reads, SensorBase::QUEUE_MAXLIMIT);
//totalCount+= count;
totalCount+= 1;
#ifdef DEBUG
......@@ -135,12 +135,12 @@ void MQTTPusher::sendReadings(SensorBase* s, reading_t* reads, std::size_t& tota
}
#endif
//try to send them to the broker
if (mosquitto_publish(_mosq, NULL, (s->getMqtt()).c_str(), sizeof(reading_t)*count, reads, 1, false) != MOSQ_ERR_SUCCESS) {
if (mosquitto_publish(_mosq, NULL, (s.getMqtt()).c_str(), sizeof(reading_t)*count, reads, 1, false) != MOSQ_ERR_SUCCESS) {
//could not send them --> push the sensor values back into the queue
LOGM(error) << "Could not send message! Trying again later";
_connected = false;
s->pushReadingQueue(reads, count);
s.pushReadingQueue(reads, count);
//totalCount -= count;
totalCount -= 1;
sleep(5);
......
......@@ -43,7 +43,7 @@ public:
}
private:
void sendReadings(SensorBase* s, reading_t* reads, std::size_t& totalCount);
void sendReadings(SensorBase& s, reading_t* reads, std::size_t& totalCount);
int _brokerPort;
std::string _brokerHost;
......
......@@ -59,7 +59,7 @@ void sigintHandler(int sig) {
LOG(info) << "Stopping sensors...";
for(auto& p : _configuration->getPlugins()) {
LOG(info) << "Stop \"" << p.id << "\" plugin";
for(auto g : p.configurator->getSensorGroups()) {
for(const auto& g : p.configurator->getSensorGroups()) {
g->stop();
}
}
......@@ -268,7 +268,7 @@ int main(int argc, char** argv) {
LOG(info) << "Init sensors...";
for(auto& p : _configuration->getPlugins()) {
LOG(info) << "Init \"" << p.id << "\" plugin";
for(auto g : p.configurator->getSensorGroups()) {
for(const auto& g : p.configurator->getSensorGroups()) {
LOG(debug) << " -Group: " << g->getGroupName();
g->init(io);
}
......@@ -278,7 +278,7 @@ int main(int argc, char** argv) {
LOG(info) << "Starting sensors...";
for(auto& p : _configuration->getPlugins()) {
LOG(info) << "Start \"" << p.id << "\" plugin";
for(auto g : p.configurator->getSensorGroups()) {
for(const auto& g : p.configurator->getSensorGroups()) {
g->start();
}
}
......
......@@ -30,7 +30,7 @@ public:
virtual bool readConfig(std::string cfgPath) = 0;
virtual bool reReadConfig() = 0;
virtual void setGlobalSettings(const pluginSettings_t& pluginSettings) = 0;
virtual std::vector<SensorGroupInterface*>& getSensorGroups() = 0;
virtual std::vector<SGroupPtr>& getSensorGroups() = 0;
protected:
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
......
......@@ -39,6 +39,9 @@ protected:
typedef std::map<std::string, SGroup*> sGroupMap_t;
typedef std::map<std::string, SEntity*> sEntityMap_t;
using SB_Ptr = std::shared_ptr<SBase>;
using SG_Ptr = std::shared_ptr<SGroup>;
public:
ConfiguratorTemplate() :
_entityName("INVALID"),
......@@ -51,9 +54,6 @@ public:
ConfiguratorTemplate(const ConfiguratorTemplate&) = delete;
virtual ~ConfiguratorTemplate() {
for (auto g : _sensorGroups) {
delete g;
}
for (auto e : _sensorEntitys) {
delete e;
}
......@@ -96,7 +96,6 @@ public:
//read groups and templates for groups. If present also entity/-template stuff
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, cfg) {
//TODO allow single sensors for convenience?
//template entity
if (boost::iequals(val.first, "template_" + _entityName)) {
LOG(debug) << "Template " << _entityName << " \"" << val.second.data() << "\"";
......@@ -153,7 +152,7 @@ public:
SGroup* group = new SGroup(val.second.data());
if (readSensorGroup(*group, val.second)) {
//group which consists of only one sensor
SBase* sensor = new SBase(val.second.data());
SB_Ptr sensor = std::make_shared<SBase>(val.second.data());
if (readSensorBase(*sensor, val.second)) {
group->pushBackSensor(sensor);
auto ret = _templateSensorGroups.insert(std::pair<std::string, SGroup*>(val.second.data(), group));
......@@ -186,45 +185,41 @@ public:
} else if (boost::iequals(val.first, _groupName)) {
LOG(debug) << _groupName << " \"" << val.second.data() << "\"";
if (!val.second.empty()) {
SGroup* group = new SGroup(val.second.data());
SG_Ptr group = std::make_shared<SGroup>(val.second.data());
if (readSensorGroup(*group, val.second)) {
storeSensorGroup(group);
} else {
LOG(warning) << _groupName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
delete group;
}
}
//single sensor
} else if (boost::iequals(val.first, "single_" + _baseName)) {
LOG(debug) << "Single " << _baseName << " \"" << val.second.data() << "\"";
if (!val.second.empty()) {
SGroup* group = new SGroup(val.second.data());
SG_Ptr group = std::make_shared<SGroup>(val.second.data());
if (readSensorGroup(*group, val.second)) {
//group which consists of only one sensor
SBase* sensor;
SB_Ptr sensor;
//perhaps one sensor is already present because it was copied from the template group
if (group->getSensors().size() != 0) {
sensor = dynamic_cast<SBase*>(group->getSensors()[0]);
sensor = static_pointer_cast<SB_Ptr::element_type>(group->getSensors()[0]);
sensor->setName(val.second.data());
if (readSensorBase(*sensor, val.second)) {
storeSensorGroup(group);
} else {
LOG(warning) << "Single " << _baseName << " " << val.second.data() << " could not be read! Omitting";
delete group;
}
} else {
sensor = new SBase(val.second.data());
sensor = std::make_shared<SBase>(val.second.data());
if (readSensorBase(*sensor, val.second)) {
group->pushBackSensor(sensor);
storeSensorGroup(group);
} else {
LOG(warning) << "Single " << _baseName << " " << val.second.data() << " could not be read! Omitting";
delete group;
}
}
} else {
LOG(warning) << "Single " << _baseName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
delete group;
}
}
}
......@@ -256,9 +251,6 @@ public:
}
//clean up sensors/groups/entitys and templates
for(auto g : _sensorGroups) {
delete g;
}
for(auto e : _sensorEntitys) {
delete e;
}
......@@ -301,12 +293,12 @@ public:
*
* @return Vector containing pointers to all sensor groups of this plugin
*/
std::vector<SensorGroupInterface*>& getSensorGroups() final {
std::vector<SGroupPtr>& getSensorGroups() final {
return _sensorGroupInterfaces;
}
protected:
void storeSensorGroup(SGroup* sGroup) {
void storeSensorGroup(SG_Ptr sGroup) {
_sensorGroups.push_back(sGroup);
_sensorGroupInterfaces.push_back(sGroup);
}
......@@ -389,12 +381,11 @@ protected:
}
} else if (boost::iequals(val.first, _baseName)) {
LOG(debug) << " " << _baseName << " " << val.second.data();
SBase* sensor = new SBase(val.second.data());
SB_Ptr sensor = std::make_shared<SBase>(val.second.data());
if (readSensorBase(*sensor, val.second)) {
sGroup.pushBackSensor(sensor);
} else {
LOG(warning) << _baseName << " " << sGroup.getGroupName() << "::" << sensor->getName() << " could not be read! Omitting";
delete sensor;
}
}
}
......@@ -430,7 +421,7 @@ protected:
sEntity = *(it->second);
for(auto g : _templateSensorGroups) {
if (isEntityOfGroup(*(it->second), *(g.second))) {
SGroup* group = new SGroup(*(g.second));
SG_Ptr group = std::make_shared<SGroup>(*(g.second));
setEntityForGroup(sEntity, *group);
storeSensorGroup(group);
}
......@@ -446,32 +437,38 @@ protected:
if (boost::iequals(val.first, _groupName)) {
LOG(debug) << " " << _groupName << " " << val.second.data();
if (!val.second.empty()) {
SGroup* group = new SGroup(val.second.data());
if(readSensorGroup(*group, val.second)) {
setEntityForGroup(sEntity, *group);
if (isTemplate) {
if (isTemplate) {
SGroup* group = new SGroup(val.second.data());
if(readSensorGroup(*group, val.second)) {
setEntityForGroup(sEntity, *group);
auto ret = _templateSensorGroups.insert(std::pair<std::string, SGroup*>(val.second.data(), group));
if(!ret.second) {
LOG(warning) << "Template " << _groupName << " " << val.second.data() << " already exists! Omitting...";
delete group;
}
} else {
storeSensorGroup(group);
LOG(warning) << _groupName << " " << group->getGroupName() << " could not be read! Omitting";
delete group;
}
} else {
LOG(warning) << _groupName << " " << group->getGroupName() << " could not be read! Omitting";
delete group;
SG_Ptr group = std::make_shared<SGroup>(val.second.data());
if(readSensorGroup(*group, val.second)) {
setEntityForGroup(sEntity, *group);
storeSensorGroup(group);
} else {
LOG(warning) << _groupName << " " << group->getGroupName() << " could not be read! Omitting";
}
}
}
} else if (boost::iequals(val.first, "single_" + _baseName)) {
LOG(debug) << "Single " << _baseName << " \"" << val.second.data() << "\"";
if (!val.second.empty()) {
SGroup* group = new SGroup(val.second.data());
//group which consists of only one sensor
if (readSensorGroup(*group, val.second)) {
setEntityForGroup(sEntity, *group);
if (isTemplate) {
SBase* sensor = new SBase(val.second.data());
if (isTemplate) {
SGroup* group = new SGroup(val.second.data());
//group which consists of only one sensor
if (readSensorGroup(*group, val.second)) {
setEntityForGroup(sEntity, *group);
SB_Ptr sensor = std::make_shared<SBase>(val.second.data());
if (readSensorBase(*sensor, val.second)) {
group->pushBackSensor(sensor);
auto ret = _templateSensorGroups.insert(std::pair<std::string, SGroup*>(val.second.data(), group));
......@@ -484,10 +481,18 @@ protected:
delete group;
}
} else {
SBase* sensor;
LOG(warning) << "Single " << _baseName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
delete group;
}
} else {
SG_Ptr group = std::make_shared<SGroup>(val.second.data());
//group which consists of only one sensor
if (readSensorGroup(*group, val.second)) {
setEntityForGroup(sEntity, *group);
SB_Ptr sensor;
//perhaps one sensor is already present because it was copied from the template group
if (group->getSensors().size() != 0) {
sensor = dynamic_cast<SBase*>(group->getSensors()[0]);
sensor = static_pointer_cast<SBase>(group->getSensors()[0]);
sensor->setName(val.second.data());
if (readSensorBase(*sensor, val.second)) {
storeSensorGroup(group);
......@@ -496,7 +501,7 @@ protected:
delete group;
}
} else {
sensor = new SBase(val.second.data());
sensor = std::make_shared<SBase>(val.second.data());
if (readSensorBase(*sensor, val.second)) {
group->pushBackSensor(sensor);
storeSensorGroup(group);
......@@ -505,10 +510,10 @@ protected:
delete group;
}
}
} else {
LOG(warning) << "Single " << _baseName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
delete group;
}
} else {
LOG(warning) << "Single " << _baseName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
delete group;
}
}
}
......@@ -637,8 +642,8 @@ protected:
std::string _cfgPath;
std::string _mqttPrefix;
unsigned int _cacheInterval;
std::vector<SensorGroupInterface*> _sensorGroupInterfaces;
std::vector<SGroup*> _sensorGroups;
std::vector<SGroupPtr> _sensorGroupInterfaces;
std::vector<SG_Ptr> _sensorGroups;
std::vector<SEntity*> _sensorEntitys;
sBaseMap_t _templateSensorBases;
sGroupMap_t _templateSensorGroups;
......
......@@ -86,8 +86,6 @@ public:
_latestValue.timestamp = reading.timestamp;
}
protected:
std::string _name;
......@@ -98,4 +96,7 @@ protected:
std::unique_ptr<boost::lockfree::spsc_queue<reading_t>> _readingQueue;
};
//for better readability
using SBasePtr = std::shared_ptr<SensorBase>;
#endif /* SRC_SENSORBASE_H_ */
......@@ -17,6 +17,7 @@
class SensorGroupInterface {
public:
SensorGroupInterface(const std::string& groupName) :
_groupName(groupName),
_mqttPart(""),
......@@ -92,8 +93,8 @@ public:
virtual void start() = 0;
virtual void stop() = 0;
virtual void pushBackSensor(SensorBase* s) = 0;
virtual std::vector<SensorBase*>& getSensors() = 0;
virtual void pushBackSensor(SBasePtr s) = 0;
virtual std::vector<SBasePtr>& getSensors() = 0;
protected:
virtual void read() = 0;
......@@ -112,4 +113,7 @@ protected:
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
};
//for better readability
using SGroupPtr = std::shared_ptr<SensorGroupInterface>;
#endif /* SENSORGROUPINTERFACE_H_ */
......@@ -13,12 +13,16 @@
#include "timestamp.h"
#include <vector>
#include <memory>
template <typename S>
class SensorGroupTemplate : public SensorGroupInterface {
//the template shall only be instantiated for classes which derive from SensorBase
static_assert(std::is_base_of<SensorBase, S>::value, "S must derive from SensorBase!");
protected:
using S_Ptr = std::shared_ptr<S>;
public:
SensorGroupTemplate(const std::string groupName) :
SensorGroupInterface(groupName) {}
......@@ -26,28 +30,24 @@ public:
SensorGroupTemplate(const SensorGroupTemplate& other) :
SensorGroupInterface(other) {
for(auto s : other._sensors) {
S* sensor = new S(*s);
S_Ptr sensor = std::make_shared<S>(*s);
_sensors.push_back(sensor);
_baseSensors.push_back(sensor);
}
}
virtual ~SensorGroupTemplate() {
for(auto s : _sensors) {
delete s;
}
_sensors.clear();
_baseSensors.clear();
}
SensorGroupTemplate& operator=(const SensorGroupTemplate& other) {
SensorGroupInterface::operator=(other);
for(auto s : _sensors) {
delete s;
}
_sensors.clear();
_baseSensors.clear();
for(auto s : other._sensors) {
S* sensor = new S(*s);
S_Ptr sensor = std::make_shared<S>(*s);
_sensors.push_back(sensor);
_baseSensors.push_back(sensor);
}
......@@ -55,8 +55,10 @@ public:
return *this;
}
virtual void pushBackSensor(SensorBase* s) override {
if (S* dSensor = dynamic_cast<S*>(s)) {
virtual void pushBackSensor(SBasePtr s) override {
//Undefined behavior will arise if a ptr type != S_Ptr is given to static_pointer_cast
//Use dynamic_pointer_cast instead if this is a problem
if (S_Ptr dSensor = static_pointer_cast<S_Ptr::element_type>(s)) {
_sensors.push_back(dSensor);
_baseSensors.push_back(s);
} else {
......@@ -64,7 +66,7 @@ public:
}
}
virtual std::vector<SensorBase*>& getSensors() override { return _baseSensors; }
virtual std::vector<SBasePtr>& getSensors() override { return _baseSensors; }
virtual void init(boost::asio::io_service& io) {
SensorGroupInterface::init(io);
......@@ -94,8 +96,8 @@ protected:
}
}
std::vector<S*> _sensors;
std::vector<SensorBase*> _baseSensors;
std::vector<S_Ptr> _sensors;
std::vector<SBasePtr> _baseSensors;
};
#endif /* SENSORGROUPTEMPLATE_H_ */
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