diff options
author | Matt Spinler <spinler@us.ibm.com> | 2018-02-19 13:09:10 -0600 |
---|---|---|
committer | Matt Spinler <spinler@us.ibm.com> | 2018-02-23 13:51:02 -0600 |
commit | c458deea83b4356f07194691e301037d8e831d46 (patch) | |
tree | a01904097309523a576612c33cdc2c3a17a69095 /src/count.hpp | |
parent | 8bbf9d2cc5a502fa0df0e70bd4232208a1ab6c01 (diff) | |
download | phosphor-dbus-monitor-c458deea83b4356f07194691e301037d8e831d46.tar.gz phosphor-dbus-monitor-c458deea83b4356f07194691e301037d8e831d46.zip |
Add oneshot support to count conditions
When a count condition is a oneshot, it will only
trigger the callback the first time the condition
is true. The condition needs to evaluate to false
at least once to rearm the callback.
A use case for this would be when a group of properties
is being watched for a subset of them hit a certain value.
Even though every properties changed signal for every property
in the group would check the condition, only the first time
the condition is true would the callback be issued.
This behavior is specified with a 'oneshot: true' entry in
the YAML. If not present, it defaults to false which is
the original behavior.
Tested: Verify this does indeed do what is advertised by
modifying condition rules to consistently pass
and checking behavior.
Change-Id: Ie185621e86c605234bf329a5f38317267dbb6fb6
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Diffstat (limited to 'src/count.hpp')
-rw-r--r-- | src/count.hpp | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/src/count.hpp b/src/count.hpp index f7b809f..51a76eb 100644 --- a/src/count.hpp +++ b/src/count.hpp @@ -26,6 +26,9 @@ namespace monitoring * one. In pass two, apply a second C++ relational operator * to the number of properties that pass the test from pass one * to a count provided by the configuration file. + * + * If the oneshot parameter is true, then this condition won't pass + * again until it fails at least once. */ template <typename T> class CountCondition : public IndexedConditional @@ -41,10 +44,12 @@ class CountCondition : public IndexedConditional CountCondition( const PropertyIndex& conditionIndex, const std::function<bool(size_t)>& _countOp, - const std::function<bool(T)>& _propertyOp) : + const std::function<bool(T)>& _propertyOp, + bool oneshot = false) : IndexedConditional(conditionIndex), countOp(_countOp), - propertyOp(_propertyOp) {} + propertyOp(_propertyOp), + oneshot(oneshot) {} bool operator()() override { @@ -70,7 +75,18 @@ class CountCondition : public IndexedConditional // *INDENT-ON* // Now apply the count condition to the count. - return countOp(count); + auto result = countOp(count); + + // If this was a oneshot and the the condition has already + // passed, then don't let it pass again until the condition + // has gone back to false. + if (oneshot && result && lastResult) + { + return false; + } + + lastResult = result; + return result; } private: @@ -78,6 +94,11 @@ class CountCondition : public IndexedConditional std::function<bool(size_t)> countOp; /** @brief The comparison to perform on each property. */ std::function<bool(T)> propertyOp; + /** @brief If the condition can be allowed to pass again + on subsequent checks that are also true. */ + const bool oneshot; + /** @brief The result of the previous check. */ + bool lastResult = false; }; } // namespace monitoring } // namespace dbus |