mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-01-30 08:03:49 +00:00
Refactor MeshMS ply-reading functions
Rename functions to use _prev instead of _next to mean reading "backwards" ie, from newest to oldest, aka, from higher offsets to lower offsets
This commit is contained in:
parent
7b5752a111
commit
b1992b3905
27
meshms.c
27
meshms.c
@ -242,7 +242,7 @@ static void ply_read_close(struct meshms_ply_read *ply)
|
|||||||
|
|
||||||
// read the next record from the ply (backwards)
|
// read the next record from the ply (backwards)
|
||||||
// returns 1 on EOF, -1 on failure
|
// returns 1 on EOF, -1 on failure
|
||||||
static int ply_read_next(struct meshms_ply_read *ply)
|
static int ply_read_prev(struct meshms_ply_read *ply)
|
||||||
{
|
{
|
||||||
ply->record_end_offset = ply->read.offset;
|
ply->record_end_offset = ply->read.offset;
|
||||||
unsigned char footer[2];
|
unsigned char footer[2];
|
||||||
@ -295,9 +295,9 @@ static int ply_read_next(struct meshms_ply_read *ply)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// keep reading past messages until you find this type.
|
// keep reading past messages until you find this type.
|
||||||
static int ply_find_next(struct meshms_ply_read *ply, char type){
|
static int ply_find_prev(struct meshms_ply_read *ply, char type){
|
||||||
while(1){
|
while(1){
|
||||||
int ret = ply_read_next(ply);
|
int ret = ply_read_prev(ply);
|
||||||
if (ret || ply->type==type)
|
if (ret || ply->type==type)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -387,8 +387,7 @@ static int update_conversation(const sid_t *my_sid, struct meshms_conversations
|
|||||||
|
|
||||||
if (ply_read_open(&ply, &conv->their_ply.bundle_id, m_theirs))
|
if (ply_read_open(&ply, &conv->their_ply.bundle_id, m_theirs))
|
||||||
goto end;
|
goto end;
|
||||||
|
ret = ply_find_prev(&ply, MESHMS_BLOCK_TYPE_MESSAGE);
|
||||||
ret = ply_find_next(&ply, MESHMS_BLOCK_TYPE_MESSAGE);
|
|
||||||
if (ret!=0){
|
if (ret!=0){
|
||||||
// no messages indicates that we didn't do anthing
|
// no messages indicates that we didn't do anthing
|
||||||
if (ret>0)
|
if (ret>0)
|
||||||
@ -419,8 +418,7 @@ static int update_conversation(const sid_t *my_sid, struct meshms_conversations
|
|||||||
goto end;
|
goto end;
|
||||||
if (ply_read_open(&ply, &conv->my_ply.bundle_id, m_ours))
|
if (ply_read_open(&ply, &conv->my_ply.bundle_id, m_ours))
|
||||||
goto end;
|
goto end;
|
||||||
|
ret = ply_find_prev(&ply, MESHMS_BLOCK_TYPE_ACK);
|
||||||
ret = ply_find_next(&ply, MESHMS_BLOCK_TYPE_ACK);
|
|
||||||
if (ret == -1)
|
if (ret == -1)
|
||||||
goto end;
|
goto end;
|
||||||
|
|
||||||
@ -737,13 +735,14 @@ int meshms_message_iterator_open(struct meshms_message_iterator *iter, const sid
|
|||||||
goto fail;
|
goto fail;
|
||||||
if (ply_read_open(&iter->_read_sender, &iter->_conv->my_ply.bundle_id, iter->_manifest_sender))
|
if (ply_read_open(&iter->_read_sender, &iter->_conv->my_ply.bundle_id, iter->_manifest_sender))
|
||||||
goto fail;
|
goto fail;
|
||||||
|
// Seek to end of my ply.
|
||||||
if (iter->_conv->found_their_ply) {
|
if (iter->_conv->found_their_ply) {
|
||||||
if ((iter->_manifest_recipient = rhizome_new_manifest()) == NULL)
|
if ((iter->_manifest_recipient = rhizome_new_manifest()) == NULL)
|
||||||
goto fail;
|
goto fail;
|
||||||
if (ply_read_open(&iter->_read_recipient, &iter->_conv->their_ply.bundle_id, iter->_manifest_recipient))
|
if (ply_read_open(&iter->_read_recipient, &iter->_conv->their_ply.bundle_id, iter->_manifest_recipient))
|
||||||
goto fail;
|
goto fail;
|
||||||
// Find the recipient's ACK so we know which messages have been delivered.
|
// Find the recipient's latest ACK so we know which messages have been delivered.
|
||||||
int r = ply_find_next(&iter->_read_recipient, MESHMS_BLOCK_TYPE_ACK);
|
int r = ply_find_prev(&iter->_read_recipient, MESHMS_BLOCK_TYPE_ACK);
|
||||||
if (r == 0) {
|
if (r == 0) {
|
||||||
if (unpack_uint(iter->_read_recipient.buffer, iter->_read_recipient.record_length, &iter->sent_ack_offset) == -1)
|
if (unpack_uint(iter->_read_recipient.buffer, iter->_read_recipient.record_length, &iter->sent_ack_offset) == -1)
|
||||||
iter->sent_ack_offset = 0;
|
iter->sent_ack_offset = 0;
|
||||||
@ -752,6 +751,8 @@ int meshms_message_iterator_open(struct meshms_message_iterator *iter, const sid
|
|||||||
if (config.debug.meshms)
|
if (config.debug.meshms)
|
||||||
DEBUGF("Found recipient last ack @%"PRId64, iter->sent_ack_offset);
|
DEBUGF("Found recipient last ack @%"PRId64, iter->sent_ack_offset);
|
||||||
}
|
}
|
||||||
|
// Re-seek to end of their ply.
|
||||||
|
iter->_read_recipient.read.offset = iter->_read_recipient.read.length;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (config.debug.meshms)
|
if (config.debug.meshms)
|
||||||
@ -779,7 +780,7 @@ void meshms_message_iterator_close(struct meshms_message_iterator *iter)
|
|||||||
meshms_free_conversations(iter->_conv);
|
meshms_free_conversations(iter->_conv);
|
||||||
}
|
}
|
||||||
|
|
||||||
int meshms_message_iterator_next(struct meshms_message_iterator *iter)
|
int meshms_message_iterator_prev(struct meshms_message_iterator *iter)
|
||||||
{
|
{
|
||||||
assert(iter->_conv != NULL);
|
assert(iter->_conv != NULL);
|
||||||
if (iter->_conv->found_my_ply) {
|
if (iter->_conv->found_my_ply) {
|
||||||
@ -792,7 +793,7 @@ int meshms_message_iterator_next(struct meshms_message_iterator *iter)
|
|||||||
if (iter->_in_ack) {
|
if (iter->_in_ack) {
|
||||||
if (config.debug.meshms)
|
if (config.debug.meshms)
|
||||||
DEBUGF("Reading other log from %"PRId64", to %"PRId64, iter->_read_recipient.read.offset, iter->_end_range);
|
DEBUGF("Reading other log from %"PRId64", to %"PRId64, iter->_read_recipient.read.offset, iter->_end_range);
|
||||||
ret = ply_find_next(&iter->_read_recipient, MESHMS_BLOCK_TYPE_MESSAGE);
|
ret = ply_find_prev(&iter->_read_recipient, MESHMS_BLOCK_TYPE_MESSAGE);
|
||||||
if (ret == 0 && iter->_read_recipient.read.offset >= iter->_end_range) {
|
if (ret == 0 && iter->_read_recipient.read.offset >= iter->_end_range) {
|
||||||
iter->offset = iter->_read_recipient.record_end_offset;
|
iter->offset = iter->_read_recipient.record_end_offset;
|
||||||
iter->text = (const char *)iter->_read_recipient.buffer;
|
iter->text = (const char *)iter->_read_recipient.buffer;
|
||||||
@ -802,7 +803,7 @@ int meshms_message_iterator_next(struct meshms_message_iterator *iter)
|
|||||||
}
|
}
|
||||||
iter->_in_ack = 0;
|
iter->_in_ack = 0;
|
||||||
}
|
}
|
||||||
if ((ret = ply_read_next(&iter->_read_sender)) == 0) {
|
if ((ret = ply_read_prev(&iter->_read_sender)) == 0) {
|
||||||
if (config.debug.meshms)
|
if (config.debug.meshms)
|
||||||
DEBUGF("Offset %"PRId64", type %d, received_read_offset %"PRId64, iter->_read_sender.read.offset, iter->_read_sender.type, iter->received_read_offset);
|
DEBUGF("Offset %"PRId64", type %d, received_read_offset %"PRId64, iter->_read_sender.read.offset, iter->_read_sender.type, iter->received_read_offset);
|
||||||
switch (iter->_read_sender.type) {
|
switch (iter->_read_sender.type) {
|
||||||
@ -971,7 +972,7 @@ int app_meshms_list_messages(const struct cli_parsed *parsed, struct cli_context
|
|||||||
bool_t marked_read = 0;
|
bool_t marked_read = 0;
|
||||||
int id = 0;
|
int id = 0;
|
||||||
int ret;
|
int ret;
|
||||||
while ((ret = meshms_message_iterator_next(&iter)) == 0) {
|
while ((ret = meshms_message_iterator_prev(&iter)) == 0) {
|
||||||
switch (iter.direction) {
|
switch (iter.direction) {
|
||||||
case SENT:
|
case SENT:
|
||||||
if (iter.delivered && !marked_delivered){
|
if (iter.delivered && !marked_delivered){
|
||||||
|
4
meshms.h
4
meshms.h
@ -100,7 +100,7 @@ void meshms_conversation_iterator_advance(struct meshms_conversation_iterator *)
|
|||||||
* if (meshms_message_iterator_open(&it, &sender_sid, &recip_sid) == -1)
|
* if (meshms_message_iterator_open(&it, &sender_sid, &recip_sid) == -1)
|
||||||
* return -1;
|
* return -1;
|
||||||
* int ret;
|
* int ret;
|
||||||
* while ((ret = meshms_message_iterator_next(&it)) == 0) {
|
* while ((ret = meshms_message_iterator_prev(&it)) == 0) {
|
||||||
* ...
|
* ...
|
||||||
* }
|
* }
|
||||||
* meshms_message_iterator_close(&it);
|
* meshms_message_iterator_close(&it);
|
||||||
@ -132,6 +132,6 @@ struct meshms_message_iterator {
|
|||||||
};
|
};
|
||||||
int meshms_message_iterator_open(struct meshms_message_iterator *, const sid_t *sender, const sid_t *recipient);
|
int meshms_message_iterator_open(struct meshms_message_iterator *, const sid_t *sender, const sid_t *recipient);
|
||||||
void meshms_message_iterator_close(struct meshms_message_iterator *);
|
void meshms_message_iterator_close(struct meshms_message_iterator *);
|
||||||
int meshms_message_iterator_next(struct meshms_message_iterator *);
|
int meshms_message_iterator_prev(struct meshms_message_iterator *);
|
||||||
|
|
||||||
#endif // __SERVAL_DNA__MESHMS_H
|
#endif // __SERVAL_DNA__MESHMS_H
|
||||||
|
@ -1201,7 +1201,7 @@ static int restful_meshms_messagelist_json(rhizome_http_request *r, const char *
|
|||||||
r->finalise_union = finalise_union_meshms_messagelist;
|
r->finalise_union = finalise_union_meshms_messagelist;
|
||||||
r->u.msglist.rowcount = 0;
|
r->u.msglist.rowcount = 0;
|
||||||
meshms_message_iterator_open(&r->u.msglist.iter, &r->sid1, &r->sid2);
|
meshms_message_iterator_open(&r->u.msglist.iter, &r->sid1, &r->sid2);
|
||||||
if ((r->u.msglist.finished = meshms_message_iterator_next(&r->u.msglist.iter)) == -1)
|
if ((r->u.msglist.finished = meshms_message_iterator_prev(&r->u.msglist.iter)) == -1)
|
||||||
return -1;
|
return -1;
|
||||||
r->u.msglist.phase = LIST_HEADER;
|
r->u.msglist.phase = LIST_HEADER;
|
||||||
http_request_response_generated(&r->http, 200, "application/json", restful_meshms_messagelist_json_content);
|
http_request_response_generated(&r->http, 200, "application/json", restful_meshms_messagelist_json_content);
|
||||||
@ -1271,7 +1271,7 @@ static int restful_meshms_messagelist_json_content_chunk(struct http_request *hr
|
|||||||
strbuf_puts(b, "]");
|
strbuf_puts(b, "]");
|
||||||
if (!strbuf_overrun(b)) {
|
if (!strbuf_overrun(b)) {
|
||||||
++r->u.msglist.rowcount;
|
++r->u.msglist.rowcount;
|
||||||
if ((r->u.msglist.finished = meshms_message_iterator_next(&r->u.msglist.iter)) == -1)
|
if ((r->u.msglist.finished = meshms_message_iterator_prev(&r->u.msglist.iter)) == -1)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user