diff options
author | fcolin | 2007-02-01 09:56:06 +0000 |
---|---|---|
committer | fcolin | 2007-02-01 09:56:06 +0000 |
commit | 6eb6f27858cd1822ef93e6988a95d3325a69ee8a (patch) | |
tree | 2c00f75b0bb18b71f08d976c1b44e739953352e3 /CSharp | |
parent | 1fa2f9f37991fe3234abf11d98bc9fd129d77a80 (diff) | |
download | ivy-csharp-6eb6f27858cd1822ef93e6988a95d3325a69ee8a.zip ivy-csharp-6eb6f27858cd1822ef93e6988a95d3325a69ee8a.tar.gz ivy-csharp-6eb6f27858cd1822ef93e6988a95d3325a69ee8a.tar.bz2 ivy-csharp-6eb6f27858cd1822ef93e6988a95d3325a69ee8a.tar.xz |
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)
Diffstat (limited to 'CSharp')
-rw-r--r-- | CSharp/Ivy/IvyPPC/Ivy.cs | 227 |
1 files changed, 118 insertions, 109 deletions
diff --git a/CSharp/Ivy/IvyPPC/Ivy.cs b/CSharp/Ivy/IvyPPC/Ivy.cs index e0573e4..a64acb8 100644 --- a/CSharp/Ivy/IvyPPC/Ivy.cs +++ b/CSharp/Ivy/IvyPPC/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 */
/// <summary>fires when a new client connect to the bus</summary>
/// <param name='appcb'>A callback handling the notification of connexions and
/// disconnections, may be null
///
/// </param>
- public event ClientConnectedHandler ClientConnected;
+ public event EventHandler<IvyEventArgs> ClientConnected;
/// <summary>fires when a client discconnect from the bus</summary>
- public event ClientDisconnectedHandler ClientDisconnected;
+ public event EventHandler<IvyEventArgs> ClientDisconnected;
/// <summary>fires when a client receive a direct message from another client</summary>
- public event DirectMessageHandler DirectMessageReceived;
+ public event EventHandler<IvyEventArgs> DirectMessageReceived;
/// <summary>fires when somebody ask for killing every client on the bus</summary>
- public event DieHandler dieReceived;
+ public event EventHandler<IvyDieEventArgs> DieReceived;
/// <summary>fires when a client receive a add binding from another client</summary>
- public event ClientAddBindingHandler BindingAdd;
+ public event EventHandler<IvyEventArgs> BindingAdd;
/// <summary>fires when a client receive a remove binding from another client</summary>
- public event ClientRemoveBindingHandler BindingRemove;
+ public event EventHandler<IvyEventArgs> BindingRemove;
/// <summary>fires when a client receive a binding from another clientand it as been filtered </summary>
- public event ClientRemoveBindingHandler BindingFilter;
+ public event EventHandler<IvyEventArgs> BindingFilter;
/// <summary>fires when a client receive a remove binding from another client</summary>
- public event ErrorMessageHandler ErrorMessage;
+ public event EventHandler<IvyEventArgs> ErrorMessage;
#if (!PocketPC)
[Bindable(true)]
@@ -117,26 +102,7 @@ namespace IvyBus }
}
- /// <summary>ClientNames accesses the name list of the connected clients</summary
- private string ClientNames
- {
- get
- {
- StringBuilder s = new StringBuilder();
- s.Append(appName);
- s.Append(" clients are: ");
- lock (clients)
- {
- foreach (IvyClient client in clients)
- {
- s.Append(client.ApplicationName);
- s.Append(" ");
- }
- }
- return s.ToString();
- }
-
- }
+
/// <summary>AppName the application name</summary>
#if (!PocketPC)
@@ -295,7 +261,7 @@ namespace IvyBus {
internal BindingType type;
internal ushort key;
- internal MessageHandler callback;
+ internal EventHandler<IvyMessageEventArgs> 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<IvyMessageEventArgs> 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<IvyMessageEventArgs>)Delegate.CreateDelegate(typeof(EventHandler<IvyMessageEventArgs>), 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<IvyMessageEventArgs> handler;
#if (PocketPC)
handler = null; // TODO
#else
- handler = (MessageHandler)Delegate.CreateDelegate(typeof(MessageHandler), obj, m);
+ handler = (EventHandler<IvyMessageEventArgs>)Delegate.CreateDelegate(typeof(EventHandler<IvyMessageEventArgs>), obj, m);
#endif
- bindMsg(attr.GetExpression(obj), handler);
+ BindMsg(attr.GetExpression(obj), handler);
}
}
@@ -428,18 +402,22 @@ namespace IvyBus /// *
///
/// </param>
- 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;
}
/// <summary> disconnects from the Ivy bus
/// </summary>
- public void stop()
+ public void Stop()
{
if (stopped)
return;
@@ -584,7 +562,7 @@ namespace IvyBus /// <returns>the number of messages actually sent
///
/// </returns>
- 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 ///
/// </returns>
//
- // 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<IvyMessageEventArgs> callback, params object[] args)
{
// creates a new binding (regexp,callback)
ApplicationBinding newbind;
@@ -650,7 +628,7 @@ namespace IvyBus /// <param name='id'>the id of the regular expression, returned when it was bound
///
/// </param>
- public void unBindMsg(ushort id)
+ public void UnbindMsg(ushort id)
{
if (!bindings.ContainsKey(id))
{
@@ -675,7 +653,7 @@ namespace IvyBus /// <param name='re'>the string for the regular expression
///
/// </param>
- 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 ///
/// </returns>
- public int bindSimpleMsg(string expression, MessageHandler callback, params object[] args)
+ public int BindSimpleMsg(string expression, EventHandler<IvyMessageEventArgs> 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<IvyClient> v = getIvyClientsByName(target);
+ List<IvyClient> 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<IvyClient> v = getIvyClientsByName(target);
+ List<IvyClient> 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 /// </summary>
/// <param name='name'>The name of the Ivy agent you're looking for
/// </param>
- public List<IvyClient> getIvyClientsByName(string name)
+ public List<IvyClient> GetClientsByName(string name)
{
List<IvyClient> v = new List<IvyClient>();
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;
+ }
}
|