summaryrefslogtreecommitdiff
path: root/Ivy/IvyWatcher.cs
diff options
context:
space:
mode:
authorfcolin2008-08-22 16:44:01 +0000
committerfcolin2008-08-22 16:44:01 +0000
commit8d10e8bbd1e19adc7c70e1101dbb69c213c910dd (patch)
treef41034ab66b1b3174277b07c8aa45791dadbaae8 /Ivy/IvyWatcher.cs
parent7053d3d604920ab708076e107be4b55666c5af80 (diff)
downloadivy-csharp-8d10e8bbd1e19adc7c70e1101dbb69c213c910dd.zip
ivy-csharp-8d10e8bbd1e19adc7c70e1101dbb69c213c910dd.tar.gz
ivy-csharp-8d10e8bbd1e19adc7c70e1101dbb69c213c910dd.tar.bz2
ivy-csharp-8d10e8bbd1e19adc7c70e1101dbb69c213c910dd.tar.xz
optimisation for parsing same regular expression from multiple client
using fxCop for code beauty fix bug on concurrent connect
Diffstat (limited to 'Ivy/IvyWatcher.cs')
-rw-r--r--Ivy/IvyWatcher.cs109
1 files changed, 71 insertions, 38 deletions
diff --git a/Ivy/IvyWatcher.cs b/Ivy/IvyWatcher.cs
index 5a3afc8..b985ada 100644
--- a/Ivy/IvyWatcher.cs
+++ b/Ivy/IvyWatcher.cs
@@ -16,6 +16,7 @@ namespace IvyBus
using System.Configuration;
using System.Text;
using System.Diagnostics;
+ using IvyBus.Properties;
/// <summary> IvyWatcher, A private Class for the Ivy rendezvous
/// </summary>
@@ -25,7 +26,7 @@ namespace IvyBus
/// that the broadcast is done using the same socket, which is not a good
/// thing.
/// </remarks>
- internal class IvyWatcher
+ internal class IvyWatcher: IDisposable
{
private Ivy bus; /* master bus controler */
private int port;
@@ -43,6 +44,7 @@ namespace IvyBus
/// </param>
internal IvyWatcher(Ivy bus, String domainaddr, int port)
{
+ int multicast_ttl = 64; // region
this.bus = bus;
this.port = port;
listenThread = new Thread(new ThreadStart(this.Run));
@@ -59,11 +61,13 @@ namespace IvyBus
broadcast.Bind(EPhost);
//test isMulticastAddress // TODO better check
- //if ( group.IsIPv6Multicast ) yes but in IPV4 how to do
+ //if ( group.IsIPv6Multicast ) //yes but in IPV4 how to do
byte[] addr = group.GetAddressBytes();
if ((addr[0] & 0xf0) == 0xe0)
{
- broadcast.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption( group ));
+ broadcast.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicast_ttl);
+ broadcast.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(group));
+
}
// TODO support the Two protocol
if (bus.protocolVersion == 4)
@@ -73,7 +77,7 @@ namespace IvyBus
}
catch (IOException e)
{
- throw new IvyException("IvyWatcher I/O error" + e);
+ throw new IvyException(Resources.WatcherIOError + e);
}
}
@@ -81,15 +85,15 @@ namespace IvyBus
/// </summary>
public void Run()
{
- Ivy.traceProtocol("IvyWatcher", "beginning of a watcher Thread");
-
- try
- {
- bool running = true;
- while (running)
- {
- ushort version;
- ushort appPort;
+ Ivy.traceProtocol(Resources.IvyWatcher, "beginning of a watcher Thread");
+
+ try
+ {
+ bool running = true;
+ while (running)
+ {
+ int version;
+ int appPort;
string appId;
string appName;
IPEndPoint remoteEP;
@@ -97,26 +101,26 @@ namespace IvyBus
stream.receiveMsg(out remoteEP, out version, out appPort, out appId, out appName);
IPAddress remotehost = remoteEP.Address;
- Ivy.traceProtocol("IvyWatcher", "Receive Broadcast from " + Dns.GetHostEntry(remotehost).HostName + ":" + remoteEP.Port);
-
+ Ivy.traceProtocol(Resources.IvyWatcher, Resources.WatcherReceive + Dns.GetHostEntry(remotehost).HostName + ":" + remoteEP.Port);
+
//TODO if ( !isInDomain( remotehost ) ) continue;
if (version != stream.ProtocolVersion)
{
- Ivy.traceError("IvyWatcher","Ignoring bad protocol version " + version + " expected " + stream.ProtocolVersion);
+ Ivy.traceError(Resources.IvyWatcher, Resources.BadVersion + version + " expected " + stream.ProtocolVersion);
continue;
}
- // filtrage des self Broadcast
+ /* check if we received our own message. SHOULD ALSO TEST THE HOST */
if (appId == bus.AppId)
continue;
if ((appPort == bus.applicationPort) && (remotehost.Equals(bus.applicationHost)))
continue;
- Ivy.traceProtocol("IvyWatcher", "reponse au Broadcast de " + Dns.GetHostEntry(remotehost).HostName + ":" + remoteEP.Port + " port " + appPort +
+ Ivy.traceProtocol(Resources.IvyWatcher, "reponse au Broadcast de " + Dns.GetHostEntry(remotehost).HostName + ":" + remoteEP.Port + " port " + appPort +
" version " + version +
" id " + appId +
- " name " + appName);
+ " name " + appName);
try
{
@@ -124,24 +128,29 @@ namespace IvyBus
IPEndPoint hostEndPoint = new IPEndPoint(remoteEP.Address, appPort);
socket.Blocking = true;
socket.Connect(hostEndPoint);
- bus.addClient(socket, appName);
+ IvyClient client = new IvyClient(this.bus, socket, appName, appPort);
+ client.SendBindings();
}
- catch (Exception e)
+ catch (SocketException e)
{
- Ivy.traceError("IvyWatcher","can't connect to " + remotehost + " port " + appPort + " \n" + e.Message);
+ Ivy.traceError(Resources.IvyWatcher, Resources.WatcherConnectError + remotehost + " port " + appPort + " \n" + e.Message);
}
-
- } // while
- }
- catch (SocketException se)
- {
- Ivy.traceError("IvyWatcher","watcher socket closed: " + se.Message);
- }
- catch (IOException ioe)
- {
- Ivy.traceError("IvyWatcher","watcher thread ended: " + ioe.Message);
- }
- Ivy.traceProtocol("IvyWatcher", "end of a watcher thread");
+
+ } // while
+ }
+ catch (ObjectDisposedException ex)
+ {
+ Ivy.traceError(Resources.IvyWatcher, Resources.WatcherSocketClosed + ex.Message);
+ }
+ catch (SocketException se)
+ {
+ Ivy.traceError(Resources.IvyWatcher, Resources.WatcherSocketClosed + se.Message);
+ }
+ catch (IOException ioe)
+ {
+ Ivy.traceError(Resources.IvyWatcher, Resources.WatcherIOException + ioe.Message);
+ }
+ Ivy.traceProtocol(Resources.IvyWatcher, "end of a watcher thread");
}
/// <summary> stops the thread waiting on the broadcast socket
@@ -150,7 +159,7 @@ namespace IvyBus
{
lock (stream)
{
- Ivy.traceProtocol("IvyWatcher", "begining stopping an IvyWatcher");
+ Ivy.traceProtocol(Resources.IvyWatcher, "begining stopping an IvyWatcher");
stream.Close();
if (listenThread != null)
{
@@ -160,7 +169,7 @@ namespace IvyBus
listenThread = null;
}
// it might not even have been created
- Ivy.traceProtocol("IvyWatcher", "ending stopping an IvyWatcher");
+ Ivy.traceProtocol(Resources.IvyWatcher, "ending stopping an IvyWatcher");
}
}
@@ -174,6 +183,30 @@ namespace IvyBus
}
}
-
- }
+
+ #region IDisposable Membres
+
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ // Free other state (managed objects).
+ }
+ // Free your own state (unmanaged objects).
+ // Set large fields to null.
+ if (stream != null)
+ {
+ stream.Close();
+ stream = null;
+ }
+ }
+
+ #endregion
+ }
} \ No newline at end of file