Skip to content

Commit

Permalink
feat(ServiceDiscovery): option to move additional records into answers
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Nov 4, 2018
1 parent 54b43a6 commit dec3947
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/ServiceDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ public ServiceDiscovery(MulticastService mdns)
/// </value>
public MulticastService Mdns { get; private set; }

/// <summary>
/// Add the additional records into the answers.
/// </summary>
/// <value>
/// Defaults to <b>false</b>.
/// </value>
/// <remarks>
/// Some malformed systems, such as js-ipfs and go-ipfs, only examine
/// the <see cref="Message.Answers"/> and not the <see cref="Message.AdditionalRecords"/>.
/// Setting this to <b>true</b>, will move the additional records
/// into the answers.
/// </remarks>
public bool AnswersContainsAdditionalRecords { get; set; }

/// <summary>
/// Gets the name server.
/// </summary>
Expand Down Expand Up @@ -193,6 +207,12 @@ void OnQuery(object sender, MessageEventArgs e)
response.AdditionalRecords.Clear();
}

if (AnswersContainsAdditionalRecords)
{
response.Answers.AddRange(response.AdditionalRecords);
response.AdditionalRecords.Clear();
}

Mdns.SendAnswer(response);
if (log.IsDebugEnabled)
log.Debug($"sent answer {response.Answers[0]}");
Expand Down
40 changes: 40 additions & 0 deletions test/ServiceDiscoveryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,45 @@ public void Discover_ServiceInstance()
}
}

[TestMethod]
public void Discover_ServiceInstance_WithAnswersContainingAdditionRecords()
{
var service = new ServiceProfile("y", "_sdtest-2._udp", 1024);
var done = new ManualResetEvent(false);
var mdns = new MulticastService();
var sd = new ServiceDiscovery(mdns)
{
AnswersContainsAdditionalRecords = true
};
Message discovered = null;
mdns.NetworkInterfaceDiscovered += (s, e) =>
{
sd.QueryServiceInstances(service.ServiceName);
};

sd.ServiceInstanceDiscovered += (s, e) =>
{
if (e.ServiceInstanceName == service.FullyQualifiedName)
{
Assert.IsNotNull(e.Message);
discovered = e.Message;
done.Set();
}
};
try
{
sd.Advertise(service);
mdns.Start();
Assert.IsTrue(done.WaitOne(TimeSpan.FromSeconds(1)), "instance not found");
Assert.AreEqual(0, discovered.AdditionalRecords.Count);
Assert.IsTrue(discovered.Answers.Count > 1);
}
finally
{
sd.Dispose();
mdns.Stop();
}
}

}
}

0 comments on commit dec3947

Please sign in to comment.