forked from pvsioweb/pvsio-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpvssocketserver.js
More file actions
350 lines (328 loc) · 15.5 KB
/
pvssocketserver.js
File metadata and controls
350 lines (328 loc) · 15.5 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
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
//#!/usr/bin/env node
/**
Copyright (c) 2012
Patrick Oladimeji
This file is part of pvsio-web.
pvsio-web is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
pvsio-web is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* This file creates a connection to a pvsio process run locally or at specified host.
* It also creates an express webserver to serve demo applications e.g. the infusion
* pump pvsio demo.
* The websocket connection started by this process listens for 3 commands:
* sendCommand: used to send a pvsio command to the processs
* startProcess: used to start the pvsio process
* getSourceCode: used to get the source code of the pvs code being executed
* @author patrick
* @date 28 Jul 2012 21:52:31
*
*/
/*jshint unused: true, undef: true*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, undef: true, node: true */
/*global __dirname*/
function run() {
"use strict";
var pvsio = require("./pvsprocess"),
path = require("path"),
ws = require("ws"),
http = require("http"),
fs = require("fs"),
express = require("express"),
bodyParser = require("body-parser"),
webserver = express(),
procWrapper = require("./processwrapper"),
uploadDir = "/public/uploads",
port = 8082,
pvsioProcessMap = {},//each client should get his own process
httpServer = http.createServer(webserver),
Promise = require("es6-promise").Promise,
logger = require("tracer").console(),
serverFuncs = require("./serverFunctions"),
baseProjectDir = __dirname + "/public/projects/";
var p, clientid = 0, WebSocketServer = ws.Server;
var writeFile = serverFuncs.writeFile,
createProject = serverFuncs.createProject,
openProject = serverFuncs.openProject,
listProjects = serverFuncs.listProjects;
/**
* Utility function that dispatches responses to websocket clients
* @param {{type:string, data}} token The token to send to the client
* @param {Socket} socket The websocket to use to send the token
*/
function processCallback(tok, socket) {
//called when any data is recieved from pvs process
//if the type of the token is 'processExited' then send message to client if the socket is still open
if (tok.type === 'processExited') {
if (socket.readyState === 1) {
socket.send(JSON.stringify(tok));
}
} else {//send the message normally
socket.send(JSON.stringify(tok));
}
}
/**
* reads and changes the settings in the .pvsioweb file in the project root
* @param {string} projectName the name of the project
* @param {string} key the key of the setting to write
* @param {object} value the value of the setting to write
* @returns {Promise} a promise that is resolved when the settings file has been written.
*/
function changeProjectSetting(projectName, key, value) {
var file = path.join(baseProjectDir, projectName, "/.pvsioweb"),
props = {};
return new Promise(function (resolve, reject) {
//if file does not exist, create it. Else read the property file and update just the key value specified
fs.exists(file, function (exists) {
if (!exists) {
props[key] = value;
writeFile(file, JSON.stringify(props, null, " "))
.then(resolve, reject);
} else {
fs.readFile(file, {encoding: "utf8"}, function (err, res) {
props = {err: err};
if (!err) {
props = JSON.parse(res) || props;
props[key] = value;
//write the file back
writeFile(file, JSON.stringify(props, null, " "))
.then(resolve, reject);
} else {//there was an error so reject the promise
reject(err);
}
});
}
});
});
}
//create logger
webserver.use("/demos", function (req, res, next) {
logger.log('Method: %s, Url: %s, IP: %s', req.method, req.url, req.connection.remoteAddress);
next();
});
//create the express static server and use public dir as the default serving directory
webserver.use(express.static(__dirname + "/public"));
webserver.use(bodyParser({ keepExtensions: true, uploadDir: __dirname + uploadDir}));
/**
* used to manage file upload process for pvsio-web
*/
webserver.all("/upload", function (req, res) {
logger.debug(JSON.stringify(req.files));
var fileName = req.files.file.path.split("/").slice(-1).join("");
//should return a map of oldname to new name for the uploaded files
res.send({fileName: fileName});
});
function typeCheck(filePath, cb) {
procWrapper().exec({
command: "proveit " + filePath,
callBack: cb
});
}
/**
get function maps for client sockets
*/
function createClientFunctionMaps() {
var map = {
"renameFile": function (token, socket, socketid) {
var oldPath = path.join(baseProjectDir, token.oldPath);
var newPath = path.join(baseProjectDir, token.newPath);
fs.rename(oldPath, newPath, function (err) {
if (err) {
logger.debug("warning, error while renaming " + token.oldPath +
" into " + token.newPath + " (" + err + ")");
}
processCallback({id: token.id, socketId: socketid, err: err}, socket);
});
},
"readDirectory": function (token, socket, socketid) {
fs.readdir(token.path, function (err, files) {
if (!err) {
//get stat attributes for all the files using an async call
var promises = files.map(function (f) {
return new Promise(function (resolve, reject) {
fs.stat(f, function (err, res) {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
});
Promise.all(promises)
.then(function (res) {
var result = res.map(function (d, i) {
return {name: files[i], isDirectory: d.isDirectory()};
});
processCallback({id: token.id, socketId: socketid, files: result, err: err}, socket);
}, function (err) {
processCallback({id: token.id, socketId: socketid, err: err}, socket);
});
} else {
processCallback({id: token.id, socketId: socketid, err: err}, socket);
}
});
},
"writeDirectory": function (token, socket, socketid) {
fs.mkdir(token.path, function (err) {
processCallback({id: token.id, socketId: socketid, err: err}, socket);
});
},
"setMainFile": function (token, socket, socketid) {
changeProjectSetting(token.projectName, "mainPVSFile", token.fileName)
.then(function (res) {
res.id = token.id;
res.socketId = socketid;
res.serverSent = new Date().getTime();
processCallback(res, socket);
});
},
"listProjects": function (token, socket, socketid) {
var result = {id: token.id, serverSent: new Date().getTime(), socketId: socketid};
listProjects()
.then(function (projects) {
result.projects = projects;
processCallback(result, socket);
}, function (err) {
result.err = err;
processCallback(result, socket);
});
},
"openProject": function (token, socket, socketid) {
var res = {id: token.id, serverSent: new Date().getTime(), socketId: socketid};
openProject(token.name)
.then(function (data) {
res.project = data;
processCallback(res, socket);
}, function (err) {
res.err = err;
processCallback(res, socket);
});
},
"createProject": function (token, socket, socketid) {
p = pvsioProcessMap[socketid] || pvsio();
pvsioProcessMap[socketid] = p;
createProject(token, function (res) {
res.id = token.id;
res.socketId = socketid;
res.serverSent = new Date().getTime();
processCallback(res, socket);
}, p);
},
"typeCheck": function (token, socket, socketid) {
typeCheck(token.filePath, function (err, stdout, stderr) {
var res = {id: token.id, err: err, stdout: stdout, stderr: stderr, socketId: socketid};
processCallback(res, socket);
});
},
"sendCommand": function (token, socket, socketid) {
p = pvsioProcessMap[socketid];
p.sendCommand(token.data.command, function (data) {
var res = {id: token.id, data: [data], socketId: socketid, type: "commandResult"};
processCallback(res, socket);
});
},
"startProcess": function (token, socket, socketid) {
logger.info("Calling start process for client... " + socketid);
var root = token.data.projectName ?
"/public/projects/" + token.data.projectName
: token.data.demoName ? "/public/demos/" + token.data.demoName : "";
p = pvsioProcessMap[socketid];
//close the process if it exists and recreate it
if (p) {
p.close('SIGTERM', true);
delete pvsioProcessMap[socketid];
}
//recreate the pvsio process
p = pvsio();
pvsioProcessMap[socketid] = p;
//set the workspace dir and start the pvs process with a callback for processing process ready and exit
//messages from the process
p.workspaceDir(__dirname + root)
.start(token.data.fileName,
function (res) {
res.id = token.id;
res.serverSent = new Date().getTime();
res.socketId = socketid;
processCallback(res, socket);
});
},
"closeProcess": function (token, socket, socketid) {//closes pvs process
p = pvsioProcessMap[socketid];
var res = {id: token.id, socketId: socketid};
if (p) {
p.close();
delete pvsioProcessMap[socketid];
res.message = "process closed";
} else {
res.type = "attempting to close undefined process";
}
processCallback(res, socket);
},
"readFile": function (token, socket, socketid) {
p = pvsioProcessMap[socketid];
var encoding = token.encoding || "utf8";
fs.readFile(token.filePath, encoding, function (err, content) {
var res = err ? {err: err} : {id: token.id, serverSent: new Date().getTime(), fileContent: content, socketId: socketid};
processCallback(res, socket);
});
},
"writeFile": function (token, socket, socketid) {
p = pvsioProcessMap[socketid];
var res = {id: token.id, serverSent: new Date().getTime(), socketId: socketid};
token.filePath = path.join(baseProjectDir, token.filePath);
writeFile(token.filePath, token.fileContent, token.encoding)
.then(function () {
processCallback(res, socket);
}, function (err) {
res.err = err;
processCallback(res, socket);
});
},
"deleteFile": function (token, socket, socketid) {
p = pvsioProcessMap[socketid];
var res = {id: token.id, serverSent: new Date().getTime(), socketId: socketid};
token.filePath = path.join(baseProjectDir, token.filePath);
p.removeFile(token.filePath, function (err) {
if (!err) {
res.type = "fileDeleted";
} else {
res.err = err;
}
processCallback(res, socket);
});
}
};
return map;
}
var wsServer = new WebSocketServer({server: httpServer});
wsServer.on("connection", function (socket) {
var socketid = clientid++;
var functionMaps = createClientFunctionMaps();
socket.on("message", function (m) {
var token = JSON.parse(m);
var f = functionMaps[token.type];
if (f && typeof f === 'function') {
//call the function with token and socket as parameter
f(token, socket, socketid);
} else {
logger.warn("f is something unexpected -- I expected a function but got type " + typeof f);
}
});
socket.on("close", function () {
logger.info("closing websocket client " + socketid);
var _p = pvsioProcessMap[socketid];
if (_p) {
_p.close();
}
delete pvsioProcessMap[socketid];
});
});
wsServer.on("error", function (err) {
logger.error(JSON.stringify(err));
});
httpServer.listen(port);
logger.info("http server started .." + "now listening on port " + port);
}
run();