-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
269 lines (226 loc) · 7.82 KB
/
index.html
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
<!Doctype HTML>
<html>
<head>
<title>Fountain code - LT</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
<link href="styles.css" rel="stylesheet">
<!--
<script src="../libs/d3.min.js"></script>
<script src="../libs/lodash.min.js"></script>
-->
<script src="./fountain.js"></script>
</head>
<body>
<section>
<h3>Fountain code demo - LT</h3>
<p>
A Fountain code demo using Luby transforms. The message is transformed into (theoretically) limitless
number of chucks. The message can then be assembled by collecting enough "droplets" that is
proportional to the original message size.
</p>
<p>
Each droplet starts a resolution process, which is coloured as follows:
<span style="padding:0 0.25rem; background:#39C">Inver by current packet</span>
<span style="padding:0 0.25rem; background:#F80">Infer previous packet</span>, and
<span style="padding:0 0.25rem; background:#0B4">Immediately known</span>
</p>
<span>
<input type="text" placeholder="Enter a short message" size=20 maxlength="20"> <button onclick="run()">Run</button>
</span>
<br><br>
<div style="min-height: 200px; display: flex; flex-direction: row">
<table></table>
<svg></svg>
</div>
<p>
<small>
* For simplicity I used simple-soliton distribution, this can cause skews and does not guarantee
completion. A robust-soliton distribution should work far better.
</small>
</p>
</section>
<a href="https://github.com/mwdchang/fountain-code"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a>
</body>
<script>
let iteration = 0;
let msg = '';
let colours = d3.scaleOrdinal(d3.schemeCategory20);
let translate = (x, y) => {
return 'translate(' + x + ',' + y + ')';
};
let fountain = new Fountain();
fountain.set('Hello world');
for (let i=0; i < 20; i++) {
fountain.decode();
}
function run() {
msg = d3.select('input').node().value;
iteration = 0;
fountain.set(msg);
for (let i=0; i < msg.length * 3; i++) {
fountain.decode();
iteration ++;
if (fountain.finished()) break;
}
if (fountain.finished()) {
console.log('finished in ' + iteration + ' iterations');
} else {
console.log('unable to complete');
}
render();
let graph = {};
graph.nodes = msg.split('').map( (c, idx) => {
return {
id: idx,
group: 0,
name: c,
isResolver: false,
isResolvee: false,
isImmediate: false
};
});
graph.links = [];
fountain.packetLog.forEach( (log, logIdx) => {
let resolve = log.resolve;
if (log.packet.idxList.length === 1) {
graph.nodes.filter(g => g.id === log.packet.idxList[0])[0].isImmediate = true;
}
resolve.forEach( r => {
graph.nodes.filter(g => g.id === r.fromIdx)[0].isResolver = true;
graph.nodes.filter(g => g.id === r.toIdx)[0].isResolvee = true;
graph.links.push({
source: r.fromIdx,
target: r.toIdx,
group: logIdx
});
});
});
d3.select('svg').style('display', 'block');
renderSVG(graph);
}
function render() {
d3.select('table').selectAll('*').remove();
// Headers
let header = d3.select('table').append('tr');
header.append('td').attr('colspan', msg.length).text('Packet');
header.append('td').classed('spacer', true);
header.append('td').attr('colspan', msg.length).text('Decoded');
header.append('td').classed('spacer', true);
// header.append('td').text('Resolve chain');
let rows = d3.select('table')
.selectAll('.row')
.data(fountain.packetLog)
.enter()
.append('tr')
.classed('row', true);
rows.each(function(d, packetIdx) {
let row = d3.select(this);
let decodedData = d.decodedData;
let resolve = d.resolve;
let packet = d.packet;
let fillColour = (idx) => {
for (let i=0; i < resolve.length; i++) {
if (resolve[i].fromIdx === idx) {
return '#39C';
}
if (resolve[i].toIdx === idx) {
return '#F80';
}
}
if (packet.idxList.length === 1 && packet.idxList[0] === idx) {
return '#0B4';
}
return null;
}
// Build input
for (let i=0; i < decodedData.length; i++) {
if (packet.idxList.indexOf(i) >= 0) {
let symbol = decodedData[i].c === null? '?' : decodedData[i].c;
row.append('td').text(symbol);
} else {
row.append('td');
}
}
// Empty
row.append('td').classed('spacer', true);
// Build decode
decodedData.forEach((d, idx) => {
let symbol = decodedData[idx].c === null? '' : decodedData[idx].c;
let td = row.append('td').text(symbol);
td.style('background', fillColour(idx));
});
// Empty
row.append('td').classed('spacer', true);
// Decode
/*
let str = '';
for (let i=0; i < resolve.length; i++) {
str += decodedData[resolve[i].fromIdx].c + ':' + decodedData[resolve[i].toIdx].c;
if (i < resolve.length-1) str += ', ';
}
row.append('td').style('max-width', '9rem').text(str);
*/
});
}
// Draw
function renderSVG(graph) {
d3.select('svg').selectAll('*').remove();
let svg = d3.select('svg').attr('width', 260).attr('height', 260);
let radius = 10;
let width = 260;
let height = 260;
let simulation = d3.forceSimulation()
.force('link', d3.forceLink().id(function(d) { return d.id; }))
.force('charge', d3.forceManyBody())
.force('center', d3.forceCenter(130, 130));
let link = svg.append('g')
.attr('class', 'links')
.selectAll('line')
.data(graph.links)
.enter().append('line')
.attr('stroke-width', 2);
let node = svg.append('g')
.attr('class', 'nodes')
.selectAll('circle')
.data(graph.nodes)
.enter().append('circle')
.attr('r', radius)
.style('stroke', function(d) {
if (d.isResolvee === true) {
return '#F80';
}
return 'none';
})
.attr('fill', function(d) {
if (d.isResolver) {
return '#39C';
} else if (d.isImmediate === true) {
return '#0B4';
}
return '#FFF';
});
let text = svg.append('g')
.selectAll('text')
.data(graph.nodes)
.enter().append('text')
.text( d => d.name);
simulation.nodes(graph.nodes).on("tick", ticked);
simulation.force("link").links(graph.links);
function ticked() {
/*
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
*/
node.attr("cx", function(d) { return d.x = Math.max(2*radius, Math.min(width - 2*radius, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(2*radius, Math.min(height - 2*radius, d.y)); });
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
text.attr("x", function(d) { return d.x = (Math.max(2*radius, Math.min(width - 2*radius, d.x)) - 10); })
.attr("y", function(d) { return d.y = (Math.max(2*radius, Math.min(height - 2*radius, d.y)) -10); });
}
}
</script>
</html>