Skip to content

Commit

Permalink
feat(ServiceDiscovery): ServiceInstanceShutdown event #58
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Jun 8, 2019
1 parent 681b3d0 commit 8545fc3
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/ServiceDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ public ServiceDiscovery(MulticastService mdns)
/// </remarks>
public event EventHandler<ServiceInstanceDiscoveryEventArgs> ServiceInstanceDiscovered;

/// <summary>
/// Raised when a servive instance is shutting down.
/// </summary>
/// <value>
/// Contains the service instance name.
/// </value>
/// <remarks>
/// <b>ServiceDiscovery</b> passively monitors the network for any answers.
/// When an answer containing a PTR to a service instance with a
/// TTL of zero is received this event is raised.
/// </remarks>
public event EventHandler<ServiceInstanceShutdownEventArgs> ServiceInstanceShutdown;

/// <summary>
/// Asks other MDNS services to send their service names.
/// </summary>
Expand Down Expand Up @@ -232,6 +245,14 @@ public void Unadvertise()
void OnAnswer(object sender, MessageEventArgs e)
{
var msg = e.Message;
if (log.IsDebugEnabled)
{
log.Debug($"Answer from {e.RemoteEndPoint}");
}
if (log.IsTraceEnabled)
{
log.Trace(msg);
}

// Any DNS-SD answers?
var sd = msg.Answers.OfType<PTRRecord>();
Expand All @@ -241,6 +262,15 @@ void OnAnswer(object sender, MessageEventArgs e)
{
ServiceDiscovered?.Invoke(this, ptr.DomainName);
}
else if (ptr.TTL == TimeSpan.Zero)
{
var args = new ServiceInstanceShutdownEventArgs
{
ServiceInstanceName = ptr.DomainName,
Message = msg
};
ServiceInstanceShutdown?.Invoke(this, args);
}
else
{
var args = new ServiceInstanceDiscoveryEventArgs
Expand Down
23 changes: 23 additions & 0 deletions src/ServiceInstanceShutdownEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Makaretu.Dns;
using System;
using System.Collections.Generic;
using System.Text;

namespace Makaretu.Dns
{
/// <summary>
/// The event data for <see cref="ServiceDiscovery.ServiceInstanceShutdown"/>.
/// </summary>
public class ServiceInstanceShutdownEventArgs : MessageEventArgs
{
/// <summary>
/// The fully qualified name of the service instance.
/// </summary>
/// <value>
/// Typically of the form "<i>instance</i>._<i>service</i>._tcp.local".
/// </value>
/// <seealso cref="ServiceProfile.FullyQualifiedName"/>
public string ServiceInstanceName { get; set; }
}
}

30 changes: 30 additions & 0 deletions test/ServiceDiscoveryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,5 +291,35 @@ public void Discover_ServiceInstance_WithAnswersContainingAdditionRecords()
Assert.AreEqual(additionalRecordsCount + 1, discovered.Answers.Count);
}
}

[TestMethod]
public void Unadvertise()
{
var service = new ServiceProfile("z", "_sdtest-7._udp", 1024);
var done = new ManualResetEvent(false);
var mdns = new MulticastService();
var sd = new ServiceDiscovery(mdns);

mdns.NetworkInterfaceDiscovered += (s, e) => sd.QueryAllServices();
sd.ServiceInstanceShutdown += (s, e) =>
{
if (e.ServiceInstanceName == service.FullyQualifiedName)
{
done.Set();
}
};
try
{
sd.Advertise(service);
mdns.Start();
sd.Unadvertise(service);
Assert.IsTrue(done.WaitOne(TimeSpan.FromSeconds(1)), "goodbye timeout");
}
finally
{
sd.Dispose();
mdns.Stop();
}
}
}
}

0 comments on commit 8545fc3

Please # to comment.