Example #1
0
        private bool IsValidInput(Module source, string s)
        {
            Command  cmd;
            Response rsp;

            if ((Response.IsResponse(s) && Response.TryParse(s, source, out rsp)) ||
                Command.IsCommand(s) && Command.TryParse(s, source, out cmd))
            {
                return(true);
            }
            return(false);
        }
Example #2
0
        private void Inject()
        {
            Module   source;
            Command  cmd;
            Response rsp;

            string[]     inputs;
            HistoryToken token;

            if ((source = (Module)cbModules.SelectedItem) == null)
            {
                return;
            }

            inputs = BulkMode ?
                     txtMessage.Text.Split(separators, StringSplitOptions.RemoveEmptyEntries) :
                     new string[] { txtMessage.Text.Trim() };

            foreach (string input in inputs)
            {
                //blackboard.Inject(source.Name + " " + input);
                token = null;
                if (Response.IsResponse(input) && Response.TryParse(input, source, out rsp))
                {
                    token = new HistoryToken(rsp);
                }
                else if (Command.IsCommand(input) && Command.TryParse(input, source, out cmd))
                {
                    token = new HistoryToken(cmd);
                }
                if (token == null)
                {
                    blackboard.Inject(source.Name + " " + input);
                    continue;
                }

                blackboard.Inject(token.StringToInject);
                AddHystoryToken(token);
            }
            txtMessage.Clear();
        }
Example #3
0
 /// <summary>
 /// Asignates the provided Response as Response for the current Command and this Command as CommandResponded for the provided response.
 /// After asignation the Response is sent to its Destination module which must match this Command Source module
 /// </summary>
 /// <param name="response">Response to asign</param>
 /// <returns>true if response was asigned and sent successfully, false otherwise</returns>
 public bool SendResponse(Response response)
 {
     if (response.Destination == null) response.Destination = this.source;
     if (!IsMatch(response)) return false;
     if (response.CommandResponded == null) response.CommandResponded = this;
     this.Response = response;
     if ((response.CommandResponded != this) || (this.response != response)) return false;
     return response.Send();
 }
Example #4
0
 /// <summary>
 /// Gets a value indicating if provided Response is a response for provided command and calculates the afinity between them
 /// </summary>
 /// <param name="command">Command to check with</param>
 /// <param name="response">Response to check</param>
 /// <param name="afinity">An integer that represents how much compatible are the command and response provided.
 /// A Zero value indicates the most high afinity between them. This parameter is passed uninitialized.</param>
 /// <returns>true if provided Response is a response for command, false otherwise</returns>
 public static bool IsMatch(Command command, Response response, out int afinity)
 {
     // This one is if the response destination is known
     afinity = 0;
     bool result = false;
     if (response.Destination != null)
     {
         result = (command.Source == response.Destination)
             && (command.Destination == response.Source)
             && (command.Command == response.Command)
             && (command.SentTime <= response.ArrivalTime);
         //if((command.Id != -1) && (response.Id != -1) && (command.Id != response.Id)) ++afinity;
         if (command.Id != response.Id) ++afinity;
         if (command.parameters != response.Parameters) ++afinity;
     }
     else
     {
         ++afinity;
         result = (command.Destination == response.Source)
             && (command.Command == response.Command)
             && (command.SentTime <= response.ArrivalTime);
         //if((command.Id != -1) && (response.Id != -1) && (command.Id != response.Id)) ++afinity;
         if (command.Id != response.Id) ++afinity;
         if (command.parameters != response.Parameters) ++afinity;
     }
     // This one is for search for an adequate command-response match
     return result;
 }
Example #5
0
        /// <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="source">The module which sent the response</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, ModuleClient source, out Response response, out Exception ex)
        {
            IPrototype proto;
            //Module destination;
            IModuleClient destination;
            //Module eSource;
            IModuleClient eSource;
            string sCommand;
            string sParams;
            string sDest;
            bool result;
            int iResult;
            int id;

            ex = null;
            response = null;

            // Extract Data
            CommandBase.XtractCommandElements(s, 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;

            // Browse for prototype and validating command
            if (!source.Parent.FindDestinationModule(sCommand, out eSource, out proto) || (eSource != source))
            //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;
        }
Example #6
0
        /// <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="source">The module which sent the response</param>
        /// <param name="destination">The destination module for the response</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, Module source, Module destination, out Response response, out Exception ex)
        {
            Regex rx;
            Match m;
            Prototype proto;
            string sCommand;
            string sParams;
            string sId;
            string sResult;
            bool result;
            int id;

            ex = null;
            response = null;

            // Module Validation
            if (source.Parent != destination.Parent)
            {
                ex = new Exception("Source and destination modules does not belong to the same blackboard");
                return false;
            }
            // Regular Expresion Generation
            rx = rxResponseFromSrcDest;
            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}");
            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);

            // Browse for an adequate prototype
            proto = null;
            for (int i = 0; i < destination.Prototypes.Count; ++i)
            {
                if (destination.Prototypes[i].Command == sCommand)
                {
                    proto = destination.Prototypes[i];
                    break;
                }
            }
            // Check if response matchs a prototype. If no prototype found, asume redirection
            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;
        }
