summaryrefslogtreecommitdiff
path: root/CSharp/Ivy/IvyPPC/IvyBinding.cs
blob: c5fd5fe7237b758ce67f827c5f88a8773d20cb7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections;
using System.Text.RegularExpressions;
	
namespace IvyBus
{
	/// <summary>
	/// Description résumée de IvyBinding.
	/// </summary>
	internal abstract class IvyBindingBase
	{
		
		internal int key;
		internal string expression;
		
		public IvyBindingBase(int id, string exp)
		{
			key = id;
			expression = exp;
		}
		public abstract IvyArgument Match(string message);
		
	}
	internal class IvyBindingRegexp :  IvyBindingBase
	{
		internal Regex regexp;
		
		public IvyBindingRegexp(int id, string exp):base(id,exp)
		{
			regexp = new Regex(expression,RegexOptions.Compiled|RegexOptions.IgnoreCase);		
		}
		public override IvyArgument Match(string message)
		{
		IvyArgument 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 IvyArgument("");
				for (int sub = 1; sub < result.Groups.Count; sub++)
				{
				args.addChild( new IvyArgument( 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 Hashtable args_values = null;	// send message args[name]=value
					
		public IvyBindingSimple(int 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 Hashtable();
			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 IvyArgument Match(string message)
		{
			// the message is already parsed by prepare 
			// 
			IvyArgument args = null;
			
			if (msgtag == msgname)
			{
				args = new IvyArgument("");
				for( int sub= 0; sub < msgargs.Length; sub++)
				{
					args.addChild( new IvyArgument( (string) args_values[ msgargs[sub]] ));
				}
			}
			return args;
		}

	}
}