using System; using System.Collections; using System.Collections.Specialized; using System.Text.RegularExpressions; namespace IvyBus { /// /// Description résumée de IvyBinding. /// internal abstract class IvyBindingBase { internal ushort key; internal string expression; public IvyBindingBase(ushort id, string exp) { key = id; expression = exp; } public abstract string[] Match(string message); } internal class IvyBindingRegexp : IvyBindingBase { internal Regex regexp; public IvyBindingRegexp(ushort id, string exp) : base(id, exp) { regexp = new Regex(expression,RegexOptions.Compiled|RegexOptions.IgnoreCase); } public override string[] Match(string message) { string[] args = null; // use of regexp to extract info Match result = regexp.Match(message); if (result.Success) { // Start at 1 because group 0 represent entire matching args = new string[result.Groups.Count-1]; for (int sub = 1; sub < result.Groups.Count; sub++) { args[sub-1] = result.Groups[sub].Value; } } return args; } } internal class IvyBindingSimple : IvyBindingBase { internal string msgname; // message name internal string[] msgargs; // list of message args names static string msgtag; // send message name static StringDictionary args_values = null; // send message args[name]=value public IvyBindingSimple(ushort id, string exp) : base(id, exp) { string[] expr = expression.Split( ' ' ); msgname = expr[0]; msgargs = new string[ expr.Length -1 ]; for ( int i = 1; i < expr.Length; i++ ) msgargs[i-1] = expr[i]; } static public void Prepare( string message ) { string[] msg = message.Split(' '); msgtag = msg[0]; args_values = new StringDictionary(); for( int sub=1 ; sub < msg.Length; sub++ ) { string[] arg = msg[sub].Split('='); // champ = valeur if ( arg.Length == 2 ) args_values[arg[0]] = arg[1]; else { Console.Out.WriteLine("abnormally Formed message expected 'msg champ=valeur champ=valeur....' :" + message); } } } public override string[] Match(string message) { // the message is already parsed by prepare // string[] args = null; if (msgtag == msgname) { args = new string[msgargs.Length]; for( int sub= 0; sub < msgargs.Length; sub++) { args[sub] = args_values[msgargs[sub]]; } } return args; } } }