summaryrefslogtreecommitdiff
path: root/IvyControl/IvyControl.cs
blob: b7f8b9b510361e5d48efe495c4c94a9f0aa34c07 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/// François-Régis Colin
/// http://www.tls.cena.fr/products/ivy/
/// *
/// (C) CENA
/// *
/// 

namespace IvyBus
{
    using System;
    using System.IO;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    using System.Configuration;
    using System.Globalization;
    using System.Text.RegularExpressions;
    using System.Text;
    using System.Reflection;
    using System.ComponentModel;
    using System.Diagnostics;

#if (!PocketPC)
    using System.ComponentModel.Design;
    using System.Collections.ObjectModel;
#endif
//    using System.Drawing.Design;


    /// <summary> The Main bus Class
    /// </summary>
    /// 
#if (!PocketPC)
    // Show this property in the property grid.
    [ToolboxItemFilter("System.Windows.Forms.Form", ToolboxItemFilterType.Allow)]
	[Description("IVY Main API")]
#endif
    [DesignerCategory("Component")]
    public class IvyControl :  System.ComponentModel.Component, ISupportInitialize      
    {
        public Ivy ivy;
        private List<IvyApplicationBindingControl> app_bindings;
        
        /* Event */
        [Description("Occurs when a new client connect to the bus")]
        public event EventHandler<IvyEventArgs> ClientConnected 
        {
            remove
            {
                ivy.ClientConnected -= value;
            }
            add
            {
                ivy.ClientConnected += value;
            }

        }
        [Description("Occurs when a client discconnect from the bus")]
        public event EventHandler<IvyEventArgs> ClientDisconnected
        {
            remove
            {
                ivy.ClientDisconnected -= value;
            }
            add
            {
                ivy.ClientDisconnected += value;
            }

        }
       [Description("Occurs when a client receive a direct message from another client")]
        public event EventHandler<IvyEventArgs> DirectMessageReceived
        {
            remove
            {
                ivy.DirectMessageReceived -= value;
            }
            add
            {
                ivy.DirectMessageReceived += value;
            }

        }
        [Description("Occurs when somebody ask for killing every client on the bus")]
        public event EventHandler<IvyDieEventArgs> DieReceived
        {
            remove
            {
                ivy.DieReceived -= value;
            }
            add
            {
                ivy.DieReceived += value;
            }

        }
        [Description("Occurs when a client receive a add binding from another client")]
        public event EventHandler<IvyEventArgs> BindingAdd
        {
            remove
            {
                ivy.BindingAdd -= value;
            }
            add
            {
                ivy.BindingAdd += value;
            }

        }
        [Description("Occurs when a client receive a remove binding from another client")]
        public event EventHandler<IvyEventArgs> BindingRemove
        {
            remove
            {
                ivy.BindingRemove -= value;
            }
            add
            {
                ivy.BindingRemove += value;
            }

        }
        [Description("Occurs when a client receive a binding from another client and it as been filtered ")]
        public event EventHandler<IvyEventArgs> BindingFilter
        {
            remove
            {
                ivy.BindingFilter -= value;
            }
            add
            {
                ivy.BindingFilter += value;
            }

        }
       [Description("Occurs when a client receive a remove binding from another client")]
        public event EventHandler<IvyEventArgs> ErrorMessage
        {
            remove
            {
                ivy.ErrorMessage -= value;
            }
            add
            {
                ivy.ErrorMessage += value;
            }

        }

#if (!PocketPC)
        [Bindable(true)]
        [Category("Ivy")]
#endif
        [DefaultValue(false)]
        public static bool DebugProtocol
        {
            get
            {
                return Ivy.DebugProtocol;
            }
            set
            {
                Ivy.DebugProtocol = value;
            }

        }
#if (!PocketPC)
        [Category("Ivy")]
#endif
        public CultureInfo Culture
        {
            get
            {
                return ivy.Culture;
            }
            set
            {
                ivy.Culture = value;
            }

        }

#if (!PocketPC)
        [Category("Ivy")]
#endif
        public bool  IpV6
        {
            get
            {
                return ivy.IpV6;
            }

        }
      
        /// <summary>AppName the application name</summary>

#if (!PocketPC)
        [Category("Ivy")]
        [Bindable(true)]
#endif
        [DefaultValue(null)]
        public string AppName
        {
            set
            {
                ivy.AppName = value;
            }
            get
            {
                return ivy.AppName;
            }

        }

