This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
158 lines (127 loc) · 3.45 KB
/
index.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
'use strict'
const http = require('http')
const url = require('url')
const express = require('express')
const serveStatic = require('serve-static')
const signalhub = require('signalhub')
const swarm = require('hybrid-swarm')
const brain = require('brain')
const {fetch} = require('fetch-ponyfill')()
const Node = require('./lib/node')
const BrainUnit = require('./lib/brain-unit')
const Executor = require('./lib/executor')
const trained = require('./trained')
const port = process.env.PORT || 5000
const topic = 'magi'
const names = ['melchior', 'balthasar', 'casper']
function allConnected(nodes) {
return Promise.all(nodes.map(node => {
return new Promise((resolve, reject) => {
node.on('connected', () => resolve())
})
}))
}
function createHttpServer(listUnits) {
const app = express()
app.get('/api/units', (req, res) => {
res.json(listUnits().map(unit => unit.id))
})
const signalhubServer = require('signalhub/server')()
app.use('/signalhub', (req, res) => {
signalhubServer.emit('request', req, res)
})
app.use(serveStatic('public'))
return new Promise((resolve, reject) => {
const server = app.listen(port, () => resolve(server))
})
}
function createSwarm(opts) {
opts = opts || {}
opts.signalhub = signalhub(topic, ['http://127.0.0.1:'+port+'/signalhub'])
const sw = swarm(opts)
if (sw.node) {
sw.node.join(topic)
}
return sw
}
function createBridge() {
return new Node(createSwarm({
//wrtc: require('wrtc')
wrtc: require('electron-webrtc')({headless: true})
}))
}
function createUnits() {
let i = 0
return names.map(name => {
const sw = createSwarm()
const net = new brain.NeuralNetwork()
net.fromJSON(trained[i])
const unit = new BrainUnit(sw, net, 'black')
i++
console.log('Created unit', name + '-' + i, unit.id)
return unit
})
}
function createNodes() {
const units = createUnits()
let nodes = units
try {
const bridge = createBridge()
console.log('Created bridge', bridge.id)
nodes = nodes.concat(bridge)
} catch (err) {
console.log('Cannot create bridge:', err)
}
return allConnected(nodes).then(() => units)
}
function fetchUnits() {
return fetch('http://127.0.0.1:'+port+'/api/units').then(res => res.json())
}
let promise = null
switch (process.argv[2]) {
case 'ask':
promise = fetchUnits().then(units => {
console.log('Got a list of %d units', units.length)
const sw = createSwarm()
const name = sw.node.id.toString('base64')
const exec = new Executor(sw, name, units)
return new Promise((resolve, reject) => {
exec.on('connected', () => resolve(exec))
})
}).then(exec => {
console.log('Node connected to network')
const question = {
id: '0',
module: exec.name,
data: { r: 0.95, g: 0.02, b: 0.43 }
}
exec.publish('question', question)
console.log('Started poll:', question)
return new Promise((resolve, reject) => {
exec.on('vote', (question, poll) => {
console.log('Got vote:', question, poll)
})
exec.on('result', (question, ok) => {
console.log('Poll result:', question, ok)
resolve({question, ok})
})
})
}).then(({question, ok}) => {
console.log(question, ok)
process.exit()
})
break
default:
let listUnits = () => null
promise = createHttpServer(() => listUnits()).then(server => {
console.log('http server listening on port %d', server.address().port)
return createNodes()
}).then(units => {
console.log('Network is fully connected')
listUnits = () => units
})
}
promise.catch(err => {
console.error(err)
process.exit(1)
})