Commit e2426341 authored by Daniele Tafani's avatar Daniele Tafani
Browse files

Made the PDU plugin thread-safe.

parent a674f138
......@@ -35,7 +35,7 @@ PDUSensorGroup& PDUSensorGroup::operator=(const PDUSensorGroup& other) {
void PDUSensorGroup::init(boost::asio::io_service& io) {
SensorGroupTemplate::init(io);
if(_pdu) {
_pdu->initializeStrand(io);
_pdu->init(io);
} else {
LOG(error) << "No PDUUnit set for sensorgroup " << _groupName << "! Cannot initialize sensor.";
}
......@@ -129,6 +129,7 @@ void PDUSensorGroup::read() {
throw std::runtime_error("Value not found!");
}
reading.value = stoll(readStr);
#ifdef DEBUG
LOG(debug) << _groupName << "::" << s->getName() << " raw reading: \"" << reading.value << "\"";
#endif
......
......@@ -9,39 +9,43 @@
#include <iostream>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/bio.h>
PDUUnit::PDUUnit() {
_host = "";
_strand = nullptr;
_ctx = nullptr;
}
PDUUnit::~PDUUnit() {
if (_strand) {
delete _strand;
}
if (_ctx) {
SSL_CTX_free(_ctx);
}
}
void PDUUnit::initializeStrand(boost::asio::io_service& io) {
void PDUUnit::init(boost::asio::io_service& io) {
if (!_strand) {
_strand = new boost::asio::io_service::strand(io);
}
SSL_library_init();
SSL_load_error_strings();
ERR_load_BIO_strings();
OpenSSL_add_all_algorithms();
_ctx = SSL_CTX_new(SSLv23_method());
}
bool PDUUnit::sendRequest(const std::string& request, std::string& response) {
SSL_library_init();
SSL_load_error_strings();
ERR_load_BIO_strings();
OpenSSL_add_all_algorithms();
SSL_CTX* ctx;
BIO* bio;
ctx = SSL_CTX_new(SSLv23_method());
if (!ctx) {
if (!_ctx) {
LOG(error) << "OpenSSL: Could not create context: " << ERR_reason_error_string(ERR_get_error());
return false;
}
......@@ -54,7 +58,7 @@ bool PDUUnit::sendRequest(const std::string& request, std::string& response) {
}
*/
bio = BIO_new_ssl_connect(ctx);
bio = BIO_new_ssl_connect(_ctx);
BIO_set_conn_hostname(bio, _host.c_str());
......@@ -62,7 +66,6 @@ bool PDUUnit::sendRequest(const std::string& request, std::string& response) {
{
LOG(error) << "OpenSSL: Could not connect: " << ERR_reason_error_string(ERR_get_error());
BIO_free_all(bio);
SSL_CTX_free(ctx);
return false;
}
......@@ -78,7 +81,6 @@ bool PDUUnit::sendRequest(const std::string& request, std::string& response) {
if (BIO_write(bio, reqBuf, len) <= 0) {
LOG(error) << "OpenSSL: Could not send request: " << ERR_reason_error_string(ERR_get_error());
BIO_free_all(bio);
SSL_CTX_free(ctx);
return false;
}
......@@ -92,12 +94,10 @@ bool PDUUnit::sendRequest(const std::string& request, std::string& response) {
if (len < 0) {
std::cerr << "OpenSSL: Could not read response: " << ERR_reason_error_string(ERR_get_error()) << std::endl;
BIO_free_all(bio);
SSL_CTX_free(ctx);
return false;
}
BIO_free_all(bio);
SSL_CTX_free(ctx);
return true;
}
......
......@@ -16,7 +16,7 @@
#include <boost/asio.hpp>
#include <boost/property_tree/ptree.hpp>
#include "logging.h"
#include <openssl/ssl.h>
typedef std::vector<std::pair<std::string, std::string>> attributesVector_t;
typedef std::vector<std::tuple<std::string, std::string, attributesVector_t>> xmlPathVector_t;
......@@ -36,7 +36,7 @@ public:
_host = host + ":443";
}
}
void initializeStrand(boost::asio::io_service& io);
void init(boost::asio::io_service& io);
const std::string& getHost() { return _host; }
boost::asio::io_service::strand* getStrand() const { return _strand; }
......@@ -57,7 +57,7 @@ private:
std::string _host;
boost::asio::io_service::strand* _strand;
SSL_CTX* _ctx;
LOGGER lg;
};
......
Supports Markdown
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