From 4146a3d1b078e2eb183eaf9cfdf97323624622bd Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Mon, 11 Dec 2017 18:52:14 +0100 Subject: 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 --- signal-composer-binding/clientApp.cpp | 36 +++++++++++++++++++--- signal-composer-binding/clientApp.hpp | 1 + .../signal-composer-binding.cpp | 25 +++++++++------ 3 files changed, 47 insertions(+), 15 deletions(-) (limited to 'signal-composer-binding') 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>& sigV) { bool set = false; // Clean up already subscribed signals to avoid duplicata - for (std::vector>::const_iterator it = sigV.begin(); - it != sigV.end(); ++it) + for (std::vector>::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>& sigV) subscribedSignals_.insert(subscribedSignals_.end(), sigV.begin(), sigV.end()); } +void clientAppCtx::subtractSignals(std::vector>& sigV) +{ + // Clean up already subscribed signals to avoid duplicata + for (std::vector>::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 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; } diff --git a/signal-composer-binding/clientApp.hpp b/signal-composer-binding/clientApp.hpp index f5c913b..f91b299 100644 --- a/signal-composer-binding/clientApp.hpp +++ b/signal-composer-binding/clientApp.hpp @@ -29,6 +29,7 @@ public: void update(Signal* sig); void appendSignals(std::vector>& sigV); + void subtractSignals(std::vector>& 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 a155062..d9f1f01 100644 --- a/signal-composer-binding/signal-composer-binding.cpp +++ b/signal-composer-binding/signal-composer-binding.cpp @@ -85,11 +85,16 @@ static int one_subscribe_unsubscribe(struct afb_req request, int err = 0; std::vector> signals = Composer::instance().searchSignals(event); - cContext->appendSignals(signals); if(subscribe) - {err = cContext->makeSubscription(request);} + { + cContext->appendSignals(signals); + err = cContext->makeSubscription(request); + } else - {err = cContext->makeUnsubscription(request);} + { + cContext->subtractSignals(signals); + err = cContext->makeUnsubscription(request); + } return err; } @@ -100,20 +105,20 @@ static int subscribe_unsubscribe(struct afb_req request, clientAppCtx* cContext) { int rc = 0; - json_object *event = nullptr; - if (args == NULL || !json_object_object_get_ex(args, "event", &event)) + json_object *eventJ = nullptr; + if (args == NULL || !json_object_object_get_ex(args, "signal", &eventJ)) { rc = one_subscribe_unsubscribe(request, subscribe, "*", args, cContext); } - else if (json_object_get_type(event) == json_type_string) + else if (json_object_get_type(eventJ) == json_type_string) { - rc = one_subscribe_unsubscribe(request, subscribe, json_object_get_string(event), args, cContext); + rc = one_subscribe_unsubscribe(request, subscribe, json_object_get_string(eventJ), args, cContext); } - else if (json_object_get_type(event) == json_type_array) + else if (json_object_get_type(eventJ) == json_type_array) { - for (int i = 0 ; i < json_object_array_length(event) ; i++) + for (int i = 0 ; i < json_object_array_length(eventJ) ; i++) { - json_object *x = json_object_array_get_idx(event, i); + json_object *x = json_object_array_get_idx(eventJ, i); rc += one_subscribe_unsubscribe(request, subscribe, json_object_get_string(x), args, cContext); } } -- cgit 1.2.3-korg