-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
112 lines (98 loc) · 3 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
import "./style.css";
import Network from "./src/Network";
import { CANVAS_HEIGHT, CANVAS_WIDTH } from "./src/canvas";
window.isPaused = false;
window.setInterval = (fn, duration) => {
let isRunning = true;
let currentDuration;
let currentCancelFn = () => null;
let currentResolve;
const mainLoop = async () => {
while (isRunning) {
currentDuration = duration;
while (currentDuration > 0) {
if (window.isPaused) {
await new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 1000);
});
continue;
}
await new Promise((resolve) => {
currentResolve = resolve;
currentCancelFn = setTimeout(() => {
resolve();
}, 200);
});
currentDuration = currentDuration - 200;
}
if (isRunning) {
fn();
}
}
};
mainLoop();
return () => {
clearTimeout(currentCancelFn);
currentResolve();
isRunning = false;
};
};
window.clearInterval = (intervalFn) => {
intervalFn();
};
document.querySelector("#app").innerHTML = `
<div class="main">
<h1>Raft</h1>
<button id="pause">Play / Pause</button>
<div id='board' style="height: ${CANVAS_HEIGHT}px; width: ${CANVAS_WIDTH}px;">
<canvas id='network'
width=${CANVAS_WIDTH}
height=${CANVAS_HEIGHT}
style="width: ${CANVAS_WIDTH}px; height:${CANVAS_HEIGHT}px;box-sizing:border-box"
></canvas>
<canvas id='nodes' width=${CANVAS_WIDTH} height=${CANVAS_HEIGHT} /></canvas>
</div>
<div id="operations">
<button id="resetLeader">Reset Leader</button>
<button id="addNode">Add Node</button>
<button id="removeNode">Remove Node</button>
<button id="addData">Send Data</button>
</div>
</div>
<div id="footer">
<div> <a href="https://github.com/Pranav2612000/raft" target="_blank">View Source</a></div>
<div>
Made by <a href="https://vasusharma7.github.io" target="_blank">Vasu Sharma</a> and <a href="https://pranavjoglekarcodes.web.app" target="_blank">Pranav Joglekar</a></div>
</div>
`;
const NUM_NODES = 5; // Default number of nodes at the start
console.log("Initializing network...");
const network = new Network(NUM_NODES);
network.setLeader(0);
document.getElementById("resetLeader").addEventListener("click", () => {
console.log("RESET LEADER TO FOLLOWER");
network.resetLeader();
});
document.getElementById("addNode").addEventListener("click", () => {
network.addNode();
});
document.getElementById("removeNode").addEventListener("click", () => {
network.removeLastNode();
});
let currentMsg = 1;
document.getElementById("addData").addEventListener("click", () => {
if (!network.leader) {
console.log("NO LEADER DEFINED");
return;
}
network.nodes[network.leader - 1].receiveData(currentMsg++);
});
document.getElementById("pause").addEventListener("click", () => {
if (window.isPaused) {
window.isPaused = false;
} else {
window.isPaused = true;
}
});