Improved logic of allocation request manager

This commit is contained in:
Pavel Kirienko
2015-05-09 23:52:51 +03:00
parent 1a763a824c
commit 67b33a712e
2 changed files with 14 additions and 6 deletions
@@ -39,6 +39,11 @@
# #
uint16 DEFAULT_REQUEST_PERIOD_MS = 1000 uint16 DEFAULT_REQUEST_PERIOD_MS = 1000
#
# Server will reset its state if there was no follow-up request in this amount of time.
#
uint16 FOLLOWUP_TIMEOUT_MS = 500
# #
# Any request message can accommodate no more than this number of bytes of unique ID. # Any request message can accommodate no more than this number of bytes of unique ID.
# This limitation is needed to ensure that all request transfers are single-frame. # This limitation is needed to ensure that all request transfers are single-frame.
@@ -133,7 +133,6 @@ class AllocationRequestManager
UAVCAN_TRACE("AllocationRequestManager", "Stage timeout, reset"); UAVCAN_TRACE("AllocationRequestManager", "Stage timeout, reset");
current_unique_id_.clear(); current_unique_id_.clear();
} }
last_message_timestamp_ = msg.getMonotonicTimestamp();
/* /*
* Checking if request stage matches the expected stage * Checking if request stage matches the expected stage
@@ -141,26 +140,25 @@ class AllocationRequestManager
const uint8_t request_stage = detectRequestStage(msg); const uint8_t request_stage = detectRequestStage(msg);
if (request_stage == InvalidStage) if (request_stage == InvalidStage)
{ {
return; // No way return; // Malformed request - ignore without resetting
} }
const uint8_t expected_stage = getExpectedStage(); const uint8_t expected_stage = getExpectedStage();
if (request_stage == InvalidStage) if (request_stage == InvalidStage)
{ {
current_unique_id_.clear();
return; return;
} }
if (request_stage != expected_stage) if (request_stage != expected_stage)
{ {
return; // Ignore - stage mismatch return; // Ignore - stage mismatch
} }
const uint8_t max_expected_bytes = static_cast<uint8_t>(current_unique_id_.capacity() - current_unique_id_.size()); const uint8_t max_expected_bytes = static_cast<uint8_t>(current_unique_id_.capacity() - current_unique_id_.size());
UAVCAN_ASSERT(max_expected_bytes > 0); UAVCAN_ASSERT(max_expected_bytes > 0);
if (msg.unique_id.size() > max_expected_bytes) if (msg.unique_id.size() > max_expected_bytes)
{ {
return; // Malformed request return; // Malformed request
} }
/* /*
@@ -189,11 +187,16 @@ class AllocationRequestManager
{ {
broadcastIntermediateAllocationResponse(); broadcastIntermediateAllocationResponse();
} }
/*
* It is super important to update timestamp only if the request has been processed successfully.
*/
last_message_timestamp_ = msg.getMonotonicTimestamp();
} }
public: public:
AllocationRequestManager(INode& node, IAllocationRequestHandler& handler) AllocationRequestManager(INode& node, IAllocationRequestHandler& handler)
: stage_timeout_(MonotonicDuration::fromMSec(protocol::dynamic_node_id::Allocation::DEFAULT_REQUEST_PERIOD_MS)) : stage_timeout_(MonotonicDuration::fromMSec(Allocation::FOLLOWUP_TIMEOUT_MS))
, active_(false) , active_(false)
, handler_(handler) , handler_(handler)
, allocation_sub_(node) , allocation_sub_(node)