From d69db73be68631d6da7ed2b1a70318de51e6b482 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 09:44:07 +0000 Subject: Utilisateur : Fcolin Date : 2/06/06 Heure : 16:47 Archivé dans $/CSharp/Ivy/Ivy Commentaire: Mise en conformite du code avec les guidelines , et changement prototype evenements (vss 65) --- CSharp/Ivy/Ivy/Ivy.cs | 227 ++++++++++++++++++++++++++------------------------ 1 file changed, 118 insertions(+), 109 deletions(-) (limited to 'CSharp') diff --git a/CSharp/Ivy/Ivy/Ivy.cs b/CSharp/Ivy/Ivy/Ivy.cs index e0573e4..a64acb8 100644 --- a/CSharp/Ivy/Ivy/Ivy.cs +++ b/CSharp/Ivy/Ivy/Ivy.cs @@ -35,42 +35,27 @@ namespace IvyBus [DesignerCategory("Component")] public class Ivy : System.ComponentModel.Component, ISupportInitialize { - /* return value for Die Event */ - public enum ApplicationExit { FORCE_EXIT, DONT_EXIT }; - /* Event handler */ - public delegate void DirectMessageHandler(IvyClient app, int id, string arg); - public delegate void ClientConnectedHandler(IvyClient app); - public delegate void ClientDisconnectedHandler(IvyClient app); - public delegate ApplicationExit DieHandler(IvyClient app, int id, string arg); - public delegate void ClientAddBindingHandler(IvyClient app, string arg); - public delegate void ClientRemoveBindingHandler(IvyClient app, string arg); - public delegate void ClientFilterBindingHandler(IvyClient app, string arg); - public delegate void ErrorMessageHandler(IvyClient app, int id, string arg); - - public delegate void MessageHandler(IvyClient app, string[] args); - - /* Event */ /// fires when a new client connect to the bus /// A callback handling the notification of connexions and /// disconnections, may be null /// /// - public event ClientConnectedHandler ClientConnected; + public event EventHandler ClientConnected; /// fires when a client discconnect from the bus - public event ClientDisconnectedHandler ClientDisconnected; + public event EventHandler ClientDisconnected; /// fires when a client receive a direct message from another client - public event DirectMessageHandler DirectMessageReceived; + public event EventHandler DirectMessageReceived; /// fires when somebody ask for killing every client on the bus - public event DieHandler dieReceived; + public event EventHandler DieReceived; /// fires when a client receive a add binding from another client - public event ClientAddBindingHandler BindingAdd; + public event EventHandler BindingAdd; /// fires when a client receive a remove binding from another client - public event ClientRemoveBindingHandler BindingRemove; + public event EventHandler BindingRemove; /// fires when a client receive a binding from another clientand it as been filtered - public event ClientRemoveBindingHandler BindingFilter; + public event EventHandler BindingFilter; /// fires when a client receive a remove binding from another client - public event ErrorMessageHandler ErrorMessage; + public event EventHandler ErrorMessage; #if (!PocketPC) [Bindable(true)] @@ -117,26 +102,7 @@ namespace IvyBus } } - /// ClientNames accesses the name list of the connected clientsAppName the application name #if (!PocketPC) @@ -295,7 +261,7 @@ namespace IvyBus { internal BindingType type; internal ushort key; - internal MessageHandler callback; + internal EventHandler callback; internal string expression; internal object[] args; } @@ -308,7 +274,7 @@ namespace IvyBus internal IPAddress applicationHost; /* Application host number */ internal string applicationUniqueId; /* identifier Application unique timestamp-ipaddress-port */ internal ushort applicationPriority = DEFAULT_PRIORITY; - private string ready_message = null; + private string ready_message; private CultureInfo culture = new CultureInfo("en-us"); // for synchronous event private Control syncControl; // null by runtime default @@ -363,15 +329,22 @@ namespace IvyBus } protected override void Dispose(bool disposing) { - if (disposing) - { - stop(); - } - base.Dispose(disposing); + try + { + if (disposing) + { + Stop(); + } + } + finally + { + base.Dispose(disposing); + } } // Autobinding on static method public void AutoBinding(Type type) { + if (type == null) return; //Get instance of the IvyBindingAttribute. foreach (MethodInfo m in type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic)) { @@ -379,21 +352,22 @@ namespace IvyBus { //TODO check paramater type MessageHandler Debug.WriteLine("IvyBinding '" + attr.GetExpression(null) + "' to Method " + m.Name); - MessageHandler handler; + EventHandler handler; #if (PocketPC) //Createdelegate mydlg = new Createdelegate(m, null, EventArgs.Empty); //bindMsg(attr.GetExpression(null), mydlg); handler = null; #else - handler = (MessageHandler)Delegate.CreateDelegate(typeof(MessageHandler), m); + handler = (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), m); #endif - bindMsg(attr.GetExpression(null), handler); + BindMsg(attr.GetExpression(null), handler); } } } // Autobinding on instance method public void AutoBinding(object obj) { + if (obj == null) return; Type type = obj.GetType(); //Get instance of the IvyBindingAttribute. foreach (MethodInfo m in type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic)) @@ -402,13 +376,13 @@ namespace IvyBus { //TODO check paramater type MessageHandler traceDebug("IvyBinding '" + attr.GetExpression(obj) + "' to Method " + m.Name); - MessageHandler handler; + EventHandler handler; #if (PocketPC) handler = null; // TODO #else - handler = (MessageHandler)Delegate.CreateDelegate(typeof(MessageHandler), obj, m); + handler = (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), obj, m); #endif - bindMsg(attr.GetExpression(obj), handler); + BindMsg(attr.GetExpression(obj), handler); } } @@ -428,18 +402,22 @@ namespace IvyBus /// * /// /// - public void start(string domainbus) + public void Start(string domainbus) { - domainbus = getDomain(domainbus); + domainbus = GetDomain(domainbus); try { + long seed = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; + Random rand = new Random( (int)seed ); + app = new MyTcpListener(IPAddress.Any, 0); app.Start(); // should be started before getting port - applicationHost = getLocalIP(); + applicationHost = GetLocalIP(); applicationPort = (ushort)((IPEndPoint)app.LocalEndpoint).Port; - applicationUniqueId = string.Format("{0}-{1}-{2}", - DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond, - applicationHost.ToString().Replace(".", ""), + applicationUniqueId = string.Format(culture,"{0}:{1}:{2}", + rand.Next(), + seed, + //applicationHost.ToString().Replace(".", ""), applicationPort); } @@ -489,7 +467,7 @@ namespace IvyBus } /* a small private method for debbugging purposes */ - public static string domains(string toparse) + public static string Domains(string toparse) { StringBuilder s = new StringBuilder("broadcasting on "); Ivy.Domain[] d = parseDomains(toparse); @@ -509,14 +487,14 @@ namespace IvyBus Domain[] d = new Domain[st.Length]; for (int i = 0; i < st.Length; i++) { - d[i] = new Domain(IvyWatcher.getDomain(st[i]), IvyWatcher.getPort(st[i])); + d[i] = new Domain(st[i]); } return d; } /// disconnects from the Ivy bus /// - public void stop() + public void Stop() { if (stopped) return; @@ -584,7 +562,7 @@ namespace IvyBus /// the number of messages actually sent /// /// - public int sendMsg(string format, params object[] args) + public int SendMsg(string format, params object[] args) { string msg = string.Format(culture, format, args); int count = 0; @@ -616,15 +594,15 @@ namespace IvyBus /// /// // - // for compatibility raison with old IVY #if OLD_API + // for compatibility raison with old IVY [Obsolete("Will be removed in next version")] public int bindMsg(string regexp, MessageHandler callback) { return bindMsg( regexp, callback, null); } #endif - public ushort bindMsg(string regexp, MessageHandler callback, params object[] args) + public ushort BindMsg(string regexp, EventHandler callback, params object[] args) { // creates a new binding (regexp,callback) ApplicationBinding newbind; @@ -650,7 +628,7 @@ namespace IvyBus /// the id of the regular expression, returned when it was bound /// /// - public void unBindMsg(ushort id) + public void UnbindMsg(ushort id) { if (!bindings.ContainsKey(id)) { @@ -675,7 +653,7 @@ namespace IvyBus /// the string for the regular expression /// /// - public bool unBindMsg(string re) + public bool UnbindMsg(string re) { foreach (ApplicationBinding bind in bindings.Values) { @@ -683,7 +661,7 @@ namespace IvyBus { try { - unBindMsg(bind.key); + UnbindMsg(bind.key); } catch (IvyException) { @@ -712,7 +690,7 @@ namespace IvyBus /// /// - public int bindSimpleMsg(string expression, MessageHandler callback, params object[] args) + public int BindSimpleMsg(string expression, EventHandler callback, params object[] args) { // creates a new binding (regexp,callback) ApplicationBinding newbind; @@ -734,14 +712,14 @@ namespace IvyBus } public int Die(string target, string message) { - List v = getIvyClientsByName(target); + List v = GetClientsByName(target); for (int i = 0; i < v.Count; i++) v[i].stream.TokenDie(0, message); return v.Count; } public int Ping(string target, string message) { - List v = getIvyClientsByName(target); + List v = GetClientsByName(target); for (int i = 0; i < v.Count; i++) v[i].stream.TokenPing(message); return v.Count; @@ -757,34 +735,34 @@ namespace IvyBus return true; else return false; } - internal void FireDirectMessage(IvyClient clnt, int id, string arg) + internal virtual void OnDirectMessage(IvyEventArgs e) { if (DirectMessageReceived != null) { if (IsSynchronous()) - syncControl.Invoke(DirectMessageReceived, clnt, id, arg ); - else DirectMessageReceived(clnt, id, arg); + syncControl.Invoke(DirectMessageReceived, this, e ); + else DirectMessageReceived(this, e); } } - internal void FireClientConnected(IvyClient clnt) + internal virtual void OnClientConnected(IvyEventArgs e) { if (ClientConnected != null) { if (IsSynchronous()) - syncControl.Invoke(ClientConnected, clnt ); - else ClientConnected(clnt); + syncControl.Invoke(ClientConnected, this, e ); + else ClientConnected(this,e); } } - internal void FireClientDisconnected(IvyClient clnt) + internal virtual void OnClientDisconnected(IvyEventArgs e) { if (ClientDisconnected != null) { try { if (IsSynchronous()) - syncControl.Invoke(ClientDisconnected, clnt ); - else ClientDisconnected(clnt); + syncControl.Invoke(ClientDisconnected, this, e ); + else ClientDisconnected(this,e); } catch (ObjectDisposedException ) { @@ -792,50 +770,49 @@ namespace IvyBus } } } - internal void FireClientAddBinding(IvyClient clnt, string regexp) + internal virtual void OnClientAddBinding(IvyEventArgs e) { if (BindingAdd != null) { if (IsSynchronous()) - syncControl.Invoke(BindingAdd, clnt, regexp ); - else BindingAdd(clnt, regexp); + syncControl.Invoke(BindingAdd, this, e ); + else BindingAdd(this, e); } } - internal void FireClientRemoveBinding(IvyClient clnt, string regexp) + internal virtual void OnClientRemoveBinding(IvyEventArgs e) { if (BindingRemove != null) { if (IsSynchronous()) - syncControl.Invoke(BindingRemove, clnt, regexp ); - else BindingRemove(clnt, regexp); + syncControl.Invoke(BindingRemove, this, e ); + else BindingRemove(this, e); } } - internal void FireClientFilterBinding(IvyClient clnt, string regexp) + internal virtual void OnClientFilterBinding(IvyEventArgs e) { if (BindingFilter != null) { if (IsSynchronous()) - syncControl.Invoke(BindingFilter, clnt, regexp); - else BindingFilter(clnt, regexp); + syncControl.Invoke(BindingFilter, this, e); + else BindingFilter(this, e); } } - internal ApplicationExit FireDie(IvyClient clnt, int id, string arg) + internal virtual void OnDie(IvyDieEventArgs e) { - if (dieReceived != null) + if (DieReceived != null) { if (IsSynchronous()) - return (ApplicationExit)syncControl.Invoke(dieReceived, clnt, id, arg ); - else return dieReceived(clnt, id, arg); + syncControl.Invoke(DieReceived, this, e ); + else DieReceived(this, e); } - return ApplicationExit.FORCE_EXIT; } - internal void FireError(IvyClient clnt, int id, string arg) + internal virtual void OnError(IvyEventArgs e) { if (ErrorMessage != null) { if (IsSynchronous()) - syncControl.Invoke(ErrorMessage, clnt, id, arg ); - else ErrorMessage(clnt, id, arg); + syncControl.Invoke(ErrorMessage, this, e ); + else ErrorMessage(this, e); } } @@ -855,7 +832,7 @@ namespace IvyBus /// /// The name of the Ivy agent you're looking for /// - public List getIvyClientsByName(string name) + public List GetClientsByName(string name) { List v = new List(); foreach (IvyClient ic in clients) @@ -881,22 +858,21 @@ namespace IvyBus { clients.Add(client); } - traceDebug(ClientNames); } - internal void FireCallback(IvyClient client, int key, string[] arg) + internal void FireCallback(IvyMessageEventArgs e) { - ApplicationBinding bind = bindings[key]; + ApplicationBinding bind = bindings[e.Id]; if (bind.callback == null) { - throw new IvyException("(callCallback) Not regexp matching id " + key); + throw new IvyException("(callCallback) Not regexp matching id " + e.Id); } if (IsSynchronous()) - syncControl.Invoke(bind.callback, client, arg ); - else bind.callback(client, arg); + syncControl.Invoke(bind.callback, this, e ); + else bind.callback(this, e); } - public static IPAddress getLocalIP() + public static IPAddress GetLocalIP() { IPAddress returnaddr = null; //TODO remove ALL reverse DNS search !!!! @@ -910,7 +886,7 @@ namespace IvyBus return returnaddr; } - public static string getDomain(string domainbus) + public static string GetDomain(string domainbus) { #if (PocketPC) if (domainbus == null || domainbus.Length == 0) @@ -1050,6 +1026,11 @@ namespace IvyBus } private string domainaddr; private int port; + public Domain(string net) + { + this.domainaddr = getDomain(net); + this.port = getPort(net); + } public Domain(string domainaddr, int port) { this.domainaddr = domainaddr; this.port = port; @@ -1058,6 +1039,34 @@ namespace IvyBus { return domainaddr + ":" + port; } + private static string getDomain(string net) + { + int sep_index = net.LastIndexOf(":"); + if (sep_index != -1) + { + net = net.Substring(0, (sep_index) - (0)); + } + try + { + net += ".255.255.255"; + Regex exp = new Regex("^(\\d+\\.\\d+\\.\\d+\\.\\d+).*"); + net = exp.Replace(net, "$1"); + } + catch (ArgumentException e) + { + traceDebug("Bad broascat addr " + net + "error " + e.Message); + return null; + } + return net; + } + + private static int getPort(string net) + { + if (net == null) return Ivy.DEFAULT_PORT; + int sep_index = net.LastIndexOf(":"); + int port = (sep_index == -1) ? Ivy.DEFAULT_PORT : Int32.Parse(net.Substring(sep_index + 1)); + return port; + } } -- cgit v1.1