-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong-cli.js
149 lines (132 loc) · 3.81 KB
/
pong-cli.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
const express = require('express');
const axios = require('axios');
const fs = require('fs');
const args = process.argv.slice(2);
const server1 = express();
const server2 = express();
const basePort = 3000;
const configFile = './config.json';
let shouldPing = true;
let options = {
running: true,
destroy: false
};
let lastRuntime = true;
if (args.length === 0) {
console.log("You didn't enter any command, can't run the server. Have a nice day.");
} else {
parseCommand(args);
}
/**
* Parse the options from the command line.
* @param arguments the arguments passed from the command line.
*/
function parseCommand(arguments) {
const command = arguments[0].toLowerCase();
switch (command) {
case 'start':
if (arguments.length > 1) {
startServers(parseInt(arguments[1]));
} else {
console.log("Please enter the timeout between the messages.");
}
break;
case 'pause':
pauseServers();
break;
case 'resume':
resumeServers();
break;
case 'stop':
stopServers();
break;
default:
console.log(`Unrecognized command ${command}, please enter a valid command`);
break;
}
}
/**
* Starts the servers after GET request.
* @param timeout the timeout between each message in milliseconds.
*/
function startServers(timeout) {
// write initial configurations.
options = {
running: true,
destroy: false
};
fs.writeFile(configFile, JSON.stringify(options), (err) => {
if (err) {
console.log(`There was an error ${err.message}`);
}
});
const serverResponse = (req, __) => {
let data = fs.readFileSync(configFile),
myObj;
myObj = JSON.parse(data);
if (myObj.destroy) {
process.exit(0)
return;
}
if (!myObj.running) return;
const port = req.get("host").split(":")[1];
pingPong(timeout, port);
};
server1.get('/', serverResponse);
server2.get('/', serverResponse);
server1.listen(basePort);
server2.listen(basePort + 1);
}
/**
* print ping/pong and notifies the second server after the specified amount of time.
* @param timeout the timeout between each message in milliseconds.
* @param port the port of the sending server.
*/
function pingPong(timeout, port) {
if (new Date().getTime() < lastRuntime + timeout) return;
console.log(shouldPing ? "Ping" : "Pong");
lastRuntime = new Date().getTime();
const pingPort = 2 * basePort + 1 - port; // (sum of the ports) => 2 * basePort + 1.
// switch between 2 numbers a and b => (a + b) - n where n is either a or b.
// noinspection JSIgnoredPromiseFromCall
setTimeout(() => axios.get(`http://localhost:${pingPort}`), timeout);
shouldPing = !shouldPing;
}
/**
* Pause messages between the servers.
*/
function pauseServers() {
options.running = false;
fs.writeFile(configFile, JSON.stringify(options), (err) => {
if (err) {
console.log("couldn't save the data");
console.log(err.message);
}
});
}
/**
* Resume the messages between the servers.
*/
function resumeServers() {
options.running = true;
fs.writeFile(configFile, JSON.stringify(options), (err) => {
if (err) {
console.log("couldn't save the data");
console.log(err.message);
return;
}
axios.get(`http://localhost:${basePort}`);
});
}
/**
* Stop the servers.
*/
function stopServers() {
options.destroy = true;
fs.writeFile(configFile, JSON.stringify(options), (err) => {
if (err) {
console.log("couldn't save the data");
console.log(err.message);
}
});
}