#!/usr/bin/node // // // // // data_collector_cgi source // // // // // // // File: data_collector_cgi // // Author: Francesco.Prelz@mi.infn.it // // Description: Sample CGI program built with server-side Javascript // (node.js) that forks a background network listener that collects data // and serves such data in JSON format to web clients. // // History: // 6-Oct-2014 Initial creation. // 10-Oct-2012 Moved away from node-cgi module. var fs = require('fs'); var path = require('path'); var childp = require('child_process'); var myself = path.basename(process.argv[1], ".nd"); var pidfile = "/var/tmp/" + myself + ".pid"; var xmldata = "/var/tmp/" + myself + "_data.xml"; var need_to_start_child = true; var child_pid; try { child_pid = fs.readFileSync(pidfile, "utf8"); child_pid = child_pid.replace(/[^0-9]*$/, ''); } catch (err) { child_pid = ""; } if (parseInt(child_pid, 10) > 0) { var child_in_proc = "/proc/"+child_pid; if (fs.existsSync(child_in_proc)) need_to_start_child = false; } if (need_to_start_child) { var child = childp.spawn("/home/guest/public_html/esercizi/wireless/jsFiles/data_collector_process", [ pidfile, xmldata ], { detached: true, stdio: [ 'ignore', 'ignore', 'ignore' ] }); child.unref(); } fs.exists(xmldata, function (file_exists) { process.stdout.write("Content-type: text/xml\n\n"); if (file_exists) { fs.createReadStream(xmldata).pipe(process.stdout); } else { process.stdout.write("<dataBlocks></dataBlocks>\n"); } });