From cc6f16e65aabe555dec9485fed438d0e7306acf5 Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 13 Jan 2019 18:02:35 +0100 Subject: [PATCH] fix for messages with later set poll priority being preferred before others in the poll sequence --- src/lib/ebus/message.cpp | 54 +++++++++++++++++++++++----------------- src/lib/ebus/message.h | 4 +-- 2 files changed, 33 insertions(+), 25 deletions(-) mode change 100755 => 100644 src/lib/ebus/message.cpp mode change 100755 => 100644 src/lib/ebus/message.h diff --git a/src/lib/ebus/message.cpp b/src/lib/ebus/message.cpp old mode 100755 new mode 100644 index 5bb97027..907ac35d --- a/src/lib/ebus/message.cpp +++ b/src/lib/ebus/message.cpp @@ -79,6 +79,9 @@ static const char* defaultMessageFieldMap[] = { // access level not included in "*name", "part", "type", "divisor/values", "unit", "comment", }; +/** the m_pollOrder of the last polled message. */ +static unsigned int g_lastPollOrder = 0; + extern DataFieldTemplates* getTemplates(const string& filename); extern result_t loadDefinitionsFromConfigPath(FileReader* reader, const string& filename, bool verbose, @@ -99,7 +102,7 @@ Message::Message(const string& circuit, const string& level, const string& name, m_data(data), m_deleteData(deleteData), m_pollPriority(pollPriority), m_usedByCondition(false), m_isScanMessage(false), m_condition(condition), - m_lastUpdateTime(0), m_lastChangeTime(0), m_pollCount(0), m_lastPollTime(0) { + m_lastUpdateTime(0), m_lastChangeTime(0), m_pollOrder(0), m_lastPollTime(0) { if (circuit == "scan") { setScanMessage(); m_pollPriority = 0; @@ -116,7 +119,7 @@ Message::Message(const string& circuit, const string& level, const string& name, m_data(data), m_deleteData(deleteData), m_pollPriority(0), m_usedByCondition(false), m_isScanMessage(true), m_condition(nullptr), - m_lastUpdateTime(0), m_lastChangeTime(0), m_pollCount(0), m_lastPollTime(0) { + m_lastUpdateTime(0), m_lastChangeTime(0), m_pollOrder(0), m_lastPollTime(0) { } @@ -596,9 +599,29 @@ bool Message::setPollPriority(size_t priority) { } bool ret = m_pollPriority == 0 && usePriority > 0; m_pollPriority = usePriority; + if (ret || m_pollOrder > g_lastPollOrder+(unsigned int)m_pollPriority) { + // ensure a later increased or newly set priority does not prefer that message before all others + m_pollOrder = g_lastPollOrder+(unsigned int)m_pollPriority; + } return ret; } +bool Message::isLessPollWeight(const Message* other) const { + if (m_pollOrder > other->m_pollOrder) { + return true; + } + if (m_pollOrder < other->m_pollOrder) { + return false; + } + if (m_pollPriority > other->m_pollPriority) { + return true; + } + if (m_pollPriority < m_pollPriority) { + return false; + } + return m_lastPollTime > other->m_lastPollTime; +} + void Message::setUsedByCondition() { if (m_usedByCondition) { return; @@ -786,26 +809,6 @@ result_t Message::decodeLastDataNumField(const char* fieldName, ssize_t fieldInd return result; } -bool Message::isLessPollWeight(const Message* other) const { - size_t tprio = m_pollPriority; - size_t oprio = other->m_pollPriority; - size_t tw = tprio * m_pollCount; - size_t ow = oprio * other->m_pollCount; - if (tw > ow) { - return true; - } - if (tw < ow) { - return false; - } - if (tprio > oprio) { - return true; - } - if (tprio < oprio) { - return false; - } - return m_lastPollTime > other->m_lastPollTime; -} - void Message::dumpHeader(const vector* fieldNames, ostream* output) { bool first = true; if (fieldNames == nullptr) { @@ -2751,11 +2754,16 @@ Message* MessageMap::getNextPoll() { if (m_pollMessages.empty()) { return nullptr; } + lock(); Message* ret = m_pollMessages.top(); m_pollMessages.pop(); - ret->m_pollCount++; + if (ret->m_pollOrder > g_lastPollOrder) { + g_lastPollOrder = ret->m_pollOrder; + } + ret->m_pollOrder += (unsigned int)ret->m_pollPriority; time(&(ret->m_lastPollTime)); m_pollMessages.push(ret); // re-insert at new position + unlock(); return ret; } diff --git a/src/lib/ebus/message.h b/src/lib/ebus/message.h old mode 100755 new mode 100644 index bccdc180..b987f0ed --- a/src/lib/ebus/message.h +++ b/src/lib/ebus/message.h @@ -642,8 +642,8 @@ class Message : public AttributedItem { /** the system time when the message content was last changed, 0 for never. */ time_t m_lastChangeTime; - /** the number of times this messages was already polled for. */ - unsigned int m_pollCount; + /** the polling order of this message (roughly number of polls * priority). */ + unsigned int m_pollOrder; /** the system time when this message was last polled for, 0 for never. */ time_t m_lastPollTime;