summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-09-25 18:29:48 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-12-14 11:00:25 +0100
commitb7dd0a6e19d5387bc8d499bd1b3a99d457fa58d9 (patch)
tree0d1a26964435e8f91b96713e2b5beb3c1e94d0b2
parentdb45a68715c07e6b8fe35e322ddcbbc6c473cb2b (diff)
Enhance pointer manage and container browsing
- SourceAPI now holds share_ptr of Signal - Signal are stored in a map<string,signal> that change way to browse the signals - subscribed property included in SourceAPI. - Clean old code - renaming struct SignalValue->signalValue Change-Id: Ic5c28296ddd7197c6562e12fbc91c504c1cc0b4d Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r--conf.d/project/lua.d/init-daemon-04-oncall.lua4
-rw-r--r--plugins/low-can.cpp11
-rw-r--r--plugins/lua2c-interface.c2
-rw-r--r--signal-composer-binding/clientApp.cpp6
-rw-r--r--signal-composer-binding/clientApp.hpp4
-rw-r--r--signal-composer-binding/signal-composer-binding.cpp12
-rw-r--r--signal-composer-binding/signal-composer.cpp42
-rw-r--r--signal-composer-binding/signal-composer.hpp16
-rw-r--r--signal-composer-binding/signal.cpp38
-rw-r--r--signal-composer-binding/signal.hpp20
-rw-r--r--signal-composer-binding/source.cpp48
-rw-r--r--signal-composer-binding/source.hpp10
12 files changed, 111 insertions, 102 deletions
diff --git a/conf.d/project/lua.d/init-daemon-04-oncall.lua b/conf.d/project/lua.d/init-daemon-04-oncall.lua
index d473121..8912f26 100644
--- a/conf.d/project/lua.d/init-daemon-04-oncall.lua
+++ b/conf.d/project/lua.d/init-daemon-04-oncall.lua
@@ -54,7 +54,7 @@ function _Unit_Converter(source, args, event)
print("Value in", targetunits, "is", value/base)
_result["result"] = value/base
- ssetSignalValue(source, _result)
+ _setsignalValue(source, _result)
elseif sourcei>targeti then
@@ -66,7 +66,7 @@ function _Unit_Converter(source, args, event)
print("Value in ", targetunits, "is", value*base)
_result["result"] = value/base
- ssetSignalValue(source, _result)
+ _setsignalValue(source, _result)
else
print("No conversion")
diff --git a/plugins/low-can.cpp b/plugins/low-can.cpp
index 62f10f3..c4f79d2 100644
--- a/plugins/low-can.cpp
+++ b/plugins/low-can.cpp
@@ -112,7 +112,6 @@ CTLP_CAPI (subscribeToLow, source, argsJ, eventJ, context) {
AFB_ERROR("Error building subscription query object");
return err;
}
-
json_object_array_add(pluginCtx->subscriptionBatch, subscribeArgsJ);
}
else
@@ -142,29 +141,29 @@ CTLP_CAPI (isOpen, source, argsJ, eventJ, context) {
return -1;
}
- struct SignalValue value = {
+ struct signalValue value = {
.hasBool = true, .boolVal = eventStatus,
.hasNum = false, .numVal = 0,
.hasStr = false, .strVal = std::string()
};
if(strcasestr(eventName, "front_left"))
{
- pluginCtx->pluginHandle->setSignalValue(eventName,(long long int)timestamp, value);
+ pluginCtx->pluginHandle->setsignalValue(eventName,(long long int)timestamp, value);
setDoor(&pluginCtx->allDoorsCtx.front_left, eventName, eventStatus);
}
else if(strcasestr(eventName, "front_right"))
{
- pluginCtx->pluginHandle->setSignalValue(eventName,(long long int)timestamp, value);
+ pluginCtx->pluginHandle->setsignalValue(eventName,(long long int)timestamp, value);
setDoor(&pluginCtx->allDoorsCtx.front_right, eventName, eventStatus);
}
else if(strcasestr(eventName, "rear_left"))
{
- pluginCtx->pluginHandle->setSignalValue(eventName,(long long int)timestamp, value);
+ pluginCtx->pluginHandle->setsignalValue(eventName,(long long int)timestamp, value);
setDoor(&pluginCtx->allDoorsCtx.rear_left, eventName, eventStatus);
}
else if(strcasestr(eventName, "rear_right"))
{
- pluginCtx->pluginHandle->setSignalValue(eventName,(long long int)timestamp, value);
+ pluginCtx->pluginHandle->setsignalValue(eventName,(long long int)timestamp, value);
setDoor(&pluginCtx->allDoorsCtx.rear_right, eventName, eventStatus);
}
else
diff --git a/plugins/lua2c-interface.c b/plugins/lua2c-interface.c
index dd69b11..0bff8d6 100644
--- a/plugins/lua2c-interface.c
+++ b/plugins/lua2c-interface.c
@@ -48,7 +48,7 @@ CTLP_ONLOAD(plugin, handle) {
return (void*)pluginCtx;
}
-CTLP_LUA2C (ssetSignalValue, label, argsJ)
+CTLP_LUA2C (_setsignalValue, label, argsJ)
{
AFB_NOTICE("label: %s, argsJ: %s", label, json_object_to_json_string(argsJ));
return 0;
diff --git a/signal-composer-binding/clientApp.cpp b/signal-composer-binding/clientApp.cpp
index ce30163..c325fcb 100644
--- a/signal-composer-binding/clientApp.cpp
+++ b/signal-composer-binding/clientApp.cpp
@@ -29,11 +29,11 @@ void clientAppCtx::update(Signal* sig)
return;
}
-void clientAppCtx::appendSignals(std::vector<Signal*>& sigV)
+void clientAppCtx::appendSignals(std::vector<std::shared_ptr<Signal>>& sigV)
{
bool set = false;
// Clean up already subscribed signals to avoid duplicata
- for (std::vector<Signal*>::const_iterator it = sigV.begin();
+ for (std::vector<std::shared_ptr<Signal>>::const_iterator it = sigV.begin();
it != sigV.end(); ++it)
{
for (auto& ctxSig: subscribedSignals_)
@@ -44,7 +44,7 @@ void clientAppCtx::appendSignals(std::vector<Signal*>& sigV)
sigV.erase(it);
continue;
}
- Signal* sig = *it;
+ std::shared_ptr<Signal> sig = *it;
sig->addObserver(this);
}
diff --git a/signal-composer-binding/clientApp.hpp b/signal-composer-binding/clientApp.hpp
index 7e1bd9d..f5c913b 100644
--- a/signal-composer-binding/clientApp.hpp
+++ b/signal-composer-binding/clientApp.hpp
@@ -22,13 +22,13 @@ class clientAppCtx: public Observer<Signal>
{
private:
std::string uuid_;
- std::vector<Signal*> subscribedSignals_;
+ std::vector<std::shared_ptr<Signal>> subscribedSignals_;
struct afb_event event_;
public:
explicit clientAppCtx(const char* uuid);
void update(Signal* sig);
- void appendSignals(std::vector<Signal*>& sigV);
+ void appendSignals(std::vector<std::shared_ptr<Signal>>& sigV);
int makeSubscription(struct afb_req request);
int makeUnsubscription(struct afb_req request);
};
diff --git a/signal-composer-binding/signal-composer-binding.cpp b/signal-composer-binding/signal-composer-binding.cpp
index 1e14aad..3c16ad3 100644
--- a/signal-composer-binding/signal-composer-binding.cpp
+++ b/signal-composer-binding/signal-composer-binding.cpp
@@ -32,10 +32,10 @@
/// @param[in] object - eventual data that comes with the event
void onEvent(const char *event, json_object *object)
{
- AFB_DEBUG("Received event json: %s", json_object_to_json_string(object));
+ AFB_NOTICE("Received event json: %s", json_object_to_json_string(object));
Composer& composer = Composer::instance();
- std::vector<Signal*> signals = composer.searchSignals(event);
+ std::vector<std::shared_ptr<Signal>> signals = composer.searchSignals(event);
if(!signals.empty())
{
for(auto& sig: signals)
@@ -52,7 +52,7 @@ static int one_subscribe_unsubscribe(struct afb_req request,
clientAppCtx* cContext)
{
int err = 0;
- std::vector<Signal*> signals = Composer::instance().searchSignals(event);
+ std::vector<std::shared_ptr<Signal>> signals = Composer::instance().searchSignals(event);
cContext->appendSignals(signals);
if(subscribe)
@@ -154,7 +154,7 @@ void list(afb_req request)
{
struct json_object *allSignalsJ = json_object_new_array();
- std::vector<Signal*> allSignals = Composer::instance().getAllSignals();
+ std::vector<std::shared_ptr<Signal>> allSignals = Composer::instance().getAllSignals();
for(auto& sig: allSignals)
{json_object_array_add(allSignalsJ, sig->toJSON());}
@@ -212,9 +212,9 @@ int execConf()
Composer& composer = Composer::instance();
int err = 0;
CtlConfigExec(composer.ctlConfig());
- std::vector<Signal*> allSignals = composer.getAllSignals();
+ std::vector<std::shared_ptr<Signal>> allSignals = composer.getAllSignals();
ssize_t sigCount = allSignals.size();
- for( Signal*& sig: allSignals)
+ for( std::shared_ptr<Signal>& sig: allSignals)
{
sig->attachToSourceSignals(composer);
}
diff --git a/signal-composer-binding/signal-composer.cpp b/signal-composer-binding/signal-composer.cpp
index b0c7d6a..71cac7b 100644
--- a/signal-composer-binding/signal-composer.cpp
+++ b/signal-composer-binding/signal-composer.cpp
@@ -21,9 +21,9 @@
#include "clientApp.hpp"
-extern "C" void setSignalValueHandle(const char* aName, long long int timestamp, struct SignalValue value)
+extern "C" void setsignalValueHandle(const char* aName, long long int timestamp, struct signalValue value)
{
- std::vector<Signal*> signals = Composer::instance().searchSignals(aName);
+ std::vector<std::shared_ptr<Signal>> signals = Composer::instance().searchSignals(aName);
if(!signals.empty())
{
for(auto& sig: signals)
@@ -42,7 +42,7 @@ bool startsWith(const std::string& str, const std::string& pattern)
}
static struct pluginCBT pluginHandle = {
- .setSignalValue = setSignalValueHandle,
+ .setsignalValue = setsignalValueHandle,
};
CtlSectionT Composer::ctlSections_[] = {
@@ -328,7 +328,7 @@ int Composer::loadSignals(CtlSectionT* section, json_object *signalsJ)
return err;
}
-void Composer::processOptions(const char** opts, Signal* sig, json_object* response) const
+void Composer::processOptions(const char** opts, std::shared_ptr<Signal> sig, json_object* response) const
{
for(int idx=0; idx < sizeof(opts); idx++)
{
@@ -461,18 +461,6 @@ int Composer::initSourcesAPI()
return err;
}
-std::vector<Signal*> Composer::getAllSignals()
-{
- std::vector<Signal*> allSignals;
- for( auto& source : sourcesListV_)
- {
- std::vector<Signal*> srcSignals = source.getSignals();
- allSignals.insert(allSignals.end(), srcSignals.begin(), srcSignals.end());
- }
-
- return allSignals;
-}
-
SourceAPI* Composer::getSourceAPI(const std::string& api)
{
for(auto& source: sourcesListV_)
@@ -483,10 +471,22 @@ SourceAPI* Composer::getSourceAPI(const std::string& api)
return nullptr;
}
-std::vector<Signal*> Composer::searchSignals(const std::string& aName)
+std::vector<std::shared_ptr<Signal>> Composer::getAllSignals()
+{
+ std::vector<std::shared_ptr<Signal>> allSignals;
+ for( auto& source : sourcesListV_)
+ {
+ std::vector<std::shared_ptr<Signal>> srcSignals = source.getSignals();
+ allSignals.insert(allSignals.end(), srcSignals.begin(), srcSignals.end());
+ }
+
+ return allSignals;
+}
+
+std::vector<std::shared_ptr<Signal>> Composer::searchSignals(const std::string& aName)
{
std::string api;
- std::vector<Signal*> signals;
+ std::vector<std::shared_ptr<Signal>> signals;
size_t sep = aName.find_first_of("/");
if(sep != std::string::npos)
{
@@ -496,8 +496,8 @@ std::vector<Signal*> Composer::searchSignals(const std::string& aName)
}
else
{
- std::vector<Signal*> allSignals = getAllSignals();
- for (Signal*& sig : allSignals)
+ std::vector<std::shared_ptr<Signal>> allSignals = getAllSignals();
+ for (std::shared_ptr<Signal>& sig : allSignals)
{
if(*sig == aName)
{signals.emplace_back(sig);}
@@ -517,7 +517,7 @@ json_object* Composer::getsignalValue(const std::string& sig, json_object* optio
&opts[2],
&opts[3]);
- std::vector<Signal*> sigP = searchSignals(sig);
+ std::vector<std::shared_ptr<Signal>> sigP = searchSignals(sig);
if(!sigP.empty())
{
for(auto& sig: sigP)
diff --git a/signal-composer-binding/signal-composer.hpp b/signal-composer-binding/signal-composer.hpp
index 2fd471c..a5ee0f1 100644
--- a/signal-composer-binding/signal-composer.hpp
+++ b/signal-composer-binding/signal-composer.hpp
@@ -42,7 +42,9 @@ private:
int loadOneSignal(json_object* signalsJ);
static int loadSignals(CtlSectionT* section, json_object *signalsJ);
- void processOptions(const char** opts, Signal* sig, json_object* response) const;
+ int initSourcesAPI();
+ SourceAPI* getSourceAPI(const std::string& api);
+ void processOptions(const char** opts, std::shared_ptr<Signal> sig, json_object* response) const;
public:
static Composer& instance();
static void* createContext(void* ctx);
@@ -52,18 +54,16 @@ public:
int loadSignals(json_object* signalsJ);
CtlConfigT* ctlConfig();
- int initSourcesAPI();
- std::vector<Signal*> getAllSignals();
- SourceAPI* getSourceAPI(const std::string& api);
- std::vector<Signal*> searchSignals(const std::string& aName);
- json_object* getSignalValue(const std::string& sig, json_object* options);
+ std::vector<std::shared_ptr<Signal>> getAllSignals();
+ std::vector<std::shared_ptr<Signal>> searchSignals(const std::string& aName);
+ json_object* getsignalValue(const std::string& sig, json_object* options);
int execSignalsSubscription();
};
struct pluginCBT
{
- void (*setSignalValue)(const char* aName, long long int timestamp, struct SignalValue value);
+ void (*setsignalValue)(const char* aName, long long int timestamp, struct signalValue value);
};
-extern "C" void setSignalValueHandle(const char* aName, long long int timestamp, struct SignalValue value);
+extern "C" void setsignalValueHandle(const char* aName, long long int timestamp, struct signalValue value);
diff --git a/signal-composer-binding/signal.cpp b/signal-composer-binding/signal.cpp
index faae4a3..51e16f9 100644
--- a/signal-composer-binding/signal.cpp
+++ b/signal-composer-binding/signal.cpp
@@ -22,6 +22,19 @@
#define MICRO 1000000
+Signal::Signal()
+:id_(""),
+ event_(""),
+ dependsSigV_(),
+ timestamp_(0.0),
+ value_({0,0,0,0,0,""}),
+ frequency_(0),
+ unit_(""),
+ onReceived_(nullptr),
+ getSignalsArgs_(nullptr),
+ subscribed_(false)
+{}
+
Signal::Signal(const std::string& id, const std::string& event, std::vector<std::string>& depends, const std::string& unit, double frequency, CtlActionT* onReceived, json_object* getSignalsArgs)
:id_(id),
event_(event),
@@ -31,7 +44,8 @@ Signal::Signal(const std::string& id, const std::string& event, std::vector<std:
frequency_(frequency),
unit_(unit),
onReceived_(onReceived),
- getSignalsArgs_(getSignalsArgs)
+ getSignalsArgs_(getSignalsArgs),
+ subscribed_(false)
{}
Signal::Signal(const std::string& id,
@@ -47,7 +61,8 @@ Signal::Signal(const std::string& id,
frequency_(frequency),
unit_(unit),
onReceived_(onReceived),
- getSignalsArgs_()
+ getSignalsArgs_(),
+ subscribed_(false)
{}
Signal::operator bool() const
@@ -119,7 +134,7 @@ json_object* Signal::toJSON() const
///
/// @param[in] timestamp - timestamp of occured signal
/// @param[in] value - value of change
-void Signal::set(long long int timestamp, struct SignalValue& value)
+void Signal::set(long long int timestamp, struct signalValue& value)
{
timestamp_ = timestamp;
value_ = value;
@@ -147,21 +162,6 @@ int Signal::onReceivedCB(json_object *queryJ)
return err;
}
-/// @brief Make a Signal observer observes a Signal observable if not already
-/// present in the Observers vector.
-///
-/// @param[in] obs - pointer to a Signal observable
-/*void Signal::attach(Signal* obs)
-{
- for ( auto& sig : Observers_)
- {
- if (obs == sig)
- {return;}
- }
-
- Observers_.push_back(obs);
-}*/
-
/// @brief Make a Signal observer observes Signals observables
/// set in its observable vector.
///
@@ -172,7 +172,7 @@ void Signal::attachToSourceSignals(Composer& composer)
{
if(srcSig.find("/") == std::string::npos)
{
- std::vector<Signal*> observables = composer.searchSignals(srcSig);
+ std::vector<std::shared_ptr<Signal>> observables = composer.searchSignals(srcSig);
if(observables[0])
{
AFB_NOTICE("Attaching %s to %s", id_.c_str(), srcSig.c_str());
diff --git a/signal-composer-binding/signal.hpp b/signal-composer-binding/signal.hpp
index 46caa23..7bc6b4a 100644
--- a/signal-composer-binding/signal.hpp
+++ b/signal-composer-binding/signal.hpp
@@ -31,12 +31,12 @@ class Composer;
/// possibility.
/// Not very efficient or optimized, maybe use of Variant in
/// C++17 but this is a bit too new to uses it for now
-struct SignalValue {
- bool hasBool = false;
+struct signalValue {
+ bool hasBool;
bool boolVal;
- bool hasNum = false;
+ bool hasNum;
double numVal;
- bool hasStr = false;
+ bool hasStr;
std::string strVal;
};
@@ -53,15 +53,16 @@ private:
std::string event_;
std::vector<std::string> dependsSigV_;
long long int timestamp_;
- struct SignalValue value_;
- std::map<long long int, struct SignalValue> history_; ///< history_ - Hold signal value history in map with <timestamp, value>
+ struct signalValue value_;
+ std::map<long long int, struct signalValue> history_; ///< history_ - Hold signal value history in map with <timestamp, value>
double frequency_;
std::string unit_;
CtlActionT* onReceived_;
json_object* getSignalsArgs_;
- //int recursionCheck(const std::string& origId) const;
public:
+ bool subscribed_; ///< subscribed_ - boolean value telling if yes or no the signal has been subcribed to the low level binding.
+ Signal();
Signal(const std::string& id, const std::string& event, std::vector<std::string>& depends, const std::string& unit, double frequency, CtlActionT* onReceived, json_object* getSignalsArgs);
Signal(const std::string& id, std::vector<std::string>& depends, const std::string& unit, double frequency, CtlActionT* onReceived);
@@ -72,7 +73,7 @@ public:
const std::string id() const;
json_object* toJSON() const;
- void set(long long int timestamp, struct SignalValue& value);
+ void set(long long int timestamp, struct signalValue& value);
void update(Signal* sig);
int onReceivedCB(json_object *queryJ);
void attachToSourceSignals(Composer& composer);
@@ -80,8 +81,7 @@ public:
double average(int seconds = 0) const;
double minimum(int seconds = 0) const;
double maximum(int seconds = 0) const;
- struct SignalValue last() const;
- //int recursionCheck() const;
+ struct signalValue last() const;
int initialRecursionCheck();
int recursionCheck(Signal* obs);
diff --git a/signal-composer-binding/source.cpp b/signal-composer-binding/source.cpp
index f7dee11..63709a1 100644
--- a/signal-composer-binding/source.cpp
+++ b/signal-composer-binding/source.cpp
@@ -30,9 +30,7 @@ int SourceAPI::init()
if(init_)
{return ActionExecOne(init_, nullptr);}
else if(api_ == afbBindingV2.api)
- {
- api_ = Composer::instance().ctlConfig()->api;
- }
+ {api_ = Composer::instance().ctlConfig()->api;}
return 0;
}
@@ -44,29 +42,43 @@ std::string SourceAPI::api() const
void SourceAPI::addSignal(const std::string& id, const std::string& event, std::vector<std::string>& depends, const std::string& sClass, const std::string& unit, double frequency, CtlActionT* onReceived, json_object* getSignalsArgs)
{
- Signal* sig = new Signal(id, event, depends, unit, frequency, onReceived, getSignalsArgs);
+ std::shared_ptr<Signal> sig = std::make_shared<Signal>(id, event, depends, unit, frequency, onReceived, getSignalsArgs);
- signalsMap_[sig] = false;
+ signalsMap_[id] = sig;
}
-std::vector<Signal*> SourceAPI::getSignals() const
+std::vector<std::shared_ptr<Signal>> SourceAPI::getSignals() const
{
- std::vector<Signal*> signals;
+ std::vector<std::shared_ptr<Signal>> signals;
for (auto& sig: signalsMap_)
{
- signals.push_back(sig.first);
+ signals.push_back(sig.second);
}
return signals;
}
-std::vector<Signal*> SourceAPI::searchSignals(const std::string& name) const
+/// @brief Search a signal in a source instance. If an exact signal name is find
+/// then it will be returned else it will be search against each signals
+/// contained in the map and signal will be deeper evaluated.
+///
+/// @param[in] name - A signal name to be searched
+///
+/// @return Returns a vector of found signals.
+std::vector<std::shared_ptr<Signal>> SourceAPI::searchSignals(const std::string& name)
{
- std::vector<Signal*> signals;
- for (auto& sig: signalsMap_)
+ std::vector<std::shared_ptr<Signal>> signals;
+
+ if(signalsMap_.count(name))
+ {signals.emplace_back(signalsMap_[name]);}
+ else
{
- if(*sig.first == name)
- {signals.emplace_back(sig.first);}
+ for (auto& sig: signalsMap_)
+ {
+ if(*sig.second == name)
+ {signals.emplace_back(sig.second);}
+ }
}
+
return signals;
}
@@ -77,19 +89,19 @@ int SourceAPI::makeSubscription()
{
for(auto& sig: signalsMap_)
{
- json_object* signalJ = sig.first->toJSON();
+ json_object* signalJ = sig.second->toJSON();
if(!signalJ)
{
- AFB_ERROR("Error building JSON query object to subscribe to for signal %s", sig.first->id().c_str());
+ AFB_ERROR("Error building JSON query object to subscribe to for signal %s", sig.second->id().c_str());
err = -1;
break;
}
- err += sig.second ? 0:ActionExecOne(getSignals_, signalJ);
+ err += ActionExecOne(getSignals_, signalJ);
if(err)
{AFB_WARNING("Fails to subscribe to signal '%s/%s'",
- api_.c_str(), sig.first->id().c_str());}
+ api_.c_str(), sig.second->id().c_str());}
else
- {sig.second = true;}
+ {sig.second->subscribed_ = true;}
}
err += ActionExecOne(getSignals_, nullptr);
}
diff --git a/signal-composer-binding/source.hpp b/signal-composer-binding/source.hpp
index 8b06b89..57836e7 100644
--- a/signal-composer-binding/source.hpp
+++ b/signal-composer-binding/source.hpp
@@ -14,11 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
#pragma once
-
-
+#include <memory>
#include "signal.hpp"
class SourceAPI {
@@ -28,7 +26,7 @@ private:
CtlActionT* init_;
CtlActionT* getSignals_;
- std::map<Signal*, bool> signalsMap_;
+ std::map<std::string, std::shared_ptr<Signal>> signalsMap_;
public:
SourceAPI();
@@ -38,8 +36,8 @@ public:
std::string api() const;
void addSignal(const std::string& id, const std::string& event, std::vector<std::string>& sources, const std::string& sClass, const std::string& unit, double frequency, CtlActionT* onReceived, json_object* getSignalsArgs);
- std::vector<Signal*> getSignals() const;
- std::vector<Signal*> searchSignals(const std::string& name) const;
+ std::vector<std::shared_ptr<Signal>> getSignals() const;
+ std::vector<std::shared_ptr<Signal>> searchSignals(const std::string& name);
int makeSubscription();
};