forked from aphilas/lamport-clocks-web-workers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
187 lines (153 loc) · 4.79 KB
/
main.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
const NO_OF_WORKERS = 3
/**
* Inline a web worker
* @param {function} fn Function with web worker code to run
* @returns {Worker}
*/
const createWorker = fn => {
const src = `;(${fn})()`
const blob = new Blob([src], {type: 'application/javascript'})
const url = URL.createObjectURL(blob)
return new Worker(url)
}
/**
* Web worker code
*
*/
const runWorker = _ => {
const NO_OF_WORKERS = 3
const EVENT_TYPES = { local: 0, send: 1, receive: 2, }
const state = {
sendPipes: [],
n: 0,
}
const nchoosek = (n,k) => { // SO
let res = 1
for(let i = 1; i <= k; i++){
res *= (n + 1 - i) / i
}
return res
}
/**
* Generate a random number between zero and n
* @param {number} max Max number (inclusive)
*/
const rand = max => Math.floor(Math.random() * (max + 1))
/**
* True with probability 1/k
* @param {number} k
*/
const toss = k => Math.random() < (1 / k)
const noOfCombinations = nchoosek(NO_OF_WORKERS, 2)
/**
* Send a message to a given worker
* @param {*} message Message to send
* @param {number} id Worker id
*/
const send = (message, id) => {
const pipe = state.sendPipes.find(pipe => pipe.id == id)
if (pipe) {
pipe.port.postMessage(message)
} else {
// console.log(`On worker ${state.id}`)
console.error(`Pipe to ${id} not found`)
// console.log(state.sendPipes)
}
}
const run = _ => {
for (let i = 0; i < 10; i++) {
setTimeout(_ => {
const isSend = toss(5)
state.n += 1
const { n, id } = state
if (!isSend) {
console.log(`Local event ${id}.${n}`)
self.postMessage({ id, n: state.n, type: EVENT_TYPES.local, })
} else {
const receiverPort = toss(2) ? state.sendPipes[0].id : state.sendPipes[1].id
send({ n, id }, receiverPort)
console.log(`Sending event ${id}.${n} to ${receiverPort}`)
self.postMessage({ id, n: state.n, type: EVENT_TYPES.send, })
}
}, Math.random() * 2)
}
/**
* Send message to all workers
*/
// for (const pipe of state.sendPipes) {
// send(`Hello worker ${pipe.id}, I am worker ${state.id}`, pipe.id)
// }
}
/**
* receive postMessage() events
*/
self.addEventListener('message', event => {
const { sendPort, receivePort, workerPair, id } = event.data
if (typeof id != 'undefined') {
state.id = id // set id, first message sent to worker
console.log(`Worker ${id} set up`)
}
if (typeof receivePort != 'undefined') {
receivePort.start()
// handle messages from other workers
receivePort.addEventListener('message', event => {
const { data: { id, n }} = event
state.n += 1
state.n = Math.max(state.n, n + 1)
console.log(`Received ${id}.${n} in ${state.id}`)
self.postMessage({ id: state.id, n: state.n, type: EVENT_TYPES.receive, source: id })
})
}
if (typeof sendPort != 'undefined') {
/**
* workerPair is array of two workers with a channel between them
*/
state.sendPipes.push({
id: workerPair.find(id => id != state.id),
port: sendPort,
})
// console.log(`On worker ${state.id}, worker pair: `)
// console.log(workerPair)
// console.log(`Saved: ${workerPair.find(id => id != state.id)}`)
// console.log(state)
if (state.sendPipes.length == noOfCombinations - 1) { // FIX
run()
}
}
}, false)
}
const workers = []
const events = []
/**
* Generate combinations of elements (not permutations)
* @param {[*]} arr Array to generate combinations from
* @returns {[[*]]} Array of combinations
*/
const combinations = arr => arr.flatMap((v, i) => arr.slice(i+1).map(w => [v, w]), 2) // SO
/**
* Create n web workers, set worker id as index i
*/
for (let i = 0; i < NO_OF_WORKERS; i++) {
// console.log(`Creating worker ${i}`)
const worker = createWorker(runWorker)
workers.push(worker)
worker.postMessage({ id: i })
// listen for messages from workers
worker.addEventListener('message', event => {
events.push(event.data)
if (events.length > 30) console.log(events) // FIX
})
}
/**
* Create all possible combinations of worker ids
*/
const workerCombinations = combinations(Array.from({ length: workers.length }, (v, i) => i))
/**
* Set up bidirectional channel between worker combinations
*/
for (const [i1, i2] of workerCombinations) {
const channel1 = new MessageChannel()
const channel2 = new MessageChannel()
workers[i1].postMessage({ receivePort: channel2.port1, sendPort: channel1.port2, workerPair: [i1, i2] }, [channel2.port1, channel1.port2])
workers[i2].postMessage({ receivePort: channel1.port1, sendPort: channel2.port2, workerPair: [i1, i2] }, [channel1.port1, channel2.port2])
}