-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
162 lines (115 loc) · 4.92 KB
/
app.js
File metadata and controls
162 lines (115 loc) · 4.92 KB
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
const Net = require('net');
const { serverChatRooms } = require('./chatRoomManager/chatRoomManager');
const { clientServer } = require('./clientServer/clientServerMain');
const { serverManager } = require('./serverManager/serverManager');
const { setConfigInfo, getAllInfo, setCoordinator, getClientPort, getCoordinationPort, getCoordinator } = require('./data/serverDetails');
const { setGlobalServersConfig, getCoordinatingPorts, getHighestPriorityServer } = require('./data/globalServerDetails');
const util = require('./util/util');
const { argv } = require('process');
const { addLocalChatRoom } = require('./data/serverChatRooms');
const { addChatroom } = require('./data/globalChatRooms');
const { heartbeat, initHeartbeat } = require('./serverManager/heartbeat');
const { broadcastNewChatroom } = require('./serverManager/broadcastCommunication');
const constants = require('./util/constants');
const { sendIamup } = require('./serverManager/leaderElection');
const { quit } = require('./clientServer/quit');
// Get serverId as the argument
const serverId = argv[2];
// Get servers config json path
const configPath = argv[3];
// set server config
const serverConfig = setConfigInfo(configPath, serverId);
const port = getClientPort();
const coordinationPort = getCoordinationPort();
constants.T4 = 2000 * serverConfig.priority;
// set coordinating servers config
setGlobalServersConfig(configPath, serverId);
// const otherCoordinationPorts = getCoordinatingPorts();
//send iamup
sendIamup();
var heartbeatCheck = false;
initHeartbeat();
// Create a client server
const serverForClients = new Net.Server();
// Called when server is connected
serverForClients.listen(port, function () {
console.log(`Server listening for client connection requests on ${serverConfig.address}:${port}`);
// Create the main hall of the server
addLocalChatRoom({
chatRoomIdentity: "MainHall-" + serverId,
owner: null,
clients: []
});
addChatroom(serverId, "MainHall-" + serverId);
broadcastNewChatroom(serverId, "MainHall-" + serverId)
});
// When a client requests a connection with the server, the server creates a new socket dedicated to it.
serverForClients.on('connection', function (socket) {
if (heartbeatCheck == false) {
setInterval(heartbeat, 2000);
heartbeatCheck = true;
}
//socket = new JsonSocket(socket);
console.log('A new connection with a client has been established.');
// Now that a TCP connection has been established, the server can send data to the client by writing to its socket.
// socket.write(util.jsonEncode({type: 'message', content: 'success'}));
// The server can also receive data from the client or another server by reading from its socket.
socket.on('data', function (bufObj) {
try {
let json = util.jsonDecode(bufObj);
console.log(`Data received from client: ` + JSON.stringify(json) + `\n`);
clientServer(socket, json);
} catch (error) { }
});
// When the client requests to end the TCP connection with the server, the server ends the connection.
socket.on('end', function () {
//quit(socket);
console.log('Closing the connection');
});
socket.on('error', function(error) {
console.log('client connection error');
});
// Don't forget to catch error, for your own sake.
socket.on('close', function (isErr) {
if(isErr) {
setImmediate(quit,socket);
}
console.log('Closing the connection');
});
});
// Create a server for communicating with other server
const serverForCoordination = new Net.Server();
serverForCoordination.listen(coordinationPort, function () {
console.log(`Server listening for server connection requests on ${serverConfig.address}:${coordinationPort}`);
});
serverForCoordination.on('connection', function (socket) {
if (heartbeatCheck == false) {
setInterval(heartbeat, 2000);
heartbeatCheck = true;
}
socket.on('data', function (bufObj) {
try {
let json = util.jsonDecode(bufObj);
if (json.type !== "heartbeat" && json.type !== "heartbeat_ack") {
console.log(`Data received from a server : ` + JSON.stringify(json) + `\n`);
}
serverManager(socket, json);
} catch (error) { }
});
socket.on('end', function () {
});
socket.on('error', function (err) {
console.log(`server connection error`);
});
});
/* serverForCoordination.on('clientError', (err, socket) => {
//if (err.code === 'ECONNRESET' || !socket.writable) socket.end('HTTP/2 400 Bad Request\n');
console.log('client error\n', err);
}); */
serverForCoordination.on('error', (err, socket) => {
//if (err.code === 'ECONNRESET' || !socket.writable) socket.end('HTTP/2 400 Bad Request\n');
console.log('server error');
});
/* if (heartbeatCheck == 1) {
setInterval(heartbeat, 2000);
} */