-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender.js
76 lines (71 loc) · 1.9 KB
/
sender.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
import {
from_network_layer,
to_physical_layer,
from_physical_layer,
construct_packet_array,
construct_frame,
} from "./util.js";
import { TIMEOUT_LENGTH, FRAME_TYPES } from "./const.js";
import require from "requirejs";
const io = require("socket.io-client");
var packet_array = [];
var packet_index = 0;
var timer;
var socket;
const send = (ip, port) => {
socket = io.connect("http://" + ip + ":" + port, { reconnect: true });
socket.on("connect_error", (err) => {
console.log("[GENERAL]".magenta + " Connection unsucessful : " + err);
process.exit();
});
socket.on("message", (data) => {
var frame = from_physical_layer(data);
handle_event(frame);
});
socket.on("connect", (socket) => {
send_current_frame();
});
};
const send_current_frame = () => {
var packet = from_network_layer(packet_array, packet_index);
var frame = construct_frame(packet, packet_index % 2, FRAME_TYPES.INFO);
to_physical_layer(socket, frame);
timer = setTimeout(() => {
console.log("[DATA LINK LAYER]".blue + " Timer timed out, resending");
send_current_frame();
}, TIMEOUT_LENGTH);
};
const handle_event = (frame) => {
if (frame.kind != FRAME_TYPES.ACK) {
console.log(
"[DATA LINK LAYER]".blue + " Damaged frame received, ignored"
);
} else {
if (frame.seq_no == packet_index % 2) {
packet_index++;
clearTimeout(timer);
if (packet_index < packet_array.length) {
send_current_frame();
} else {
console.log("[GENERAL]".magenta + " Message sent, exiting.");
socket.disconnect();
process.exit();
}
} else {
console.log(
"[DATA LINK LAYER]".blue +
" Out of order acknowledgement received, ignored"
);
}
}
};
var args = process.argv;
if (args.length < 5) {
console.log("usage : node sender.js [IP] [PORT] [MESSAGE]");
process.exit();
}
const ip = args[2];
const port = args[3];
const message = args[4];
packet_array = construct_packet_array(message);
send(ip, port);