var command_ready = false;
var graphic_ready = false;
+L('process id: ' + process.pid);
var cmdPort = 10000 + process.pid;
var grpPort = 10001 + process.pid;
var dspPort = 10002;
var imagepath;
+var quitTO = 0;
+var msgHistory = [];
+
//start server to chat with dispatcher
var dispatchio = require('socket.io')(dspPort);
+
dispatchio.on('connection', function (dspSocket) {
-
+ //new connection incoming
+ L('dispatchio connection');
dspSocket.on('command', function (msg) {
L('command (' + process.pid + ') : ' + msg.data);
commandio.emit('command', msg);
dspSocket.on('quit', function () {
L('quit' + '(' + process.pid + ')');
- commandio.emit('command', {data:'quit'});
- commandio.close();
- graphicio.close();
- process.exit(0);
+
+ //prevent accidental close, wait 10 minutes before really close Scilab.
+ quitTO = setTimeout( function() {
+ L('send quit to Scilab');
+ commandio.emit('command', {data:'quit'});
+ commandio.close();
+ graphicio.close();
+ process.exit(0);
+ }, /*60 * */10 * 1000); //10 seconds to test
+ });
+
+ dspSocket.on('reconnection', function () {
+ L('reconnection');
+ clearTimeout(quitTO);
+ quitTO = 0;
+ //resend all gui creation information
+ var size = msgHistory.length;
+ L('history: ' + size);
+ for(var i = 0 ; i < size ; ++i) {
+ //L('%d : %s', i+1, msgHistory[i]);
+ dspSocket.emit('graphic_create', msgHistory[i]);
+ }
});
+ L('open commandio socket');
//start command server to chat with Scilab
var commandio = require('socket.io')(cmdPort);
commandio.on('connection', function (socket) {
//send to server scilab is ready
command_ready = true;
if(graphic_ready) {
+ msgHistory = []; //reset history, sciab was closed or has crashed.
dspSocket.emit('status', {data:'ready'});
}
});
socket.emit('imagepath', imagepath);
socket.on('graphic_create', function (msg) {
+ msgHistory.push(msg);
dspSocket.emit('graphic_create', msg);
});
socket.on('graphic_delete', function (msg) {
+ msgHistory.push(msg);
dspSocket.emit('graphic_delete', msg);
});
socket.on('graphic_update', function (msg) {
+ msgHistory.push(msg);
dspSocket.emit('graphic_update', msg);
});
socket.on('disconnect', function () {
+ msgHistory = []; //reset history, sciab was closed or has crashed.
+
L('scilab graphic disconnected'+ '(' + process.pid + ')');
graphic_ready = false;
});
var server = require('http').Server(app);
var io = require('socket.io')(server);
-var client = require('socket.io-client');
-
app.use(express.static('static'));
server.listen(1337);
res.sendFile(__dirname + '/static/index.html');
});
-io.on('connection', function (socket) {
- //start process
- var child = fork('./scilab_process.js');
- //in future version, it must be a docker so we can't use 'internal' pipe
- //so we need to connect a socket, another ...
- var prcAddr = 'http://127.0.0.1:10002';
- var prcSocket = require('socket.io-client')(prcAddr);
- prcSocket.on('connect', function() {
- L('Dispatcher connected');
+var processAlive = false;
- prcSocket.emit('imagepath', {path:__dirname + '/static/'});
-
- prcSocket.on('command_end', function() {
- socket.emit('command_end');
- });
+var prcSocket;
+io.on('connection', function (socket) {
+ //incoming connection from web users
+
+ if(processAlive === false) {
+ //start process
+ fork('./scilab_process.js');
+ processAlive = true;
+
+ var prcAddr = 'http://127.0.0.1:10002';
+ prcSocket = require('socket.io-client')(prcAddr);
+ L('dispatcher connected');
+ } else {
+ //send ready message to client
+ socket.emit('status', {data:'reconnection'});
+ prcSocket.emit('reconnection');
+ }
+
+ prcSocket.emit('imagepath', {path:__dirname + '/static/'});
+
+ prcSocket.on('command_end', function() {
+ socket.emit('command_end');
+ });
- prcSocket.on('graphic_create', function(msg) {
- socket.emit('graphic_create', msg);
- });
+ prcSocket.on('graphic_create', function(msg) {
+ //L(msg);
+ socket.emit('graphic_create', msg);
+ });
- prcSocket.on('graphic_delete', function(msg) {
- socket.emit('graphic_delete', msg);
- });
+ prcSocket.on('graphic_delete', function(msg) {
+ socket.emit('graphic_delete', msg);
+ });
- prcSocket.on('graphic_update', function(msg) {
- socket.emit('graphic_update', msg);
- });
+ prcSocket.on('graphic_update', function(msg) {
+ socket.emit('graphic_update', msg);
+ });
- prcSocket.on('status', function(msg) {
- if(msg.data === 'ready') {
- //send ready message to client
- socket.emit('status', msg);
- //send execution of initial script to scilab
- prcSocket.emit('command', {data:"exec(getenv('SCIFILES') + '" + "/start" + ".sce', -1);"});
- }
- });
+ prcSocket.on('status', function(msg) {
+ if(msg.data === 'ready') {
+ //send ready message to client
+ socket.emit('status', msg);
+ //send execution of initial script to scilab
+ prcSocket.emit('command', {data:"exec(getenv('SCIFILES') + '" + "/start" + ".sce', -1);"});
+ }
});
-
+
+ prcSocket.on('disconnect', function(msg) {
+ L('dispatcher disconnected');
+ prcSocket.close();
+ processAlive = false;
+ });
+
//receive command from client
socket.on('command', function (msg) {
prcSocket.emit('command', msg);
});
socket.on('disconnect', function () {
- L('disconnect');
+ L('User disconnected');
prcSocket.emit('quit');
- prcSocket.close();
});
-
});
L('Server running on port '+server.address().port);