-
Notifications
You must be signed in to change notification settings - Fork 10
/
handshake.js
183 lines (155 loc) · 7.5 KB
/
handshake.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
module.exports = { connect, init, sync, done }
// L
function connect(){
const { host, port } = this
delete this.timeouts.retry
if (this.status == 'connecting')
return deb('abandon - already connecting', this.id.grey, this.uuid.bgRed)
if (this.status != 'disconnected' && this.status != 'throttled')
return deb('abandon - unexpected status', this.status, this.id.grey, this.uuid.bgRed)
this.setStatus('connecting')
if (this.socket) {
deb('(L) connect', `${this.socket.localAddress}:${this.socket.localPort} → ${this.socket.remoteAddress}:${this.socket.remotePort}`.grey, this.uuid.bgRed)
const delay = jit(1)(this.peers.constants.connections.jitter*(1 + this.peers.lists.disconnected.length + this.peers.lists.connecting.length))
this.timeouts.connect = time(delay, d => {
if (this.status != 'connecting')
return deb('abandon - not connecting', this.id.grey, this.uuid.bgRed)
if (this.socket.attempted)
return deb('abandon - connect attempted', this.id.grey, this.uuid.bgRed)
this.timeouts.connection = time(this.peers.constants.connections.timeout, d => {
if (this.status == 'connecting') {
deb('connection timeout'.red, this.id.grey, this.uuid.bgRed)
this.fail()
}
})
this.socket.attempted = true
this.send({
laddress: this.peers.me ? this.peers.me.address : `client ${this.peers.group}`
, lpeers: this.peers.lists.connected.map(d => d.id)
, lpartitions: this.peers.me ? this.peers.cache.partitions.heads() : {}
, lid: this.peers.me ? this.peers.me.id : ''
}, this.peers.constants.commands.init)
})
}
else
this.socket = net.connect(port, host, this.connect.bind(this))
}
// R
function init(message) {
const { peer } = message
peer.socket.attempted = true
const { laddress, lpeers, lpartitions, lid } = message.value
, rpartitions = peer.peers.me ? peer.peers.cache.partitions.heads() : {}
, rpeers = peer.peers.lists.connected.map(d => d.id)
, raddress = peer.peers.me ? peer.peers.me.address : `client ${peer.peers.group}`
, dirpartitions = canFastForwardPartitions(lpartitions, rpartitions, laddress, raddress)
, dirpeers = true // TODO: canFastForwardPeers(lpeers, rpeers)
if (is.in(rpeers)(lid))
return deb('abandon - connected init', rid.grey, peer.uuid.bgRed)
if (laddress.startsWith('client') && !peer.peers.ready)
return deb('server (R) not ready'), peer.socket.destroy()
// deb('init rneed diff', rneed.length, peer.uuid.bgRed)
// deb('(R) init', dirpartitions, dirpeers, `${peer.peers.me.address} ← ${peer.socket.remoteAddress}:${peer.socket.remotePort}`.grey, peer.uuid.bgRed)
if (!dirpartitions || !dirpeers || (is.str(dirpartitions) && is.str(dirpeers) && dirpartitions !== dirpeers)) {
return deb('cannot fast forward ..?', dirpartitions, dirpeers, '\n', lpeers, '\n', rpeers)
} else if (dirpartitions == 'L → R') {
deb('(R) init L → R', rpartitions, peer.uuid.bgRed)
peer.send({ raddress, rpartitions }, peer.peers.constants.commands.sync)
} else {//if (dirpartitions == 'R → L') {
const rdiff = peer.peers.cache.partitions.diff(lpartitions)
deb('(R) init R → L', rdiff.length, peer.uuid.bgRed)
peer.send({ raddress, rpeers, rdiff }, peer.peers.constants.commands.sync) // TODO: JSON is wasteful here, we have the buffers
if (laddress.startsWith('client')) {
peer.setStatus('client')
peer.client = laddress.slice(7) || true
} else {
peer.setStatus('connected', laddress)
peer.server = true
}
}
}
// L
function sync(message) {
const { peer } = message
let { raddress, rpartitions, rpeers, rdiff } = message.value
, laddress = peer.peers.me ? peer.peers.me.address : `client ${peer.peers.group}`
if (raddress.startsWith('client') && !peer.peers.ready)
return deb('server (L) not ready'), peer.socket.destroy()
if (rdiff) {
deb('(L) sync R → L', rdiff.length, peer.uuid.bgRed)
if (!peer.peers.me) peer.peers.cache.reset()
rdiff.map(change => peer.peers.cache.partitions.append(createChange(change)))
rpeers
.filter(not(isConnected(peer.peers)))
.map(dot)
.map(split(':'))
.map(([host, port]) => peer.peers.create(host, port, undefined, true))
} else {
const ldiff = peer.peers.cache.partitions.diff(rpartitions)
, lpeers = peer.peers.lists.connected.map(d => d.id)
deb('(L) sync L → R', ldiff.length, peer.uuid.bgRed)
peer.send({ laddress, ldiff, lpeers }, peer.peers.constants.commands.done)
}
if (raddress.startsWith('client')) {
peer.setStatus('client')
peer.client = raddress.slice(7) || true
} else {
peer.setStatus('connected', raddress)
peer.server = true
}
}
// R
function done(message) {
const { peer } = message
, { laddress, ldiff, lpeers } = message.value
if (ldiff) {
deb('(R) done L → R', ldiff.length, peer.uuid.bgRed)
if (!peer.peers.me) peer.peers.cache.reset()
ldiff
.map(change => peer.peers.cache.partitions.append(createChange(change)))
lpeers
.filter(not(isConnected(peer.peers)))
.map(dot)
.map(split(':'))
.map(([host, port]) => peer.peers.create(host, port, undefined, true))
} else {
deb('(R) done R → L', peer.uuid.bgRed)
}
if (laddress.startsWith('client')) {
peer.setStatus('client')
peer.client = laddress.slice(7) || true
} else {
peer.setStatus('connected', laddress)
peer.server = true
}
}
const net = require('net')
, deb = require('./deb')('han'.bgWhite.black.bold)
, isConnected = peers => id => id in peers && (peers[id].status === 'connected')
, { key, keys, not, is, time, split, str, parse, flatten, extend } = require('utilise/pure')
, { jit } = require('./utils')
, dot = str => str.replace(/-/g, '.')
, Change = require('./messages/change')
, createChange = ({ type, key, value }) => extend(new Change(type, key, value))({ replay: true })
// function canFastForward(lpartitions, lpeers, rpartitions, rpeers) {
// const partitions = canFastForwardPartitions(lpartitions, rpartitions)
// , peers = canFastForwardPeers(lpeers, rpeers)
// return !partitions || !peers ? false
// : partitions === true && peers === true ? true
// : partitions === peers ? partitions
// : false
// }
// TODO: Allow bidirectional conflict-free merges
const canFastForwardPartitions = (lpartitions, rpartitions, laddress, raddress) =>
laddress.startsWith('client') ? 'R → L'
: raddress.startsWith('client') ? 'L → R'
: str(lpartitions) == str(rpartitions) ? true
: keys(rpartitions).every(id => id in lpartitions && lpartitions[id].head >= rpartitions[id].head) ? 'L → R'
: keys(lpartitions).every(id => id in rpartitions && rpartitions[id].head >= lpartitions[id].head) ? 'R → L'
: false
function canFastForwardPeers(lpeers, rpeers) {
return lpeers.every(is.in(rpeers)) && rpeers.every(is.in(lpeers)) ? true
: !lpeers.every(is.in(rpeers)) && rpeers.every(is.in(lpeers)) ? 'L → R'
: !rpeers.every(is.in(lpeers)) && lpeers.every(is.in(rpeers)) ? 'R → L'
: false
}