diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-12-11 18:52:14 +0100 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-12-14 11:00:49 +0100 |
commit | 4146a3d1b078e2eb183eaf9cfdf97323624622bd (patch) | |
tree | 667bcf93cf11214d44b7500d6c48f334f317d9e0 /signal-composer-binding/clientApp.cpp | |
parent | b77a6a7841b203bf8e86179e67b9141cbc3cd83f (diff) |
Improve subscription process
Unsubscription remove asked signals from client session
then when no more signals are requested by the client
unsubscribe from event handle.
Correctly initialize members to by correctly evaluated after
Change-Id: Icabcb5a2446c62cab4eea9bf807613b719324ea0
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'signal-composer-binding/clientApp.cpp')
-rw-r--r-- | signal-composer-binding/clientApp.cpp | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/signal-composer-binding/clientApp.cpp b/signal-composer-binding/clientApp.cpp index c325fcb..da4bb72 100644 --- a/signal-composer-binding/clientApp.cpp +++ b/signal-composer-binding/clientApp.cpp @@ -18,7 +18,8 @@ #include "clientApp.hpp" clientAppCtx::clientAppCtx(const char* uuid) -: uuid_(uuid) +: uuid_{uuid}, + event_{nullptr,nullptr} {} void clientAppCtx::update(Signal* sig) @@ -33,8 +34,8 @@ void clientAppCtx::appendSignals(std::vector<std::shared_ptr<Signal>>& sigV) { bool set = false; // Clean up already subscribed signals to avoid duplicata - for (std::vector<std::shared_ptr<Signal>>::const_iterator it = sigV.begin(); - it != sigV.end(); ++it) + for (std::vector<std::shared_ptr<Signal>>::const_iterator it = sigV.cbegin(); + it != sigV.cend(); ++it) { for (auto& ctxSig: subscribedSignals_) {if(*it == ctxSig) {set = true;}} @@ -51,6 +52,26 @@ void clientAppCtx::appendSignals(std::vector<std::shared_ptr<Signal>>& sigV) subscribedSignals_.insert(subscribedSignals_.end(), sigV.begin(), sigV.end()); } +void clientAppCtx::subtractSignals(std::vector<std::shared_ptr<Signal>>& sigV) +{ + // Clean up already subscribed signals to avoid duplicata + for (std::vector<std::shared_ptr<Signal>>::const_iterator it = sigV.cbegin(); + it != sigV.cend(); ++it) + { + for (auto ctxSig = subscribedSignals_.cbegin(); ctxSig != subscribedSignals_.cend();ctxSig++) + { + if(*it == *ctxSig) + { + subscribedSignals_.erase(ctxSig); + break; + } + } + std::shared_ptr<Signal> sig = *it; + sig->delObserver(this); + AFB_NOTICE("Signal %s delete from subscription", sig->id().c_str()); + } +} + int clientAppCtx::makeSubscription(struct afb_req request) { event_ = afb_event_is_valid(event_) ? @@ -60,6 +81,11 @@ int clientAppCtx::makeSubscription(struct afb_req request) int clientAppCtx::makeUnsubscription(struct afb_req request) { - return afb_event_is_valid(event_) ? - afb_req_unsubscribe(request, event_) : -1; + if(subscribedSignals_.empty()) + { + AFB_NOTICE("No more signals subscribed, releasing."); + return afb_event_is_valid(event_) ? + afb_req_unsubscribe(request, event_) : -1; + } + return 0; } |