bootstrap RTC from contact.lastmod and improve slot overwrite logic
slot overwrite logic can now safely use contact.lastmod to find oldest contact for overwrite
This commit is contained in:
@@ -862,6 +862,7 @@ void MyMesh::begin(bool has_display) {
|
|||||||
|
|
||||||
resetContacts();
|
resetContacts();
|
||||||
_store->loadContacts(this);
|
_store->loadContacts(this);
|
||||||
|
bootstrapRTCfromContacts();
|
||||||
addChannel("Public", PUBLIC_GROUP_PSK); // pre-configure Andy's public channel
|
addChannel("Public", PUBLIC_GROUP_PSK); // pre-configure Andy's public channel
|
||||||
_store->loadChannels(this);
|
_store->loadChannels(this);
|
||||||
|
|
||||||
|
|||||||
@@ -55,17 +55,29 @@ void BaseChatMesh::sendAckTo(const ContactInfo& dest, uint32_t ack_hash) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BaseChatMesh::bootstrapRTCfromContacts() {
|
||||||
|
uint32_t latest = 0;
|
||||||
|
for (int i = 0; i < num_contacts; i++) {
|
||||||
|
if (contacts[i].lastmod > latest) {
|
||||||
|
latest = contacts[i].lastmod;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (latest != 0) {
|
||||||
|
getRTCClock()->setCurrentTime(latest + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ContactInfo* BaseChatMesh::allocateContactSlot() {
|
ContactInfo* BaseChatMesh::allocateContactSlot() {
|
||||||
if (num_contacts < MAX_CONTACTS) {
|
if (num_contacts < MAX_CONTACTS) {
|
||||||
return &contacts[num_contacts++];
|
return &contacts[num_contacts++];
|
||||||
} else if (shouldOverwriteWhenFull()) {
|
} else if (shouldOverwriteWhenFull()) {
|
||||||
// Find oldest non-favourite contact by last_advert_timestamp
|
// Find oldest non-favourite contact by oldest lastmod timestamp
|
||||||
int oldest_idx = -1;
|
int oldest_idx = -1;
|
||||||
uint32_t oldest_timestamp = 0xFFFFFFFF;
|
uint32_t oldest_lastmod = 0xFFFFFFFF;
|
||||||
for (int i = 0; i < num_contacts; i++) {
|
for (int i = 0; i < num_contacts; i++) {
|
||||||
bool is_favourite = (contacts[i].flags & 0x01) != 0;
|
bool is_favourite = (contacts[i].flags & 0x01) != 0;
|
||||||
if (!is_favourite && contacts[i].last_advert_timestamp < oldest_timestamp) {
|
if (!is_favourite && contacts[i].lastmod < oldest_lastmod) {
|
||||||
oldest_timestamp = contacts[i].last_advert_timestamp;
|
oldest_lastmod = contacts[i].lastmod;
|
||||||
oldest_idx = i;
|
oldest_idx = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -750,9 +762,6 @@ bool BaseChatMesh::addContact(const ContactInfo& contact) {
|
|||||||
ContactInfo* dest = allocateContactSlot();
|
ContactInfo* dest = allocateContactSlot();
|
||||||
if (dest) {
|
if (dest) {
|
||||||
*dest = contact;
|
*dest = contact;
|
||||||
if (dest->last_advert_timestamp == 0) { // ensure non-zero timestamp to prevent contacts added from discover list being considered 'oldest'
|
|
||||||
dest->last_advert_timestamp = getRTCClock()->getCurrentTimeUnique();
|
|
||||||
}
|
|
||||||
dest->shared_secret_valid = false; // mark shared_secret as needing calculation
|
dest->shared_secret_valid = false; // mark shared_secret as needing calculation
|
||||||
return true; // success
|
return true; // success
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ protected:
|
|||||||
memset(connections, 0, sizeof(connections));
|
memset(connections, 0, sizeof(connections));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void bootstrapRTCfromContacts();
|
||||||
void resetContacts() { num_contacts = 0; }
|
void resetContacts() { num_contacts = 0; }
|
||||||
void populateContactFromAdvert(ContactInfo& ci, const mesh::Identity& id, const AdvertDataParser& parser, uint32_t timestamp);
|
void populateContactFromAdvert(ContactInfo& ci, const mesh::Identity& id, const AdvertDataParser& parser, uint32_t timestamp);
|
||||||
ContactInfo* allocateContactSlot(); // helper to find slot for new contact
|
ContactInfo* allocateContactSlot(); // helper to find slot for new contact
|
||||||
|
|||||||
Reference in New Issue
Block a user