Files: 86a0d059a6618c5b0e01793ac5a710c66c9b4dda / notes.txt
4722 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 | UPDATE |
46 | |
47 | all negative notes are shifted -1. to say you have sequence 1 |
48 | but do not want more on that feed, send -2. -1 now means |
49 | "I do not have and do not want this feed" |
50 | |
51 | requesting 0 means "I don't have anything from this feed, please send it" |
52 | and a -ve number means "I have this but stop sending, because I have a better source" |
53 | you can't send -0 because if you have a better source for nothing |
54 | that means you don't want it. which is what -1 means. |
55 | if you have seq 1, and someone else trys to give it to you, |
56 | |
57 | |
58 | */ |
59 | |
60 | |
61 | OUR_SEQ, THEIR_SEQ, THEM_RECV, US_RECV |
62 | |
63 | onRecieveValidMessage: //after we have processed the message. |
64 | if(OUR_SEQ > THEIR_SEQ && THEM_RECV) |
65 | send(msg) |
66 | else if(OUR_SEQ < THEIR_SEQ && US_RECV) |
67 | send({id: msg.author, seq: msg.sequence}) //OPTIONAL, don't have to do this every time |
68 | onReceiveMessage: |
69 | if(OUR_SEQ > msg.sequence) |
70 | send({id: msg.author, seq: - OUR_SEQ}) //tell them to stop sending. |
71 | //else, validate the message and continue. |
72 | onRecieveNote: |
73 | if(note.seq < 0 && THEM_RECV) { |
74 | THEM_RECV = false //they have asked us to stop sending this feed to them. |
75 | } |
76 | if(Math.abs(note.seq) > OUR_SEQ) { |
77 | US_RECV = true |
78 | send({id: note.id, seq: OUR_SEQ}) |
79 | } |
80 | |
81 | onBeginReplication: |
82 | for(var id in feeds) |
83 | send({id: id, seq: feed[id].seq}) |
84 | |
85 | //okay I feel satisfied that is the correct logic |
86 | //but how do I make this FSM pull? |
87 | |
88 | //I know, functional style state |
89 | |
90 | //For each peer, for each feed keep the state of: |
91 | |
92 | remote = {requested: Seq, sent: Seq, ready: msg|null, sending: bool, receiving: bool} //sending to, receiving from |
93 | |
94 | //also keep a map of local = {id: seq} |
95 | //then I think all events can be handled sync |
96 | |
97 | READ: |
98 | if ready!= null, the puller takes `ready` then sets it to `null` |
99 | if ready was a msg, this also triggers retriving the next msg, if this feed.sent < local[id] |
100 | if ready was a note, it's just sent without triggering anything. |
101 | |
102 | RECEIVE_MSG: //receive a message from this peer, before it's validated. |
103 | remote.requested = msg.sequence //always remember they are up to this sequence. |
104 | |
105 | if(msg.seq <= local[msg.author]) //if we already know about this message |
106 | if(remote.receiving) { |
107 | remote.ready = {id, seq: - local[id]} //tell them we do not need this feed. |
108 | remote.receiving = false |
109 | } |
110 | if(!remote.receiving) { |
111 | we have asked them to stop sending, but they havn't got the note yet. |
112 | anyway, remember they know this sequence. |
113 | remote.requested = msg.sequence |
114 | } |
115 | else if(remote.sending) { |
116 | this is an error, they should never send to us if we are sending. |
117 | if this ever happens it's a programmer error. maybe should tell them to top sending? |
118 | you might receive a |
119 | } |
120 | RECEIVE_NOTE: |
121 | if(remote.sending) { |
122 | if(note.seq < 0) //stop sending |
123 | remote.sending = false, remote.ready = null, remote.requested = abs(note.seq) |
124 | } |
125 | else if(!remote.receiving) { |
126 | if(abs(note.seq) > local[id]) //if this is the fastest peer for this feed, ask them to send. |
127 | |
128 | |
129 | } |
130 | |
131 | |
132 | |
133 |
Built with git-ssb-web