summaryrefslogtreecommitdiffstats
path: root/signal-composer-binding
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-09-21 18:15:13 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-12-14 11:00:25 +0100
commitc27b88e3c36585d11e866902f73d1e961357c45c (patch)
tree2f38106fc68a0e039d3fccde221b02ea93b5dea6 /signal-composer-binding
parente93cb3415e883bbd73528ca6a08e53e854f7bee0 (diff)
Rename bindingApp to Composer
Change-Id: Ib3256f5c89197a91d2bbfca7a92774e91de6f664 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'signal-composer-binding')
-rw-r--r--signal-composer-binding/signal-composer-binding.cpp24
-rw-r--r--signal-composer-binding/signal-composer.cpp293
-rw-r--r--signal-composer-binding/signal-composer.hpp15
-rw-r--r--signal-composer-binding/signal.cpp6
-rw-r--r--signal-composer-binding/signal.hpp4
-rw-r--r--signal-composer-binding/source.cpp2
6 files changed, 187 insertions, 157 deletions
diff --git a/signal-composer-binding/signal-composer-binding.cpp b/signal-composer-binding/signal-composer-binding.cpp
index 5dcb53f..95bfc36 100644
--- a/signal-composer-binding/signal-composer-binding.cpp
+++ b/signal-composer-binding/signal-composer-binding.cpp
@@ -33,9 +33,9 @@
void onEvent(const char *event, json_object *object)
{
AFB_DEBUG("Received event json: %s", json_object_to_json_string(object));
- bindingApp& bApp = bindingApp::instance();
+ Composer& composer = Composer::instance();
- std::vector<std::shared_ptr<Signal>> signals = bApp.searchSignals(event);
+ std::vector<std::shared_ptr<Signal>> signals = composer.searchSignals(event);
if(!signals.empty())
{
for(auto& sig: signals)
@@ -106,7 +106,7 @@ void loadConf(afb_req request)
wrap_json_unpack(args, "{s:s}", "filepath", &filepath);
fileJ = json_object_from_file(filepath);
- if(bindingApp::instance().loadSignals(fileJ))
+ if(Composer::instance().loadSignals(fileJ))
{afb_req_fail_f(request, "Loading configuration or subscription error", "Error code: -1");}
else
{
@@ -120,7 +120,7 @@ void list(afb_req request)
{
struct json_object *allSignalsJ = json_object_new_array();
- std::vector<std::shared_ptr<Signal>> allSignals = bindingApp::instance().getAllSignals();
+ std::vector<std::shared_ptr<Signal>> allSignals = Composer::instance().getAllSignals();
for(auto& sig: allSignals)
{json_object_array_add(allSignalsJ, sig->toJSON());}
@@ -152,7 +152,7 @@ void get(struct afb_req request)
return;
}
- ans = bindingApp::instance().getSignalValue(sig, options);
+ ans = Composer::instance().getSignalValue(sig, options);
if (ans)
afb_req_success(request, ans, NULL);
@@ -166,22 +166,22 @@ int loadConf()
const char* rootdir = strncat(GetBindingDirPath(), "/etc",
sizeof(GetBindingDirPath()) - strlen(GetBindingDirPath()) -1);
- bindingApp& bApp = bindingApp::instance();
- err = bApp.loadConfig(rootdir);
+ Composer& composer = Composer::instance();
+ err = composer.loadConfig(rootdir);
return err;
}
int execConf()
{
- bindingApp& bApp = bindingApp::instance();
+ Composer& composer = Composer::instance();
int err = 0;
- CtlConfigExec(bApp.ctlConfig());
- std::vector<std::shared_ptr<Signal>> allSignals = bApp.getAllSignals();
+ CtlConfigExec(composer.ctlConfig());
+ std::vector<std::shared_ptr<Signal>> allSignals = composer.getAllSignals();
ssize_t sigCount = allSignals.size();
for( std::shared_ptr<Signal>& sig: allSignals)
{
- sig->attachToSourceSignals(bApp);
+ sig->attachToSourceSignals(composer);
}
for(auto& sig: allSignals)
@@ -193,7 +193,7 @@ int execConf()
}
}
- bApp.execSubscription();
+ composer.execSubscription();
AFB_DEBUG("Signal Composer Control configuration Done.\n signals=%d", (int)sigCount);
diff --git a/signal-composer-binding/signal-composer.cpp b/signal-composer-binding/signal-composer.cpp
index 0eba8a2..c6ffdd4 100644
--- a/signal-composer-binding/signal-composer.cpp
+++ b/signal-composer-binding/signal-composer.cpp
@@ -22,7 +22,7 @@
extern "C" void setSignalValueHandle(const char* aName, long long int timestamp, struct SignalValue value)
{
- std::vector<std::shared_ptr<Signal>> signals = bindingApp::instance().searchSignals(aName);
+ std::vector<std::shared_ptr<Signal>> signals = Composer::instance().searchSignals(aName);
if(!signals.empty())
{
for(auto& sig: signals)
@@ -44,7 +44,7 @@ static struct pluginCBT pluginHandle = {
.setSignalValue = setSignalValueHandle,
};
-CtlSectionT bindingApp::ctlSections_[] = {
+CtlSectionT Composer::ctlSections_[] = {
[0]={.key="plugins" , .label = "plugins", .info=nullptr,
.loadCB=PluginConfig,
.handle=&pluginHandle},
@@ -59,68 +59,19 @@ CtlSectionT bindingApp::ctlSections_[] = {
.handle=nullptr}
};
-bindingApp::bindingApp()
+///////////////////////////////////////////////////////////////////////////////
+// PRIVATE METHODS //
+///////////////////////////////////////////////////////////////////////////////
+
+Composer::Composer()
{}
-bindingApp::~bindingApp()
+Composer::~Composer()
{
free(ctlConfig_);
}
-int bindingApp::loadConfig(const std::string& filepath)
-{
- ctlConfig_ = CtlConfigLoad(filepath.c_str(), ctlSections_);
- if(ctlConfig_ != nullptr) {return 0;}
- return -1;
-}
-
-bindingApp& bindingApp::instance()
-{
- static bindingApp bApp;
- return bApp;
-}
-
-SourceAPI* bindingApp::getSourceAPI(const std::string& api)
-{
- for(auto& source: sourcesListV_)
- {
- if (source.api() == api)
- {return &source;}
- }
- return nullptr;
-}
-int bindingApp::initSourcesAPI()
-{
- int err = 0;
- for(auto& src: sourcesListV_)
- {
- err += src.init();
- }
- return err;
-}
-
-std::vector<std::string> bindingApp::parseURI(const std::string& uri)
-{
- std::vector<std::string> uriV;
- std::string delimiters = "/";
-
- std::string::size_type start = 0;
- auto pos = uri.find_first_of(delimiters, start);
- while(pos != std::string::npos)
- {
- if(pos != start) // ignore empty tokens
- uriV.emplace_back(uri, start, pos - start);
- start = pos + 1;
- pos = uri.find_first_of(delimiters, start);
- }
-
- if(start < uri.length()) // ignore trailing delimiter
- uriV.emplace_back(uri, start, uri.length() - start); // add what's left of the string
-
- return uriV;
-}
-
-CtlActionT* bindingApp::convert2Action(const std::string& name, json_object* actionJ)
+CtlActionT* Composer::convert2Action(const std::string& name, json_object* actionJ)
{
json_object *functionArgsJ = nullptr, *action = nullptr;
char *function;
@@ -143,7 +94,7 @@ CtlActionT* bindingApp::convert2Action(const std::string& name, json_object* act
else if(startsWith(function, "api://"))
{
std::string uri = std::string(function).substr(6);
- std::vector<std::string> uriV = bindingApp::parseURI(uri);
+ std::vector<std::string> uriV = Composer::parseURI(uri);
if(uriV.size() != 2)
{
AFB_ERROR("Miss something in uri either plugin name or function name. Uri has to be like: api://<plugin-name>/<function-name>");
@@ -158,7 +109,7 @@ CtlActionT* bindingApp::convert2Action(const std::string& name, json_object* act
else if(startsWith(function, "plugin://"))
{
std::string uri = std::string(function).substr(9);
- std::vector<std::string> uriV = bindingApp::parseURI(uri);
+ std::vector<std::string> uriV = Composer::parseURI(uri);
if(uriV.size() != 2)
{
AFB_ERROR("Miss something in uri either plugin name or function name. Uri has to be like: plugin://<plugin-name>/<function-name>");
@@ -183,7 +134,7 @@ CtlActionT* bindingApp::convert2Action(const std::string& name, json_object* act
return nullptr;
}
-int bindingApp::loadOneSourceAPI(json_object* sourceJ)
+int Composer::loadOneSourceAPI(json_object* sourceJ)
{
json_object *initJ = nullptr, *getSignalsJ = nullptr;
CtlActionT *initCtl = nullptr, *getSignalsCtl = nullptr;
@@ -215,10 +166,10 @@ int bindingApp::loadOneSourceAPI(json_object* sourceJ)
return err;
}
-int bindingApp::loadSourcesAPI(CtlSectionT* section, json_object *sourcesJ)
+int Composer::loadSourcesAPI(CtlSectionT* section, json_object *sourcesJ)
{
int err = 0;
- bindingApp& bApp = instance();
+ Composer& composer = instance();
if(sourcesJ)
{
@@ -237,23 +188,23 @@ int bindingApp::loadSourcesAPI(CtlSectionT* section, json_object *sourcesJ)
for (int idx = 0; idx < count; idx++)
{
json_object *sourceJ = json_object_array_get_idx(sourcesJ, idx);
- err = bApp.loadOneSourceAPI(sourceJ);
+ err = composer.loadOneSourceAPI(sourceJ);
if (err) return err;
}
}
else
{
- if ((err = bApp.loadOneSourceAPI(sourcesJ))) return err;
- if (sigCompJ && (err = bApp.loadOneSourceAPI(sigCompJ))) return err;
+ if ((err = composer.loadOneSourceAPI(sourcesJ))) return err;
+ if (sigCompJ && (err = composer.loadOneSourceAPI(sigCompJ))) return err;
}
}
else
- {err += bindingApp::instance().initSourcesAPI();}
+ {err += Composer::instance().initSourcesAPI();}
return err;
}
-int bindingApp::loadOneSignal(json_object* signalJ)
+int Composer::loadOneSignal(json_object* signalJ)
{
json_object *onReceivedJ = nullptr, *dependsJ = nullptr, *getSignalsArgs = nullptr;
CtlActionT* onReceivedCtl;
@@ -351,10 +302,10 @@ int bindingApp::loadOneSignal(json_object* signalJ)
return err;
}
-int bindingApp::loadSignals(CtlSectionT* section, json_object *signalsJ)
+int Composer::loadSignals(CtlSectionT* section, json_object *signalsJ)
{
int err = 0;
- bindingApp& bApp = instance();
+ Composer& composer = instance();
if(signalsJ)
{
@@ -365,76 +316,18 @@ int bindingApp::loadSignals(CtlSectionT* section, json_object *signalsJ)
for (int idx = 0; idx < count; idx++)
{
json_object *signalJ = json_object_array_get_idx(signalsJ, idx);
- err = bApp.loadOneSignal(signalJ);
+ err = composer.loadOneSignal(signalJ);
if (err) return err;
}
}
else
- {err = bApp.loadOneSignal(signalsJ);}
+ {err = composer.loadOneSignal(signalsJ);}
}
return err;
}
-int bindingApp::loadSignals(json_object* signalsJ)
-{
- return loadSignals(nullptr, signalsJ);
-}
-
-std::vector<std::shared_ptr<Signal>> bindingApp::searchSignals(const std::string& aName)
-{
- std::string api;
- std::vector<std::shared_ptr<Signal>> signals;
- size_t sep = aName.find_first_of("/");
- if(sep != std::string::npos)
- {
- api = aName.substr(0, sep);
- SourceAPI* source = getSourceAPI(api);
- return source->searchSignals(aName);
- }
- else
- {
- std::vector<std::shared_ptr<Signal>> allSignals = getAllSignals();
- for (std::shared_ptr<Signal>& sig : allSignals)
- {
- if(*sig == aName)
- {signals.emplace_back(sig);}
- }
- }
- return signals;
-}
-
-std::vector<std::shared_ptr<Signal>> bindingApp::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;
-}
-
-CtlConfigT* bindingApp::ctlConfig()
-{
- return ctlConfig_;
-}
-
-int bindingApp::execSubscription()
-{
- int err = 0;
- for(SourceAPI& srcAPI: sourcesListV_)
- {
- if (srcAPI.api() != std::string(ctlConfig_->api))
- {
- err = srcAPI.makeSubscription();
- }
- }
- return err;
-}
-
-void bindingApp::processOptions(const char** opts, std::shared_ptr<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++)
{
@@ -482,7 +375,129 @@ void bindingApp::processOptions(const char** opts, std::shared_ptr<Signal> sig,
}
}
}
-json_object* bindingApp::getSignalValue(const std::string& sig, json_object* options)
+
+///////////////////////////////////////////////////////////////////////////////
+// PUBLIC METHODS //
+///////////////////////////////////////////////////////////////////////////////
+
+Composer& Composer::instance()
+{
+ static Composer composer;
+ return composer;
+}
+
+void* Composer::createContext(void* ctx)
+{
+ uuid_t x;
+ uuid_generate(x);
+ ctx = (clientAppCtxT*)calloc(1, sizeof(clientAppCtxT));
+ clientAppCtxT* ret = (clientAppCtxT*) ctx;
+ uuid_copy(ret->uid, x);
+ ret->subscribedSignals = std::vector<std::shared_ptr<Signal>>();
+ ret->event = afb_daemon_make_event("evt");
+
+ return (void*)ret;
+}
+
+void Composer::destroyContext(void* ctx)
+{
+ free(ctx);
+}
+
+std::vector<std::string> Composer::parseURI(const std::string& uri)
+{
+ std::vector<std::string> uriV;
+ std::string delimiters = "/";
+
+ std::string::size_type start = 0;
+ auto pos = uri.find_first_of(delimiters, start);
+ while(pos != std::string::npos)
+ {
+ if(pos != start) // ignore empty tokens
+ uriV.emplace_back(uri, start, pos - start);
+ start = pos + 1;
+ pos = uri.find_first_of(delimiters, start);
+ }
+
+ if(start < uri.length()) // ignore trailing delimiter
+ uriV.emplace_back(uri, start, uri.length() - start); // add what's left of the string
+
+ return uriV;
+}
+
+int Composer::loadConfig(const std::string& filepath)
+{
+ ctlConfig_ = CtlConfigLoad(filepath.c_str(), ctlSections_);
+ if(ctlConfig_ != nullptr) {return 0;}
+ return -1;
+}
+
+int Composer::loadSignals(json_object* signalsJ)
+{
+ return loadSignals(nullptr, signalsJ);
+}
+
+CtlConfigT* Composer::ctlConfig()
+{
+ return ctlConfig_;
+}
+
+int Composer::initSourcesAPI()
+{
+ int err = 0;
+ for(auto& src: sourcesListV_)
+ {
+ err += src.init();
+ }
+ return err;
+}
+
+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;
+}
+
+SourceAPI* Composer::getSourceAPI(const std::string& api)
+{
+ for(auto& source: sourcesListV_)
+ {
+ if (source.api() == api)
+ {return &source;}
+ }
+ return nullptr;
+}
+
+std::vector<std::shared_ptr<Signal>> Composer::searchSignals(const std::string& aName)
+{
+ std::string api;
+ std::vector<std::shared_ptr<Signal>> signals;
+ size_t sep = aName.find_first_of("/");
+ if(sep != std::string::npos)
+ {
+ api = aName.substr(0, sep);
+ SourceAPI* source = getSourceAPI(api);
+ return source->searchSignals(aName);
+ }
+ else
+ {
+ std::vector<std::shared_ptr<Signal>> allSignals = getAllSignals();
+ for (std::shared_ptr<Signal>& sig : allSignals)
+ {
+ if(*sig == aName)
+ {signals.emplace_back(sig);}
+ }
+ }
+ return signals;
+}
+
+json_object* Composer::getSignalValue(const std::string& sig, json_object* options)
{
const char **opts = nullptr;
json_object *response = nullptr, *finalResponse = json_object_new_array();
@@ -493,7 +508,6 @@ json_object* bindingApp::getSignalValue(const std::string& sig, json_object* opt
&opts[2],
&opts[3]);
-
std::vector<std::shared_ptr<Signal>> sigP = searchSignals(sig);
if(!sigP.empty())
{
@@ -528,3 +542,16 @@ json_object* bindingApp::getSignalValue(const std::string& sig, json_object* opt
return response;
}
+
+int Composer::execSubscription()
+{
+ int err = 0;
+ for(SourceAPI& srcAPI: sourcesListV_)
+ {
+ if (srcAPI.api() != std::string(ctlConfig_->api))
+ {
+ err = srcAPI.makeSubscription();
+ }
+ }
+ return err;
+}
diff --git a/signal-composer-binding/signal-composer.hpp b/signal-composer-binding/signal-composer.hpp
index 5cf5858..bbc9a36 100644
--- a/signal-composer-binding/signal-composer.hpp
+++ b/signal-composer-binding/signal-composer.hpp
@@ -16,13 +16,14 @@
*/
#pragma once
+#include <uuid.h>
#include <memory>
#include <vector>
#include <string>
#include "source.hpp"
-class bindingApp
+class Composer
{
private:
CtlConfigT* ctlConfig_;
@@ -30,9 +31,9 @@ private:
static CtlSectionT ctlSections_[]; ///< Config Section definition (note: controls section index should match handle retrieval in)
std::vector<SourceAPI> sourcesListV_;
- explicit bindingApp(const std::string& filepath);
- bindingApp();
- ~bindingApp();
+ explicit Composer(const std::string& filepath);
+ Composer();
+ ~Composer();
CtlActionT* convert2Action(const std::string& name, json_object* action);
@@ -42,8 +43,11 @@ private:
int loadOneSignal(json_object* signalsJ);
static int loadSignals(CtlSectionT* section, json_object *signalsJ);
+ void processOptions(const char** opts, std::shared_ptr<Signal> sig, json_object* response) const;
public:
- static bindingApp& instance();
+ static Composer& instance();
+ static void* createContext(void* ctx);
+ static void destroyContext(void* ctx);
static std::vector<std::string> parseURI(const std::string& uri);
int loadConfig(const std::string& filepath);
int loadSignals(json_object* signalsJ);
@@ -54,7 +58,6 @@ public:
SourceAPI* getSourceAPI(const std::string& api);
std::vector<std::shared_ptr<Signal>> searchSignals(const std::string& aName);
json_object* getSignalValue(const std::string& sig, json_object* options);
- void processOptions(const char** opts, std::shared_ptr<Signal> sig, json_object* response) const;
int execSubscription();
};
diff --git a/signal-composer-binding/signal.cpp b/signal-composer-binding/signal.cpp
index a9a18b0..2d49c3c 100644
--- a/signal-composer-binding/signal.cpp
+++ b/signal-composer-binding/signal.cpp
@@ -159,14 +159,14 @@ void Signal::attach(Signal* obs)
/// @brief Make a Signal observer observes Signals observables
/// set in its observable vector.
///
-/// @param[in] bApp - bindinApp instance
-void Signal::attachToSourceSignals(bindingApp& bApp)
+/// @param[in] composer - bindinApp instance
+void Signal::attachToSourceSignals(Composer& composer)
{
for (const std::string& srcSig: dependsSigV_)
{
if(srcSig.find("/") == std::string::npos)
{
- std::vector<std::shared_ptr<Signal>> sig = bApp.searchSignals(srcSig);
+ std::vector<std::shared_ptr<Signal>> sig = composer.searchSignals(srcSig);
if(sig[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 c029a64..870843e 100644
--- a/signal-composer-binding/signal.hpp
+++ b/signal-composer-binding/signal.hpp
@@ -22,7 +22,7 @@
#include <vector>
#include <ctl-config.h>
-class bindingApp;
+class Composer;
struct SignalValue {
bool hasBool = false;
@@ -67,7 +67,7 @@ public:
void set(long long int timestamp, struct SignalValue& value);
void update(long long int timestamp, struct SignalValue value);
int onReceivedCB(json_object *queryJ);
- void attachToSourceSignals(bindingApp& bApp);
+ void attachToSourceSignals(Composer& composer);
double average(int seconds = 0) const;
double minimum(int seconds = 0) const;
diff --git a/signal-composer-binding/source.cpp b/signal-composer-binding/source.cpp
index 8481740..6c991f0 100644
--- a/signal-composer-binding/source.cpp
+++ b/signal-composer-binding/source.cpp
@@ -31,7 +31,7 @@ int SourceAPI::init()
{return ActionExecOne(init_, nullptr);}
else if(api_ == afbBindingV2.api)
{
- api_ = bindingApp::instance().ctlConfig()->api;
+ api_ = Composer::instance().ctlConfig()->api;
}
return 0;