Skip to content

Commit

Permalink
feat: implement StoreAndForward lastRequest map handling (2) (#3246)
Browse files Browse the repository at this point in the history
* feat: implement StoreAndForward `lastRequest` map handling

* Correct type, check if NodeNum is in map

---------

Co-authored-by: andrekir <[email protected]>
Co-authored-by: Ben Meadors <[email protected]>
  • Loading branch information
3 people authored Feb 18, 2024
1 parent 143ee9c commit 5672e68
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
18 changes: 9 additions & 9 deletions src/modules/esp32/StoreForwardModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ void StoreForwardModule::populatePSRAM()
*
* @param msAgo The number of milliseconds ago from which to start sending messages.
* @param to The recipient ID to send the messages to.
* @param last_request_index The index in the packet history of the last request from this node.
*/
void StoreForwardModule::historySend(uint32_t msAgo, uint32_t to, uint32_t last_request_index)
void StoreForwardModule::historySend(uint32_t msAgo, uint32_t to)
{
uint32_t queueSize = storeForwardModule->historyQueueCreate(msAgo, to, &last_request_index);
uint32_t lastIndex = lastRequest.find(to) != lastRequest.end() ? lastRequest[to] : 0;
uint32_t queueSize = storeForwardModule->historyQueueCreate(msAgo, to, &lastIndex);

if (queueSize) {
LOG_INFO("*** S&F - Sending %u message(s)\n", queueSize);
Expand All @@ -114,7 +114,8 @@ void StoreForwardModule::historySend(uint32_t msAgo, uint32_t to, uint32_t last_
sf.which_variant = meshtastic_StoreAndForward_history_tag;
sf.variant.history.history_messages = queueSize;
sf.variant.history.window = msAgo;
sf.variant.history.last_request = last_request_index;
sf.variant.history.last_request = lastIndex;
lastRequest[to] = lastIndex;
storeForwardModule->sendMessage(to, sf);
}

Expand All @@ -130,10 +131,10 @@ uint32_t StoreForwardModule::historyQueueCreate(uint32_t msAgo, uint32_t to, uin
{

this->packetHistoryTXQueue_size = 0;
// If our history was cleared, ignore what the client is telling us
uint32_t last_index = *last_request_index >= this->packetHistoryCurrent ? 0 : *last_request_index;
// If our history was cleared, ignore the last request index
uint32_t last_index = *last_request_index > this->packetHistoryCurrent ? 0 : *last_request_index;

for (int i = last_index; i < this->packetHistoryCurrent; i++) {
for (uint32_t i = last_index; i < this->packetHistoryCurrent; i++) {
/*
LOG_DEBUG("SF historyQueueCreate\n");
LOG_DEBUG("SF historyQueueCreate - time %d\n", this->packetHistory[i].time);
Expand Down Expand Up @@ -398,8 +399,7 @@ bool StoreForwardModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp,
} else {
if ((p->which_variant == meshtastic_StoreAndForward_history_tag) && (p->variant.history.window > 0)) {
// window is in minutes
storeForwardModule->historySend(p->variant.history.window * 60000, getFrom(&mp),
p->variant.history.last_request);
storeForwardModule->historySend(p->variant.history.window * 60000, getFrom(&mp));
} else {
storeForwardModule->historySend(historyReturnWindow * 60000, getFrom(&mp)); // defaults to 4 hours
}
Expand Down
6 changes: 5 additions & 1 deletion src/modules/esp32/StoreForwardModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "configuration.h"
#include <Arduino.h>
#include <functional>
#include <unordered_map>

struct PacketHistoryStruct {
uint32_t time;
Expand Down Expand Up @@ -36,6 +37,9 @@ class StoreForwardModule : private concurrency::OSThread, public ProtobufModule<
bool is_client = false;
bool is_server = false;

// Unordered_map stores the last request for each nodeNum (`to` field)
std::unordered_map<NodeNum, uint32_t> lastRequest;

public:
StoreForwardModule();

Expand All @@ -48,7 +52,7 @@ class StoreForwardModule : private concurrency::OSThread, public ProtobufModule<
*/
void historyAdd(const meshtastic_MeshPacket &mp);
void statsSend(uint32_t to);
void historySend(uint32_t msAgo, uint32_t to, uint32_t last_request_index = 0);
void historySend(uint32_t msAgo, uint32_t to);

uint32_t historyQueueCreate(uint32_t msAgo, uint32_t to, uint32_t *last_request_index);

Expand Down

0 comments on commit 5672e68

Please sign in to comment.