Skip to content

Commit 7df2da6

Browse files
committed
Introduce peer-lookup
1 parent 0ca47ac commit 7df2da6

22 files changed

+535
-249
lines changed

Diff for: app.js

+30-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1-
class AppRegistry {
2-
bootstrap () {
1+
const { EventEmitter } = require('events');
32

3+
class App extends EventEmitter {
4+
get accessor () {
5+
if (!this._accessor) {
6+
throw new Error('App does not have accessor, maybe not up yet');
7+
}
8+
9+
return this._accessor;
10+
}
11+
12+
set accessor (accessor) {
13+
this._accessor = accessor;
14+
}
15+
16+
addDiscovery (...args) {
17+
return this.accessor.addDiscovery(...args);
418
}
519

6-
debootstrap () {
20+
removeDiscovery (...args) {
21+
return this.accessor.removeDiscovery(...args);
22+
}
23+
24+
broadcast (...args) {
25+
return this.accessor.broadcast(...args);
26+
}
27+
28+
send (...args) {
29+
return this.accessor.send(...args);
30+
}
731

32+
getPeerDefinition (...args) {
33+
return this.accessor.getPeerDefinition(...args);
834
}
935
}
1036

11-
module.exports = { AppRegistry };
37+
module.exports = { App };

Diff for: app/peer-lookup/discovery.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const debug = require('debug')('swarm:discovery:peer-lookup');
2+
const { Discovery } = require('../../discovery');
3+
4+
let id = 0;
5+
6+
class PeerLookupDiscovery extends Discovery {
7+
constructor (app) {
8+
super();
9+
10+
this.id = id++;
11+
this.app = app;
12+
}
13+
14+
lookup (address) {
15+
return new Promise(async resolve => {
16+
let definition = await this.app.doLookup(address);
17+
if (definition) {
18+
resolve(definition);
19+
}
20+
});
21+
}
22+
}
23+
24+
module.exports = PeerLookupDiscovery;

Diff for: app/peer-lookup/index.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const debug = require('debug')('swarm:app:peer-lookup');
2+
const Discovery = require('./discovery');
3+
const { App } = require('../../app');
4+
5+
class PeerLookup extends App {
6+
constructor () {
7+
super();
8+
9+
this.cache = {};
10+
this.discovery = new Discovery(this);
11+
}
12+
13+
get name () {
14+
return 'peer-lookup';
15+
}
16+
17+
async onMessage ({ address, command, payload }) {
18+
switch (command) {
19+
case 'q':
20+
let query = payload;
21+
// debug(this.accessor.address, address, 'onMessage:q?', query);
22+
// see if swarm already have peer with address
23+
let definition = this.getPeerDefinition(query);
24+
if (!definition) {
25+
// debug(this.accessor.address, address, 'onMessage:q next');
26+
// lookup more to peers
27+
definition = await this.doLookup(query);
28+
}
29+
30+
if (!definition) {
31+
return;
32+
}
33+
34+
// debug(this.accessor.address, address, 'onMessage:q found');
35+
// found peer
36+
if (this.cache[query]) {
37+
this.cache[query].definition = definition;
38+
this.cache[query].resolve();
39+
}
40+
41+
// debug('send answer');
42+
this.send({ address, command: 'a', payload: definition });
43+
break;
44+
case 'a': {
45+
// debug(this.accessor.address, address, 'onMessage:a');
46+
let query = payload.address;
47+
if (this.cache[query]) {
48+
this.cache[query].definition = payload;
49+
this.cache[query].resolve();
50+
}
51+
break;
52+
}
53+
}
54+
}
55+
56+
async doLookup (address) {
57+
if (address in this.cache === false) {
58+
await new Promise((resolve, reject) => {
59+
this.cache[address] = { definition: null, resolve, reject };
60+
this.broadcast({ command: 'q', payload: address });
61+
});
62+
}
63+
64+
return this.cache[address].definition;
65+
}
66+
67+
up () {
68+
debug('peer-lookup up');
69+
this.addDiscovery(this.discovery);
70+
}
71+
72+
down () {
73+
debug('peer-lookup down');
74+
this.removeDiscovery(this.discovery);
75+
}
76+
}
77+
78+
module.exports = PeerLookup;

Diff for: channel/channel.js renamed to channel.js

-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
const { EventEmitter } = require('events');
22

3-
/**
4-
* Channel must implements these methods:
5-
* - up
6-
* - down
7-
* - connect
8-
*/
93
class Channel extends EventEmitter {
104

115
}

Diff for: channel/index.js

-3
This file was deleted.

Diff for: channel/adapters/tcp.js renamed to channel/tcp.js

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const net = require('net');
2-
const { Channel } = require('../index');
32
const { URL } = require('url');
4-
const debug = require('debug')('swarm:channel:adapters:tcp');
3+
const debug = require('debug')('swarm:channel:tcp');
4+
const { Channel } = require('../channel');
55

66
class Tcp extends Channel {
77
constructor ({ port = 1212, host = '0.0.0.0' } = {}) {
@@ -11,14 +11,10 @@ class Tcp extends Channel {
1111
this.port = port;
1212
}
1313

14-
get kind () {
14+
get proto () {
1515
return 'tcp';
1616
}
1717

18-
get listening () {
19-
return this.server ? this.server.listening : false;
20-
}
21-
2218
async up () {
2319
debug(`Getting up at ${this.host}:${this.port} ...`);
2420
this.server = net.createServer(this._onListening.bind(this));
@@ -34,10 +30,8 @@ class Tcp extends Channel {
3430

3531
_onListening (socket) {
3632
let { address, port } = socket.address();
37-
let url = `tcp://${address}:${port}`;
33+
let url = `${this.proto}://${address}:${port}`;
3834
this.emit('incoming', { url, socket });
39-
40-
debug(`Incoming from ${url}`);
4135
}
4236

4337
connect (url) {
@@ -60,8 +54,8 @@ class Tcp extends Channel {
6054
});
6155
}
6256

63-
ipUrls (ips) {
64-
return ips.map(ip => `tcp:/${ip}:${this.port}`);
57+
formatUrl (ip, port) {
58+
return `${this.proto}://${ip}:${this.port}`;
6559
}
6660
}
6761

Diff for: discovery/discovery.js renamed to discovery.js

File renamed without changes.

Diff for: discovery/adapters/boot.js

-15
This file was deleted.

Diff for: discovery/index.js

-4
This file was deleted.
File renamed without changes.

Diff for: discovery/registry.js

-51
This file was deleted.

Diff for: lib/accessor.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = function accessor (app, swarm) {
2+
return {
3+
get address () {
4+
return swarm.address;
5+
},
6+
7+
addDiscovery (method) {
8+
return swarm.addDiscovery(method);
9+
},
10+
11+
removeDiscovery (method) {
12+
return swarm.removeDiscovery(method);
13+
},
14+
15+
broadcast ({ command, payload }) {
16+
return swarm.broadcast({ app: app.name, command, payload });
17+
},
18+
19+
send ({ address, command, payload }) {
20+
return swarm.send({ address, app: app.name, command, payload });
21+
},
22+
23+
getPeerDefinition (address) {
24+
let peer = swarm.get(address);
25+
if (peer) {
26+
return peer.dump();
27+
}
28+
},
29+
};
30+
};

Diff for: lib/assert-module.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const assert = require('assert');
2+
const { Channel } = require('../channel');
3+
const { Discovery } = require('../discovery');
4+
const { App } = require('../app');
5+
6+
function channel (channel) {
7+
assert(channel instanceof Channel, 'Channel must be instance of Channel');
8+
assert('proto' in channel, 'Channel must define proto');
9+
assert('up' in channel, 'Channel must implement up');
10+
assert('down' in channel, 'Channel must implement down');
11+
assert('connect' in channel, 'Channel must implement connect');
12+
assert('formatUrl' in channel, 'Channel must implement formatUrl');
13+
}
14+
15+
function app (app) {
16+
assert(app instanceof App, 'App must be instance of App');
17+
assert('name' in app, 'App must define name');
18+
assert('up' in app, 'App must implement up');
19+
assert('down' in app, 'App must implement down');
20+
assert('onMessage' in app, 'App must implement onMessage');
21+
}
22+
23+
function discovery (method) {
24+
assert(method instanceof Discovery, 'Discovery must be instance of Discovery');
25+
assert('lookup' in method, 'Discovery must implement lookup');
26+
}
27+
28+
module.exports = { channel, app, discovery };

Diff for: lib/sleep.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module.exports = function sleep (ms) {
1+
module.exports = function sleep (ms = 0) {
22
return new Promise(resolve => setTimeout(resolve, ms));
33
};

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
"js-sha3": "^0.6.1",
2929
"minimist": "^1.2.0",
3030
"node-fetch": "^1.7.1",
31-
"node-rsa": "^0.4.2"
31+
"node-rsa": "^0.4.2",
32+
"split": "^1.0.0",
33+
"uuid": "^3.1.0"
3234
}
3335
}

0 commit comments

Comments
 (0)