-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpServer.js
161 lines (143 loc) · 5.23 KB
/
httpServer.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
const http = require('http');
const readline = require('readline');
const fs = require('fs');
const { Server } = require("socket.io");
const { prettyPrintJson } = require('pretty-print-json');
//-------------------------------------------------------------------------------
// Parsafix HTTP server class
//-------------------------------------------------------------------------------
class HttpServer
{
server;
io;
socketArr;
htmlFile;
cssFile;
jsFile;
mpsData;
xmlData;
parsafixData;
spoofaxData;
constructor()
{
this.mpsData = '';
this.xmlData = '';
this.parsafixData = '';
this.spoofaxData = '';
this.socketArr = [];
fs.readFile('./www/index.html', function(err, data)
{
if (err) { console.log("\nFile read error: " + err); }
this.htmlFile = data;
}.bind(this));
fs.readFile('./www/style.css', function(err, data)
{
if (err) { console.log("\nFile read error: " + err); }
this.cssFile = data;
}.bind(this));
fs.readFile('./www/main.js', function(err, data)
{
if (err) { console.log("\nFile read error: " + err); }
this.jsFile = data;
}.bind(this));
}
//-------------------------------------------------------------------------------
// Starts up HTTP server
//-------------------------------------------------------------------------------
start = function()
{
this.server = http.createServer(this.requestListener.bind(this));
this.server.listen(38101);
this.handleIO();
}
handleIO()
{
this.io = new Server(this.server);
this.io.on('connection', function(socket)
{
this.socketArr.push(socket);
//console.log('\nA user is viewing the project translation data');
socket.on('disconnect', () =>
{
//console.log('\nA user disconnected');
});
}.bind(this));
}
//-------------------------------------------------------------------------------
// Stops HTTP server
//-------------------------------------------------------------------------------
stop = function()
{
try
{
//Disconnect all clients
for (let i = 0; i < this.socketArr.length; i++)
{
this.socketArr[i].disconnect();
}
//Close the server
this.server.close();
}
catch (error) {}
}
//-------------------------------------------------------------------------------
// Listens for HTTP requests and responds
//-------------------------------------------------------------------------------
requestListener(req, res)
{
//console.log("\nRequest received: ", req.url);
switch (req.url) {
case "/style.css":
res.writeHead(200, {"Content-Type": "text/css"});
res.end(this.cssFile);
break;
case "/main.js":
res.writeHead(200, {"Content-Type": "text/javascript"});
res.end(this.jsFile);
break;
default:
res.writeHead(200, {"Content-Type": "text/html"});
res.end(this.htmlFile);
this.updateMpsData(this.mpsData);
//this.updateXmlData(this.xmlData);
this.updateParsafixData(this.parsafixData);
this.updateSpoofaxData(this.spoofaxData);
}
}
//-------------------------------------------------------------------------------
// Updates the MPS project data
//-------------------------------------------------------------------------------
updateMpsData(newData)
{
//console.log("\nUpdating MPS data");
this.mpsData = newData;
this.io.emit('mps-data', '<pre>' + prettyPrintJson.toHtml(this.mpsData) + '</pre>');
}
//-------------------------------------------------------------------------------
// Updates the Parsafix data
//-------------------------------------------------------------------------------
updateParsafixData(newData)
{
this.parsafixData = newData;
this.io.emit('parsafix-data', '<pre>' + prettyPrintJson.toHtml(this.parsafixData) + '</pre>');
}
//-------------------------------------------------------------------------------
// Updates the Spoofax project data
//-------------------------------------------------------------------------------
updateSpoofaxData(newData)
{
//console.log("\nUpdating Spoofax data: " + JSON.stringify(newData));
this.spoofaxData = newData;
this.io.emit('spoofax-data', this.spoofaxData);
}
getMpsData()
{
return this.mpsData;
}
}
//-------------------------------------------------------------------------------
// Make the following functions available
//-------------------------------------------------------------------------------
module.exports = {
HttpServer
}