summaryrefslogtreecommitdiff
path: root/CSharp/Ivy/IvyProbe/IvyProbe.cs
blob: 9e4e34bfb369284b24efbdd23095c52d12406410 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
namespace IvyTest
{
	using System;
	using System.Windows.Forms;
	using IvyBus;
	
	class IvyTest :  System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox tbRegexp;
		private System.Windows.Forms.TextBox tbMsg;
		private System.Windows.Forms.Button btBind;
		private System.Windows.Forms.Button btSend;
		private System.Windows.Forms.TextBox ta;
		
		/// <summary>
		/// Variable nécessaire au concepteur.
		/// </summary>
		private System.ComponentModel.Container components = null;

		internal Ivy bus;
		internal String regexp;
		internal int regexp_id;
		public IvyTest()
		{
			//
			// Requis pour la prise en charge du Concepteur Windows Forms
			//
			InitializeComponent();

			regexp = "";
			bus = new Ivy("C# TESTBUS", "Testbus ridi",this);
			bus.Debug = true;
			bus.clientConnected += new Ivy.ClientConnectedHandler( connect );
			bus.clientDisconnected += new Ivy.ClientDisconnectedHandler( disconnect );
			bus.dieReceived += new Ivy.DieHandler( die );
			bus.directMessageReceived += new Ivy.DirectMessageHandler( directMessage );
			bus.start(null);
			regexp_id = -1;
		}
		
		/// <summary>
		/// Nettoyage des ressources utilisées.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			stop();
			bus = null;
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
		/// le contenu de cette méthode avec l'éditeur de code.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.tbRegexp = new System.Windows.Forms.TextBox();
			this.tbMsg = new System.Windows.Forms.TextBox();
			this.ta = new System.Windows.Forms.TextBox();
			this.btBind = new System.Windows.Forms.Button();
			this.btSend = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(40, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Regexp:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 248);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "Msg:";
			// 
			// tbRegexp
			// 
			this.tbRegexp.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.tbRegexp.Location = new System.Drawing.Point(64, 0);
			this.tbRegexp.Name = "tbRegexp";
			this.tbRegexp.Size = new System.Drawing.Size(224, 20);
			this.tbRegexp.TabIndex = 2;
			this.tbRegexp.Text = "(.*)";
			this.tbRegexp.TextChanged += new System.EventHandler(this.tbRegexp_TextChanged);
			// 
			// tbMsg
			// 
			this.tbMsg.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.tbMsg.Location = new System.Drawing.Point(72, 240);
			this.tbMsg.Name = "tbMsg";
			this.tbMsg.Size = new System.Drawing.Size(216, 20);
			this.tbMsg.TabIndex = 3;
			this.tbMsg.Text = "msg";
			this.tbMsg.TextChanged += new System.EventHandler(this.tbMsg_TextChanged);
			// 
			// ta
			// 
			this.ta.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.ta.Location = new System.Drawing.Point(0, 32);
			this.ta.Multiline = true;
			this.ta.Name = "ta";
			this.ta.Size = new System.Drawing.Size(368, 200);
			this.ta.TabIndex = 4;
			this.ta.Text = "";
			// 
			// btBind
			// 
			this.btBind.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.btBind.Location = new System.Drawing.Point(296, 0);
			this.btBind.Name = "btBind";
			this.btBind.Size = new System.Drawing.Size(64, 24);
			this.btBind.TabIndex = 5;
			this.btBind.Text = "bind";
			this.btBind.Click += new System.EventHandler(this.RegexpCB);
			// 
			// btSend
			// 
			this.btSend.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btSend.Location = new System.Drawing.Point(296, 240);
			this.btSend.Name = "btSend";
			this.btSend.Size = new System.Drawing.Size(64, 24);
			this.btSend.TabIndex = 6;
			this.btSend.Text = "Send";
			this.btSend.Click += new System.EventHandler(this.SendCB);
			// 
			// IvyTest
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(368, 266);
			this.Controls.Add(this.btSend);
			this.Controls.Add(this.btBind);
			this.Controls.Add(this.ta);
			this.Controls.Add(this.tbMsg);
			this.Controls.Add(this.tbRegexp);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Name = "IvyTest";
			this.Text = "IvyTest";
			this.ResumeLayout(false);

		}
		#endregion
		public void  stop()
		{
			bus.clientConnected -= new Ivy.ClientConnectedHandler( connect );
			bus.clientDisconnected -= new Ivy.ClientDisconnectedHandler( disconnect );
			bus.dieReceived -= new Ivy.DieHandler( die );
			bus.directMessageReceived -= new Ivy.DirectMessageHandler( directMessage );
			bus.stop();
		}
		private void  connect(IvyClient client)
		{
			append(client.ApplicationName + " connected ");
		}
		private void  disconnect(IvyClient client)
		{
			append(client.ApplicationName + " disconnected ");
		}
		private bool  die(IvyClient client, int id)
		{
			append("receive die from "+client.ApplicationName );
			// return true terminate the application
			return false;
		}
		private void  directMessage(IvyClient client, int id, System.String arg)
		{
			append(client.ApplicationName + " direct Message " + id + arg);
		}
		
		public void  RegexpCB(System.Object event_sender, System.EventArgs e)
		{
			// enlever l'ancienne regex
			if ( regexp_id >=0 ) 
				bus.unBindMsg(regexp_id);
			// ajoute la nouvelle regex
			regexp = tbRegexp.Text;
			regexp.Trim();
			regexp_id = bus.bindMsg(regexp, new Ivy.MessageHandler(receive));
			tbRegexp.Text = "";
			append( "bind("+regexp_id+") ->"+regexp);
		}
		private void  receive(IvyClient client, String[] args)
		{
			String out_Renamed = "client " + client.ApplicationName + " envoie: [ ";
				for (int i = 0; i < args.Length; i++)
				out_Renamed = out_Renamed + args[i] + ((i < args.Length - 1)?" , ":"");
			out_Renamed = out_Renamed + " ]";
			append(out_Renamed);
		}
		private void  append(System.String s)
		{
			// je mettrais bien la date, aussi.
			ta.AppendText(s+"\r\n");
		}
		
		public void  SendCB(System.Object event_sender, System.EventArgs e)
		{
				int count;
				System.String tosend = tbMsg.Text;
				tbMsg.Text = "";
				count = bus.sendMsg(tosend);
				append("Sending '" + tosend + "' count " + count);
				
		}
		
		[STAThread]
		public static void  Main(System.String[] args)
		{
			IvyTest tb = new IvyTest();
			Application.Run(tb);
			
		}

		private void tbMsg_TextChanged(object sender, System.EventArgs e)
		{
			btSend.Enabled = (tbMsg.Text != "");
		}

		private void tbRegexp_TextChanged(object sender, System.EventArgs e)
		{
			btBind.Enabled = (tbRegexp.Text != "");
		}
		
	}
	
	// EOF
}