-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
394 lines (354 loc) · 11.4 KB
/
client.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
window.mything = function () {
/*global window */
/*global WebSocket */
var build_colors = function () {
var colors = [];
console.log('Inside set_colors', pieces);
for (var i = 0; i < 30; i++ ) {
var color;
if (i < 10) {
color = color_scale_10(i);
console.log(color, i);
colors.push(color);
console.log(colors);
} else {
color = color_scale_20(i - 10);
console.log(color, i);
colors.push(color);
console.log(colors);
}
}
return colors;
};
var BAR_ATTR = {
height : 16,
offset : 10
};
var PIECE_ATTR = {
height : 15,
width : 15,
duration : 500
};
var PEER_ATTR = {
height: 30,
width: 30,
margin: 2
};
var peers_div_bottom_margin = 5
var peers = {};
var peers_div;
var pieces = {};
var files = [];
var partial_message = "";
var want_file_pos = [];
var xScale;
var div_height = 50;
var padding = 1;
var text_height = 10;
var parent_svg;
var piece_y = Math.floor(BAR_ATTR.height / 2) - Math.floor(PIECE_ATTR.height / 2);
var mouse_pointer_width = 15;
var POPOVER_ATTR = {
height: div_height - (2 * text_height),
width: (div_height - (2 * text_height)) * 4,
text_height: 10,
internal_padding: 2,
duration: 300,
opacity: 0.85,
box_offset: padding,
peer_square_size: 20,
text_offset: 4
};
var color_scale_10 = d3.scale.category10();
var color_scale_20 = d3.scale.category20c();
var colors = build_colors();
var parser = document.createElement('a');
parser.href = document.location.origin;
var ws_address = "ws://" + parser.hostname + ":8001";
window.THEWEBSOCKET = new WebSocket(ws_address);
window.onbeforeunload = function () { window.THEWEBSOCKET.close(); };
var build_model = function (init_dict) {
want_file_pos = init_dict.want_file_pos;
want_file_pos.forEach( function(want_index, i) {
files[i] = {};
var head_and_tail = init_dict.heads_and_tails[i];
files[i].path = init_dict.files[want_index].path;
files[i].relevant = [];
for (var j = head_and_tail[0]; j <= head_and_tail[1]; j++) {
files[i].relevant.push(j);
}
});
draw_files();
};
var draw_files = function () {
peers_div = d3.select("body")
.append("div")
.attr("class", "peers_div")
.style("margin-bottom", peers_div_bottom_margin);
var divs = d3
.select("body")
.selectAll("div.file")
.data(files)
.enter()
.append("div")
.attr("class", "file")
.style("height", div_height);
var texts = divs
.append("text")
.text(function (d) { return d.path.join('/'); })
.style("font-size", text_height)
.style("font-family", "sans-serif")
.style("color", "#09C53B")
.attr("y", 30);
var svgs = divs
.append("svg")
.attr("class", "file");
var vis_bars = svgs
.append("g")
.attr("class", "bar")
.attr("transform", "translate(0," + BAR_ATTR.offset + ")")
.attr("id", function (d, i) {
console.log('inside vis_bars', pieces);
return "#bar" + d.ip;
})
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", function (d) {
return PIECE_ATTR.width * d.relevant.length + padding;
})
.attr("height", BAR_ATTR.height)
.attr("fill", "white")
.attr("stroke", "gray")
.attr("stroke-width", 1)
.attr("opacity", 1)
.attr("id", function (d, i) {return ("#bar" + i); });
var vis_pieces = svgs.select("g.bar")
.selectAll("rect.piece")
.data(function (d) { return d.relevant; })
.enter()
.append("rect")
.attr("class", "piece")
.attr("class", function (d) {
return ("piece" + d); })
.attr("x", function (d, i) { return padding + i * PIECE_ATTR.width; })
.attr("y", (BAR_ATTR.height / 2) - (PIECE_ATTR.height / 2))
.attr("height", PIECE_ATTR.height - padding)
.attr("width", PIECE_ATTR.width - padding)
.attr("opacity", 0)
var piece_click = vis_pieces.on("click", function (d, i, j) {
var piece_color = this.getAttribute("fill");
var piece_id = this.getAttribute("class").replace("piece", "");
var loc_dict = peers[pieces[piece_id]].location
var xPosition = parseFloat(d3.select(this).attr("x"));
var yPosition = parseFloat(d3.select(this).attr("y"));
var tooltip = d3.select(svgs[0][j])
.append("g")
.attr("class", "tooltip")
.attr("transform", "translate(" + (xPosition + mouse_pointer_width) + "," + yPosition + ")")
.append("rect")
.attr("class", "popover_main")
.attr("x", 0)
.attr("y", 0)
.attr("width", function () {
return POPOVER_ATTR.width;
})
.attr("height", POPOVER_ATTR.height)
.attr("fill", "white")
.attr("stroke", "black")
.attr("stroke-width", 1)
.attr("opacity", 0)
.transition()
.duration(POPOVER_ATTR.duration)
.attr("opacity", POPOVER_ATTR.opacity);
var piece_text = d3.select("g.tooltip")
.append("text")
.attr("x", POPOVER_ATTR.internal_padding + POPOVER_ATTR.peer_square_size + POPOVER_ATTR.text_offset)
.attr("y", POPOVER_ATTR.text_height + POPOVER_ATTR.internal_padding)
.text('piece ' + piece_id)
.attr("font-size", POPOVER_ATTR.text_height)
.attr("opacity", 0)
.transition()
.duration(POPOVER_ATTR.duration)
.attr("opacity", POPOVER_ATTR.opacity);
var peer_ip_text = d3.select("g.tooltip")
.append("text")
.attr("x", function () {
return POPOVER_ATTR.internal_padding +
POPOVER_ATTR.peer_square_size + POPOVER_ATTR.text_offset;
})
.attr("y", 2 * (POPOVER_ATTR.text_height + POPOVER_ATTR.internal_padding))
.text(pieces[piece_id])
.attr("font-size", POPOVER_ATTR.text_height)
.attr("opacity", 0)
.transition()
.duration(POPOVER_ATTR.duration)
.attr("opacity", POPOVER_ATTR.opacity);
var peer_location_box = d3.select("g.tooltip")
.append("g")
.attr("class", "location_text")
.attr("transform", function() {
return "translate(" + (peer_ip_text[0][0].getComputedTextLength() + (3 * POPOVER_ATTR.internal_padding) + POPOVER_ATTR.peer_square_size) + ",0)";
});
var country_text = peer_location_box.append("text")
.attr("x", function () {
return POPOVER_ATTR.internal_padding + POPOVER_ATTR.text_offset;
})
.attr("y", POPOVER_ATTR.text_height + POPOVER_ATTR.internal_padding)
// Location is in the peer's location property
.text(loc_dict.country_name)
.attr("font-size", POPOVER_ATTR.text_height)
.attr("opacity", 0)
.transition()
.duration(POPOVER_ATTR.duration)
.attr("opacity", POPOVER_ATTR.opacity);
var region_text = peer_location_box.append("text");
if (loc_dict.region_name !== '' || loc_dict.city !== '') {
region_text.attr("x", function () {
return POPOVER_ATTR.internal_padding + POPOVER_ATTR.text_offset;
})
.attr("y", 2 * (POPOVER_ATTR.text_height + POPOVER_ATTR.internal_padding))
// Location is in the peer's location property
.text( function () {
if (loc_dict.city !== '' && loc_dict.region_name !== '') {
return loc_dict.city + ', ' + loc_dict.region_name;
} else if (loc_dict.region_name !== ''){
return loc_dict.region_name;
} else if (loc_dict.city !== '') {
return loc_dict.city;
};
return loc_string;
})
.attr("font-size", POPOVER_ATTR.text_height)
.attr("opacity", 0)
.transition()
.duration(POPOVER_ATTR.duration)
.attr("opacity", POPOVER_ATTR.opacity);
}
var popover_piece_square = d3.select("g.tooltip")
.append("rect")
.attr("class", "piece_square")
.attr("fill", piece_color)
.attr("x", POPOVER_ATTR.internal_padding)
.attr("y", (POPOVER_ATTR.height / 2) - (POPOVER_ATTR.peer_square_size / 2))
.attr("width", POPOVER_ATTR.peer_square_size)
.attr("height", POPOVER_ATTR.peer_square_size)
.attr("opacity", 0)
.transition()
.duration(POPOVER_ATTR.duration)
.attr("opacity", 1);
var adjust_box_size = d3.select("rect.popover_main")
.attr("width", function () {
var total_width = POPOVER_ATTR.internal_padding + POPOVER_ATTR.peer_square_size + (3 * POPOVER_ATTR.internal_padding) +
peer_ip_text[0][0].getComputedTextLength() +
d3.max([country_text[0][0].getComputedTextLength(),
region_text[0][0].getComputedTextLength()]) +
(4 * POPOVER_ATTR.internal_padding);
return total_width;
});
});
var mouseout = vis_pieces.on("mouseout", function ()
{
d3.selectAll("g.tooltip")
.remove();
})
};
var vis_request = function (req_dict) {
};
var get_color = function (ip) {
return peers[ip]["color"];
};
var vis_write = function (write_dict) {
var piece_index = write_dict.piece_index;
ip = write_dict.peer[0];
pieces[piece_index] = ip;
peers[ip].pieces.push(piece_index);
d3.selectAll("rect.piece" + piece_index)
.attr("fill", get_color(ip))
.transition()
.duration(PIECE_ATTR.duration)
.attr("opacity", 1);
};
var build_peer = function (peer_dict) {
var peers_list = [];
var ip = peer_dict.address[0];
var color = colors.shift();
var location = peer_dict.location;
peers[ip] = {"color": color, "location": location, "pieces": []};
for(var key in peers) {
peers_list.push(key);
}
draw_peer_legend(peers_list);
};
var draw_peer_legend = function(peers_list) {
try {
var peer_divs = peers_div
.selectAll("div.peer")
.data(peers_list)
.enter()
.append("div")
.attr("width", PEER_ATTR.width)
.attr("height", PEER_ATTR.height)
.style("width", PEER_ATTR.width)
.style("height", PEER_ATTR.height)
.style("display", "inline-block")
.style("padding", PEER_ATTR.margin)
.attr("class", "peer");
peer_svgs = peer_divs
.append("svg")
.attr("class", "peer_box")
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", PEER_ATTR.width)
.attr("height", PEER_ATTR.height)
.style("width", PEER_ATTR.width)
.style("height", PEER_ATTR.height)
.attr("fill", function (d) {
return peers[d].color;
});
} catch (e) {
console.log(e);
};
};
window.THEWEBSOCKET.onmessage = function (chunk) {
var messages = (partial_message + chunk.data).split("\r\n\r\n");
partial_message = "";
messages.forEach(function (message) {
try {
var meat = JSON.parse(message);
} catch (e) {
if (message[0] === "") {
return;
} else if (message[0] === "{") {
partial_message = message;
console.log("partial message: " + partial_message);
console.log("message: " + message);
} else {
console.log("Parsing failed: " + messages);
console.log(e);
}
}
if (meat.kind === "init") {
build_model(meat);
} else if (meat.kind === "request") {
vis_request(meat);
} else if (meat.kind === "piece") {
vis_write(meat);
} else if (meat.kind === "activate") {
console.log("Activate");
build_peer(meat);
} else {
throw "Data kind invalid";
}
}
);
};
var send_piece = function (piece_no) {
var template = {"peer": ["46.165.240.14", 50035], "kind": "piece", "piece_index": NaN};
template.piece_index = Math.floor(Math.random() * 50);
window.THEWEBSOCKET.send(JSON.stringify(template));
};
window.addEventListener("click", send_piece);
};
mything();