-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
274 lines (235 loc) · 8.46 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
270
271
272
273
274
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>LCR Visualization</title>
<script src="raphael.min.js"></script>
<style>
pre {
white-space: pre-wrap; /* Since CSS 2.1 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
#edgeMatrix {
width: 600px;
height: 1000px;
}
</style>
</head>
<body>
<pre id="edgeMatrix"></pre>
<svg width="960" height="500">
</svg>
<script type="text/javascript">
// util methods
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement)
};
// Raphael JS Visualization
var paper = Raphael(0, 0, window.screen.availWidth, window.screen.availHeight)
// define the graph and nodes
function Graph(number) {
// network of processes containing nodes and the edges between them
this.nodes = []
this.edges = []
// initialize the nodes and the edges
// create an array of numbers in random order
// use this array for creating nodes and adding edges
var arr = []
for (var i = 0 ; i < number ; i++) arr.push(i)
shuffle(arr)
var arr2 = arr.slice(0).reverse()
for(var i = 0 ; i < number ; i++) {
var id1 = arr.pop()
// console.log(id1)
var node = new Node({id: id1})
this.nodes.push(node)
}
this.edges = new Array(number)
for(var i = 0 ; i < this.edges.length ; i++) {
this.edges[i] = new Array(number)
for(var j = 0 ; j < this.edges[i].length ; j++) {
this.edges[i][j] = 0
}
}
// add all the links in the ring
for(var i = 0 ; i < numberOfNodes - 1 ; i++) {
var find1 = this.nodes.find(node => node.id == arr2[i])
var find2 = this.nodes.find(node => node.id == arr2[i+1])
this.addEdge(find1, find2)
}
var find1 = this.nodes.find(node => node.id == arr2[numberOfNodes - 1])
var find2 = this.nodes.find(node => node.id == arr2[0])
this.addEdge(find1, find2)
}
Graph.prototype.getNode = function(id) {
return this.nodes.find(node => node.id == id)
}
Graph.prototype.addEdge = function(nodeA, nodeB) {
// get the id number if sent the node itself
if (typeof(nodeA) == 'object') nodeA = nodeA.id
if (typeof(nodeB) == 'object') nodeB = nodeB.id
// add an empty message between the 2 nodes
this.edges[nodeA][nodeB] = {}
}
Graph.prototype.clearEdges = function() {
for(var i = 0 ; i < this.edges.length ; i++) {
for(var j = 0 ; j < this.edges[i].length ; j++) {
if (this.edges[i][j] != 0) {
this.edges[i][j] = {}
}
}
}
}
function Node(args) {
this.leader = null
this.id = args.id
this.max_id = this.id
}
// places a message for a node's neighbor in their channel
Graph.prototype.SendMessage = function(nodeA, nodeB, msg) {
// get the id number if sent the node itself
if (typeof(nodeA) == 'object') nodeA = nodeA.id
if (typeof(nodeB) == 'object') nodeB = nodeB.id
this.edges[nodeA][nodeB] = Object.assign({}, msg)
}
Graph.prototype.isLeaderElected = function() {
for(var i = 0 ; i < this.nodes.length ; i++) {
if (this.nodes[i].leader != null) {
return true
}
}
return false
}
// get the clockwise neighbor for a node
Graph.prototype.getNeighbor = function(id) {
for(var i = 0 ; i < numberOfNodes ; i++) {
if (g.edges[id][i] != 0) {
return i
}
}
return -1
}
Node.prototype.msgFunc = function() {
var msg = {id: this.max_id}
var neighbor = g.getNeighbor(this.id, 1)
if (neighbor == -1) return
g.edges[this.id][neighbor] = msg
// increment message progress
messagesText.attr('text', "No of Messages: " + ++messagesCount)
// visualize the message
var idx = raphaelCircles.find(circle => circle.id == this.id)
if (idx) {
idx.msgText.attr('text', 'sending ' + this.max_id)
}
}
Node.prototype.transitionFunc = function() {
console.log('calling lcr transition for node ' + this.id + ', cur maxId = ' + this.max_id)
var msg
for(var i = 0 ; i < g.edges.length ; i++) {
for(var j = 0 ; j < g.edges[i].length ; j++) {
if (j == this.id && g.edges[i][j] != 0) {
msg = g.edges[i][j]
break
}
}
}
if (msg == undefined) return
if (msg.id > this.max_id) {
console.log('discovered new max id ' + msg.id + '! For node ' + this.id + ' prev maxid was ' + this.max_id)
this.max_id = msg.id
msg_send = {id: msg.id}
} else if (msg.id == this.id) {
console.log('got my own message back! I must be leader')
this.leader = this.id
} else {
// increment discarded messages count
discardedMessagesText.attr('text', "Discarded Messages: " + ++discardedMessagesCount)
}
}
var rounds = 0
var messagesCount = 0
var discardedMessagesCount = 0
var roundsText = paper.text(window.screen.width/2, 25, "Starting in 3..2..1..").attr({font: '20px Arial'})
var messagesText = paper.text(window.screen.width/2, 50, "Messages Sent: " + messagesCount).attr({font: '16px Arial'})
var discardedMessagesText = paper.text(window.screen.width/2, 75, "Messages Discarded: " + discardedMessagesCount).attr({font: '16px Arial'})
function leaderElectionLoop() {
if (g.isLeaderElected() == false) {
roundsText.attr('text', "Round " + rounds + " (leader not elected yet)")
console.log('round ' + rounds++ + ', leader not elected till now, running 1/2/3/4')
// in lock-step synchrony
// 1. apply msg generation func for each node
// 2. put generated messages in correct channels
// 3. apply state transition func for each node with its incoming message
// 4. remove all the received messages from channels
// 1. and 2.
for(var i = 0 ; i < g.nodes.length ; i++) {
g.nodes[i].msgFunc()
}
// visualize the edges matrix
document.getElementById("edgeMatrix").innerHTML = JSON.stringify(g.edges).replaceAll(/],/, '],\n')
// 3.
for(var i = 0 ; i < g.nodes.length ; i++) {
var prev_max_id = g.nodes[i].max_id
g.nodes[i].transitionFunc()
// visualize change in max_id
if (prev_max_id != g.nodes[i].max_id) {
var idx = raphaelCircles.find(circle => circle.id == g.nodes[i].id)
if (idx) {
idx.text.attr('text', g.nodes[i].max_id)
idx.circle.animate({fill: "red", "stroke-width": 20, "stroke-opacity": 0.5}, 500, function() {
this.animate({fill: "green", "stroke-width": 0}, 500)
}
)
}
}
}
// 4.
for(var i = 0 ; i < g.nodes.length ; i++) {
g.clearEdges()
}
// call the loop again every 1 seconds
setTimeout(leaderElectionLoop, 2000)
} else {
roundsText.attr('text', 'Leader is elected after ' +parseInt(rounds-1)+ ' rounds.')
console.log('Leader is elected! FINISHED in ' +parseInt(rounds-1)+ ' rounds.')
}
}
var numberOfNodes = 10
let g = new Graph(numberOfNodes)
// visualize each node along with its max_id
var idx = 200,
iBase = window.screen.width/2,
jBase = window.screen.height/3 + 50,
slice = 2 * Math.PI / g.nodes.length,
raphaelCircles = [],
center = [iBase, jBase],
radius = iBase
for(var i = 0 ; i < g.nodes.length ; i++) {
var horizontal = idx * Math.cos(slice * i) + iBase
var vertical = idx * Math.sin(slice * i) + jBase
var circle = paper.circle(horizontal, vertical, 20)
.attr({fill: "green"})
.attr({opacity: 0.75})
var text = paper.text(horizontal, vertical, g.nodes[i].max_id)
.attr({font: '14px Arial'})
var msgText = paper.text(horizontal, vertical+30, '').attr({font: '12px Arial'})
// store the circle and the text in the array for later modification
raphaelCircles.push({id: g.nodes[i].id, circle: circle, text: text, msgText: msgText})
}
// visualize the edges matrix
document.getElementById("edgeMatrix").innerHTML = JSON.stringify(g.edges).replaceAll(/],/, '],\n')
// console.log('Starting leader election')
setTimeout(leaderElectionLoop, 3000)
function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
</script>
</body>
</html>