diff options
Diffstat (limited to 'CSharp/Ivy')
-rw-r--r-- | CSharp/Ivy/IvyPPC/Ivy.cs | 145 |
1 files changed, 73 insertions, 72 deletions
diff --git a/CSharp/Ivy/IvyPPC/Ivy.cs b/CSharp/Ivy/IvyPPC/Ivy.cs index 7332eed..7a3cec2 100644 --- a/CSharp/Ivy/IvyPPC/Ivy.cs +++ b/CSharp/Ivy/IvyPPC/Ivy.cs @@ -8,7 +8,9 @@ namespace IvyBus using System;
using System.IO;
using System.Collections;
- using System.Net;
+ using System.Collections.Specialized;
+ using System.Collections.Generic;
+ using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Configuration;
@@ -22,14 +24,14 @@ namespace IvyBus public class Ivy
{
/* Event handler */
- public delegate void DirectMessageHandler (IvyClient app, int id, byte[] arg );
+ public delegate void DirectMessageHandler (IvyClient app, int id, string arg );
public delegate void ClientConnectedHandler (IvyClient app);
public delegate void ClientDisconnectedHandler (IvyClient app);
- public delegate bool DieHandler (IvyClient app, int id );
+ public delegate bool 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 MessageHandler ( IvyClient app, IvyArgument args );
+ public delegate void MessageHandler ( IvyClient app, string[] args );
/* Event */
@@ -52,7 +54,7 @@ namespace IvyBus /// <summary>IvyClients accesses the list of the connected clients</summary>
- public ArrayList IvyClients
+ public List<IvyClient> IvyClients
{
get
{
@@ -80,7 +82,7 @@ namespace IvyBus StringBuilder s = new StringBuilder();
s.Append(appName);
s.Append( " clients are: " );
- lock( clients.SyncRoot )
+ lock( clients )
{
foreach (IvyClient client in clients )
{
@@ -109,16 +111,16 @@ namespace IvyBus }
}
- public int AppPriority
+ public ushort AppPriority
{
set
{
applicationPriority = value;
- lock ( clients.SyncRoot )
+ lock ( clients )
{
foreach (IvyClient client in clients )
{
- client.stream.sendApplicationId(applicationPriority, AppId);
+ client.stream.TokenApplicationId(applicationPriority, AppId);
}
}
@@ -158,36 +160,37 @@ namespace IvyBus /// the name of the application on the bus
internal string appName;
/// the port for the UDP rendez vous, if none is supplied
- internal const int DEFAULT_PORT = 2010;
+ internal const ushort DEFAULT_PORT = 2010;
// client default priority
- internal const int DEFAULT_PRIORITY = 100;
+ internal const ushort DEFAULT_PRIORITY = 100;
/// the domain for the UDP rendez vous
internal static readonly string DEFAULT_DOMAIN = "127.255.255.255:" + DEFAULT_PORT;
internal const string libVersion = "2.0.0";
private bool debug;
- private static int serial = 0; /* an unique ID for each regexp */
+ private static ushort serial = 0; /* an unique ID for each regexp */
private MyTcpListener app;
- private ArrayList watchers;
+ private List<IvyWatcher> watchers;
private volatile Thread serverThread; // to ensure quick communication of the end
internal enum BindingType { BindRegexp, BindSimple };
internal struct ApplicationBinding /* Self Applications bindings */
{
internal BindingType type;
- internal Int32 key;
+ internal ushort key;
internal MessageHandler callback;
internal string regexp;
internal object[] args;
}
-
- internal Hashtable bindings;
- private ArrayList clients;
+
+ internal Dictionary<int,ApplicationBinding> bindings;
+ private List<IvyClient> clients;
private string[] sent_messageClasses = null;
private bool stopped = false;
- internal int applicationPort; /* Application port number */
- internal string applicationUniqueId; /* identifier Application unique timestamp-ipaddress-port */
- internal int applicationPriority = DEFAULT_PRIORITY;
+ internal ushort applicationPort; /* Application port number */
+ internal IPAddress applicationHost; /* Application host number */
+ internal string applicationUniqueId; /* identifier Application unique timestamp-ipaddress-port */
+ internal ushort applicationPriority = DEFAULT_PRIORITY;
internal string ready_message = null;
// for synchronous event
@@ -216,16 +219,13 @@ namespace IvyBus /// </param>
public Ivy(string name, string message)
{
- clients = ArrayList.Synchronized( new ArrayList() );
- //callbacks = Hashtable.Synchronized( new Hashtable() );
- //clients_data = Hashtable.Synchronized( new Hashtable() );
- //regexp_out = Hashtable.Synchronized( new Hashtable());
- bindings = Hashtable.Synchronized( new Hashtable());
- appName = name;
+ //clients = ArrayList.Synchronized( new ArrayList() );
+ //bindings = Hashtable.Synchronized( new Hashtable());
+ clients = new List<IvyClient>();
+ bindings = new Dictionary<int,ApplicationBinding>();
+ appName = name;
ready_message = message;
- string debug_str = ConfigurationSettings.AppSettings["IVY_DEBUG"];
- if ( debug_str != null )
- debug = bool.Parse(debug_str);
+ debug = Properties.Settings.Default.IvyDebug;
}
public Ivy(string name, string message, Control sync) :this( name, message )
{
@@ -258,7 +258,8 @@ namespace IvyBus IPAddress localaddr = Dns.Resolve(Dns.GetHostName()).AddressList[0];
app = new MyTcpListener(IPAddress.Any,0);
app.Start();
- applicationPort = ((IPEndPoint) app.LocalEndpoint).Port;
+ applicationPort = (ushort)((IPEndPoint) app.LocalEndpoint).Port;
+ applicationHost = localaddr;
applicationUniqueId = string.Format("{0}-{1}-{2}",
DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond,
localaddr.ToString().Replace(".",""),
@@ -268,8 +269,8 @@ namespace IvyBus {
throw new IvyException("can't open TCP service socket " + e);
}
- traceDebug("lib: " + libVersion + " protocol: " + IvyUDPStreamV4.PROCOCOLVERSION + " TCP service open on port " + applicationPort);
- watchers = new ArrayList();
+ traceDebug("lib: " + libVersion + " protocol: " + Properties.Settings.Default.IvyProtocolVersion + " TCP service open on port " + applicationPort);
+ watchers = new List<IvyWatcher>();
Domain[] d = parseDomains(domainbus);
// readies the rendezvous : an IvyWatcher (thread) per domain bus
@@ -290,14 +291,15 @@ namespace IvyBus // sends the broadcasts and listen to incoming connexions
for (int i = 0; i < watchers.Count; i++)
{
- ((IvyWatcher) watchers[i]).start();
+ watchers[i].start();
}
}
internal void SortClients()
{
- lock ( clients.SyncRoot )
+ lock ( clients )
{
- clients.Sort(new IvyClient.IvyClientPriority());
+ //clients.Sort(new IvyClient.IvyClientPriority());
+ clients.Sort();
}
}
/* a small private method for debbugging purposes */
@@ -361,7 +363,7 @@ namespace IvyBus {
IvyClient[] copyClient;
copyClient = new IvyClient[clients.Count];
- lock( clients.SyncRoot )
+ lock( clients )
{
clients.CopyTo( copyClient,0 );
}
@@ -400,7 +402,7 @@ namespace IvyBus int count = 0;
// an alternate implementation would one sender thread per client
// instead of one for all the clients. It might be a performance issue
- lock ( clients.SyncRoot )
+ lock ( clients )
{
foreach (IvyClient client in clients )
{
@@ -476,13 +478,13 @@ namespace IvyBus newbind.regexp = regexp;
newbind.callback = callback;
newbind.args = args;
- lock (bindings.SyncRoot) bindings.Add( newbind.key, newbind);
+ lock (bindings) bindings.Add( newbind.key, newbind);
// notifies the other clients this new regexp
- lock ( clients.SyncRoot )
+ lock ( clients )
{
foreach (IvyClient c in clients )
{
- c.stream.sendBinding(newbind);
+ c.stream.TokenAddRegexp(newbind.key,newbind.regexp);
}
}
return newbind.key;
@@ -493,21 +495,21 @@ namespace IvyBus /// <param name='id'>the id of the regular expression, returned when it was bound
///
/// </param>
- public void unBindMsg(int id)
+ public void unBindMsg(ushort id)
{
if ( ! bindings.ContainsKey( id ) )
{
throw new IvyException("client wants to remove an unexistant regexp " + id);
}
ApplicationBinding bind = (ApplicationBinding) bindings[id];
- lock( clients.SyncRoot )
+ lock( clients )
{
foreach (IvyClient c in clients )
{
- c.stream.delBinding(bind);
+ c.stream.TokenDelBinding(id);
}
}
- lock( bindings.SyncRoot ) bindings.Remove( id );
+ lock( bindings ) bindings.Remove( id );
}
/// <summary> unsubscribes a regular expression
@@ -564,32 +566,32 @@ namespace IvyBus newbind.regexp = expression;
newbind.callback = callback;
newbind.args = args;
- lock (bindings.SyncRoot) bindings.Add( newbind.key, newbind);
+ lock (bindings) bindings.Add( newbind.key, newbind);
// notifies the other clients this new regexp
- lock ( clients.SyncRoot )
+ lock ( clients )
{
foreach (IvyClient c in clients )
{
- c.stream.sendBinding(newbind);
+ c.stream.TokenAddBinding(newbind.key,newbind.regexp);
}
}
return newbind.key;
}
public int Die( string target, string message )
{
- System.Collections.ArrayList v = getIvyClientsByName(target);
+ List<IvyClient> v = getIvyClientsByName(target);
for (int i = 0; i < v.Count; i++)
- ((IvyClient) v[i]).stream.sendDie(message);
+ v[i].stream.TokenDie(0,message);
return v.Count;
}
public int Ping( string target, string message)
{
- System.Collections.ArrayList v = getIvyClientsByName(target);
+ List<IvyClient> v = getIvyClientsByName(target);
for (int i = 0; i < v.Count; i++)
- ((IvyClient)v[i]).stream.sendPing(message);
+ v[i].stream.TokenPing(message);
return v.Count;
}
- internal void FireDirectMessage (IvyClient app, int id, byte[] arg )
+ internal void FireDirectMessage(IvyClient app, int id, string arg)
{
if ( directMessageReceived != null )
{
@@ -634,13 +636,13 @@ namespace IvyBus else removeBinding( app,regexp);
}
}
- internal bool FireDie (IvyClient app, int id )
+ internal bool FireDie (IvyClient app, int id, string arg )
{
if ( dieReceived != null )
{
if ( syncControl != null )
- syncControl.Invoke( dieReceived, new object[]{app,id});
- else return dieReceived( app, id);
+ syncControl.Invoke( dieReceived, new object[]{app,id,arg});
+ else return dieReceived( app, id, arg);
}
return false;
}
@@ -651,7 +653,7 @@ namespace IvyBus */
internal void removeClient(IvyClient c)
{
- lock( clients.SyncRoot )
+ lock( clients )
{
clients.Remove(c);
}
@@ -662,12 +664,12 @@ namespace IvyBus /// </summary>
/// <param name='name'>The name of the Ivy agent you're looking for
/// </param>
- public ArrayList getIvyClientsByName(string name)
+ public List<IvyClient> getIvyClientsByName(string name)
{
- ArrayList v = new ArrayList();
+ List<IvyClient> v = new List<IvyClient>();
foreach (IvyClient ic in clients )
{
- if (((ic.ApplicationName).CompareTo(name)) == 0)
+ if (ic.ApplicationName.CompareTo(name) == 0)
v.Add(ic);
}
return v;
@@ -684,23 +686,22 @@ namespace IvyBus if (stopped)
return ;
IvyClient client = new IvyClient(this,socket,appname);
- lock ( clients.SyncRoot )
+ lock ( clients )
{
clients.Add(client);
}
traceDebug(ClientNames);
}
-
- internal void callCallback(IvyClient client, int key, byte[] data)
+
+ internal void FireCallback(IvyClient client, int key, string[] arg)
{
- ApplicationBinding bind = ( ApplicationBinding ) bindings[key];
+ ApplicationBinding bind = bindings[key];
if (bind.callback == null)
{
throw new IvyException("(callCallback) Not regexp matching id " + key);
}
- IvyArgument arg = IvyArgument.Deserialize( data );
if ( syncControl != null )
- syncControl.Invoke( bind.callback, new object[]{client,arg});
+ syncControl.Invoke(bind.callback, new object[] { client, arg });
else bind.callback(client, arg);
}
@@ -709,19 +710,19 @@ namespace IvyBus {
if (domainbus == null)
{
- domainbus = ConfigurationSettings.AppSettings["IVYBUS"];
- }
- if (domainbus == null)
- {
domainbus = Environment.GetEnvironmentVariable("IVYBUS");
}
- if (domainbus == null)
+ if (domainbus == null)
+ {
+ domainbus = Properties.Settings.Default.IvyBus;
+ }
+ if (domainbus == null)
domainbus = DEFAULT_DOMAIN;
return domainbus;
}
- /// checks the "validity" of a regular expression.
+ /// checks the "validity" of a regular expression. //TODO put in IvyBinding
internal bool CheckRegexp(string exp)
{
bool regexp_ok = true;
@@ -752,7 +753,7 @@ namespace IvyBus {
if (clnt.AppPort == 0)
return false;
- lock( clients.SyncRoot )
+ lock( clients )
{
foreach (IvyClient client in clients )
{
|