1 var spawn = require('child_process').spawn;
6 var command_ready = false;
7 var graphic_ready = false;
9 L('process id: ' + process.pid);
10 var cmdPort = 10000 + process.pid;
11 var grpPort = 10001 + process.pid;
18 //start server to chat with dispatcher
19 var dispatchio = require('socket.io')(dspPort);
21 dispatchio.on('connection', function (dspSocket) {
22 //new connection incoming
23 L('dispatchio connection');
24 dspSocket.on('command', function (msg) {
25 L('command (' + process.pid + ') : ' + msg.data);
26 commandio.emit('command', msg);
29 dspSocket.on('callback', function (msg) {
30 graphicio.emit('callback', msg);
33 dspSocket.on('force_reload', function (msg) {
34 msgHistory = []; //reset history, sciab was closed or has crashed.
37 dspSocket.on('imagepath', function (msg) {
41 dspSocket.on('quit', function () {
42 L('quit' + '(' + process.pid + ')');
44 //prevent accidental close, wait 10 minutes before really close Scilab.
45 quitTO = setTimeout( function() {
46 L('send quit to Scilab');
47 commandio.emit('command', {data:'quit'});
51 }, /*60 * 10 * 1000*/5 * 1000); //5 seconds to test
54 dspSocket.on('reconnection', function () {
58 //resend all gui creation information
59 var size = msgHistory.length;
60 L('history: ' + size);
61 dspSocket.emit("graphic_reconnection", "start");
62 for(var i = 0 ; i < size ; ++i) {
63 //L('%d : %s', i+1, msgHistory[i]);
64 dspSocket.emit('graphic_create', msgHistory[i]);
66 dspSocket.emit("graphic_reconnection", "end");
69 L('open commandio socket');
70 //start command server to chat with Scilab
71 var commandio = require('socket.io')(cmdPort);
72 commandio.on('connection', function (socket) {
73 L('Scilab command connected');
75 socket.on('command_end', function () {
77 dspSocket.emit('command_end');
80 socket.on('disconnect', function () {
81 L('scilab command disconnected'+ '(' + process.pid + ')');
82 command_ready = false;
85 //send to server scilab is ready
88 msgHistory = []; //reset history, sciab was closed or has crashed.
89 dspSocket.emit('status', {data:'ready'});
93 //start graphic server to chat with graphic MVC
94 var graphicio = require('socket.io')(grpPort);
95 graphicio.on('connection', function (socket) {
96 L('Scilab graphic connected');
98 socket.emit('imagepath', imagepath);
99 socket.on('graphic_create', function (msg) {
100 msgHistory.push(msg);
101 dspSocket.emit('graphic_create', msg);
104 socket.on('graphic_delete', function (msg) {
105 msgHistory.push(msg);
106 dspSocket.emit('graphic_delete', msg);
109 socket.on('graphic_update', function (msg) {
110 msgHistory.push(msg);
111 dspSocket.emit('graphic_update', msg);
114 socket.on('disconnect', function () {
115 msgHistory = []; //reset history, sciab was closed or has crashed.
117 L('scilab graphic disconnected'+ '(' + process.pid + ')');
118 graphic_ready = false;
121 //send to server scilab is ready
122 graphic_ready = true;
124 dspSocket.emit('status', {data:'ready'});
128 //launch scilab with init command
130 if(process.platform === "win32") {
131 app = process.env.SCIPATH + "/bin/wscilex.exe";
133 app = process.env.SCIPATH + "/bin/scilab";
136 var addr = 'http://127.0.0.1';
137 var commandaddr = addr + ':' + cmdPort;
138 var graphicaddr = addr + ':' + grpPort;
140 var scilabApp = spawn(app, ['-nw', '-commandaddr', commandaddr, '-graphicaddr', graphicaddr]);
145 scilabApp.stdout.on('data', function(data) {
146 L('scilab out :' + data.toString());
149 scilabApp.stderr.on('data', function(data) {
150 L('scilab err :' + data.toString());