Example #7
0
        /// <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="source">The module which sent the response</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, Module source, out Response response, out Exception ex)
        {
            Regex rx;
            Match m;
            Prototype proto;
            //Module destination;
            IModule destination;
            //Module eSource;
            IModule eSource;
            string sCommand;
            string sParams;
            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 = rxResponseFromSource;
            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}");
            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);
            // Browse for prototype and validating command
            if (!source.Parent.FindDestinationModule(sCommand, out eSource, out proto) || (eSource != source))
            //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;
        }
Example #8
0
        /// <summary>
        /// Converts the string representation of a response to a Command object.
        /// A return value indicates whether the conversion succeded or not.
        /// <param name="s">A string containing the response to convert</param>
        /// <param name="localEndpoint">The remote endpoint source of the string</param>
        /// <param name="destination">The module which received the response</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, System.Net.IPEndPoint localEndpoint, ModuleServer destination, out Response response, out Exception ex)
        {
            Regex rx;
            Match m;
            Prototype proto;
            //Module source;
            //Module destination;
            IModule src;
            IModule dest;
            string sCommand;
            string sParams;
            string sSrc;
            string sDest;
            string sId;
            string sResult;
            bool result;
            int id;

            ex = null;
            response = null;

            // Regular Expresion Generation
            rx = rxResponseFromEndpoint;
            m = rx.Match(s);
            // Check if input string matches Regular Expression
            if (!m.Success)
            {
                ex = new ArgumentException("Invalid String", "s");
                return false;
            }
            // Extract Data
            sCommand = m.Result("${cmd}").ToLower();
            if (!destination.SupportCommand(sCommand))
            {
                ex = new Exception("Unsupported command provided");
                return false;
            }
            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);
            // When at least one module is specified...
            if ((sSrc != null) && (sSrc.Length > 0))
            {
                // If only one module is specified, there is two options:
                // the specified module is this (and we dont know the source module) or
                // the specified module is the source module
                if ((sDest == null) || (sDest.Length < 1))
                {
                    if (sSrc == destination.Name) dest = destination;
                    else src = new Module(sSrc, localEndpoint.Address, localEndpoint.Port);
                }

                // If both, source and destination module are specified, since there is no
                // way of get the source module, a new one is created.
                // However its necesary to check if this is the apropiate destination module
                else
                {
                    if (sDest != destination.Name)
                    {
                        ex = new Exception("Invalid destination module");
                        return false;
                    }
                    dest = destination;
                    src = new Module(sSrc, localEndpoint.Address, localEndpoint.Port);
                }
            }
            // If no module is specified, then its set to null
            src = null;
            // Browse for an adequate prototype
            proto = destination.Prototypes[sCommand];
            // 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
            response = new Response(src, destination, sCommand, sParams, result, id);
            response.prototype = proto;
            if (!response.success) response.failReason = ResponseFailReason.ExecutedButNotSucceded;
            return true;
        }
Example #9
0
        /// <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="source">The module which sent the response</param>
        /// <param name="destination">The destination module for the response</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, ModuleClient source, ModuleClient destination, out Response result)
        {
            Exception ex;

            return TryParse(s, source, destination, out result, out ex);
        }
Example #10
0
        /// <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);
        }
Example #11
0
        /// <summary>
        /// Creates a response from a command data
        /// </summary>
        /// <param name="command">The command to use as base for the response</param>
        /// <param name="failReason">the reason for which command has failed</param>
        /// <returns>A generic response for the command with same parameters</returns>
        public static Response CreateFromCommand(Command command, ResponseFailReason failReason)
        {
            Response response = new Response();
            response.commandResponded = command;
            response.source = command.Destination;
            response.destination = command.Source;
            response.command = command.Command;
            response.parameters = command.Parameters;
            response.id = command.Id;
            response.failReason = failReason;
            response.success = (failReason == ResponseFailReason.None);

            response.prototype = command.Prototype;
            if (command.SentTime >= DateTime.Now)
                response.arrivalTime = command.SentTime.AddMilliseconds(5);
            response.arrivalTime = DateTime.Now;
            return response;
        }
Example #12
0
        /// <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="source">The module which sent the response</param>
        /// <param name="destination">The destination module for the response</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, ModuleClient source, ModuleClient destination, out Response response, out Exception ex)
        {
            IPrototype proto;
            string sCommand;
            string sParams;
            bool result;
            int iResult;
            int id;

            ex = null;
            response = null;

            // Module Validation
            if (source.Parent != destination.Parent)
            {
                ex = new Exception("Source and destination modules does not belong to the same blackboard");
                return false;
            }
            // Extract Data
            CommandBase.XtractCommandElements(s, out sCommand, out sParams, out iResult, out id);
            if ((sCommand == null) || (iResult == -1))
            {
                ex = new ArgumentException("Invalid String", "s");
                return false;
            }
            result = iResult == 1;

            // Browse for an adequate prototype
            proto = null;
            if (destination.Prototypes.Contains(sCommand))
                proto = destination.Prototypes[sCommand];
            // Check if response matchs a prototype. If no prototype found, asume redirection
            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;
        }