summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2018-11-23 10:00:09 +0100
committerRomain Forlot <romain.forlot@iot.bzh>2018-11-23 10:00:56 +0100
commit74c0e1f8e2a6dbc5b7485c3b06e12d4ad7f43c6b (patch)
treed518d880d94d7df189ad1a34093dc8ad348691ba
parente1ef945da61ae0dfec7a5c8d1e9cde2716c0c1fd (diff)
Use specific binder's event handlersguppy_6.99.2guppy/6.99.26.99.2
Use specific binder's event handlers which allows you to bind a callback with an incoming event based on a pattern matching. This replace the global event listener and internal callback search algorythm which is now delegated to the binder. Clean unused constructor Bug-AGL: SPEC-1968 Change-Id: I4b22c003661189fb71498efc4d9021c1a54ae866 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r--signal-composer-binding/signal-composer-apidef.json2
-rw-r--r--signal-composer-binding/signal-composer-binding.cpp49
-rw-r--r--signal-composer-binding/signal.cpp16
-rw-r--r--signal-composer-binding/signal.hpp1
-rw-r--r--signal-composer-binding/source.cpp83
-rw-r--r--signal-composer-binding/source.hpp3
6 files changed, 85 insertions, 69 deletions
diff --git a/signal-composer-binding/signal-composer-apidef.json b/signal-composer-binding/signal-composer-apidef.json
index cf45aec..6baff07 100644
--- a/signal-composer-binding/signal-composer-apidef.json
+++ b/signal-composer-binding/signal-composer-apidef.json
@@ -11,7 +11,7 @@
"prefix": "",
"postfix": "",
"start": null ,
- "onevent": "onEvent",
+ "onevent": null,
"preinit": "loadConf",
"init": "execConf",
"scope": "",
diff --git a/signal-composer-binding/signal-composer-binding.cpp b/signal-composer-binding/signal-composer-binding.cpp
index 6c60526..15b9c93 100644
--- a/signal-composer-binding/signal-composer-binding.cpp
+++ b/signal-composer-binding/signal-composer-binding.cpp
@@ -25,55 +25,6 @@
#include "signal-composer-apidef.h"
#include "clientApp.hpp"
-/// @brief callback for receiving message from low bindings. This will callback
-/// an action defined in the configuration files depending on the event received
-///
-/// @param[in] event - event name
-/// @param[in] object - eventual data that comes with the event
-void onEvent(const char *event, json_object *object)
-{
- std::vector<std::shared_ptr<Signal>> signals { Composer::instance().searchSignals(event) };
-
- if(!signals.empty())
- {
- // If there is more than 1 element then maybe we can find a more
- // detailled event name in JSON object as 1 event may carry several
- // signals. Try to find that one.
- if(signals.size() > 1)
- {
- bool found = false;
- json_object_iterator iter = json_object_iter_begin(object);
- json_object_iterator iterEnd = json_object_iter_end(object);
- while(!json_object_iter_equal(&iter, &iterEnd))
- {
- json_object *value = json_object_iter_peek_value(&iter);
- if(json_object_is_type(value, json_type_string))
- {
- std::string name = json_object_get_string(value);
- for(auto& sig: signals)
- {
- if(*sig == name)
- {
- found = true;
- sig->onReceivedCB(object);
- }
- }
- }
- json_object_iter_next(&iter);
- }
- // If nothing found in JSON data then apply onReceived callback
- // for all signals found
- if(! found)
- {
- for(auto& sig: signals)
- {sig->onReceivedCB(object);}
- }
- }
- else
- {signals[0]->onReceivedCB(object);}
- }
-}
-
static int one_subscribe_unsubscribe(AFB_ReqT request,
bool subscribe,
const std::string& event,
diff --git a/signal-composer-binding/signal.cpp b/signal-composer-binding/signal.cpp
index 98eddf1..7735a08 100644
--- a/signal-composer-binding/signal.cpp
+++ b/signal-composer-binding/signal.cpp
@@ -25,22 +25,6 @@
#define USEC_TIMESTAMP_FLAG 1506514324881224
-Signal::Signal()
-:id_(""),
- event_(""),
- dependsSigV_(),
- timestamp_(0.0),
- value_(),
- retention_(0),
- frequency_(0),
- unit_(""),
- metadata_(nullptr),
- onReceived_(nullptr),
- getSignalsArgs_(nullptr),
- signalCtx_({nullptr, nullptr, nullptr, nullptr}),
- subscribed_(false)
-{}
-
Signal::Signal(const std::string& id, const std::string& event, std::vector<std::string>& depends, const std::string& unit, json_object *metadata, int retention, double frequency, CtlActionT* onReceived, json_object* getSignalsArgs)
:id_(id),
event_(event),
diff --git a/signal-composer-binding/signal.hpp b/signal-composer-binding/signal.hpp
index 0dec877..e40164b 100644
--- a/signal-composer-binding/signal.hpp
+++ b/signal-composer-binding/signal.hpp
@@ -66,7 +66,6 @@ private:
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, json_object *metadata, int retention, double frequency, CtlActionT* onReceived, json_object* getSignalsArgs);
~Signal();
diff --git a/signal-composer-binding/source.cpp b/signal-composer-binding/source.cpp
index f400731..a33db22 100644
--- a/signal-composer-binding/source.cpp
+++ b/signal-composer-binding/source.cpp
@@ -49,9 +49,12 @@ void SourceAPI::init()
CtlSourceT source;
source.uid = init_->uid;
ActionExecOne(&source, init_, json_object_new_object());
+ std::string sourceAPI_events = api_ + "/*";
+ afb_api_event_handler_add(afbBindingV3root, sourceAPI_events.c_str(), SourceAPI::onSourceEvents, NULL);
return;
}
- else if(api_ == afbBindingV3root->apiname)
+
+ if(api_ == afbBindingV3root->apiname)
{api_ = Composer::instance().ctlConfig()->api;}
}
@@ -60,6 +63,78 @@ std::string SourceAPI::api() const
return api_;
}
+/// @brief callback for receiving message from low bindings. This will call back
+/// an action defined in the configuration files depending on the events
+/// received from an API.
+///
+/// @param[in] object - an opaq pointer holding userdata
+/// @param[in] event - event name
+/// @param[in] object - eventual data that comes with the event
+/// @param[in] object - the api that subscribed the event
+///
+void SourceAPI::onSourceEvents(void *closure, const char *event_name, json_object *event_obj, AFB_ApiT api)
+{
+ std::vector<std::shared_ptr<Signal>> signals { Composer::instance().searchSignals(event_name) };
+
+ if(signals.empty())
+ {
+ AFB_NOTICE("This event '%s' isn't registered within the signal composer configuration. Continue.", event_name);
+ return;
+ }
+ // If there is more than 1 element then maybe we can find a more
+ // detailled event name in JSON object as 1 event may carry several
+ // signals. Try to find that one.
+ if(signals.size() > 1)
+ {
+ bool found = false;
+ json_object_iterator iter = json_object_iter_begin(event_obj);
+ json_object_iterator iterEnd = json_object_iter_end(event_obj);
+ while(!json_object_iter_equal(&iter, &iterEnd))
+ {
+ json_object *value = json_object_iter_peek_value(&iter);
+ if(json_object_is_type(value, json_type_string))
+ {
+ std::string name = json_object_get_string(value);
+ for(auto& sig: signals)
+ {
+ if(*sig == name)
+ {
+ found = true;
+ sig->onReceivedCB(event_obj);
+ }
+ }
+ }
+ json_object_iter_next(&iter);
+ }
+ // If nothing found in JSON data then apply onReceived callback
+ // for all signals found
+ if(! found)
+ {
+ for(auto& sig: signals)
+ {sig->onReceivedCB(event_obj);}
+ }
+ }
+ else
+ {
+ signals[0]->onReceivedCB(event_obj);
+ }
+}
+
+/// @brief callback for receiving message from low bindings. This will call back
+/// an action defined in the configuration files depending on the events
+/// received from an API.
+///
+/// @param[in] object - an opaq pointer holding userdata
+/// @param[in] event - event name
+/// @param[in] object - eventual data that comes with the event
+/// @param[in] object - the api that subscribed the event
+///
+void SourceAPI::onSignalEvents(void *closure, const char *event_name, json_object *event_obj, AFB_ApiT api)
+{
+ Signal *sig = (Signal*) closure;
+ sig->onReceivedCB(event_obj);
+}
+
const struct signalsDefault& SourceAPI::signalsDefault() const
{
return signalsDefault_;
@@ -69,13 +144,17 @@ void SourceAPI::addSignal(const std::string& id, const std::string& event, std::
{
std::shared_ptr<Signal> sig = std::make_shared<Signal>(id, event, depends, unit, metadata, retention, frequency, onReceived, getSignalsArgs);
+ if(onReceived && ! event.empty())
+ afb_api_event_handler_add(afbBindingV3root, event.c_str(), SourceAPI::onSignalEvents, (void*)sig.get());
+
newSignalsM_[id] = sig;
}
void SourceAPI::initSignals()
{
- Composer& composer = Composer::instance();
int err = 0;
+ Composer& composer = Composer::instance();
+
for(auto& i: newSignalsM_)
{i.second->attachToSourceSignals(composer);}
diff --git a/signal-composer-binding/source.hpp b/signal-composer-binding/source.hpp
index b4f1348..bc28b54 100644
--- a/signal-composer-binding/source.hpp
+++ b/signal-composer-binding/source.hpp
@@ -46,6 +46,9 @@ public:
void init();
std::string api() const;
+
+ static void onSourceEvents(void *closure, const char *event_name, json_object *event_obj, AFB_ApiT api);
+ static void onSignalEvents(void *closure, const char *event_name, json_object *event_obj, AFB_ApiT api);
const struct signalsDefault& signalsDefault() const;
void addSignal(const std::string& id, const std::string& event, std::vector<std::string>& sources, int retention, const std::string& unit, json_object *metadata, double frequency, CtlActionT* onReceived, json_object* getSignalsArgs);