-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
351 lines (301 loc) · 11.2 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
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
const { dataPacket, topology, dataFrame, errorProbability } = require('./config')
let statusTracker = [];
let status = '';
let payloadReceived = '';
function flipRandomBit(codeWord) {
const codeWordBits = codeWord.split('');
const noiseyCodeWordBits = new Array(7).fill(0);
for (let i = 0; i < 7; i++) {
noiseyCodeWordBits[i] = codeWordBits[i];
if (Math.random() < errorProbability) {
noiseyCodeWordBits[i] = (codeWordBits[i] + 1) % 2;
}
}
const noiseyCodeWord = noiseyCodeWordBits.join('');
return noiseyCodeWord;
}
function sendFrame(frame) {
frame.codeWord = flipRandomBit(frame.codeWord);
const { sourceMac, destinationMac, codeWord, EtherType } = frame;
const visited = new Set();
function destinationFound(nodePorts, destinationMac) {
{
status = `\t\tchecking Ports for destination `
statusTracker.push(status)
status = '';
}
for (const portId in nodePorts) {
{
status = `\t\t- ${portId}:\t ${nodePorts[portId]}`
statusTracker.push(status);
}
if (destinationMac === nodePorts[portId]) {
{
status = `\t\tforwarding to --> \t${portId}:\t ${nodePorts[portId]}`
statusTracker.push(status);
}
return portId;
}
}
{
status = `\t\tdestination Not Found at the device port(s)`;
statusTracker.push(status);
}
return null;
}
function forwardAllPorts(nodePorts) {
{
status = `\t\tforwarding to all Ports...`;
statusTracker.push(status);
}
let targetFound = null;
for (const portId in nodePorts) {
const nextmacAddress = nodePorts[portId];
if (!visited.has(nextmacAddress)) {
const targetNode = getNode(nextmacAddress)
const foundNode = traverse(targetNode);
if (foundNode) {
targetFound = foundNode;
}
}
}
return targetFound;
}
function traverse(node) {
const { macAddress, nodePorts, nodeName, nodeBuffer, nodeType } = node;
{
status = `\n\t--> MAC: ${macAddress} \tName: ${nodeName} \tType: ${nodeType}`;
statusTracker.push(status);
}
visited.add(macAddress);
if (nodeType !== "Hub") {
node.nodeBuffer = Object.assign({}, frame);
}
switch (nodeType) {
case "Computer":
if (macAddress === destinationMac) {
{
status =
`\t\t\t+------------------------+\n` +
`\t\t\t| Destination |\n` +
`\t\t\t+------------------------+\n`;
statusTracker.push(status);
{
status = `\t\t\t+Destination Node+`;
statusTracker.push(status);
status =
`\t\t\tNode Name:\t ${node.nodeName}\n` +
`\t\t\tNode MAC:\t ${node.macAddress}\n` +
`\t\t\tNode Buffer:\t ${JSON.stringify(node.nodeBuffer)}\n`;
statusTracker.push(status);
status = `\t\t\tFrame received...`;
statusTracker.push(status);
}
}
return node;
}
else {
const targetPort = destinationFound(nodePorts, destinationMac)
if (targetPort) {
const targetNode = getNode(nodePorts[targetPort])
return traverse(targetNode);
}
else {
return forwardAllPorts(nodePorts)
}
}
case "Switch":
{
status = `\t\tReading Frame...\n\t\tFrame: ${JSON.stringify(node.nodeBuffer)}`;
statusTracker.push(status);
}
const targetPort = destinationFound(nodePorts, destinationMac)
if (targetPort) {
const targetNode = getNode(nodePorts[targetPort])
return traverse(targetNode);
}
else {
return forwardAllPorts(nodePorts)
}
case "Hub":
{
status = '';
for (const portId in nodePorts) {
status += `\t\t- ${portId}:\t ${nodePorts[portId]}\n`
}
statusTracker.push(status)
}
return forwardAllPorts(nodePorts)
default:
console.warn(`Unknown node type: ${nodeType}`);
}
return null;
}
const sourceNode = getNode(sourceMac);
if (!sourceNode) {
console.error(`Source node with ID ${sourceMac} not found`);
return null;
}
return traverse(sourceNode);
}
const nodes = [...topology];
function getNode(macAddress) {
return nodes.find((n) => n.macAddress === macAddress)
}
const source_macAddress = dataFrame.sourceMACAddress;
const destination_macAddress = dataFrame.destinationMACAddress;
const sourceNode = getNode(source_macAddress);
const destinationNode = getNode(destination_macAddress);
{
status =
`-------------------------------------------------------\n` +
`Source: \t ${sourceNode.nodeName} \t MAC: ${sourceNode.macAddress}\nDestination: \t ${destinationNode.nodeName} \t MAC: ${destinationNode.macAddress}` +
`\n-------------------------------------------------------`;
statusTracker.push(status)
}
function parityCheckAndCorrect(codeWord) {
const codeWordBits = codeWord.split('');
const P = new Array(3).fill(0);
const dn = 4, hn = 7;
//P1
P[0] = codeWordBits[hn - 1] ^ codeWordBits[hn - 3] ^ codeWordBits[hn - 5] ^ codeWordBits[hn - 7];
//P2
P[1] = codeWordBits[hn - 2] ^ codeWordBits[hn - 3] ^ codeWordBits[hn - 6] ^ codeWordBits[hn - 7];
//P3
P[2] = codeWordBits[hn - 4] ^ codeWordBits[hn - 5] ^ codeWordBits[hn - 6] ^ codeWordBits[hn - 7];
const errorPosition = parseInt(P.join(''), 2);
if (errorPosition) {
{
status = `\tError detected at ${errorPosition} bit\n\tcorrecting Codeword...`;
statusTracker.push(status);
}
codeWordBits[hn - errorPosition] = ~codeWordBits[hn - errorPosition] & 1; // flip all the bits
{
status = `\tCorrected Codeword: ${codeWordBits}\n\textracting Dataword...`;
statusTracker.push(status);
}
return codeWordBits.join('');
}
else {
{
status = `\tDataword is error free`;
statusTracker.push(status);
}
return codeWord;
}
}
function receiveFrame(frame) {
{
status = `\n\t---------------` + `Data Link Layer` + `---------------\n`;
statusTracker.push(status);
status = `\tCodeword: ${frame.codeWord} `;
statusTracker.push(status);
status = `\tchecking for errors...`;
statusTracker.push(status);
}
const codeWord = parityCheckAndCorrect(frame.codeWord);
const codeWordbits = codeWord.split('');
const dataWordBits = new Array(4).fill(0);
const hn = 7, dn = 4;
dataWordBits[dn - 1] = codeWordbits[hn - 3]
dataWordBits[dn - 2] = codeWordbits[hn - 5]
dataWordBits[dn - 3] = codeWordbits[hn - 6]
dataWordBits[dn - 4] = codeWordbits[hn - 7]
const dataWord = dataWordBits.join('');
payloadReceived += (dataWord + " ")
{
status = `\tDataword: ${dataWord}`;
statusTracker.push(status);
}
}
function dataLinkLayer(dataPacket) {
{
status = `---------------` + `Data Link Layer` + `---------------\n`;
statusTracker.push(status);
status =
`Data Packet Received from Network Layer:\n` +
`Source IP:\t ${dataPacket.sourceIP}\n` +
`Destination IP:\t ${dataPacket.destinationIP}\n` +
`Protocol:\t ${dataPacket.protocol}\n` +
`Payload:\t ${dataPacket.payload}\n`
;
statusTracker.push(status);
}
const payload = dataPacket.payload;
const dataWords = payload.split(' ');
const getCodeWord = (dataWord) => {
const dataWordBits = dataWord.split('');
const hammingCode = new Array(7).fill(0);
const dn = 4, hn = 7;
// D4 D3 D2 P4 D1 P2 P1
// 7 6 5 4 3 2 1
// P1
hammingCode[hn - 1] = dataWordBits[dn - 1] ^ dataWordBits[dn - 2] ^ dataWordBits[dn - 4];
// P2
hammingCode[hn - 2] = dataWordBits[dn - 1] ^ dataWordBits[dn - 3] ^ dataWordBits[dn - 4];
// P4
hammingCode[hn - 4] = dataWordBits[dn - 2] ^ dataWordBits[dn - 3] ^ dataWordBits[dn - 4];
hammingCode[hn - 3] = dataWordBits[dn - 1]; // D1
hammingCode[hn - 5] = dataWordBits[dn - 2]; // D2
hammingCode[hn - 6] = dataWordBits[dn - 3]; // D3
hammingCode[hn - 7] = dataWordBits[dn - 4]; // D4
const codeWord = hammingCode.join('');
return codeWord;
}
dataWords.forEach(dataWord => {
const frame = {
sourceMac: dataFrame.sourceMACAddress,
destinationMac: dataFrame.destinationMACAddress,
EtherType: "IPv4",
codeWord: getCodeWord(dataWord),
}
{
status = `---------------` + `Data Link Layer` + `---------------\n`;
statusTracker.push(status);
status = `Generating Frame....\nDataword: ${dataWord} `;
statusTracker.push(status);
status =
`- Source MAC: ${frame.sourceMac}\n` +
`- Destination MAC: ${frame.destinationMac}\n` +
`- EtherType: ${frame.EtherType}\n` +
`- CodeWord: ${frame.codeWord}\n`;
statusTracker.push(status);
}
physicalLayer(frame);
{
status = `\n\t>>>> Received Payload: ${payloadReceived}\n`;
statusTracker.push(status);
// status = `\n\t>>>> Sent Payload: ${dataPacket.payload}\n`;
// statusTracker.push(status);
}
});
}
function physicalLayer(frame) {
{
status = `Sending Frame...`;
statusTracker.push(status);
status = `---------------` + `Physical Layer` + `---------------`;
statusTracker.push(status);
}
sourceNode.nodeBuffer = {
frame: frame,
}
const destinationNode = sendFrame(frame)
if (destinationNode) {
receiveFrame(destinationNode.nodeBuffer);
} else {
console.log("Destination node not found");
}
}
dataLinkLayer(dataPacket)
// ------------------------------------------------------------------
let index = 0;
function printStatus() {
if (index < statusTracker.length) {
console.log(statusTracker[index]);
index++;
} else {
clearInterval(intervalId);
}
}
const intervalId = setInterval(printStatus, 1000);