public GUISettings(Blackboard blackboard, string bbConfigFile) : this() { if(blackboard == null) throw new ArgumentNullException(); this.verbosityLevel = blackboard.VerbosityLevel; this.conigurationFileName = bbConfigFile; }
public Kernel(Blackboard blk) { this.blackboard = blk; this.procMan = new ModuleProcessManager((Robotics.Utilities.LogWriter)blackboard.Log); this.commands = new Dictionary<string, StringEventHandler>(); this.tracers = new Dictionary<string, SharedVariableWrittenEventHandler>(); this.rxCommandSplitter = new Regex(@"^(?<cmd>\w+)(\s+(?<par>.*))?$"); RegisterCommands(); RegisterSystemPrototypes(); }
public ConsoleManager(Blackboard blk, Kernel kernel, ConsoleLogWriter log) { this.kernel = kernel; this.blackboard = blk; this.log = log; this.currentLine = String.Empty; this.tabSw = new Stopwatch(); this.firstTab = false; this.history = new CommandHistoryManager(); this.completionTree = new CompletionTree(); this.kernel.ConsoleManager = this; this.kernel.FillCompletionTree(this.completionTree); }
/// <summary> /// Fetch the blackboard test time out /// </summary> /// <param name="doc">XML document which contains the Blackboard data</param> /// <param name="blackboard">The blackboard which data will be set</param> /// <param name="log">The log writer</param> private static void LoadBlackboardTestTimeOut(XmlDocument doc, Blackboard blackboard, ILogWriter log) { string sTime; int iTime; TimeSpan time; if (doc.GetElementsByTagName("testTimeOut").Count == 1) { sTime = doc.GetElementsByTagName("testTimeOut")[0].InnerText.Trim(); if (Int32.TryParse(sTime, out iTime) && (iTime > 0)) { blackboard.TestTimeOut = new TimeSpan(0, 0, iTime); log.WriteLine(1, "Blackboard Test Timeout: " + blackboard.TestTimeOut); } else if (TimeSpan.TryParse(sTime, out time) && (time > TimeSpan.Zero)) { blackboard.TestTimeOut = time; log.WriteLine(1, "Blackboard Test Timeout: " + time); } else log.WriteLine(1, "Blackboard Test Timeout disabled"); } else log.WriteLine(1, "Blackboard Test Timeout disabled"); }
/// <summary> /// Loads the Blackboard from the config file /// </summary> /// <returns>true if the Blackboard was leaded successfully, false otherwise</returns> private bool LoadBlackboard() { FileInfo fi; try { fi = new FileInfo(this.configFile); this.configFile = fi.FullName; SetupLogfile(); log.WriteLine("Loading Blackboard"); this.blackboard = Blackboard.FromXML(this.configFile, log); this.blackboard.StartupSequence.Method = Blk.Engine.Remote.ModuleStartupMethod.None; blackboard.VerbosityLevel = 1; SetupBlackboardModules(); log.WriteLine("Blackboard loaded"); } catch (Exception ex) { log.WriteLine("Error loading blackboard"); log.WriteLine("\t" + ex.Message.Replace("\n", "\n\t")); return false; } return true; }
/// <summary> /// Converts the string representation of a command to a Command object. /// A return value indicates whether the conversion succeded or not. /// </summary> /// <param name="s">A string containing the command to convert</param> /// <param name="blackboard">A blackboard which contains all information about modules</param> /// <param name="cmd">When this method returns, contains the Command equivalent to the command /// contained in s, if the conversion succeeded, or null if the conversion failed. /// The conversion fails if the s parameter is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// <param name="ex">When this method returns, contains the Exception generated during the parse operation, /// if the conversion failed, or null if the conversion succeeded. The conversion fails if the s parameter /// is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// <returns>true if conversion was successfull, false otherwise</returns> protected static bool TryParse(string s, Blackboard blackboard, out Command cmd, out Exception ex) { Regex rx; Match m; Prototype proto; //Module source; //Module destination; IModule source; IModule destination; string sCommand; string sParams; string sSrc; string sDest; string sId; int id; cmd = null; ex = null; // Regular Expresion Generation //rx = new Regex(@"((?<src>[\w\-]+)(\s+(?<dest>[\w\-]+))?\s+)?(?<cmd>[A-Za-z_]+)\s+(""(?<params>[^""]*)"")?\s+(@(?<id>\d+))?"); rx = rxCommandFromBlackboard; m = rx.Match(s); // Check if input string matchs Regular Expression if (!m.Success) { ex = new ArgumentException("Invalid String", "s"); return false; } // Extract Data sCommand = m.Result("${cmd}").ToLower(); sParams = m.Result("${params}"); sId = m.Result("${id}"); sSrc = m.Result("${src}"); sDest = m.Result("${dest}"); if ((sId == null) || (sId.Length < 1)) id = -1; else id = Int32.Parse(sId); // Check if source module is specified if ((sSrc == null) || (sSrc.Length < 1) || !blackboard.Modules.Contains(sSrc)) { ex = new Exception("No source module found for command provided"); return false; } source = blackboard.Modules[sSrc]; // Browse for destination module and prototype if (!blackboard.FindDestinationModule(sCommand, out destination, out proto)) //throw new Exception("No destination module found for command provided"); { // No destination module found. Must check if destination is availiable if ((sDest == null) || (sDest.Length < 1) || !blackboard.Modules.Contains(sDest)) { ex = new Exception("No destination module found for command provided"); return false; } destination = blackboard.Modules[sDest]; proto = null; } // Check if command matchs a prototype if ((proto != null) && proto.ParamsRequired && ((sParams == null) || (sParams.Length < 1))) { ex = new Exception("Invalid string. The Command requires parameters"); return false; } // Create the Command cmd = new Command(source, destination, sCommand, sParams, id); cmd.prototype = proto; cmd.sentTime = DateTime.Now; return true; }
/// <summary> /// Converts the string representation of a command to a Command object. /// <param name="s">A string containing the command to convert</param> /// <param name="blackboard">A blackboard which contains all information about modules</param> /// </summary> /// <returns>A command object that represents the command contained in s</returns> public static Command Parse(string s, Blackboard blackboard) { Exception ex; Command command; if (!TryParse(s, blackboard, out command, out ex)) throw ex; else return command; }
/// <summary> /// Adds a module to the blackboard /// </summary> private static void AddModuleToBlackboard(Blackboard blackboard, ILogWriter log, ModuleClient mod, SortedList<string, ModuleClient> disabledModules) { if (mod.Enabled) { blackboard.modules.Add(mod); log.WriteLine(2, "Loading module complete!"); } else { disabledModules.Add(mod.Name, mod); log.WriteLine(2, "Disabled module enqueued!"); } }
/// <summary> /// Creates a blackboard from a XML configuration file /// </summary> /// <param name="path">The path of the XML configuration file</param> /// <param name="log">Output Log</param> /// <returns>The configured blackboard</returns> public static Blackboard FromXML(string path, ILogWriter log) { #region Variables Blackboard blackboard; XmlDocument doc; XmlDocument tmpDoc; int clientModulesAddedd; #endregion blackboard = new Blackboard(); //if ((Log != null) && (Log != TextWriter.Null)) blackboard.Log = Log; if (log != null) blackboard.Log = log; else log = blackboard.Log; log.VerbosityTreshold = blackboard.verbosity; if (!File.Exists(path)) { log.WriteLine(0, "File does not exist"); throw new FileLoadException("File does not exist"); } doc = new XmlDocument(); doc.Load(path); if ( (doc.GetElementsByTagName("blackboard").Count != 1) || (doc.GetElementsByTagName("configuration").Count != 1) || (doc.GetElementsByTagName("modules").Count < 1)) { log.WriteLine(0, "Incorrect format"); throw new FileLoadException("Incorrect format"); } LoadBlackboardConfiguration(blackboard, doc, log); clientModulesAddedd = LoadBlackboardModules(blackboard, doc, log); #region Move program start actions to be executed by the virtual module foreach (ModuleClient mc in blackboard.modules) { if(mc == blackboard.virtualModule) continue; MoveActions(mc.RestartActions, blackboard.virtualModule.RestartActions); MoveActions(mc.RestartTestActions, blackboard.virtualModule.RestartTestActions); MoveActions(mc.StartupActions, blackboard.virtualModule.StartupActions); MoveActions(mc.StopActions, blackboard.virtualModule.StopActions); MoveActions(mc.TestTimeOutActions, blackboard.virtualModule.TestTimeOutActions); } #endregion #region Load of shared variables if (doc.GetElementsByTagName("sharedVariables").Count == 1) { tmpDoc = new XmlDocument(); tmpDoc.LoadXml(doc.GetElementsByTagName("sharedVariables")[0].OuterXml); SetupSharedVariables(tmpDoc, blackboard); } #endregion if (clientModulesAddedd < 1) throw new Exception("No modules has been added"); log.WriteLine(0, "Load complete!"); return blackboard; }
/// <summary> /// Creates and configures the shared variables specified in the provided XML document for the specified blackboard /// </summary> /// <param name="doc">XML document which contains shared variables initialization data</param> /// <param name="blackboard">Blackboard object in which the shared variables will be configured</param> private static void SetupSharedVariables(XmlDocument doc, Blackboard blackboard) { if ((doc == null) || (blackboard == null)) throw new ArgumentNullException(); if (blackboard.virtualModule == null) throw new Exception("Uninitialized blackboard"); int i, j; ILogWriter log; XmlDocument tmpDoc; XmlNode node; XmlNodeList xmlVarList; XmlNodeList xmlWriterModuleList; SharedVariable shVar; List<string> writers; string shVarName; string shVarType; string shVarValue; bool shVarIsArray; int bracketPos; log = blackboard.log; tmpDoc = new XmlDocument(); if (doc.GetElementsByTagName("sharedVariables").Count < 1) { log.WriteLine(1, "No shared variables was defined in XML file."); return; } tmpDoc.LoadXml(doc.GetElementsByTagName("sharedVariables")[0].OuterXml); #region Load of shared variables // Load of shared variables log.WriteLine(1, "Reading shared variables"); xmlVarList = tmpDoc.GetElementsByTagName("var"); for (i = 0; i < xmlVarList.Count; ++i) { #region Get variable Name node = xmlVarList[i]; if ((node == null) || (node.Name != "var") || (node.Attributes["name"] == null)) continue; shVarName = node.Attributes["name"].InnerText; shVarType = (node.Attributes["type"] != null) ? node.Attributes["type"].InnerText : "var"; if (String.IsNullOrEmpty(shVarType) || !SharedVariable.RxVarTypeValidator.IsMatch(shVarType)) shVarType = "var"; bracketPos = shVarType.IndexOf("["); if (shVarIsArray = (bracketPos != -1)) shVarType = shVarType.Remove(bracketPos); if (blackboard.VirtualModule.SharedVariables.Contains(shVarName)) { log.WriteLine(2, "Error loading shared variable " + shVarName + ". Variable already exists."); continue; } #endregion #region Get variable initial value shVarValue = ""; if (node.Attributes["value"] != null) { shVarValue = node.Attributes["value"].Value; } else if (node.Attributes["fromFile"] != null) { shVarValue = node.Attributes["fromFile"].Value; if (File.Exists(shVarValue)) { try { shVarValue = File.ReadAllText(shVarValue); } catch { log.WriteLine(2, "Error loading variable content from file " + shVarValue + " for the shared variable " + shVarName + ". Variable was set to null"); shVarValue = ""; } } } #endregion #region Get list of modules with write permission writers = new List<string>(); tmpDoc = new XmlDocument(); tmpDoc.LoadXml(node.OuterXml); xmlWriterModuleList = tmpDoc.GetElementsByTagName("writers"); if (xmlWriterModuleList.Count == 1) { writers.Add(blackboard.VirtualModule.Name); tmpDoc = new XmlDocument(); tmpDoc.LoadXml(xmlWriterModuleList[0].OuterXml); xmlWriterModuleList = tmpDoc.GetElementsByTagName("writer"); for (j = 0; j < xmlWriterModuleList.Count; ++j) { node = xmlWriterModuleList[j]; if (node.InnerText == "*") { writers.Clear(); writers.Add("*"); break; } if (!blackboard.modules.Contains(node.InnerText)) continue; else writers.Add(node.InnerText); /* if (!writers.Contains(node.InnerText)) writers.Add(node.InnerText); */ } } else writers.Add("*"); #endregion #region Create and add the shared variable shVar = new SharedVariable(blackboard.VirtualModule, shVarType, shVarName, shVarIsArray, -1); //shVar.Data = shVarValue; shVar.WriteStringData(blackboard.VirtualModule, shVarType, -1, shVarValue); writers.Sort(); shVar.AllowedWriters = writers; blackboard.VirtualModule.SharedVariables.Add(shVar); log.WriteLine(4, "Added shared variable " + shVarType +" "+ shVarName); #endregion } #endregion }
/// <summary> /// Converts the string representation of a response to a Response object. /// A return value indicates whether the conversion succeded or not. /// <param name="s">A string containing the response to convert</param> /// <param name="blackboard">A blackboard which contains all information about modules</param> /// <param name="result">When this method returns, contains the Response equivalent to the response /// contained in s, if the conversion succeeded, or null if the conversion failed. /// The conversion fails if the s parameter is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// </summary> /// <returns>true if conversion was successfull, false otherwise</returns> public static bool TryParse(string s, Blackboard blackboard, out Response result) { Exception ex; return TryParse(s, blackboard, out result, out ex); }
/// <summary> /// Converts the string representation of a response to a Response object. /// <param name="s">A string containing the response to convert</param> /// <param name="blackboard">A blackboard which contains all information about modules</param> /// </summary> /// <returns>A response object that represents the response contained in s</returns> public static Response Parse(string s, Blackboard blackboard) { Exception ex; Response response; if (!TryParse(s, blackboard, out response, out ex)) throw ex; else return response; }
/// <summary> /// Initializes a new instance of BlackboardConfig /// </summary> public BlackboardWrapper(Blackboard blackboard) { this.blackboard = blackboard; }
/// <summary> /// Initializes a new instance of BlackboardConfig /// </summary> public BlackboardWrapper() { this.blackboard = null; }
private void SetupBlackboard() { SetupLogfile(); tbsw = new XmlTextBoxStreamWriter(txtOutputLog, bbLogFile, 1024); tbsw.AppendDate = true; log = new LogWriter(tbsw); processManager = new ModuleProcessManager(log); txtOutputLog.Clear(); lvwRedirectionHistory.Items.Clear(); txtOutputLog.AppendText("Loading Blackboard\r\n"); blackboard = Blackboard.FromXML(ConfigFile, log); blackboard.VerbosityLevel = 1; //blackboard = Blackboard.FromXML("bb.xml"); LoadPlugins(); interactionTool.Blackboard = blackboard; SetupBlackboardModules(); SetupBlackboardEvents(); //tcLog.Enabled = true; BlackboardControlsEnabled = true; txtOutputLog.AppendText("Blackboard loaded\r\n"); if(chkAutostart.Checked) Start(); }
private static bool LoadModule(Blackboard blackboard, XmlNode modules, ILogWriter log, SortedList<string, ModuleClient> disabledModules) { XmlNode node; string moduleName; bool moduleEnabled; string processName; string programPath; string programArgs; string moduleAlias; string moduleAuthor; bool aliveCheck; bool requirePrefix; int sendDelay; bool simulate; double simulationSuccessRatio; ModuleSimulationOptions simOptions; List<IPAddress> ips; int port; int checkInterval; ModuleClient mod; XmlDocument tmpDoc; try { // Module check if ((modules.Name != "module") || (modules.Attributes.Count < 1) || (modules.Attributes["name"] == null) || (modules.Attributes["name"].Value.Length < 1)) return false; #region Module information extraction FetchModuleInfo(modules, out moduleName, out moduleEnabled, out moduleAlias, out moduleAuthor); log.WriteLine(1, "Loading module " + moduleName + (moduleAlias != moduleName ? " alias " + moduleAlias : "")); // Create a XML sub-document XML tmpDoc = new XmlDocument(); tmpDoc.LoadXml(modules.OuterXml); #region Get program path and program arguments FetchProgramInfo(tmpDoc, out processName, out programPath, out programArgs); #endregion // Leo el comando de inicio //if (tmpDoc.GetElementsByTagName("startupCommand").Count != 0) // startupMessage = Command.Parse(tmpDoc.GetElementsByTagName("startupCommand")[0].InnerText); // Get the array of ip addresses where the module can be if (!FetchModuleConnectionSettings(tmpDoc, log, out ips, out port)) return false; // Veify if Blackbard must check Module's alive status FetchModuleStatusCheckSettings(tmpDoc, out aliveCheck, out checkInterval); // Verify if the Module requires SOURCE DESTINATION prefix if ( (tmpDoc.GetElementsByTagName("requirePrefix").Count == 0) || !Boolean.TryParse(tmpDoc.GetElementsByTagName("requirePrefix")[0].InnerText, out requirePrefix) ) requirePrefix = false; // Delay between send operations if ( (tmpDoc.GetElementsByTagName("sendDelay").Count == 0) || !Int32.TryParse(tmpDoc.GetElementsByTagName("sendDelay")[0].InnerText, out sendDelay) ) sendDelay = -1; // Simulation options simOptions = ModuleSimulationOptions.SimulationDisabled; if (tmpDoc.GetElementsByTagName("simulate").Count != 0) { node = tmpDoc.GetElementsByTagName("simulate")[0]; simulationSuccessRatio = -1; if ((node.Attributes["successRatio"] == null) || !Double.TryParse(node.Attributes["successRatio"].InnerText, out simulationSuccessRatio) || (simulationSuccessRatio < 0) || (simulationSuccessRatio > 1)) simulationSuccessRatio = -1; if (Boolean.TryParse(node.InnerText, out simulate)) simOptions = (simulationSuccessRatio != -1) ? new ModuleSimulationOptions(simulationSuccessRatio) : new ModuleSimulationOptions(simulate); } #endregion #region Module Validation ValidateModuleName(blackboard, log, ref moduleName, ref moduleEnabled, moduleAlias, disabledModules); #endregion // Module Creation mod = new ModuleClientTcp(moduleName, ips, port); mod.Enabled = moduleEnabled; mod.Author = moduleAuthor; mod.ProcessInfo.ProcessName = processName; mod.ProcessInfo.ProgramPath = programPath; mod.ProcessInfo.ProgramArgs = programArgs; mod.AliveCheck = aliveCheck; mod.RequirePrefix = requirePrefix; mod.SendDelay = sendDelay; mod.Simulation = simOptions; mod.Alias = moduleAlias; mod.CheckInterval = checkInterval; #region Actions Extraction // Startup actions extraction LoadModuleActions("onStart", "Startup", tmpDoc, mod.StartupActions, log); // Restart actions extraction LoadModuleActions("onRestart", "Restart", tmpDoc, mod.RestartActions, log); // Restart Test actions extraction LoadModuleActions("onRestartTest", "Restart-Test", tmpDoc, mod.RestartTestActions, log); // Stop actons extraction LoadModuleActions("onStop", "Stop", tmpDoc, mod.StopActions, log); // Test Timeout actons extraction LoadModuleActions("onTestTimeOut", "Test Timeout", tmpDoc, mod.TestTimeOutActions, log); #endregion //Extract Module Commands ExtractPrototypes(mod, tmpDoc, log); //if(mod.Prototypes.Count < 1) // return false; // Add module to blackboard AddModuleToBlackboard(blackboard, log, mod, disabledModules); } catch { // Error al cargar el modulo log.WriteLine(2, "Invalid module"); // Continuo con el siguiente return false; } return mod.Enabled; }
/// <summary> /// Loads the startup sequence of the blackboard modules /// </summary> /// <param name="blackboard">The blackboard where will be loaded the startup sequence</param> /// <param name="doc">The xml document from which the actions will be loaded</param> /// <param name="log">The log writer</param> private static void LoadStartupSequence(Blackboard blackboard, XmlDocument doc, ILogWriter log) { XmlNodeList modules; string moduleName; if (doc.GetElementsByTagName("startupSequence").Count != 1) { log.WriteLine(1, "Startup sequence not found"); return; } modules = doc.GetElementsByTagName("startupSequence")[0].ChildNodes; for(int i = 0; i < modules.Count; ++i) { try { // Module check if ((modules[i].Name != "module") || //(modules[i].Attributes.Count < 1) || //(modules[i].Attributes["name"].Value.Length < 1) || (modules[i].InnerText == null) || (modules[i].InnerText.Length < 1)) continue; //moduleName = modules[i].Attributes["name"].Value.ToUpper(); moduleName = modules[i].InnerText.ToUpper(); if(blackboard.StartupSequence.ModuleSequence.Contains(moduleName)) continue; blackboard.StartupSequence.ModuleSequence.Add(moduleName); } catch { continue; } } log.WriteLine(1, "Startup sequence: " + String.Join(", ", blackboard.StartupSequence.ModuleSequence.ToArray())); }
/// <summary> /// Converts the string representation of a response to a Response object. /// A return value indicates whether the conversion succeded or not. /// <param name="s">A string containing the response to convert</param> /// <param name="blackboard">A blackboard which contains all information about modules</param> /// <param name="response">When this method returns, contains the Response equivalent to the response /// contained in s, if the conversion succeeded, or null if the conversion failed. /// The conversion fails if the s parameter is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// <param name="ex">When this method returns, contains the Exception generated during the parse operation, /// if the conversion failed, or null if the conversion succeeded. The conversion fails if the s parameter /// is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// </summary> /// <returns>true if conversion was successfull, false otherwise</returns> protected static bool TryParse(string s, Blackboard blackboard, out Response response, out Exception ex) { Regex rx; Match m; Prototype proto; //Module source; IModule source; //Module destination; IModule destination; string sCommand; string sParams; string sSrc; string sDest; string sId; string sResult; bool result; int id; ex = null; response = null; // Regular Expresion Generation //rx = new Regex(@"((?<src>[\w\-]+)(\s+(?<dest>[\w\-]+))?\s+)?(?<cmd>[A-Za-z_]+)\s+(""(?<params>[^""]*)"")?\s+(@(?<id>\d+))?"); rx = rxResponseFromBlackboard; m = rx.Match(s); // Check if input string matchs Regular Expression if (!m.Success) { ex = new ArgumentException("Invalid String", "s"); return false; } // Extract Data sCommand = m.Result("${cmd}").ToLower(); sParams = m.Result("${params}"); sId = m.Result("${id}"); sSrc = m.Result("${src}"); sDest = m.Result("${dest}"); sResult = m.Result("${result}"); if ((sResult == null) || ((sResult != "1") && (sResult != "0"))) { ex = new Exception("Invalid string. No suitable result value found"); return false; } result = (sResult == "1"); if ((sId == null) || (sId.Length < 1)) id = -1; else id = Int32.Parse(sId); // Check if source module is specified if ((sSrc == null) || (sSrc.Length < 1) || !blackboard.Modules.Contains(sSrc)) { ex = new Exception("No source module found for response provided"); return false; } source = blackboard.Modules[sSrc]; // Browse for destination module and prototype if (!blackboard.FindDestinationModule(sCommand, out source, out proto)) //Since the response arrived from a module which can't generate it a exception is rised { ex = new Exception("Sender module and generator module does not match"); return false; } // if destination module is specified, then it mus be set if ((sDest != null) && (sDest.Length > 0) && source.Parent.Modules.Contains(sDest)) destination = source.Parent.Modules[sDest]; // Else, since there is no way at this point to determine which is destination module for response // and this is no specified, it is set to null, to allow blackboard to find an apropiate one else destination = null; // Check if response matchs a prototype if ((proto != null) && proto.ParamsRequired && ((sParams == null) || (sParams.Length < 1))) { ex = new Exception("Invalid string. The Response requires parameters"); return false; } // Create the Response response = new Response(source, destination, sCommand, sParams, result, id); response.prototype = proto; if (!response.success) response.failReason = ResponseFailReason.ExecutedButNotSucceded; return true; }
/// <summary> /// Validates the name of a module and renames it until a valid name is found /// </summary> private static void ValidateModuleName(Blackboard blackboard, ILogWriter log, ref string moduleName, ref bool moduleEnabled, string moduleAlias, SortedList<string, ModuleClient> disabledModules) { // Module validation. // If a module with the same name exists, rename the current module and disable it if (blackboard.Modules.Contains(moduleName) || disabledModules.ContainsKey(moduleName)) { int n = 1; string newModuleName = moduleName + n.ToString().PadLeft(2, '0'); while (blackboard.Modules.Contains(newModuleName) || disabledModules.ContainsKey(newModuleName)) { ++n; newModuleName = moduleName + n.ToString().PadLeft(2, '0'); } log.WriteLine(1, "Module " + moduleName + " already exists. Renamed to " + newModuleName + (moduleAlias != moduleName ? " alias " + moduleAlias : "")); moduleName = newModuleName; moduleEnabled = false; } }
/// <summary> /// Converts the string representation of a response to a Response object. /// A return value indicates whether the conversion succeded or not. /// <param name="s">A string containing the response to convert</param> /// <param name="blackboard">A blackboard which contains all information about modules</param> /// <param name="response">When this method returns, contains the Response equivalent to the response /// contained in s, if the conversion succeeded, or null if the conversion failed. /// The conversion fails if the s parameter is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// <param name="ex">When this method returns, contains the Exception generated during the parse operation, /// if the conversion failed, or null if the conversion succeeded. The conversion fails if the s parameter /// is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// </summary> /// <returns>true if conversion was successfull, false otherwise</returns> protected static bool TryParse(string s, Blackboard blackboard, out Response response, out Exception ex) { IPrototype proto; IModuleClient source; IModuleClient destination; string sCommand; string sParams; string sSrc; string sDest; bool result; int iResult; int id; ex = null; response = null; // Extract Data CommandBase.XtractCommandElements(s, out sSrc, out sDest, out sCommand, out sParams, out iResult, out id); if ((sCommand == null) || (iResult == -1)) { ex = new ArgumentException("Invalid String", "s"); return false; } result = iResult == 1; // Check if source module is specified if ((sSrc == null) || (sSrc.Length < 1) || !blackboard.Modules.Contains(sSrc)) { ex = new Exception("No source module found for response provided"); return false; } source = blackboard.Modules[sSrc]; // Browse for destination module and prototype if (!blackboard.FindDestinationModule(sCommand, out source, out proto)) //Since the response arrived from a module which can't generate it a exception is rised { ex = new Exception("Sender module and generator module does not match"); return false; } // if destination module is specified, then it mus be set if ((sDest != null) && (sDest.Length > 0) && source.Parent.Modules.Contains(sDest)) destination = source.Parent.Modules[sDest]; // Else, since there is no way at this point to determine which is destination module for response // and this is no specified, it is set to null, to allow blackboard to find an apropiate one else destination = null; // Check if response matchs a prototype if ((proto != null) && proto.ParamsRequired && ((sParams == null) || (sParams.Length < 1))) { ex = new Exception("Invalid string. The Response requires parameters"); return false; } // Create the Response response = new Response(source, destination, sCommand, sParams, result, id); response.prototype = proto; if (!response.success) response.failReason = ResponseFailReason.ExecutedButNotSucceded; return true; }
private static void AddDisabledModules(Blackboard blackboard, SortedList<string, ModuleClient> disabledModules, ILogWriter log) { log.WriteLine(2, "Adding disabled modules"); for (int i = 0; i < disabledModules.Count; ++i) { try { ModuleClient mod = disabledModules.Values[i]; blackboard.Modules.Add(mod); log.WriteLine(3, "Added module " + mod.Name + (mod.Alias != mod.Name ? " alias " + mod.Alias : String.Empty)); } catch { } } }
/// <summary> /// Loads general blackboard configuration fro the provided XML document /// </summary> /// <param name="blackboard">The blackboard where will be loaded the settings</param> /// <param name="doc">The xml document from which the settings will be loaded</param> /// <param name="log">The log writer</param> private static void LoadBlackboardConfiguration(Blackboard blackboard, XmlDocument doc, ILogWriter log) { XmlDocument tmpDoc; ModuleClient mod; int sendAttempts; int globalCheckInterval; int moduleLoadDelay; log.WriteLine(0, "Loading configuration..."); tmpDoc = new XmlDocument(); tmpDoc.LoadXml(doc.GetElementsByTagName("configuration")[0].OuterXml); #region Blackboard Input port, Auto-Stop Time, Test Timeout and Name LoadBlackboardPort(tmpDoc, blackboard, log); LoadBlackboardAutoStopTime(tmpDoc, blackboard, log); LoadBlackboardTestTimeOut(doc, blackboard, log); LoadBlackboardName(doc, blackboard, log); #endregion #region Startup/Shutdown sequence LoadStartupSequence(blackboard, doc, log); LoadShutdownSequence(blackboard, doc, log); #endregion #region Other Configurations // Read global alive/busy/ready check interval if ((tmpDoc.GetElementsByTagName("globalCheckInterval").Count == 0) || !Int32.TryParse(tmpDoc.GetElementsByTagName("globalCheckInterval")[0].InnerText, out globalCheckInterval) || (globalCheckInterval < ModuleClient.MinCheckInterval) || (globalCheckInterval > ModuleClient.MaxCheckInterval)) { log.WriteLine(1, "No alive/busy/ready check interval specified (or invalid), using default: " + ModuleClient.GlobalCheckInterval); } else { ModuleClient.GlobalCheckInterval = globalCheckInterval; log.WriteLine(1, "alive/busy/ready check interval: " + ModuleClient.GlobalCheckInterval); } // Module load delay if ((tmpDoc.GetElementsByTagName("moduleLoadDelay").Count == 0) || !Int32.TryParse(tmpDoc.GetElementsByTagName("moduleLoadDelay")[0].InnerText, out moduleLoadDelay) || (moduleLoadDelay < 0) || (moduleLoadDelay > 10000)) { log.WriteLine(1, "Module load delay not specified (or invalid), using default: " + blackboard.ModuleLoadDelay.ToString()); } else { blackboard.ModuleLoadDelay = moduleLoadDelay; log.WriteLine(1, "Module load delay: " + blackboard.ModuleLoadDelay.ToString()); } // Leo intentos de reenvio if ((tmpDoc.GetElementsByTagName("sendAttempts").Count == 0) || !Int32.TryParse(tmpDoc.GetElementsByTagName("sendAttempts")[0].InnerText, out sendAttempts) || (sendAttempts < 0) && (sendAttempts > 5)) { log.WriteLine(1, "No response send attempts specified (or invalid), using default: " + blackboard.SendAttempts); } else { blackboard.sendAttempts = sendAttempts; log.WriteLine(1, "Response send attempts: " + blackboard.SendAttempts); } #endregion #region Actions Extraction mod = blackboard.VirtualModule; // Startup actions extraction LoadModuleActions("onStart", "Startup", tmpDoc, mod.StartupActions, log); // Restart actions extraction LoadModuleActions("onRestart", "Restart", tmpDoc, mod.RestartActions, log); // Restart Test actions extraction LoadModuleActions("onRestartTest", "Restart-Test", tmpDoc, mod.RestartTestActions, log); // Stop actons extraction LoadModuleActions("onStop", "Stop", tmpDoc, mod.StopActions, log); // Test Timeout actons extraction LoadModuleActions("onTestTimeOut", "Test Timeout", tmpDoc, mod.TestTimeOutActions, log); #endregion }
/// <summary> /// Creates a module using data contained in a xml module /// </summary> /// <param name="blackboard">The blackboard which will contain the module</param> /// <param name="xmlModuleNode">Xml node used to fetch the required module information.</param> /// <param name="log">The log writer</param> /// <returns>The module created with the data contained in the xml document</returns> private static ModuleClient CreateModule(Blackboard blackboard, XmlNode xmlModuleNode, ILogWriter log) { XmlDocument tmpDoc; XmlNode node; ModuleClient mod; string moduleName; bool moduleEnabled; string moduleAlias; bool aliveCheck; bool requirePrefix; int sendDelay; bool simulate; double simulationSuccessRatio; ModuleSimulationOptions simOptions; string processName; string programPath; string programArgs; List<IPAddress> ips; int port; int checkInterval; // Enabled moduleEnabled = true; if ((xmlModuleNode.Attributes["enabled"] != null) && Boolean.TryParse(xmlModuleNode.Attributes["enabled"].Value, out moduleEnabled) && !moduleEnabled) return null; moduleName = xmlModuleNode.Attributes["name"].Value.ToUpper(); // Alias if (xmlModuleNode.Attributes["alias"] != null) moduleAlias = xmlModuleNode.Attributes["alias"].Value; else moduleAlias = moduleName; log.WriteLine(1, "Loading module " + moduleName + (moduleAlias != moduleName ? " alias " + moduleAlias : "")); // Create a XML sub-document XML tmpDoc = new XmlDocument(); tmpDoc.LoadXml(xmlModuleNode.OuterXml); #region Get program path and program arguments FetchProgramInfo(tmpDoc, out processName, out programPath, out programArgs); #endregion // Leo el comando de inicio //if (tmpDoc.GetElementsByTagName("startupCommand").Count != 0) // startupMessage = Command.Parse(tmpDoc.GetElementsByTagName("startupCommand")[0].InnerText); // Get the array of ip addresses where the module can be ips = FetchIpAddresses(tmpDoc); if ((ips == null) || (ips.Count < 1)) { log.WriteLine(2, "\tNo valid IP Address provided"); log.WriteLine(1, "Module skipped"); return null; } // Leo el puerto de conexion del modulo if ( (tmpDoc.GetElementsByTagName("port").Count == 0) || !Int32.TryParse(tmpDoc.GetElementsByTagName("port")[0].InnerText, out port) || (port <= 1024)) { log.WriteLine(2, "\tInvalid port"); log.WriteLine(1, "Module skipped"); return null; } log.WriteLine("\t" + ips[0].ToString() + ":" + port); // Veify if Blackbard must check Module's alive status checkInterval = ModuleClient.GlobalCheckInterval; if (tmpDoc.GetElementsByTagName("aliveCheck").Count != 0) { node = tmpDoc.GetElementsByTagName("aliveCheck")[0]; if (!Boolean.TryParse(node.InnerText, out aliveCheck)) aliveCheck = true; // Read alive/busy/ready check interval if ((node.Attributes["interval"] == null) || !Int32.TryParse(node.Attributes["interval"].InnerText, out checkInterval) || (checkInterval < ModuleClient.MinCheckInterval) || (checkInterval > ModuleClient.MaxCheckInterval)) checkInterval = ModuleClient.GlobalCheckInterval; } else aliveCheck = true; // Verify if the Module requires SOURCE DESTINATION prefix if ( (tmpDoc.GetElementsByTagName("requirePrefix").Count == 0) || !Boolean.TryParse(tmpDoc.GetElementsByTagName("requirePrefix")[0].InnerText, out requirePrefix) ) requirePrefix = false; // Delay between send operations if ( (tmpDoc.GetElementsByTagName("sendDelay").Count == 0) || !Int32.TryParse(tmpDoc.GetElementsByTagName("sendDelay")[0].InnerText, out sendDelay) ) sendDelay = -1; // Simulation options simOptions = ModuleSimulationOptions.SimulationDisabled; if (tmpDoc.GetElementsByTagName("simulate").Count != 0) { node = tmpDoc.GetElementsByTagName("simulate")[0]; simulationSuccessRatio = -1; if ((node.Attributes["successRatio"] == null) || !Double.TryParse(node.Attributes["successRatio"].InnerText, out simulationSuccessRatio) || (simulationSuccessRatio < 0) || (simulationSuccessRatio > 1)) simulationSuccessRatio = -1; if (Boolean.TryParse(node.InnerText, out simulate)) simOptions = (simulationSuccessRatio != -1) ? new ModuleSimulationOptions(simulationSuccessRatio) : new ModuleSimulationOptions(simulate); } // Module Creation mod = new ModuleClientTcp(moduleName, ips, port); mod.ProcessInfo.ProcessName = processName; mod.ProcessInfo.ProgramPath = programPath; mod.ProcessInfo.ProgramArgs = programArgs; mod.AliveCheck = aliveCheck; mod.RequirePrefix = requirePrefix; mod.SendDelay = sendDelay; mod.Simulation = simOptions; mod.Alias = moduleAlias; mod.CheckInterval = checkInterval; return mod; }
/// <summary> /// Loads modules configuration from the provided XML document /// </summary> /// <param name="blackboard">The blackboard where will be loaded the modules</param> /// <param name="doc">The xml document from which the modules will be loaded</param> /// <param name="log">The log writer</param> /// <returns>The number of enabled modules loaded</returns> private static int LoadBlackboardModules(Blackboard blackboard, XmlDocument doc, ILogWriter log) { XmlNodeList modules; int i; //Command startupCommand; SortedList<string, ModuleClient> disabledModules; int clientModulesAdded; if (doc.GetElementsByTagName("modules").Count < 1) { log.WriteLine(0, "No modules to load"); throw new Exception("No modules to load"); } modules = doc.GetElementsByTagName("modules")[0].ChildNodes; clientModulesAdded = 0; disabledModules = new SortedList<string, ModuleClient>(); for (i = 0; i < modules.Count; ++i) { if(LoadModule(blackboard, modules[i], log, disabledModules)) ++clientModulesAdded; } // Add disabled modules AddDisabledModules(blackboard, disabledModules, log); return clientModulesAdded; }
/// <summary> /// Converts the string representation of a command to a Command object. /// A return value indicates whether the conversion succeded or not. /// </summary> /// <param name="s">A string containing the command to convert</param> /// <param name="blackboard">A blackboard which contains all information about modules</param> /// <param name="command">When this method returns, contains the Command equivalent to the command /// contained in s, if the conversion succeeded, or null if the conversion failed. /// The conversion fails if the s parameter is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// <returns>true if conversion was successfull, false otherwise</returns> public static bool TryParse(string s, Blackboard blackboard, out Command command) { Exception ex; return TryParse(s, blackboard, out command, out ex); }
/// <summary> /// Fetch the blackboard virtual module name /// </summary> /// <param name="doc">XML document which contains the Blackboard data</param> /// <param name="blackboard">The blackboard which data will be set</param> /// <param name="log">The log writer</param> private static void LoadBlackboardName(XmlDocument doc, Blackboard blackboard, ILogWriter log) { string moduleName; if ((doc.GetElementsByTagName("name").Count != 1) || ((moduleName = doc.GetElementsByTagName("name")[0].InnerText.Trim()).Length < 3)) { log.WriteLine(1, "No Virtual module name specified."); log.WriteLine(1, "\tUsing virtual module name: BLACKBOARD"); blackboard.VirtualModule = new ModuleBlackboard("BLACKBOARD"); } else { moduleName = moduleName.ToUpper(); blackboard.VirtualModule = new ModuleBlackboard(moduleName.ToUpper()); log.WriteLine(1, "Blackboard virtual module name: " + moduleName); moduleName = null; } }
/// <summary> /// Converts the string representation of a command to a Command object. /// A return value indicates whether the conversion succeded or not. /// </summary> /// <param name="s">A string containing the command to convert</param> /// <param name="blackboard">A blackboard which contains all information about modules</param> /// <param name="cmd">When this method returns, contains the Command equivalent to the command /// contained in s, if the conversion succeeded, or null if the conversion failed. /// The conversion fails if the s parameter is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// <param name="ex">When this method returns, contains the Exception generated during the parse operation, /// if the conversion failed, or null if the conversion succeeded. The conversion fails if the s parameter /// is a null reference (Nothing in Visual Basic) or is not of the correct format. /// This parameter is passed uninitialized</param> /// <returns>true if conversion was successfull, false otherwise</returns> protected static bool TryParse(string s, Blackboard blackboard, out Command cmd, out Exception ex) { IPrototype proto; //Module source; //Module destination; IModuleClient source; IModuleClient destination; string sCommand; string sParams; string sSrc; string sDest; int result; int id; cmd = null; ex = null; // Extract Data CommandBase.XtractCommandElements(s, out sSrc, out sDest, out sCommand, out sParams, out result, out id); if ((sCommand == null) || (result != -1)) { ex = new ArgumentException("Invalid String", "s"); return false; } // Check if source module is specified if ((sSrc == null) || (sSrc.Length < 1) || !blackboard.Modules.Contains(sSrc)) { ex = new Exception("No source module found for command provided"); return false; } source = blackboard.Modules[sSrc]; // Browse for destination module and prototype if (!blackboard.FindDestinationModule(sCommand, out destination, out proto)) //throw new Exception("No destination module found for command provided"); { // No destination module found. Must check if destination is availiable if ((sDest == null) || (sDest.Length < 1) || !blackboard.Modules.Contains(sDest)) { ex = new Exception("No destination module found for command provided"); return false; } destination = blackboard.Modules[sDest]; proto = null; } // Check if command matchs a prototype if ((proto != null) && proto.ParamsRequired && ((sParams == null) || (sParams.Length < 1))) { ex = new Exception("Invalid string. The Command requires parameters"); return false; } // Create the Command cmd = new Command(source, destination, sCommand, sParams, id); cmd.prototype = proto; cmd.sentTime = DateTime.Now; return true; }
/// <summary> /// Fetch the blackboard input port /// </summary> /// <param name="doc">XML document which contains the Blackboard data</param> /// <param name="blackboard">The blackboard which data will be set</param> /// <param name="log">The log writer</param> private static void LoadBlackboardPort(XmlDocument doc, Blackboard blackboard, ILogWriter log) { // Leo puerto if ((doc.GetElementsByTagName("port").Count != 1) || !Int32.TryParse(doc.GetElementsByTagName("port")[0].InnerText, out blackboard.port)) { blackboard.port = 2300; log.WriteLine(1, "No Blackboard port specified, using default: " + blackboard.Port); } else log.WriteLine(1, "Blackboard port: " + blackboard.Port); }