2014-01-22 06:11:22 +00:00
|
|
|
/*
|
|
|
|
Serval DNA MeshMS
|
|
|
|
Copyright (C) 2014 Serval Project Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __SERVAL_DNA__MESHMS_H
|
|
|
|
#define __SERVAL_DNA__MESHMS_H
|
|
|
|
|
|
|
|
#include "serval.h"
|
|
|
|
#include "rhizome.h"
|
|
|
|
|
|
|
|
// the manifest details for one half of a conversation
|
|
|
|
struct meshms_ply {
|
|
|
|
rhizome_bid_t bundle_id;
|
|
|
|
uint64_t version;
|
|
|
|
uint64_t tail;
|
|
|
|
uint64_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct meshms_conversations {
|
|
|
|
// binary tree
|
|
|
|
struct meshms_conversations *_left;
|
|
|
|
struct meshms_conversations *_right;
|
|
|
|
// keeping a pointer to parent node here means the traversal iterator does not need a stack, so
|
|
|
|
// there is no fixed limit on the tree depth
|
|
|
|
struct meshms_conversations *_parent;
|
|
|
|
|
|
|
|
// who are we talking to?
|
|
|
|
sid_t them;
|
|
|
|
|
|
|
|
char found_my_ply;
|
|
|
|
struct meshms_ply my_ply;
|
|
|
|
|
|
|
|
char found_their_ply;
|
|
|
|
struct meshms_ply their_ply;
|
|
|
|
|
|
|
|
// what is the offset of their last message
|
|
|
|
uint64_t their_last_message;
|
|
|
|
// what is the last message we marked as read
|
|
|
|
uint64_t read_offset;
|
|
|
|
// our cached value for the last known size of their ply
|
|
|
|
uint64_t their_size;
|
|
|
|
};
|
|
|
|
|
2014-01-24 01:11:31 +00:00
|
|
|
// cursor state for reading one half of a conversation
|
|
|
|
struct meshms_ply_read {
|
|
|
|
// rhizome payload
|
|
|
|
struct rhizome_read read;
|
|
|
|
// block buffer
|
|
|
|
struct rhizome_read_buffer buff;
|
|
|
|
// details of the current record
|
|
|
|
uint64_t record_end_offset;
|
|
|
|
uint16_t record_length;
|
|
|
|
size_t buffer_size;
|
|
|
|
char type;
|
|
|
|
// raw record data
|
|
|
|
unsigned char *buffer;
|
|
|
|
};
|
|
|
|
|
2014-01-22 06:11:22 +00:00
|
|
|
/* Fetch the list of all MeshMS conversations into a binary tree whose nodes
|
|
|
|
* are all allocated by malloc(3).
|
|
|
|
*/
|
|
|
|
int meshms_conversations_list(const sid_t *my_sid, const sid_t *their_sid, struct meshms_conversations **conv);
|
|
|
|
void meshms_free_conversations(struct meshms_conversations *conv);
|
|
|
|
|
|
|
|
/* For iterating over a binary tree of all MeshMS conversations, as created by
|
|
|
|
* meshms_conversations_list().
|
2014-01-24 01:11:31 +00:00
|
|
|
*
|
|
|
|
* struct meshms_conversation_iterator it;
|
|
|
|
* meshms_conversation_iterator_start(&it, conv);
|
|
|
|
* while (it.current) {
|
|
|
|
* ...
|
|
|
|
* meshms_conversation_iterator_advance(&it);
|
|
|
|
* }
|
2014-01-22 06:11:22 +00:00
|
|
|
*/
|
|
|
|
struct meshms_conversation_iterator {
|
|
|
|
struct meshms_conversations *current;
|
|
|
|
};
|
|
|
|
void meshms_conversation_iterator_start(struct meshms_conversation_iterator *, struct meshms_conversations *);
|
|
|
|
void meshms_conversation_iterator_advance(struct meshms_conversation_iterator *);
|
|
|
|
|
2014-01-24 01:11:31 +00:00
|
|
|
/* For iterating through the messages in a single MeshMS conversation; both
|
|
|
|
* plys threaded (interleaved) in the order as seen by the sender.
|
|
|
|
*
|
|
|
|
* struct meshms_message_iterator it;
|
|
|
|
* if (meshms_message_iterator_open(&it, &sender_sid, &recip_sid) == -1)
|
|
|
|
* return -1;
|
|
|
|
* int ret;
|
2014-01-28 06:28:44 +00:00
|
|
|
* while ((ret = meshms_message_iterator_prev(&it)) == 0) {
|
2014-01-24 01:11:31 +00:00
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
* meshms_message_iterator_close(&it);
|
|
|
|
* if (ret == -1)
|
|
|
|
* return -1;
|
|
|
|
* ...
|
|
|
|
*/
|
|
|
|
struct meshms_message_iterator {
|
2014-01-29 05:00:31 +00:00
|
|
|
// Public fields that remain fixed for the life of the iterator:
|
|
|
|
const sid_t *my_sid;
|
|
|
|
const sid_t *their_sid;
|
2014-01-28 07:37:40 +00:00
|
|
|
const rhizome_bid_t *my_ply_bid;
|
|
|
|
const rhizome_bid_t *their_ply_bid;
|
2014-01-29 05:00:31 +00:00
|
|
|
uint64_t latest_ack_offset; // offset in remote (their) ply of most recent ACK
|
|
|
|
uint64_t latest_ack_my_offset; // offset in my ply of most recent message ACKed by them
|
|
|
|
uint64_t read_offset; // offset in remote (their) ply of most recent message read by me
|
|
|
|
// The following public fields change per message:
|
2014-01-28 07:37:40 +00:00
|
|
|
enum meshms_which_ply { MY_PLY, THEIR_PLY } which_ply;
|
2014-01-29 05:00:31 +00:00
|
|
|
enum { MESSAGE_SENT, MESSAGE_RECEIVED, ACK_RECEIVED } type;
|
|
|
|
// For MESSAGE_SENT 'offset' is the byte position within the local ply
|
|
|
|
// (mine). For MESSAGE_RECEIVED and ACK_RECEIVED, it is the byte position
|
|
|
|
// within the remote ply (theirs).
|
2014-01-24 01:11:31 +00:00
|
|
|
uint64_t offset;
|
|
|
|
const char *text; // NUL terminated text of message
|
|
|
|
union {
|
2014-01-29 05:00:31 +00:00
|
|
|
bool_t delivered; // for MESSAGE_SENT
|
|
|
|
bool_t read; // for MESSAGE_RECEIVED
|
|
|
|
uint64_t ack_offset; // for ACK_RECEIVED
|
2014-01-24 01:11:31 +00:00
|
|
|
};
|
|
|
|
// Private implementation -- could change, so don't use them.
|
2014-01-29 05:00:31 +00:00
|
|
|
sid_t _my_sid;
|
2014-01-24 01:11:31 +00:00
|
|
|
struct meshms_conversations *_conv;
|
2014-01-29 05:00:31 +00:00
|
|
|
rhizome_manifest *_my_manifest;
|
|
|
|
rhizome_manifest *_their_manifest;
|
|
|
|
struct meshms_ply_read _my_reader;
|
|
|
|
struct meshms_ply_read _their_reader;
|
2014-01-24 01:11:31 +00:00
|
|
|
uint64_t _end_range;
|
|
|
|
bool_t _in_ack;
|
|
|
|
};
|
2014-01-29 05:00:31 +00:00
|
|
|
int meshms_message_iterator_open(struct meshms_message_iterator *, const sid_t *me, const sid_t *them);
|
2014-01-28 07:37:40 +00:00
|
|
|
int meshms_message_iterator_is_open(const struct meshms_message_iterator *);
|
2014-01-24 01:11:31 +00:00
|
|
|
void meshms_message_iterator_close(struct meshms_message_iterator *);
|
2014-01-28 06:28:44 +00:00
|
|
|
int meshms_message_iterator_prev(struct meshms_message_iterator *);
|
2014-01-24 01:11:31 +00:00
|
|
|
|
2014-01-22 06:11:22 +00:00
|
|
|
#endif // __SERVAL_DNA__MESHMS_H
|