/// 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; /// The Main bus Class /// /// #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 app_bindings; /* Event */ [Description("Occurs when a new client connect to the bus")] public event EventHandler ClientConnected { remove { ivy.ClientConnected -= value; } add { ivy.ClientConnected += value; } } [Description("Occurs when a client discconnect from the bus")] public event EventHandler ClientDisconnected { remove { ivy.ClientDisconnected -= value; } add { ivy.ClientDisconnected += value; } } [Description("Occurs when a client receive a direct message from another client")] public event EventHandler DirectMessageReceived { remove { ivy.DirectMessageReceived -= value; } add { ivy.DirectMessageReceived += value; } } [Description("Occurs when somebody ask for killing every client on the bus")] public event EventHandler DieReceived { remove { ivy.DieReceived -= value; } add { ivy.DieReceived += value; } } [Description("Occurs when a client receive a add binding from another client")] public event EventHandler BindingAdd { remove { ivy.BindingAdd -= value; } add { ivy.BindingAdd += value; } } [Description("Occurs when a client receive a remove binding from another client")] public event EventHandler 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 BindingFilter { remove { ivy.BindingFilter -= value; } add { ivy.BindingFilter += value; } } [Description("Occurs when a client receive a remove binding from another client")] public event EventHandler 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; } } /// AppName the application name #if (!PocketPC) [Category("Ivy")] [Bindable(true)] #endif [DefaultValue(null)] public string AppName { set { ivy.AppName = value; } get { return ivy.AppName; } } ///SentMessageClasses the first word token of sent messages /// optimise the parsing process when sending messages /// #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 SentMessageFilter { get { return ivy.SentMessageFilter; } } /// ReadyMessage message send when Application receive all the regexp at the connection of the client #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 Bindings { get { return app_bindings; } } /// /// Initializes a new instance of the class. /// public IvyControl() { ivy = new Ivy(); app_bindings = new List(); // 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); } /// /// Readies the structures for the software bus connexion. /// /// This sample shows how to start working with Ivy. /// /// Ivy bus = new Ivy("Dummy agent","ready"); /// bus.bindMsg("(.*)",myMessageListener); /// bus.start(null); /// /// How to send & receive: /// the Ivy agent A performs b.bindMsg("^Hello (*)",cb); /// the Ivy agent B performs b2.sendMsg("Hello world"); /// a thread in A will run the callback cb with its second argument set /// to a array of string, with one single element, "world" /// /// /// the real work begin in the start() method /// /// /// The name of your Ivy agent on the software bus /// /// The hellow message you will send once ready /// 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(); } } finally { base.Dispose(disposing); } } /// /// Send a formated message to someone on the bus /// /// /// 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. /// /// A string message format to build the message /// args used in message format /// /// the number of messages actually sent /// public int SendMsg(string format, params object[] args) { return ivy.SendMsg(format, args); } // public int BindMsg(IvyApplicationBinding newbind) { return ivy.BindMsg(newbind); } /// /// Subscribes to a regular expression. /// /// a regular expression, groups are done with parenthesis /// any objects implementing the Ivy.MessageListener /// The args. /// the id of the regular expression /// /// 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. /// // public int BindMsg(string regexp, EventHandler callback, params object[] args) { return ivy.BindMsg(regexp, callback,args); } /// /// unsubscribes a regular expression /// /// the id of the regular expression, returned when it was bound public void UnbindMsg(int id) { ivy.UnbindMsg(id); } /// /// unsubscribes a regular expression /// /// the string for the regular expression /// /// a boolean, true if the regexp existed, false otherwise or /// whenever an exception occured during unbinding /// public bool UnbindMsg(string re) { return ivy.UnbindMsg(re); } /// /// Subscribes to a simple expression ( msg ar1 arg2 arg3 etc). /// /// /// 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. /// /// /// a regular expression, groups are done with parenthesis /// any objects implementing the EventHandler /// /// the id of the regular expression /// public int BindSimpleMsg(string expression, EventHandler callback, params object[] args) { return ivy.BindSimpleMsg(expression, callback, args); } /// /// gives a list of IvyClient(s) with the name given in parameter /// /// The name of the Ivy agent you're looking for /// public ReadOnlyCollection 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 } }