Skip to content

Commit

Permalink
Allow multiple messages to be scheduled with the same ID if the inter…
Browse files Browse the repository at this point in the history
…val is set to 0.
  • Loading branch information
LandryNorris committed Sep 11, 2024
1 parent 74d7209 commit 83b9cea
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/main/native/include/rev/Drivers/DriverDeviceThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,35 @@ class DriverDeviceThread {
return nullptr; // If no matching element found
}

detail::CANThreadSendQueueElement* findFirstMatchingIdWithNonZeroInterval(int targetId) {
for (auto& element : m_sendQueue) {
if (element.m_msg.GetMessageId() == targetId && element.m_intervalMs > 0) {
return &element;
}
}
return nullptr; // If no matching element found
}

void removeElementsWithId(int targetId) {
m_sendQueue.erase(std::remove_if(m_sendQueue.begin(), m_sendQueue.end(), [targetId](detail::CANThreadSendQueueElement element) { return element.m_msg.GetMessageId() == targetId; }), m_sendQueue.end());
}

bool EnqueueMessage(const CANMessage& msg, int32_t timeIntervalMs) {
std::lock_guard<std::mutex> lock(m_writeMutex);

detail::CANThreadSendQueueElement* existing = findFirstMatchingId(msg.GetMessageId());

if(existing) {
existing->m_intervalMs = timeIntervalMs;
existing->m_msg = msg;
} else {
if(timeIntervalMs == 0) {
// If the time interval is 0, we want to allow duplicates.
m_sendQueue.push_back(detail::CANThreadSendQueueElement(msg, timeIntervalMs));
} else {
// We don't want to replace elements with zero as the interval. Those should be guaranteed to be sent
detail::CANThreadSendQueueElement* existing = findFirstMatchingIdWithNonZeroInterval(msg.GetMessageId());

if(existing) {
existing->m_intervalMs = timeIntervalMs;
existing->m_msg = msg;
} else {
m_sendQueue.push_back(detail::CANThreadSendQueueElement(msg, timeIntervalMs));
}
}

// TODO: Limit the max queue size
Expand Down

0 comments on commit 83b9cea

Please sign in to comment.