Commit 71b60c6b authored by Michael Ott's avatar Michael Ott
Browse files

Check regularly for modified published sensors

parent 9d109dfa
...@@ -250,10 +250,7 @@ int main(int argc, char *argv[]) ...@@ -250,10 +250,7 @@ int main(int argc, char *argv[])
_httpsServer = new RestAPI(restAPISettings, hierarchySettings, _cassandraConnection, io); _httpsServer = new RestAPI(restAPISettings, hierarchySettings, _cassandraConnection, io);
_configuration->readRestAPIUsers(_httpsServer); _configuration->readRestAPIUsers(_httpsServer);
LOG(info) << "Retrieving published sensor names and topics..."; _httpsServer->checkPublishedSensorsAsync();
if(!_httpsServer->buildTree()) {
throw std::runtime_error("Unable to initialize the sensor navigator!");
}
if (globalSettings.daemonize) { if (globalSettings.daemonize) {
//boost.log does not support forking officially. //boost.log does not support forking officially.
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include "RestAPI.h" #include "RestAPI.h"
#include "dcdb/unitconv.h" #include "dcdb/unitconv.h"
#include "timestamp.h"
#include <sstream> #include <sstream>
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
...@@ -44,6 +45,8 @@ RestAPI::RestAPI(serverSettings_t settings, ...@@ -44,6 +45,8 @@ RestAPI::RestAPI(serverSettings_t settings,
RESTHttpsServer(settings, io), RESTHttpsServer(settings, io),
_connection(cassandraConnection), _connection(cassandraConnection),
_updating(false), _updating(false),
_timer(io, boost::posix_time::seconds(0)),
_publishedSensorsWritetime(0),
_hierarchySettings(hierarchySettings) { _hierarchySettings(hierarchySettings) {
//Configuring endpoints //Configuring endpoints
...@@ -68,6 +71,8 @@ bool RestAPI::buildTree() { ...@@ -68,6 +71,8 @@ bool RestAPI::buildTree() {
//Spinlock to ensure that only one sensor navigator update runs at a time //Spinlock to ensure that only one sensor navigator update runs at a time
while(_updating.exchange(true)) {} while(_updating.exchange(true)) {}
LOG(info) << "Retrieving published sensor names and topics...";
//Get the list of all public sensors and topics. //Get the list of all public sensors and topics.
if(!_sensorConfig || _sensorConfig->getPublicSensorsVerbose(publicSensors)!=DCDB::SC_OK) { if(!_sensorConfig || _sensorConfig->getPublicSensorsVerbose(publicSensors)!=DCDB::SC_OK) {
LOG(error) << "Unable to fetch list of public sensors!"; LOG(error) << "Unable to fetch list of public sensors!";
...@@ -79,8 +84,8 @@ bool RestAPI::buildTree() { ...@@ -79,8 +84,8 @@ bool RestAPI::buildTree() {
std::shared_ptr<MetadataStore> newMetadataStore = std::make_shared<MetadataStore>(); std::shared_ptr<MetadataStore> newMetadataStore = std::make_shared<MetadataStore>();
for(auto& s : publicSensors) { for(auto& s : publicSensors) {
topics.push_back(s.pattern); topics.push_back(s.name);
newMetadataStore->store(s.pattern, DCDB::PublicSensor::publicSensorToMetadata(s)); newMetadataStore->store(s.name, DCDB::PublicSensor::publicSensorToMetadata(s));
} }
//Build the tree navigator //Build the tree navigator
...@@ -90,12 +95,26 @@ bool RestAPI::buildTree() { ...@@ -90,12 +95,26 @@ bool RestAPI::buildTree() {
//Replacing the old navigator and metadata store //Replacing the old navigator and metadata store
_navigator = newNavigator; _navigator = newNavigator;
_metadataStore = newMetadataStore; _metadataStore = newMetadataStore;
_sensorConfig->getPublishedSensorsWritetime(_publishedSensorsWritetime);
_updating.store(false); _updating.store(false);
LOG(info) << "Built a sensor navigator of size " << std::to_string(_navigator->getTreeSize()) << " and depth " << std::to_string(_navigator->getTreeDepth()) << "."; LOG(info) << "Built a sensor navigator of size " << std::to_string(_navigator->getTreeSize()) << " and depth " << std::to_string(_navigator->getTreeDepth()) << ".";
return true; return true;
} }
void RestAPI::checkPublishedSensorsAsync() {
uint64_t t;
if (_sensorConfig->getPublishedSensorsWritetime(t) == DCDB::SC_OK) {
if (t > _publishedSensorsWritetime) {
LOG(debug) << "Fetching updated sensors";
buildTree();
}
}
_timer.expires_at(timestamp2ptime(getTimestamp() + S_TO_NS(60)));
_timer.async_wait(std::bind(&RestAPI::checkPublishedSensorsAsync, this));
}
//Dummy GET request to create a datasource. All necessary checks that could be peformed here are //Dummy GET request to create a datasource. All necessary checks that could be peformed here are
//already done by the RESTAPIServer (e.g., user credentials, connectivity to the DB,...). //already done by the RESTAPIServer (e.g., user credentials, connectivity to the DB,...).
void RestAPI::GET_datasource(endpointArgs) { void RestAPI::GET_datasource(endpointArgs) {
......
...@@ -74,9 +74,10 @@ public: ...@@ -74,9 +74,10 @@ public:
//Builds the internal sensor navigator //Builds the internal sensor navigator
bool buildTree(); bool buildTree();
void checkPublishedSensorsAsync();
private:
private:
/******************************************************************************/ /******************************************************************************/
/** /**
* GET "/" * GET "/"
...@@ -162,6 +163,8 @@ private: ...@@ -162,6 +163,8 @@ private:
DCDB::SensorConfig* _sensorConfig; DCDB::SensorConfig* _sensorConfig;
DCDB::SensorDataStore* _sensorDataStore; DCDB::SensorDataStore* _sensorDataStore;
atomic<bool> _updating; atomic<bool> _updating;
boost::asio::deadline_timer _timer;
uint64_t _publishedSensorsWritetime;
std::string _separator; std::string _separator;
hierarchySettings_t _hierarchySettings; hierarchySettings_t _hierarchySettings;
......
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