summaryrefslogtreecommitdiff
path: root/IvyControl/IvyControl.cs
diff options
context:
space:
mode:
authorfcolin2007-11-09 10:20:21 +0000
committerfcolin2007-11-09 10:20:21 +0000
commit3a1a7b17f7765edf56d2714c337c8501adcf2189 (patch)
tree6e56713f4b5983e89c2280ca37ad6c2e3015c4d4 /IvyControl/IvyControl.cs
parentbd1c7b7348b8e5532040b178a891f5295e46d35b (diff)
downloadivy-csharp-3a1a7b17f7765edf56d2714c337c8501adcf2189.zip
ivy-csharp-3a1a7b17f7765edf56d2714c337c8501adcf2189.tar.gz
ivy-csharp-3a1a7b17f7765edf56d2714c337c8501adcf2189.tar.bz2
ivy-csharp-3a1a7b17f7765edf56d2714c337c8501adcf2189.tar.xz
separation des references a Windows Form
Diffstat (limited to 'IvyControl/IvyControl.cs')
-rw-r--r--IvyControl/IvyControl.cs473
1 files changed, 473 insertions, 0 deletions
diff --git a/IvyControl/IvyControl.cs b/IvyControl/IvyControl.cs
new file mode 100644
index 0000000..a432914
--- /dev/null
+++ b/IvyControl/IvyControl.cs
@@ -0,0 +1,473 @@
+/// 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;
+#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;
+ }
+
+ }
+
+
+ /// <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>AppPriority the Application Priority: the clients list is sorted against priority</summary>
+#if (!PocketPC)
+ [Category("Ivy")]
+#endif
+ [DefaultValue(Ivy.DEFAULT_PRIORITY)]
+ public ushort AppPriority
+ {
+ set
+ {
+ ivy.AppPriority = value;
+ }
+ get
+ {
+ return ivy.AppPriority;
+ }
+
+ }
+
+ ///<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 List<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.BindAttibute(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.BindAttibute(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.BindAttibute(assembly);
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ try
+ {
+ if (disposing && ivy != null )
+ {
+ ivy.Stop();
+ }
+ }
+ 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 ushort 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 ushort 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(ushort 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 List<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
+
+ }
+} \ No newline at end of file