From a45e086dc57f51efb882bf864e78fe678238deea Mon Sep 17 00:00:00 2001 From: Ratan Gupta Date: Wed, 21 Feb 2018 19:03:13 +0530 Subject: Add callback contexts Add the notion of a callback context. This enables callbacks to have logic around the conditions they were invoked in. There are two context on which call back can be invoked 1) Startup: during startup all the call backs will be called 2) Signal: As part of condition match on the watched properties. Callback would behave differently based on the context. eg: eventCallback 1) Startup: Don't take any action. 2) Signal: Create the Dbus Object for the event. Change-Id: If455558798ac3e44bbd8a93de0ce1b09d2e308ae Signed-off-by: Ratan Gupta --- src/callback.hpp | 26 ++++++++++++++++---------- src/data_types.hpp | 6 ++++++ src/elog.cpp | 2 +- src/elog.hpp | 2 +- src/event.hpp | 2 +- src/journal.cpp | 2 +- src/journal.hpp | 2 +- src/main.cpp | 2 +- src/method.hpp | 4 ++-- src/propertywatch.hpp | 2 +- src/propertywatchimpl.hpp | 6 +++--- src/resolve_errors.cpp | 2 +- src/resolve_errors.hpp | 2 +- src/test/callbacktest.cpp | 2 +- src/watch.hpp | 4 +++- 15 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/callback.hpp b/src/callback.hpp index 1d248bd..d476d97 100644 --- a/src/callback.hpp +++ b/src/callback.hpp @@ -25,8 +25,14 @@ class Callback Callback& operator=(Callback&&) = default; virtual ~Callback() = default; - /** @brief Run the callback. */ - virtual void operator()() = 0; + /** @brief Run the callback. + * @param[in] ctx - caller context + * Context could be Startup or Signal + * Startup: Callback is called as part of process startup. + * Signal: Callback is called as part of watch condition has been met. + * + */ + virtual void operator()(Context ctx) = 0; }; /** @class Conditional @@ -89,7 +95,7 @@ class IndexedCallback : public Callback : Callback(), index(callbackIndex) {} /** @brief Run the callback. */ - virtual void operator()() override = 0; + virtual void operator()(Context ctx) override = 0; protected: @@ -122,11 +128,11 @@ class GroupOfCallbacks : public Callback : graph(graphEntry) {} /** @brief Run the callbacks. */ - void operator()() override + void operator()(Context ctx) override { for (auto e : graph) { - (*CallbackAccess::get()[e])(); + (*CallbackAccess::get()[e])(ctx); } } @@ -154,11 +160,11 @@ class ConditionalCallback: public Callback : graph(graphEntry), condition(cond) {} /** @brief Run the callback if the condition is satisfied. */ - virtual void operator()() override + virtual void operator()(Context ctx) override { if (condition()) { - (*CallbackAccess::get()[graph[0]])(); + (*CallbackAccess::get()[graph[0]])(ctx); } } @@ -202,15 +208,15 @@ class DeferrableCallback : public ConditionalCallback delayInterval(delay), timer(nullptr) {} - void operator()() override + void operator()(Context ctx) override { if (!timer) { timer = std::make_unique( // **INDENT-OFF** - [this](auto & source) + [ctx, this](auto & source) { - this->ConditionalCallback::operator()(); + this->ConditionalCallback::operator()(ctx); }); // **INDENT-ON** timer->disable(); diff --git a/src/data_types.hpp b/src/data_types.hpp index 1ba0b8a..12d1d6d 100644 --- a/src/data_types.hpp +++ b/src/data_types.hpp @@ -21,6 +21,12 @@ constexpr auto propertyIndex = 2; constexpr auto valueIndex = 2; constexpr auto metaIndex = 1; +enum class Context +{ + START, + SIGNAL, +}; + /** @brief A map with references as keys. */ template using RefKeyMap = std::map, Value, std::less>; diff --git a/src/elog.cpp b/src/elog.cpp index 9f937c3..116691e 100644 --- a/src/elog.cpp +++ b/src/elog.cpp @@ -22,7 +22,7 @@ namespace dbus namespace monitoring { -void ElogBase::operator()() +void ElogBase::operator()(Context ctx) { log(); } diff --git a/src/elog.hpp b/src/elog.hpp index e4f5456..c3b0d02 100644 --- a/src/elog.hpp +++ b/src/elog.hpp @@ -30,7 +30,7 @@ class ElogBase : public Callback Callback() {} /** @brief Callback interface implementation. */ - void operator()() override; + void operator()(Context ctx) override; private: /** @brief Delegate type specific calls to subclasses. */ diff --git a/src/event.hpp b/src/event.hpp index 8006c23..22834bf 100644 --- a/src/event.hpp +++ b/src/event.hpp @@ -30,7 +30,7 @@ class EventBase : public IndexedCallback IndexedCallback(index) {} /** @brief Callback interface implementation. */ - void operator()() override + void operator()(Context ctx) override { for (const auto& n : index) { diff --git a/src/journal.cpp b/src/journal.cpp index 46f74e9..c348cfb 100644 --- a/src/journal.cpp +++ b/src/journal.cpp @@ -22,7 +22,7 @@ namespace dbus namespace monitoring { -void JournalBase::operator()() +void JournalBase::operator()(Context ctx) { for (const auto& n : index) { diff --git a/src/journal.hpp b/src/journal.hpp index cc74451..d9209af 100644 --- a/src/journal.hpp +++ b/src/journal.hpp @@ -31,7 +31,7 @@ class JournalBase : public IndexedCallback IndexedCallback(index), message(msg) {} /** @brief Callback interface implementation. */ - void operator()() override; + void operator()(Context ctx) override; private: /** @brief Delegate type specific calls to subclasses. */ diff --git a/src/main.cpp b/src/main.cpp index ffa9f8d..08cd849 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,7 +39,7 @@ int main(void) for (auto& watch : ConfigPropertyWatches::get()) { - watch->callback(); + watch->callback(Context::START); } Loop::run(); diff --git a/src/method.hpp b/src/method.hpp index 66c19db..9475fb8 100644 --- a/src/method.hpp +++ b/src/method.hpp @@ -65,7 +65,7 @@ class MethodBase : public Callback method(m) {} /** @brief Callback interface implementation. */ - void operator()() override = 0; + void operator()(Context ctx) override = 0; protected: const std::string& bus; @@ -100,7 +100,7 @@ class Method : public MethodBase args(std::forward(arguments)...) {} /** @brief Callback interface implementation. */ - void operator()() override + void operator()(Context ctx) override { std::experimental::apply( detail::CallDBusMethod::op, diff --git a/src/propertywatch.hpp b/src/propertywatch.hpp index 55f6e5b..cd286a5 100644 --- a/src/propertywatch.hpp +++ b/src/propertywatch.hpp @@ -50,7 +50,7 @@ class PropertyWatch : public Watch * * Watch callback interface implementation for PropertyWatch. */ - void callback() override; + void callback(Context ctx) override; /** @brief Update properties. * diff --git a/src/propertywatchimpl.hpp b/src/propertywatchimpl.hpp index 3ef5de5..43875b9 100644 --- a/src/propertywatchimpl.hpp +++ b/src/propertywatchimpl.hpp @@ -104,12 +104,12 @@ void PropertyWatch::start() } template -void PropertyWatch::callback() +void PropertyWatch::callback(Context ctx) { // Invoke callback if present. if (this->alreadyRan && this->cb) { - (*this->cb)(); + (*this->cb)(ctx); } } @@ -149,7 +149,7 @@ void PropertyWatchOfType::propertiesChanged( std::get<2>(item->second).get() = p.second.template get(); // Invoke callback if present. - this->callback(); + this->callback(Context::SIGNAL); } } diff --git a/src/resolve_errors.cpp b/src/resolve_errors.cpp index 67f7313..54f0b78 100644 --- a/src/resolve_errors.cpp +++ b/src/resolve_errors.cpp @@ -34,7 +34,7 @@ using namespace phosphor::logging; using EndpointList = std::vector; using EndpointsProperty = sdbusplus::message::variant; -void ResolveCallout::operator()() +void ResolveCallout::operator()(Context ctx) { //Resolve all errors for this callout: // 1) Read the 'endpoints' property for the callout/fault object diff --git a/src/resolve_errors.hpp b/src/resolve_errors.hpp index a455ab4..cae4bbc 100644 --- a/src/resolve_errors.hpp +++ b/src/resolve_errors.hpp @@ -41,7 +41,7 @@ class ResolveCallout : public Callback * Resolves all error log entries that are associated * with the callout. */ - void operator()() override; + void operator()(Context ctx) override; private: diff --git a/src/test/callbacktest.cpp b/src/test/callbacktest.cpp index 1152e12..78736f8 100644 --- a/src/test/callbacktest.cpp +++ b/src/test/callbacktest.cpp @@ -10,6 +10,6 @@ TEST(JournalTest, Test) // make sure the program runs without crashing... for (auto& c : ConfigPropertyCallbacks::get()) { - (*c)(); + (*c)(Context::START); } } diff --git a/src/watch.hpp b/src/watch.hpp index e36af9c..d420458 100644 --- a/src/watch.hpp +++ b/src/watch.hpp @@ -1,5 +1,7 @@ #pragma once +#include "data_types.hpp" + namespace phosphor { namespace dbus @@ -34,7 +36,7 @@ class Watch virtual void start() = 0; /** @brief Invoke the callback associated with the watch. */ - virtual void callback() = 0; + virtual void callback(Context ctx) = 0; }; -- cgit v1.2.1