forked from gritzko/swarm
/
Host.js
327 lines (291 loc) · 9.83 KB
/
Host.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
"use strict";
var env = require('./env');
var Spec = require('./Spec');
var Syncable = require('./Syncable');
var Pipe = require('./Pipe');
var SecondPreciseClock = require('./SecondPreciseClock');
/**
* Host is (normally) a singleton object registering/coordinating
* all the local Swarm objects, connecting them to appropriate
* external uplinks, maintaining clocks, etc.
* Host itself is not fully synchronized like a Model but still
* does some event gossiping with peer Hosts.
* @constructor
*/
function Host(id, ms, storage) {
this.objects = {};
this.sources = {};
this.storage = storage;
this._host = this; // :)
this._lstn = [','];
this._id = id;
this._server = /^swarm~.*/.test(id);
var clock_fn = env.clockType || SecondPreciseClock;
this.clock = new clock_fn(this._id, ms||0);
if (this.storage) {
this.sources[this._id] = this.storage;
this.storage._host = this;
}
delete this.objects[this.spec()];
if (!env.multihost) {
if (env.localhost) {
throw new Error('use multihost mode');
}
env.localhost = this;
}
}
Host.MAX_INT = 9007199254740992;
Host.MAX_SYNC_TIME = 60 * 60000; // 1 hour (milliseconds)
Host.HASH_POINTS = 3;
Host.hashDistance = function hashDistance(peer, obj) {
if ((obj).constructor !== Number) {
if (obj._id) {
obj = obj._id;
}
obj = env.hashfn(obj);
}
if (peer._id) {
peer = peer._id;
}
var dist = 4294967295;
for (var i = 0; i < Host.HASH_POINTS; i++) {
var hash = env.hashfn(peer._id + ':' + i);
dist = Math.min(dist, hash ^ obj);
}
return dist;
};
module.exports = Syncable.extend(Host, {
deliver: function (spec, val, repl) {
if (spec.type() !== 'Host') {
var typeid = spec.filter('/#');
var obj = this.get(typeid);
if (obj) {
// TODO seeTimestamp()
obj.deliver(spec, val, repl);
}
} else {
this._super.deliver.apply(this, arguments);
}
},
init: function (spec, val, repl) {
},
get: function (spec, callback) {
if (spec && spec.constructor === Function && spec.prototype._type) {
spec = '/' + spec.prototype._type;
}
spec = new Spec(spec);
var typeid = spec.filter('/#');
if (!typeid.has('/')) {
throw new Error('invalid spec');
}
var o = typeid.has('#') && this.objects[typeid];
if (!o) {
var t = Syncable.types[spec.type()];
if (!t) {
throw new Error('type unknown: ' + spec);
}
o = new t(typeid, undefined, this);
if (typeof(callback) === 'function') {
o.on('.init', callback);
}
}
return o;
},
addSource: function hostAddPeer(spec, peer) {
//FIXME when their time is off so tell them so
// if (false) { this.clockOffset; }
var old = this.sources[peer._id];
if (old) {
old.deliver(this.newEventSpec('off'), '', this);
}
this.sources[peer._id] = peer;
if (spec.op() === 'on') {
peer.deliver(this.newEventSpec('reon'), this.clock.ms(), this);
}
for (var sp in this.objects) {
this.objects[sp].checkUplink();
}
},
neutrals: {
/**
* Host forwards on() calls to local objects to support some
* shortcut notations, like
* host.on('/Mouse',callback)
* host.on('/Mouse.init',callback)
* host.on('/Mouse#Mickey',callback)
* host.on('/Mouse#Mickey.init',callback)
* host.on('/Mouse#Mickey!baseVersion',repl)
* host.on('/Mouse#Mickey!base.x',trackfn)
* The target object may not exist beforehand.
* Note that the specifier is actually the second 3sig parameter
* (value). The 1st (spec) reflects this /Host.on invocation only.
*/
on: function hostOn(spec, filter, lstn) {
if (!filter) {
// the subscriber needs "all the events"
return this.addSource(spec, lstn);
}
if (filter.constructor === Function && filter.id) {
filter = new Spec(filter.id, '/');
} else if (filter.constructor === String) {
filter = new Spec(filter, '.');
}
// either suscribe to this Host or to some other object
if (!filter.has('/') || filter.type() === 'Host') {
this._super._neutrals.on.call(this, spec, filter, lstn);
} else {
var objSpec = new Spec(filter);
if (!objSpec.has('#')) {
throw new Error('no id to listen');
}
objSpec = objSpec.set('.on').set(spec.version(), '!');
this.deliver(objSpec, filter, lstn);
}
},
reon: function hostReOn(spec, ms, host) {
if (spec.type() !== 'Host') {
throw new Error('Host.reon(/NotHost.reon)');
}
this.clock.adjustTime(ms);
this.addSource(spec, host);
},
off: function (spec, nothing, peer) {
peer.deliver(peer.spec().add(this.time(), '!').add('.reoff'), '', this);
this.removeSource(spec, peer);
},
reoff: function hostReOff(spec, nothing, peer) {
this.removeSource(spec, peer);
}
}, // neutrals
removeSource: function (spec, peer) {
if (spec.type() !== 'Host') {
throw new Error('Host.removeSource(/NoHost)');
}
if (this.sources[peer._id] !== peer) {
console.error('peer unknown', peer._id); //throw new Error
return;
}
delete this.sources[peer._id];
for (var sp in this.objects) {
var obj = this.objects[sp];
if (obj.getListenerIndex(peer, true) > -1) {
obj.off(sp, '', peer);
obj.checkUplink(sp);
}
}
},
/**
* Returns an unique Lamport timestamp on every invocation.
* Swarm employs 30bit integer Unix-like timestamps starting epoch at
* 1 Jan 2010. Timestamps are encoded as 5-char base64 tokens; in case
* several events are generated by the same process at the same second
* then sequence number is added so a timestamp may be more than 5
* chars. The id of the Host (+user~session) is appended to the ts.
*/
time: function () {
var ts = this.clock.issueTimestamp();
this._version = ts;
return ts;
},
/**
* Returns an array of sources (caches,storages,uplinks,peers)
* a given replica should be subscribed to. This default
* implementation uses a simple consistent hashing scheme.
* Note that a client may be connected to many servers
* (peers), so the uplink selection logic is shared.
* @param {Spec} spec some object specifier
* @returns {Array} array of currently available uplinks for specified object
*/
getSources: function (spec) {
var self = this,
uplinks = [],
mindist = 4294967295,
rePeer = /^swarm~/, // peers, not clients
target = env.hashfn(spec),
closestPeer = null;
if (rePeer.test(this._id)) {
mindist = Host.hashDistance(this._id, target);
closestPeer = this.storage;
} else {
uplinks.push(self.storage); // client-side cache
}
for (var id in this.sources) {
if (!rePeer.test(id)) {
continue;
}
var dist = Host.hashDistance(id, target);
if (dist < mindist) {
closestPeer = this.sources[id];
mindist = dist;
}
}
if (closestPeer) {
uplinks.push(closestPeer);
}
return uplinks;
},
isUplinked: function () {
for (var id in this.sources) {
if (/^swarm~.*/.test(id)) {
return true;
}
}
return false;
},
isServer: function () {
return this._server;
},
register: function (obj) {
var spec = obj.spec();
if (spec in this.objects) {
return this.objects[spec];
}
this.objects[spec] = obj;
return obj;
},
unregister: function (obj) {
var spec = obj.spec();
// TODO unsubscribe from the uplink - swarm-scale gc
if (spec in this.objects) {
delete this.objects[spec];
}
},
// waits for handshake from stream
accept: function (stream_or_url, pipe_env) {
new Pipe(this, stream_or_url, pipe_env);
},
// initiate handshake with peer
connect: function (stream_or_url, pipe_env) {
var pipe = new Pipe(this, stream_or_url, pipe_env);
pipe.deliver(new Spec('/Host#'+this._id+'!0.on'), '', this); //this.newEventSpec
return pipe;
},
disconnect: function (id) {
for (var peer_id in this.sources) {
if (id && peer_id != id) {
continue;
}
if (peer_id === this._id) {
// storage
continue;
}
var peer = this.sources[peer_id];
// normally, .off is sent by a downlink
peer.deliver(peer.spec().add(this.time(), '!').add('.off'));
}
},
close: function (cb) {
for(var id in this.sources) {
if (id===this._id) {continue;}
this.disconnect(id);
}
if (this.storage) {
this.storage.close(cb);
} else if (cb) {
cb();
}
},
checkUplink: function (spec) {
// TBD Host event relay + PEX
}
});