00001
00002 var emitter = require( 'events' ).EventEmitter;
00003 var fs = require( 'fs' );
00004 var ac = new emitter( );
00005 var path = require( 'path' );
00006 var xml = require( 'xml2js' );
00007 var parser = new xml.Parser( { explicitArray: false } );
00008 var util = require( 'util' );
00009
00010 function generateDefaultConfig() {
00011 var output = {};
00012 output.expertMode = false;
00013 output.configName = "Default";
00014 output.artdaqDir = "/path/to/artdaq-demo-base";
00015 output.setupScript = "setupARTDAQDEMO";
00016 output.needsRestart = false;
00017 output.dataDir = "/tmp";
00018 output.logDir = "/tmp";
00019 output.dataLogger = {};
00020 output.dataLogger.enabled = true;
00021 output.dataLogger.fileValue = 0;
00022 output.dataLogger.fileMode = "Events";
00023 output.dataLogger.runValue = -1;
00024 output.dataLogger.runMode = "Events";
00025 output.dataLogger.hostname = "localhost";
00026 output.dataLogger.name = "DataLogger";
00027 output.onlineMonitor = {};
00028 output.onlineMonitor.enabled = false;
00029 output.onlineMonitor.viewerEnabled = false;
00030 output.onlineMonitor.hostname = "localhost";
00031 output.onlineMonitor.name = "OnlineMonitor";
00032 output.eventBuilders = {};
00033 output.eventBuilders.basename = "EVB";
00034 output.eventBuilders.count = 2;
00035 output.eventBuilders.compress = false;
00036 output.eventBuilders.hostnames = ["localhost","localhost"];
00037
00038 return output;
00039 }
00040
00041 function traverse( obj, parent ) {
00042 var output = "";
00043
00044
00045 for ( var key in obj ) {
00046 var keyPrint = key;
00047 if ( key.length == 1 && parseInt( key ) >= 0 ) {
00048 keyPrint = parent.slice( 0,parent.length - 1 );
00049 }
00050 if ( obj.hasOwnProperty( key ) ) {
00051
00052 output += "<" + keyPrint + ">";
00053 if ( obj[key] !== null && typeof ( obj[key] ) === "object" ) {
00054
00055
00056 output += "\n" + traverse( obj[key], key );
00057 } else {
00058
00059 output += obj[key];
00060 }
00061
00062 output += "</" + keyPrint + ">\n";
00063 }
00064 }
00065
00066 return output;
00067 }
00068
00069 function serializeXML( config,who,fileName ) {
00070 console.log( "Saving Configuration to " + fileName );
00071 var xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
00072
00073 var configurationXML = "<author>" + who + "</author>\n";
00074 configurationXML += traverse( config, "" );
00075
00076 console.log("Putting Generated configuration into artdaq-configuration block");
00077 xmlData += "<artdaq-configuration>\n" + configurationXML + "</artdaq-configuration>";
00078
00079 console.log( "Writing out file" );
00080 fs.writeFileSync( fileName,xmlData );
00081 console.log( "Done Writing Configuration" );
00082 return true;
00083 }
00084
00085 function checkBools( object ) {
00086 for ( var key in object ) {
00087 if ( object.hasOwnProperty( key ) ) {
00088 if ( object[key] === "true" ) {
00089 object[key] = true;
00090 } else if ( object[key] === "false" ) {
00091 object[key] = false;
00092 } else if ( object !== null && typeof ( object ) === "object" ) {
00093 object[key] = checkBools( object[key] );
00094 }
00095 }
00096 }
00097 return object;
00098 }
00099
00100 function deserializeXML( fileName ) {
00101 console.log( "Reading " + fileName + ":" );
00102 var xmlData = fs.readFileSync( fileName );
00103 var xmlObj;
00104 parser.parseString( xmlData,function ( err,result ) { xmlObj = result; } );
00105
00106 output = {};
00107 output.artdaqDir = xmlObj['artdaq-configuration'].artdaqDir;
00108 output.expertMode = xmlObj['artdaq-configuration'].expertMode;
00109 output.configName = xmlObj['artdaq-configuration'].configName;
00110 output.dataDir = xmlObj['artdaq-configuration'].dataDir;
00111 output.logDir = xmlObj['artdaq-configuration'].logDir;
00112 output.setupScript = xmlObj['artdaq-configuration'].setupScript;
00113 output.dataLogger = xmlObj['artdaq-configuration'].dataLogger;
00114 output.onlineMonitor = xmlObj['artdaq-configuration'].onlineMonitor;
00115 output.eventBuilders = xmlObj['artdaq-configuration'].eventBuilders;
00116 output.eventBuilders.hostnames = xmlObj['artdaq-configuration'].eventBuilders.hostnames.hostname;
00117 output.boardReaders = xmlObj['artdaq-configuration'].boardReaders.boardReader;
00118
00119 return checkBools( output );
00120 }
00121
00122 ac.MasterInitFunction = function ( workerData ) {
00123 workerData["artdaq-configuration"] = "ac";
00124 };
00125 ac.WorkerInitFunction = function () { return null; };
00126
00127 function getFiles( mask,dir ) {
00128 dir = typeof ( dir ) !== 'undefined' ? dir : __dirname;
00129
00130 console.log( "Directory is " + dir + ", and mask is " + mask );
00131 var output = [];
00132 var files = fs.readdirSync( dir );
00133 var f, l = files.length;
00134 for ( var i = 0; i < l; i++ ) {
00135 f = files[i].toString( );
00136 if ( f.search( mask ) > 0 ) {
00137 var cfgName = f.substring( 0,f.indexOf( mask ) );
00138 console.log( "Found file satisfying mask " + mask + ": " + f + " (" + cfgName + ")" );
00139 output.push( "<option value=\"" + cfgName +mask+ "\">" + cfgName + "</option>" );
00140 }
00141 }
00142 return output;
00143 }
00144
00145 ac.GET_Default = function () {
00146 return "";
00147 }
00148
00149 ac.GET_GeneratorTypes = function () {
00150 var types = [];
00151 types.push( "<option value=\"Default\">No Generator Selected</option>" );
00152 types.push( "<optgroup label=\"Simulators\">" );
00153 types = types.concat( getFiles( "_simulator.html" ,__dirname + "/../client" ));
00154 types.push( "</optgroup>" );
00155 types.push( "<optgroup label=\"Recievers\">" );
00156 types = types.concat( getFiles( "_receiver.html" ,__dirname + "/../client" ));
00157 types.push( "</optgroup>" );
00158 types.push( "<optgroup label=\"Readers\">" );
00159 types = types.concat( getFiles( "_reader.html" ,__dirname + "/../client" ));
00160 types.push( "</optgroup>" );
00161
00162 return types;
00163 }
00164
00165 ac.GET_NamedConfigs = function () {
00166 var configs = getFiles( ".xml" );
00167
00168 configs.push("<option value=\"Default\">Default Configuration</option>");
00169 if ( configs.length < 2 ) {
00170 console.log( "I found no named configs!" );
00171
00172 }
00173 return configs;
00174 };
00175
00176 ac.RW_saveConfig = function ( POST ) {
00177 var success = false;
00178 console.log( "Request to save configuration recieved. Configuration data:" );
00179 var config = JSON.parse( POST.config );
00180 if ( config.configName.search("\\.\\.") >= 0 ) {
00181 console.log("Possible break-in attempt! NOT Proceeding!");
00182 return { Success: false };
00183 }
00184 console.log( util.inspect( config,false,null ) );
00185
00186 success = serializeXML( config,POST.who,path.join( __dirname,config.configName + ".xml" ) );
00187 return { Success: success };
00188 }
00189
00190 ac.RO_LoadNamedConfig = function ( POST ) {
00191 console.log( "Request for configuration with file name \"" + POST.configFile + "\" received." );
00192 if ( POST.configFile === "Default" ) {
00193 return generateDefaultConfig( );
00194 } else if ( POST.configFile.search("\\.\\.") >= 0 ) {
00195 console.log("Possible break-in attempt! NOT Proceeding!");
00196 return "";
00197 }
00198 var output = deserializeXML( path.join( __dirname,POST.configFile ) );
00199 console.log( "Deserialized XML:" );
00200 console.log( output );
00201 return output;
00202 };
00203
00204 module.exports = function ( module_holder ) {
00205 module_holder["artdaq-configuration"] = ac;
00206 };