Files: d7274a7dbfa5c6ea2e60d2a7e9e592973469a894 / notes.txt
4204 bytesRaw
1 | /* |
2 | sending: |
3 | +n+1 |
4 | they are ahead, ask to receive. |
5 | this might happen on initial connections, if they think you might be further ahead. |
6 | they have asked us to send, but they'll probably change their mind. +send +receive |
7 | +n |
8 | we are in sync, and neither knows who is closer to the source. |
9 | since the actual receiver does not. +send |
10 | +n-1 |
11 | they are behind us, get the next item for them. +send !get |
12 | |
13 | -(n+1) |
14 | they are ahead of us, stop sending. |
15 | ask to receive from them (but they don't want anything from us) -send +receive |
16 | -n |
17 | stop sending, we know they are in sync, so no need to send notes. -send |
18 | -(n-1) |
19 | stop sending, but send notes sometimes. -send |
20 | |
21 | receiving: |
22 | +n+1 |
23 | they ask to receive from us, but we are not up to there. |
24 | *enable send* I guess, but don't expect to send. +send |
25 | +n |
26 | they ask to receive from us, but we are already in sync. |
27 | send a note to let the know we are in sync, if we havn't already. |
28 | +(n-1) |
29 | they ask to receive, but are behind us. +send |
30 | |
31 | -(n+1) |
32 | expect them still to send to us, though. -send |
33 | -(n) |
34 | they just let us know we are in sync |
35 | -(n-1) |
36 | they are behind us, but are still getting messages faster than they would from us. |
37 | get ready to send a note about where we are up to. |
38 | |
39 | if the absolute value is greater, and we are not already receiving, ask to receive. |
40 | else if positive, turn on send (and get ready if we have something) |
41 | else if negative, turn off send. |
42 | */ |
43 | |
44 | |
45 | OUR_SEQ, THEIR_SEQ, THEM_RECV, US_RECV |
46 | |
47 | onRecieveValidMessage: //after we have processed the message. |
48 | if(OUR_SEQ > THEIR_SEQ && THEM_RECV) |
49 | send(msg) |
50 | else if(OUR_SEQ < THEIR_SEQ && US_RECV) |
51 | send({id: msg.author, seq: msg.sequence}) //OPTIONAL, don't have to do this every time |
52 | onReceiveMessage: |
53 | if(OUR_SEQ > msg.sequence) |
54 | send({id: msg.author, seq: - OUR_SEQ}) //tell them to stop sending. |
55 | //else, validate the message and continue. |
56 | onRecieveNote: |
57 | if(note.seq < 0 && THEM_RECV) { |
58 | THEM_RECV = false //they have asked us to stop sending this feed to them. |
59 | } |
60 | if(Math.abs(note.seq) > OUR_SEQ) { |
61 | US_RECV = true |
62 | send({id: note.id, seq: OUR_SEQ}) |
63 | } |
64 | |
65 | onBeginReplication: |
66 | for(var id in feeds) |
67 | send({id: id, seq: feed[id].seq}) |
68 | |
69 | //okay I feel satisfied that is the correct logic |
70 | //but how do I make this FSM pull? |
71 | |
72 | //I know, functional style state |
73 | |
74 | //For each peer, for each feed keep the state of: |
75 | |
76 | remote = {requested: Seq, sent: Seq, ready: msg|null, sending: bool, receiving: bool} //sending to, receiving from |
77 | |
78 | //also keep a map of local = {id: seq} |
79 | //then I think all events can be handled sync |
80 | |
81 | READ: |
82 | if ready!= null, the puller takes `ready` then sets it to `null` |
83 | if ready was a msg, this also triggers retriving the next msg, if this feed.sent < local[id] |
84 | if ready was a note, it's just sent without triggering anything. |
85 | |
86 | RECEIVE_MSG: //receive a message from this peer, before it's validated. |
87 | remote.requested = msg.sequence //always remember they are up to this sequence. |
88 | |
89 | if(msg.seq <= local[msg.author]) //if we already know about this message |
90 | if(remote.receiving) { |
91 | remote.ready = {id, seq: - local[id]} //tell them we do not need this feed. |
92 | remote.receiving = false |
93 | } |
94 | if(!remote.receiving) { |
95 | we have asked them to stop sending, but they havn't got the note yet. |
96 | anyway, remember they know this sequence. |
97 | remote.requested = msg.sequence |
98 | } |
99 | else if(remote.sending) { |
100 | this is an error, they should never send to us if we are sending. |
101 | if this ever happens it's a programmer error. maybe should tell them to top sending? |
102 | you might receive a |
103 | } |
104 | RECEIVE_NOTE: |
105 | if(remote.sending) { |
106 | if(note.seq < 0) //stop sending |
107 | remote.sending = false, remote.ready = null, remote.requested = abs(note.seq) |
108 | } |
109 | else if(!remote.receiving) { |
110 | if(abs(note.seq) > local[id]) //if this is the fastest peer for this feed, ask them to send. |
111 | |
112 | |
113 | } |
114 | |
115 | |
116 | |
117 |
Built with git-ssb-web