        ///<summary>SentMessageClasses the first word token of sent messages
        ///<remarks> optimise the parsing process when sending messages </remarks>
        ///</summary>
#if (!PocketPC)
        [Category("Ivy")]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        // sinon bug System.String constructor not found !
        [Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing")]
#endif
        public Collection<string> SentMessageFilter
        {
            get
            {
                return ivy.SentMessageFilter;
            }
        }
        /// <summary>ReadyMessage message send when Application receive all the regexp at the connection of the client</summary>

#if (!PocketPC)
        [Bindable(true)]
        [Category("Ivy")]
#endif
        [DefaultValue(null)]
        public string ReadyMessage
        {
            get { return ivy.ReadyMessage; }
            set { ivy.ReadyMessage = value; }
        }



#if (PocketPC)
#if (!PocketPC)
        [Category("Ivy")]
#endif
        [DefaultValue(null)]
        public System.Windows.Forms.ContainerControl ContainerControl
        {
            get { return parentControl; }
            set
            {
                parentControl = value;
            }
        }
#endif
#if (!PocketPC)
        [Category("Ivy")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
#endif
        public List<IvyApplicationBindingControl> Bindings
        {
            get { return app_bindings; }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="T:Ivy"/> class.
        /// </summary>
        public IvyControl()
        {
            ivy = new Ivy();
            app_bindings = new List<IvyApplicationBindingControl>();
            
            // get binding from Attribute IvyBinding
            //TODO Autobinding attribute
#if (PocketPC)
            if (parentControl != null)
                BindAttibute(parentControl);
#endif
            Assembly assembly = Assembly.GetCallingAssembly();
            if ( assembly != this.GetType().Assembly )
                ivy.BindAttribute(assembly);
        }
        public IvyControl(IContainer container)
            : this()
        {
            container.Add(this);
            // get binding from Attribute IvyBinding
            //TODO Autobinding attribute
            Assembly assembly = Assembly.GetCallingAssembly();
            if (assembly != this.GetType().Assembly)
                ivy.BindAttribute(assembly);
        }
        /// <summary>
        /// Readies the structures for the software bus connexion.
        /// </summary>
        /// <example> This sample shows how to start working with Ivy.
        /// <code>
        /// Ivy bus = new Ivy("Dummy agent","ready");
        /// bus.bindMsg("(.*)",myMessageListener);
        /// bus.start(null);
        /// </code>
        /// How to send & receive:
        /// the Ivy agent A performs <c>b.bindMsg("^Hello (*)",cb);</c>
        /// the Ivy agent B performs <c>b2.sendMsg("Hello world");</c>
        /// a thread in A will run the callback cb with its second argument set
        /// to a array of string, with one single element, "world"
        /// </example>
        /// <remarks>
        /// the real work begin in the start() method
        /// </remarks>
        /// <seealso cref=" Ivy.start"/>
        /// <param name='name'>The name of your Ivy agent on the software bus
        /// </param>
        /// <param name='message'>The hellow message you will send once ready
        /// </param>
        public IvyControl(string name, string rdy_message)
            : this()
        {
            ivy.AppName = name;
            ivy.ReadyMessage = rdy_message;
            // get binding from Attribute IvyBinding
            //TODO Autobinding attribute
            Assembly assembly = Assembly.GetCallingAssembly();
            if (assembly != this.GetType().Assembly)
                ivy.BindAttribute(assembly);
        }

        protected override void Dispose(bool disposing)
        {
             try
             {
                 if (disposing && ivy != null )
                 {
                     ivy.Stop();
                     ivy.Dispose();
                 }
             }
             finally
             {
                 base.Dispose(disposing);
             } 
        }
          

        /// <summary> 
        /// Send a formated message to someone on the bus
        /// </summary>
        /// <remarks>
        /// Performs a pattern matching according to everyone's regexps, and sends
        /// the results to the relevant ivy agents.
        /// There is one thread for each client connected, we could also
        /// create another thread each time we send a message.
        /// </remarks>
        /// <param name='format'>A string message format to build  the message
        /// <param name='args'>args used in message format
        /// <returns>
        /// the number of messages actually sent
        /// </returns>
        public int SendMsg(string format, params object[] args)
        {
            return ivy.SendMsg(format, args);
        }
        // 
        public int BindMsg(IvyApplicationBinding newbind)
        {     
            return ivy.BindMsg(newbind);
        }
        /// <summary>
        /// Subscribes to a regular expression.
        /// </summary>
        /// <param name="regexp">a regular expression, groups are done with parenthesis</param>
        /// <param name="callback">any objects implementing the Ivy.MessageListener</param>
        /// <param name="args">The args.</param>
        /// <returns>the id of the regular expression</returns>
        /// <remarks>
        /// The callback will be executed with
        /// the saved parameters of the regexp as arguments when a message will sent
        /// by another agent. A program doesn't receive its own messages.
        /// </remarks>
        //
        public int BindMsg(string regexp, EventHandler<IvyMessageEventArgs> callback, params object[] args)
        {
            return ivy.BindMsg(regexp, callback,args);
        }

        /// <summary>
        /// unsubscribes a regular expression
        /// </summary>
        /// <param name="id">the id of the regular expression, returned when it was bound</param>
        public void UnbindMsg(int id)
        {
            ivy.UnbindMsg(id);
        }

        /// <summary>
        /// unsubscribes a regular expression
        /// </summary>
        /// <param name="re">the string for the regular expression</param>
        /// <returns>
        /// a boolean, true if the regexp existed, false otherwise or
        /// whenever an exception occured during unbinding
        /// </returns>
        public bool UnbindMsg(string re)
        {
            return ivy.UnbindMsg(re);
        }


        /// <summary>
        /// Subscribes to a simple expression ( msg ar1 arg2 arg3 etc).
        /// </summary>
        /// <remarks>
        /// The callback will be executed with
        /// the saved parameters of the regexp as arguments when a message will sent
        /// by another agent. A program doesn't receive its own messages.
        /// </remarks>
        /// 
        /// <param name='regexp'>a regular expression, groups are done with parenthesis
        /// <param name='callback'>any objects implementing the EventHandler<IvyMessageEventArgs>
        /// <returns>
        /// the id of the regular expression
        /// </returns>
        public int BindSimpleMsg(string expression, EventHandler<IvyMessageEventArgs> callback, params object[] args)
        {
            return ivy.BindSimpleMsg(expression, callback, args);
        }
 
        /// <summary>
        /// gives a list of IvyClient(s) with the name given in parameter
        /// </summary>
        /// <param name="name">The name of the Ivy agent you're looking for</param>
        /// <returns></returns>
        public ReadOnlyCollection<IvyClient> GetClientsByName(string name)
        {
            
            return ivy.GetClientsByName( name );
        }


        #region ISupportInitialize Members

        public void BeginInit()
        {
       
        }

        public void EndInit()
        {
            // TODO ugly should be added directly the bindings Dictionary !
            foreach (IvyApplicationBindingControl bind in app_bindings)
            {
                ivy.BindMsg(bind.binding);
            }
        }

        #endregion

    }
}