private void HAL9000CmdMan_ResponseReceived(Response response) { this.ParseResponse(response); }
public bool IsResponseReceived(JustinaCommands expectedCmdResp, out Response receivedResp) { receivedResp = null; if (this.IsResponseReceived(expectedCmdResp)) { receivedResp = this.justinaCmdAndResp[(int)expectedCmdResp].Response; return true; } else return false; }
protected override void ParseResponse(Response response) { base.ParseResponse(response); this.sortedCmdAndResp[response.CommandName].Response = response; this.sortedCmdAndResp[response.CommandName].IsResponseReceived = true; TextBoxStreamWriter.DefaultLog.WriteLine(2, "HAL9000CmdMan: Response Received: " + response.StringToSend); }
/// <summary> /// Generates a SharedVariableReport object from a Response object /// </summary> /// <param name="response">The Response object to be used to generate the report</param> /// <returns>A SharedVariableReport object created from the Response object</returns> public static SharedVariableReport CreateFromResponse(Response response) { Exception ex; SharedVariableReport report; if (!CreateFromResponse(response, out report, out ex)) throw ex; return report; }
/// <summary> /// Generates a SharedVariableReport object from a Response object /// </summary> /// <param name="response">The Response object to be used to generate the report</param> /// <param name="report">When this method returns contains the SharedVariableReport object /// extracted from the response if the parse operation was completed successfully, null otherwise</param> /// <param name="ex">When this method returns contains null if the parse operation was completed successfully, /// or the exception to be thrown if the operation failed</param> /// <returns>A SharedVariableReport object created from the Response object</returns> internal static bool CreateFromResponse(Response response, out SharedVariableReport report, out Exception ex) { string parameters; string data; string writer; SharedVariableReportType reportType; SharedVariableSubscriptionType subscriptionType; ISharedVariableInfo varInfo; report = null; ex = null; parameters = response.Parameters; // 1. Get writer if (!GetWriter(ref parameters, out writer, out ex)) return false; // 2. Get subscription type if(!GetSubscriptionType(ref parameters, out subscriptionType, out ex)) return false; // 3. Get report type. if (!GetReportType(ref parameters, out reportType, out ex)) return false; // 4. Get variable info if (!GetVariableInfo(ref parameters, out varInfo, out data, out ex)) return false; // 5. Create the report report = new SharedVariableReport(varInfo, data, reportType, subscriptionType, writer); return true; }
public static void MainSignature() { Signature s; bool result; SignatureBuilder sb = new SignatureBuilder(); System.Diagnostics.Stopwatch sw; sw = new System.Diagnostics.Stopwatch(); sb.AddNewFromDelegate(new DoubleEventHandler(Doble)); sb.AddNewFromDelegate(new VoidEventHandler(Vacio)); sb.AddNewFromDelegate(new DosDobles(Suma)); sb.AddNewFromDelegate(new DoubleArrayDelegate(DoubleArray)); sb.AddNewFromDelegate(new StringArrayEventHandler(StringArray)); //Console.WriteLine(sb.RegexPattern); s = sb.GenerateSignature("command"); Command cmd = new Command("command", "Hola Mundo!"); sw.Start(); result = s.CallIfMatch(cmd); sw.Stop(); Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds); cmd.Parameters = ""; result = s.CallIfMatch(cmd); cmd.Parameters = "3"; result = s.CallIfMatch(cmd); cmd.Parameters = "3.141592 2.71"; result = s.CallIfMatch(cmd); cmd.Parameters = "3.141592 2.71 3.141592 2.71 3.141592 2.71 3.141592 2.71 3.141592 2.71"; result = s.CallIfMatch(cmd); Response rsp = Response.CreateFromCommand(cmd, false); rsp.Parameters = "3.1416 1"; SignatureAnalysisResult sar = s.Analyze(rsp); double a = 0; double b = 0; sar.Update("d1", ref a); Console.WriteLine("a: " + a.ToString()); sar.GetParameter("d2", out b); Console.WriteLine("b: " + b.ToString()); sar.Execute(); Console.WriteLine(); Console.WriteLine("Ejecutando Benchmark"); //TextWriter cout = Console.Out; //Console.SetOut(TextWriter.Null); //Thread thread = new Thread(new ThreadStart(new VoidEventHandler(delegate() //{ // while (true) { sar = s.Analyze(rsp); sar.Execute(); } //}))); //i = 0; //thread.Start(); //Thread.Sleep(1000); //thread.Abort(); sb.Clear(); sb.AddNewFromDelegate(new DosDobles(Nothing)); sb.AddNewFromDelegate(new VoidEventHandler(Nothing)); s = sb.GenerateSignature("command"); rsp = new Response("command", "1 2", true, -1); TimeSpan total = new TimeSpan(0); sw = new Stopwatch(); sar = s.Analyze(rsp); for (int count = 0; count < 100; ++count) { Console.Write("\r"); Console.Write(count); Console.Write("%\t"); sw.Reset(); sw.Start(); for (i = 0; i < 100000; ++i) { sar.Execute(); } sw.Stop(); total +=sw.Elapsed; } Console.WriteLine(); total = new TimeSpan(total.Ticks / 100); //Console.SetOut(cout); Console.WriteLine("Ejecutado 100 veces 100k ciclos. Promedio de rafaga: " + total.TotalMilliseconds + "ms"); Console.WriteLine("Promedio de ejecucion: " + (total.TotalMilliseconds/(100.0)) + "us"); //s.Parse(null, a, b, c); }
public JustinaCmdAndResp(string cmdName) { this.command = new Command(cmdName, ""); this.response = Response.CreateFromCommand(this.command, false); this.isResposeReceived = false; }
/// <summary> /// Initializes a new CommandResponsePair /// </summary> /// <param name="command">Asociated Command object</param> /// <param name="response">Asociated Response object</param> public CommandResponsePair(Command command, Response response) { this.command = command; this.response = response; }
/// <summary> /// Gets a value indicating if provided Response is a response for current command /// </summary> /// <param name="response">Response to check</param> /// <returns>true if provided Response is a response for command, false otherwise</returns> public bool IsMatch(Response response) { bool matchId = ((this.Id != -1) && (response.Id != -1)) ? (this.Id == response.Id) : true; return (this.CommandName == response.CommandName) && matchId; }
/// <summary> /// Raises the ExecutionFinished Event /// </summary> /// <param name="command">The command which execution was finished</param> /// <param name="response">The response generated by the execution of the command (if any)</param> /// <remarks>The OnExecutionFinished method also allows derived classes to handle the event without attaching a delegate. /// This is the preferred technique for handling the event in a derived class. /// When overriding OnExecutionFinished in a derived class, be sure to call the base class's OnExecutionFinished method so that registered /// delegates receive the event</remarks> protected virtual void OnExecutionFinished(Command command, Response response) { if (command == null) return; if (ExecutionFinished != null) ExecutionFinished(this, command, response); if (this.cmdMan != null) this.cmdMan.OnCommandExecuted(this, command, response); }
/// <summary> /// Sends a Response through the asociated ConnectionManager of the CommandExecuter object that this instance of CommandExecuter is bound to /// </summary> /// <param name="response">Response object to be sent</param> protected internal void SendResponse(Response response) { if (cmdMan != null) cmdMan.SendResponse(response); }
/// <summary> /// Creates a response and sends it through the asociated ConnectionManager of the CommandExecuter object that this instance of CommandExecuter is bound to /// </summary> /// <param name="success">Indicates if command succeded</param> /// <param name="command">Command to respond</param> /// <param name="response">Generated response</param> protected internal void SendResponse(bool success, Command command, out Response response) { response = null; if (cmdMan != null) cmdMan.SendResponse(success, command, out response); }
/// <summary> /// Converts the string representation of a response to a Response object. /// A return value indicates whether the conversion succeded or not. /// <param name="source">The IMessageSource object source of the command parsed</param> /// <param name="s">A string containing the response to convert</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(IMessageSource source, string s, out Response result) { result = null; Regex rx; Match m; string sCommand; string sParams; string sSrc; string sDest; string sId; string sResult; bool responseResult; int id; // Regular Expresion Generation //rx = new Regex(@"((?<src>[\w\-]+)\s+)?(?<cmd>[A-Za-z_]+)(\s+""(?<params>[^""]*)"")?\s+(?<result>[10])(\s+@(?<id>\d+))?"); rx = rxParse; m = rx.Match(s); // Check if input string matchs Regular Expression if (!m.Success) return false; // Extract Data sCommand = m.Result("${cmd}").ToLower(); sParams = m.Result("${params}"); sId = m.Result("${id}"); sSrc = m.Result("${src}"); if ((source != null) && ((sSrc == null) || (sSrc == ""))) sSrc = source.ModuleName; sDest = ""; // m.Result("${dest}"); sResult = m.Result("${result}"); if ((sResult == null) || ((sResult != "1") && (sResult != "0"))) return false; responseResult = (sResult == "1"); if ((sId == null) || (sId.Length < 1)) id = -1; else id = Int32.Parse(sId); // Check if response matchs a prototype //if (((sParams == null) || (sParams.Length < 1))) // throw new Exception("Invalid string. The Response requires parameters"); // Create the Response sParams = sParams.Replace("\\\"", "\""); result = new Response(sSrc, sDest, sCommand, sParams, responseResult, id); result.MessageSource = source; return true; }
/// <summary> /// Converts the string representation of a response to a Response object. /// <param name="s">A string containing the response to convert</param> /// </summary> /// <returns>A response object that represents the response contained in s</returns> public static Response Parse(string s) { Regex rx; Match m; Response res; string sCommand; string sParams; string sSrc; string sDest; string sId; string sResult; bool responseResult; int id; // Regular Expresion Generation //rx = new Regex(@"((?<src>[\w\-]+)\s+)?(?<cmd>[A-Za-z_]+)(\s+""(?<params>[^""]*)"")?\s+(?<result>[10])(\s+@(?<id>\d+))?"); rx = rxParse; m = rx.Match(s); // Check if input string matchs Regular Expression if (!m.Success) throw new ArgumentException("Invalid String", "s"); // 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"))) throw new Exception("Invalid string. No suitable result value found"); responseResult = (sResult == "1"); if ((sId == null) || (sId.Length < 1)) id = -1; else id = Int32.Parse(sId); // Check if response matchs a prototype //if (((sParams == null) || (sParams.Length < 1))) // throw new Exception("Invalid string. The Response requires parameters"); // Create the Response sParams = sParams.Replace("\\\"", "\""); res = new Response(sSrc, sDest, sCommand, sParams, responseResult, id); return res; }