diff options
author | SaloniGoyal | 2016-06-07 11:28:21 +0530 |
---|---|---|
committer | SaloniGoyal | 2016-06-07 11:28:21 +0530 |
commit | 3dc823713ffd58b6c07e46e490c0c2b059b03dd3 (patch) | |
tree | 4f8815d172924da3fbfba1ae85eb1931a918d254 | |
parent | bc130a761f20f323009217f7b289118d7c2e1edd (diff) | |
download | eSimWebApp-3dc823713ffd58b6c07e46e490c0c2b059b03dd3.tar.gz eSimWebApp-3dc823713ffd58b6c07e46e490c0c2b059b03dd3.tar.bz2 eSimWebApp-3dc823713ffd58b6c07e46e490c0c2b059b03dd3.zip |
Added error handling while running ngspice
-rw-r--r-- | routes/routes.js | 102 | ||||
-rw-r--r-- | views/script/savenetlist.js | 10 |
2 files changed, 77 insertions, 35 deletions
diff --git a/routes/routes.js b/routes/routes.js index 942960f..67857c5 100644 --- a/routes/routes.js +++ b/routes/routes.js @@ -8,44 +8,86 @@ module.exports = function(express,app,io,fs,exec){ app.use('/',router); - + io.on('connection',function(socket){ - console.log('a user connected'); - socket.on('disconnect', function(){ - console.log('user disconnected'); - fs.stat('/tmp/' + socket.id + '.cir.out', function(err, stat){ - if(err == null){ - fs.unlink('/tmp/' + socket.id + '.cir.out'); - } + var socketId = getSocketID(socket); + var analysisFile = '/tmp/' + socketId + '.cir.out'; + //Conversion to lower case is required as NgSpice internally converts the filename to lower case. + var dumpv = '/tmp/' + socketId.toLowerCase() + '-dumpv.txt'; + var dumpi = '/tmp/' + socketId.toLowerCase() + '-dumpi.txt'; + + console.log('Client with socket id ' + socketId + ' is now connected.'); + + socket.on('disconnect', function(){ + console.log('Client with socket id ' + socketId + ' has disconnected.'); + deleteOnExit(analysisFile); + deleteOnExit(dumpv); + deleteOnExit(dumpi); }); - fs.stat('/tmp' + socket.id.toLowerCase() + '-dumpv.txt', function(err, stat){ - if(err == null){ - fs.unlink('/tmp' + socket.id.toLowerCase() + '-dumpv.txt'); - } - + + + socket.on('netlist', function(msg){ + //Replacing to be created filenames in the generated netlist, so as to identify the client + var update = msg.replace('dumpv.txt', dumpv); + var result = update.replace('dumpi.txt', dumpi); + + fs.writeFile(analysisFile, result, function(err){ + if(err){ + return console.log(err); + } + }); + + executeNgspiceNetlist(analysisFile); }); - fs.stat('/tmp' + socket.id.toLowerCase() + '-dumpi.txt', function(err, stat){ + }); + + function deleteOnExit(filename){ + fs.stat(filename, function(err, stat){ if(err == null){ - fs.unlink('/tmp' + socket.id.toLowerCase() + '-dumpi.txt'); + fs.unlink(filename); + console.log('File: ' + filename + ' deleted.') } - - }); - - }); - socket.on('netlist', function(msg){ - var update = msg.replace('dumpv', '/tmp' + socket.id.toLowerCase() + '-dumpv'); - var result = update.replace('dumpi', '/tmp' + socket.id.toLowerCase() + '-dumpi'); - fs.writeFile('/tmp/' + socket.id + '.cir.out', result, function(err){ - if(err){ - return console.log(err); - } - }); - exec('ngspice ' + '/tmp/' + socket.id + '.cir.out'); + else + console.log('Unable to delete file : ' + filename + '\n' + err ); + }); + } + + function getSocketID(socket){ + socketID = socket.id; + //Removing first two char i.e '/#' from socket id + socketID = socketID.substring(2); + console.log("Return :"+socketID) + return socketID; + } + + + function executeNgspiceNetlist(fileName) + { + fs.exists(fileName, function(exists) { + if (exists) { + exec('ngspice '+fileName, function(code, stdout, stderr) { + console.log('Exit code:', code); + console.log('Program output:', stdout); + console.log('Program stderr:', stderr); + + if(stderr){ + switch(stderr){ + case (stderr.match(/Error/) || stderr.match(/error/)||{}).input: + console.log("Error in executing ngspice netlist"); + socket.emit('serverMessage','Error while executing ngspice netlist: '+stderr); + break; + default: + break; + } + } + }); + } + }); + + } - }); -}); } diff --git a/views/script/savenetlist.js b/views/script/savenetlist.js index befc939..43aece6 100644 --- a/views/script/savenetlist.js +++ b/views/script/savenetlist.js @@ -6,11 +6,11 @@ For Simulation of Netlist and Removal of netlist Window jQuery("#webtronics_netlist_simulate").click(function(){ socket.emit('netlist', jQuery("#webtronics_netlist_text_area").val()); - console.log("simulation button clicked"); - // jQuery('#webtronics_netlist_text_div').hide(); - // jQuery('#webtronics_netlist_buttons').hide(); - // jQuery('#webtronics_netlist_text').hide(); - // jQuery('#webtronics_disable').hide(); + //console.log("simulation button clicked"); + jQuery('#webtronics_netlist_text_div').hide(); + jQuery('#webtronics_netlist_buttons').hide(); + jQuery('#webtronics_netlist_text').hide(); + jQuery('#webtronics_disable').hide(); |